# GNU Make file for Sumatra PDF

SUMATRA_DIR = .
-include $(SUMATRA_DIR)/config.mk

FITZ_DIR ?= $(SUMATRA_DIR)/fitz
# SUMATRA_PDFSYNC_GUI ?= yes
SUMATRA_SYNCTEX ?= yes

SUMATRA_CONFIG ?= $(BUILD)
SUMATRA_INC_DIR ?= $(INC_DIR)
SUMATRA_CPPFLAGS ?= $(CPPFLAGS)
SUMATRA_CFLAGS ?= $(CFLAGS)
SUMATRA_LDFLAGS ?= $(LDFLAGS)
SUMATRA_LDLIBS ?= $(LDLIBS)
# Python is optional, but use it if it is found:
SUMATRA_PYTHON ?= $(SYSTEMDRIVE)/Python25/python

SUMATRA_CONFIG_debug = yes
SUMATRA_FITZ_CONFIG_debug ?= debug
SUMATRA_CPPFLAGS_debug ?= -D DEBUG
SUMATRA_LDFLAGS_debug ?= -mconsole

SUMATRA_CONFIG_release ?= yes
SUMATRA_FITZ_CONFIG_release ?= release
SUMATRA_CPPFLAGS_release ?= -D NDEBUG
SUMATRA_CFLAGS_release ?= -Os
SUMATRA_LDFLAGS_release ?= -static -s -mwindows

ifeq ($(SUMATRA_CONFIG), )
    SUMATRA_CONFIG = release
endif
ifeq ($(SUMATRA_CONFIG_$(SUMATRA_CONFIG)), )
    $(warning SUMATRA_CONFIG should be "debug" or "release")
endif
FITZ_CONFIG ?= $(SUMATRA_FITZ_CONFIG_$(SUMATRA_CONFIG))
SUMATRA_CPPFLAGS += $(SUMATRA_CPPFLAGS_$(SUMATRA_CONFIG))
SUMATRA_CFLAGS += $(SUMATRA_CFLAGS_$(SUMATRA_CONFIG))
SUMATRA_LDFLAGS += $(SUMATRA_LDFLAGS_$(SUMATRA_CONFIG))

SUMATRA_BUILD = $(SUMATRA_DIR)/build/$(SUMATRA_CONFIG)
$(warning Sumatra build directory is $(SUMATRA_BUILD))

sumatra: sumatrapdf
.PHONY: sumatra

include $(SUMATRA_DIR)/fitz.mk

# TODO: precompiled headers?
# TODO: auto header depenencies

SUMATRA_INC_DIR += $(SUMATRA_DIR)/baseutils
SUMATRA_INC_DIR += $(SUMATRA_DIR)/src
SUMATRA_INC_DIR += $(FITZ_INCLUDE)
SUMATRA_INC_DIR += $(SUMATRA_DIR)/DialogSizer
SUMATRA_CPPFLAGS += $(addprefix -I ,$(SUMATRA_INC_DIR))
SUMATRA_RCFLAGS += $(addprefix -I ,$(SUMATRA_INC_DIR))
SUMATRA_CFLAGS += -Wall
SUMATRA_CFLAGS += -g
SUMATRA_CPPFLAGS += -D DISABLE_STRSAFE
# SUMATRA_CPPFLAGS += -D BUILD_RM_VERSION

ifeq ($(SUMATRA_PDFSYNC_GUI), yes)
    SUMATRA_CPPFLAGS += -D _PDFSYNC_GUI_ENHANCEMENT
endif

ifeq ($(SUMATRA_SYNCTEX), yes)
    SUMATRA_CPPFLAGS += -D SYNCTEX_FEATURE
endif

# Mingw API headers don't seem to take notice of MBCS
# SUMATRA_CPPFLAGS += -D _MBCS
SUMATRA_LDFLAGS += $(addprefix -L ,$(LIB_DIR))

# <winuser.h> missing some VK_OEM stuff unless _WIN32_WINNT >= 0x0500
# I think this means we're building for Windows 5.0 aka Windows 2000
# Also need Windows 2000 or Windows 98 for MonitorFromRect API
SUMATRA_CPPFLAGS += -D WINVER=0x0500
SUMATRA_CPPFLAGS += -D _WIN32_WINNT=0x0500

# Mingw API is missing these symbols from <wininet.h>:
SUMATRA_CPPFLAGS += -D INTERNET_STATUS_INTERMEDIATE_RESPONSE=120
SUMATRA_CPPFLAGS += -D INTERNET_STATUS_STATE_CHANGE=200

SUMATRAPDF_RES_OBJ = $(SUMATRA_BUILD)/src/SumatraPDF.rc.o

