# AiReader - build with MSYS2 UCRT64:  ./u.sh make        (./build_mupdf.sh first, once)
# or cross-compile on a Mac/Linux:     make CROSS=x86_64-w64-mingw32-
MUPDF   = third_party/mupdf
CROSS   =
CC      = $(CROSS)gcc
WINDRES = $(CROSS)windres
CFLAGS  = -O2 -march=x86-64-v2 -Wall -Wno-unused-function -I$(MUPDF)/include -DUNICODE -D_UNICODE -D_WIN32_WINNT=0x0A00
ifdef DEBUG
CFLAGS += -g -O0 -DAIREADER_PROFILER
SRC_DEBUG = src/prof.c
STRIP =
else
STRIP = -s
endif
LDFLAGS = -static -municode -mwindows $(STRIP) -Wl,--gc-sections
LIBS    = $(MUPDF)/build/release/libmupdf.a $(MUPDF)/build/release/libmupdf-third.a \
          -lgdi32 -luser32 -lcomdlg32 -lcomctl32 -lshell32 -lshfolder -ladvapi32 -ldwmapi -lmsimg32 -ldbghelp -lwinmm -lm
SRC     = src/main.c src/engine.c src/text.c src/misc.c src/edit.c $(SRC_DEBUG)
OBJ     = $(SRC:src/%.c=build/%.o) build/app.res.o

all: AiReader.exe

build/%.o: src/%.c src/app.h src/misc.h src/resource.h
	@mkdir -p build
	$(CC) $(CFLAGS) -c $< -o $@

build/app.res.o: res/app.rc res/app.ico res/app.manifest src/resource.h
	@mkdir -p build
	$(WINDRES) res/app.rc -O coff -o $@

AiReader.exe: $(OBJ)
	$(CC) -o $@ $(OBJ) $(LDFLAGS) $(LIBS)

# portable single file: the name containing "portable" makes it keep its data next to itself
VERSION = $(shell sed -n 's/^\#define APP_VERSION  *"\(.*\)"/\1/p' src/app.h)
portable: AiReader.exe
	@mkdir -p dist
	cp AiReader.exe dist/AiReader-$(VERSION)-portable.exe

clean:
	rm -rf build AiReader.exe

.PHONY: all clean portable
