USE_DEBUG = NO
USE_64BIT = NO
USE_UNICODE = NO
USE_CLANG = YES

USE_CYGWIN = NO

# if USE_INNO = YES, build the Inno Setup installer
# if USE_INNO = NO, `setup` and `install` targets will not be used
USE_INNO = YES

# the legacy version of qualify.cpp, does not depend upon c++ string class
USE_LEGACY = NO

include der_libs\tool_select.mak
include der_libs\release.mak

ifeq ($(USE_DEBUG),YES)
CFLAGS=-Wall -O -g
LFLAGS= -mwindows 
else
CFLAGS=-Wall -O2 -c 
LFLAGS=-s -mwindows 
endif
CFLAGS += -Wno-write-strings
CFLAGS += -Wno-format-overflow
CFLAGS += -Weffc++ 

#LiFLAGS = -Ider_libs
CFLAGS += -Ider_libs
# CFLAGS += -Imingw_libs

# LFLAGS += -Lmingw_libs

ifeq ($(USE_STATIC),YES)
LFLAGS += -static
endif

CPPSRC=wbigcalc.cpp bigcalc.cpp bigmath.cpp bigmisc.cpp bigprint.cpp \
config.cpp options.cpp about.cpp \
der_libs/hyperlinks.cpp \
der_libs/common_funcs.cpp \
der_libs/common_win.cpp \
der_libs/winmsgs.cpp \
der_libs/tooltips.cpp \
der_libs/statbar.cpp
	
OBJS = $(CPPSRC:.cpp=.o) dlgres.o

BASE=WBigCalc
BIN=$(BASE).exe

LIBS=-lcomctl32 -lgdi32 -lcomdlg32 -lhtmlhelp

#************************************************************
# version.h is GENERATED from CHANGELOG.md (single source of truth for the
# version). VERSION itself is set in der_libs\release.mak: the first
# "## [x.y]" header in CHANGELOG.md. The program reads it as VerNum.
# $(file ...) writes the header without echo/shell quoting differences between
# cmd.exe and Cygwin (needs GNU Make 4.0 or later).
#
# TO ADD THIS TO ANOTHER PROJECT, ALL OF THESE ARE NEEDED:
#  1. Copy this block, and change VerNum if that project's source uses
#     another macro name.
#  2. Source: #include "version.h" and use VerNum; delete the old hand-
#     maintained version string. Move any OTHER #defines that the old
#     version.h held into another header: it gets overwritten on every
#     regeneration.
#  3. clean: add version.h to the rm line (see "clean:" below).
#  4. Repo: stop tracking the old file:  git rm --cached version.h
#     (a tracked copy would just be rewritten and show up as modified).
#  5. .gitignore: add version.h. Otherwise release.mak's check-clean sees an
#     untracked file and refuses to release. (Installer projects should also
#     ignore Output/ and temp_notes.md for the same reason.)
#  6. depend: make "depend" depend on version.h (see "depend:" below), so
#     makedepend always finds it and records every .o that includes it; then
#     run "make depend". If the .rc file includes version.h, add
#     "dlgres.o: version.h" by hand (makedepend can't scan .rc files).
#  7. Nothing needed for the installer: iscc gets the version from VERSION via
#     /DMyAppVersion (release.mak), not from version.h.
define VERSION_H_CONTENT
//  Auto-generated by Makefile from CHANGELOG.md -- do not edit
#define VerNum "$(VERSION)"
endef

version.h: CHANGELOG.md
	@$(file >version.h,$(VERSION_H_CONTENT))

# Distribution targets: the same names (setup, dist, install) work in both
# modes; USE_INNO (top of file) decides what they do.
#
# USE_INNO = YES  (Inno Setup installer; the generic rules installer,
#                  installer-zip and install-silent live in der_libs\release.mak)
#   make            build the exe
#   make setup      exe (if out of date) -> iscc -> Output\wbigcalcV<ver>.setup.exe
#   make dist       setup, then Output\wbigcalcV<ver>.setup.zip
#   make install    silent-install the setup exe, for smoke-testing
#   "make release" / "make update" (release.mak) depend on "dist" and upload
#   only the setup zip. A missing or broken .iss makes iscc fail loudly.
#   DIST_ZIP points at the setup zip, so release.mak's plain "sha256" target
#   checksums the installer zip.
#
# USE_INNO = NO   (legacy loose-files distribution)
#   make            build the exe
#   make setup      does nothing (make reports "Nothing to be done")
#   make dist       portable zip $(BASE)V<ver>.zip
#   make install    does nothing
#   "make release" / "make update" upload the portable zip + CHANGELOG.md
#   (release.mak's default RELEASE_ASSETS).

# Force these action-only targets to always run
.PHONY: setup dist install

ifeq ($(USE_INNO),YES)

DIST_ZIP = $(SETUP_ZIP)
RELEASE_ASSETS = ./$(SETUP_ZIP)

# Recipe-less rule: adds a prerequisite to the generic "installer" target in
# release.mak, so the installer always packages a freshly built exe.
installer: $(BIN)

# PrettyReMark-style names for the generic release.mak targets.
setup: installer
dist: installer-zip
install: install-silent

else

DIST_ZIP := $(BASE)V$(VERSION).zip

# No installer in this mode: no prerequisites and no recipe, so make just
# reports "Nothing to be done".
setup:
install:

# Clears old zips from the project folder, then builds the portable zip.
dist:
	rm -f *.zip
	zip $(DIST_ZIP) $(BASE).exe $(BASE).chm bigcalc.txt CHANGELOG.md LICENSE.txt readme.md $(BASE).ini

endif

#************************************************************
%.o: %.cpp
	$(TOOLS)/$(GNAME) $(CFLAGS) $< -o $@

all: $(BIN)

clean:
	rm -vf $(BIN) $(OBJS) version.h
ifeq ($(USE_INNO),YES)
	rm -rf $(SETUP_DIR)
endif

wc:
	wc -l *.cpp *.rc

clint:
	cmd /C "python ..\ClaudeLint.py --exclude der_libs"
	
cppc:
	cmd /C "cppcheck --project=compile_commands.json --std=c++14 --suppressions-list=./.suppress.cppcheck"

check:
	cmd /C "d:\llvm\bin\clang-tidy.exe $(CPPSRC)"

# version.h must exist first so makedepend sees it (see the version.h block).
depend: version.h
	makedepend $(CPPSRC)

#************************************************************
$(BIN): $(OBJS)
	$(TOOLS)/$(GNAME) $(OBJS) $(LFLAGS) -o $(BIN) $(LIBS) 

dlgres.o: dlgres.rc version.h
	$(TOOLS)\$(WRNAME) $< -O COFF -o $@

# DO NOT DELETE

wbigcalc.o: version.h keywin32.h resource.h bigcalc.h
bigcalc.o: resource.h bigcalc.h keywin32.h
bigmath.o: bigcalc.h
bigmisc.o: keywin32.h bigcalc.h
bigprint.o: bigcalc.h
config.o: bigcalc.h
options.o: resource.h bigcalc.h
about.o: resource.h version.h
der_libs/hyperlinks.o: der_libs/iface_32_64.h der_libs/hyperlinks.h
der_libs/common_funcs.o: der_libs/common.h
der_libs/common_win.o: der_libs/common.h der_libs/commonw.h
der_libs/tooltips.o: der_libs/iface_32_64.h der_libs/common.h
der_libs/tooltips.o: der_libs/tooltips.h
der_libs/statbar.o: der_libs/common.h der_libs/commonw.h der_libs/statbar.h