SUMATRAPDF_C += AppPrefs.cc
BASEUTILS_C += base_util.c
BASEUTILS_C += benc_util.c
SUMATRAPDF_C += DisplayModel.cc
SUMATRAPDF_C += DisplayState.cc
BASEUTILS_C += dstring.c
BASEUTILS_C += file_util.c
SUMATRAPDF_C += FileHistory.cc
BASEUTILS_C += geom_util.c
SUMATRAPDF_C += PdfEngine.cc
SUMATRAPDF_C += PdfSearch.cc
BASEUTILS_C += str_util.c
BASEUTILS_C += strlist_util.c
SUMATRAPDF_C += SumatraDialogs.cc
SUMATRAPDF_C += SumatraPDF.cpp
SUMATRAPDF_C += translations.cpp
SUMATRAPDF_C += translations_txt.c
BASEUTILS_C += utf_util.c
BASEUTILS_C += win_util.c
BASEUTILS_C += WinUtil.cpp
BASEUTILS_C += wstr_util.c
SUMATRAPDF_C += LangMenuDef.cpp
SUMATRAPDF_C += FileWatch.cc
SUMATRAPDF_C += PdfSync.cpp
DIALOGSIZER_C += dialogsizer_set.cpp

ifeq ($(SUMATRA_SYNCTEX), yes)
    SUMATRAPDF_C += synctex_parser.c
endif

BASEUTILS_OBJ = \
    $(addprefix $(SUMATRA_BUILD)/baseutils/,$(addsuffix .o,$(BASEUTILS_C)))
SUMATRAPDF_C_OBJ = \
    $(addprefix $(SUMATRA_BUILD)/src/,$(addsuffix .o,$(SUMATRAPDF_C)))
DIALOGSIZER_OBJ = $(addprefix \
    $(SUMATRA_BUILD)/DialogSizer/,$(addsuffix .o,$(DIALOGSIZER_C)))
SUMATRA_C_OBJ = $(BASEUTILS_OBJ) $(SUMATRAPDF_C_OBJ) $(DIALOGSIZER_OBJ)
SUMATRAPDF_OBJ = $(SUMATRA_C_OBJ) $(SUMATRAPDF_RES_OBJ)
SUMATRAPDF_EXE = $(SUMATRA_BUILD)/src/SumatraPDF.exe
LDLIBS_$(notdir $(SUMATRAPDF_EXE)) += \
    -lgdi32 -lmsimg32 -lcomdlg32 -lcomctl32 -lwinspool $(MUPDF_LDLIBS)
LDLIBS_$(notdir $(SUMATRAPDF_EXE)) += -lwininet -lole32
SUMATRA_PROD = $(SUMATRAPDF_OBJ) $(SUMATRAPDF_EXE)
SUMATRA_SUBDIR = baseutils src DialogSizer

sumatrapdf: $(SUMATRAPDF_EXE)
.PHONY: sumatrapdf

.SUFFIXES:

define SUMATRA_LD_CMD
    @echo CXX -o $@ $(LDLIBS_$(notdir $@))
    @$(CXX) $(SUMATRA_LDFLAGS) -o $@ $^ $(LDLIBS_$(notdir $@)) \
        $(SUMATRA_LDLIBS)
endef
$(SUMATRAPDF_EXE): $(SUMATRAPDF_OBJ) $(MUPDF_LIBS)
	$(SUMATRA_LD_CMD)

$(BASEUTILS_OBJ): $(SUMATRA_BUILD)/baseutils
$(SUMATRAPDF_C_OBJ) $(SUMATRAPDF_RES_OBJ): $(SUMATRA_BUILD)/src
$(DIALOGSIZER_OBJ): $(SUMATRA_BUILD)/DialogSizer

$(SUMATRA_C_OBJ): $(SUMATRA_BUILD)/%.o: $(SUMATRA_DIR)/%
	@echo CC -c $<
	@$(CC) -c $(SUMATRA_CPPFLAGS) $(SUMATRA_CFLAGS) -o $@ $<

$(SUMATRAPDF_RES_OBJ): $(SUMATRA_BUILD)/%.o: $(SUMATRA_DIR)/%
	windres $(SUMATRA_RCFLAGS) -I $(dir $<) $< $@

$(addprefix $(SUMATRA_BUILD)/,$(SUMATRA_SUBDIR)):
	mkdir -p $@

# Update string translations if Python installed
SUMATRA_XLATE_TARG = \
    translations_txt.c translations_txt.h LangMenuDef.cpp LangMenuDef.h
SUMATRA_XLATE_DEP = strings.txt gen_c_from_strings_file.py extract_strings.py
ifeq ($(shell test -e $(SUMATRA_PYTHON) && echo yes), yes)
    $(addprefix $(SUMATRA_DIR)/src/,$(SUMATRA_XLATE_TARG)): \
        $(addprefix $(SUMATRA_DIR)/src/,$(SUMATRA_XLATE_DEP))
	cd src && $(SUMATRA_PYTHON) gen_c_from_strings_file.py
endif

clean-sumatra:
	@echo CLEAN $(SUMATRA_BUILD)
	@rm -f $(SUMATRA_PROD)
realclean-sumatra:
	rm -rf $(SUMATRA_BUILD) || rmdir $(SUMATRA_BUILD)
	rm -f src/extract_strings.pyc
.PHONY: clean-sumatra realclean-sumatra
