# GNU Makefile for third party libraries used by MuPDF

ifeq ($(USE_SYSTEM_LIBS),yes)
  USE_SYSTEM_FREETYPE := yes
  USE_SYSTEM_GUMBO := yes
  USE_SYSTEM_HARFBUZZ := yes
  USE_SYSTEM_JBIG2DEC := yes
  USE_SYSTEM_JPEGXR := no # not available
  USE_SYSTEM_LCMS2 := no # lcms2mt is strongly preferred
  USE_SYSTEM_LIBJPEG := yes
  USE_SYSTEM_MUJS := no # not available
  USE_SYSTEM_OPENJPEG := yes
  USE_SYSTEM_ZLIB := yes
  USE_SYSTEM_GLUT := yes
  USE_SYSTEM_CURL := yes
  USE_SYSTEM_LEPTONICA := yes
  USE_SYSTEM_TESSERACT := yes
endif

ifeq ($(OS),MACOS)
  USE_SYSTEM_GLUT := yes
endif

ifeq ($(OS),Linux)
  USE_SYSTEM_CURL := yes
endif

# Use of Tesseract is optional:
ifeq ($(USE_TESSERACT),yes)
  ifeq ($(USE_SYSTEM_LEPTONICA),yes)
    HAVE_LEPTONICA := $(HAVE_SYS_LEPTONICA)
  else
    ifneq ($(wildcard thirdparty/leptonica/src/*),)
      HAVE_LEPTONICA := yes
    endif
  endif
  ifeq ($(USE_SYSTEM_TESSERACT),yes)
    HAVE_TESSERACT := $(HAVE_SYS_TESSERACT)
  else
    ifneq ($(wildcard thirdparty/tesseract/src/*),)
      HAVE_TESSERACT := yes
    endif
  endif
endif

# --- Include source file lists ---

include Makelists

# --- FREETYPE 2 ---

ifeq ($(USE_SYSTEM_FREETYPE),yes)
  THIRD_CFLAGS += $(SYS_FREETYPE_CFLAGS)
  THIRD_LIBS += $(SYS_FREETYPE_LIBS)
else
  THIRD_CFLAGS += $(FREETYPE_CFLAGS)
  THIRD_LIBS += $(FREETYPE_LIBS)
  THIRD_SRC += $(FREETYPE_SRC)
$(OUT)/thirdparty/freetype/%.o: thirdparty/freetype/%.c
	$(CC_CMD) $(LIB_CFLAGS) $(FREETYPE_CFLAGS) $(FREETYPE_BUILD_CFLAGS)
endif

# --- GUMBO (HTML5 parser) ---

ifeq ($(USE_SYSTEM_GUMBO),yes)
  THIRD_CFLAGS += $(SYS_GUMBO_CFLAGS)
  THIRD_LIBS += $(SYS_GUMBO_LIBS)
else
  THIRD_CFLAGS += $(GUMBO_CFLAGS)
  THIRD_LIBS += $(GUMBO_LIBS)
  THIRD_SRC += $(GUMBO_SRC)
$(OUT)/thirdparty/gumbo-parser/%.o: thirdparty/gumbo-parser/%.c
	$(CC_CMD) $(LIB_CFLAGS) $(GUMBO_CFLAGS) $(GUMBO_BUILD_CFLAGS)
endif

# --- HARFBUZZ ---

ifeq ($(USE_SYSTEM_HARFBUZZ),yes)
  THIRD_CFLAGS += $(SYS_HARFBUZZ_CFLAGS)
  THIRD_LIBS += $(SYS_HARFBUZZ_LIBS)
else
  THIRD_CFLAGS += $(HARFBUZZ_CFLAGS)
  THIRD_LIBS += $(HARFBUZZ_LIBS)
  THIRD_SRC += $(HARFBUZZ_SRC)
$(OUT)/thirdparty/harfbuzz/%.o: thirdparty/harfbuzz/%.cc
	$(CXX_CMD) $(LIB_CFLAGS) $(HARFBUZZ_CFLAGS) $(HARFBUZZ_BUILD_CFLAGS)
endif

# --- JPEG-XR ---

ifeq ($(HAVE_JPEGXR),yes)
  THIRD_CFLAGS += -DHAVE_JPEGXR
ifeq ($(USE_SYSTEM_JPEGXR),yes)
  THIRD_CFLAGS += $(SYS_JPEGXR_CFLAGS)
  THIRD_LIBS += $(SYS_JPEGXR_LIBS)
else
  THIRD_CFLAGS += $(JPEGXR_CFLAGS)
  THIRD_LIBS += $(JPEGXR_LIBS)
  THIRD_SRC += $(JPEGXR_SRC)
$(OUT)/thirdparty/jpegxr/%.o: thirdparty/jpegxr/%.c
	$(CC_CMD) $(LIB_CFLAGS) $(JPEGXR_CFLAGS) $(JPEGXR_BUILD_CFLAGS)
endif
endif

# --- LIBJPEG ---

ifeq ($(USE_SYSTEM_LIBJPEG),yes)
  THIRD_CFLAGS += $(SYS_LIBJPEG_CFLAGS) -DSHARE_JPEG
  THIRD_LIBS += $(SYS_LIBJPEG_LIBS)
else
  THIRD_CFLAGS += $(LIBJPEG_CFLAGS)
  THIRD_LIBS += $(LIBJPEG_LIBS)
  THIRD_SRC += $(LIBJPEG_SRC)
$(OUT)/thirdparty/libjpeg/%.o: thirdparty/libjpeg/%.c
	$(CC_CMD) $(LIB_CFLAGS) $(LIBJPEG_CFLAGS) $(LIBJPEG_BUILD_CFLAGS)
endif

# --- LCMS2 ---

ifeq ($(USE_SYSTEM_LCMS2),yes)
  THIRD_CFLAGS += $(SYS_LCMS2_CFLAGS)
  THIRD_LIBS += $(SYS_LCMS2_LIBS)
else
  THIRD_CFLAGS += $(LCMS2_CFLAGS)
  THIRD_LIBS += $(LCMS2_LIBS)
  THIRD_SRC += $(LCMS2_SRC)
$(OUT)/thirdparty/lcms2/%.o: thirdparty/lcms2/%.c
	$(CC_CMD) $(LIB_CFLAGS) $(LCMS2_CFLAGS) $(LCMS2_BUILD_CFLAGS)
endif

# --- MuJS ---

ifeq ($(USE_SYSTEM_MUJS),yes)
  THIRD_CFLAGS += $(SYS_MUJS_CFLAGS)
  THIRD_LIBS += $(SYS_MUJS_LIBS)
else
  THIRD_CFLAGS += $(MUJS_CFLAGS)
  THIRD_LIBS += $(MUJS_LIBS)
  THIRD_SRC += $(MUJS_SRC)
$(OUT)/thirdparty/mujs/%.o: thirdparty/mujs/%.c
	$(CC_CMD) $(LIB_CFLAGS) $(MUJS_CFLAGS) $(MUJS_BUILD_CFLAGS)
endif

# --- ZLIB ---

ifeq ($(USE_SYSTEM_ZLIB),yes)
  THIRD_CFLAGS += $(SYS_ZLIB_CFLAGS)
  THIRD_LIBS += $(SYS_ZLIB_LIBS)
else
  THIRD_CFLAGS += $(ZLIB_CFLAGS)
  THIRD_LIBS += $(ZLIB_LIBS)
  THIRD_SRC += $(ZLIB_SRC)
$(OUT)/thirdparty/zlib/%.o: thirdparty/zlib/%.c
	$(CC_CMD) $(LIB_CFLAGS) $(ZLIB_CFLAGS) $(ZLIB_BUILD_CFLAGS)
endif

# --- JBIG2DEC ---

ifeq ($(USE_SYSTEM_JBIG2DEC),yes)
  THIRD_CFLAGS += $(SYS_JBIG2DEC_CFLAGS)
  THIRD_LIBS += $(SYS_JBIG2DEC_LIBS)
else
  THIRD_CFLAGS += $(JBIG2DEC_CFLAGS)
  THIRD_LIBS += $(JBIG2DEC_LIBS)
  THIRD_SRC += $(JBIG2DEC_SRC)
$(OUT)/thirdparty/jbig2dec/%.o: thirdparty/jbig2dec/%.c
	$(CC_CMD) $(WARNING_CFLAGS) $(LIB_CFLAGS) $(JBIG2DEC_CFLAGS) $(JBIG2DEC_BUILD_CFLAGS)
endif

# --- OPENJPEG ---

ifeq ($(USE_SYSTEM_OPENJPEG),yes)
  THIRD_CFLAGS += $(SYS_OPENJPEG_CFLAGS)
  THIRD_LIBS += $(SYS_OPENJPEG_LIBS)
else
  THIRD_CFLAGS += $(OPENJPEG_CFLAGS)
  THIRD_LIBS += $(OPENJPEG_LIBS)
  THIRD_SRC += $(OPENJPEG_SRC)
$(OUT)/thirdparty/openjpeg/%.o: thirdparty/openjpeg/%.c
	$(CC_CMD) $(LIB_CFLAGS) $(OPENJPEG_CFLAGS) $(OPENJPEG_BUILD_CFLAGS)
endif

# --- FreeGLUT ---

ifeq ($(USE_SYSTEM_GLUT),yes)
  THIRD_GLUT_CFLAGS := $(SYS_GLUT_CFLAGS)
  THIRD_GLUT_LIBS += $(SYS_GLUT_LIBS)
else
  THIRD_GLUT_CFLAGS += $(GLUT_CFLAGS)
  THIRD_GLUT_LIBS += $(GLUT_LIBS)
  THIRD_GLUT_SRC += $(GLUT_SRC)
$(OUT)/thirdparty/freeglut/%.o: thirdparty/freeglut/%.c
	$(CC_CMD) $(LIB_CFLAGS) $(GLUT_CFLAGS) $(GLUT_BUILD_CFLAGS)
endif

# --- cURL ---

ifeq ($(USE_SYSTEM_CURL),yes)
  ifeq ($(HAVE_SYS_CURL),yes)
    HAVE_CURL := yes
    CURL_CFLAGS := $(SYS_CURL_CFLAGS) -DHAVE_CURL
    CURL_LIBS := $(SYS_CURL_LIBS)
  endif
endif

# --- LEPTONICA ---

ifeq ($(HAVE_LEPTONICA),yes)
  THIRD_CFLAGS += -DHAVE_LEPTONICA
ifeq ($(USE_SYSTEM_LEPTONICA),yes)
  THIRD_CFLAGS += $(SYS_LEPTONICA_CFLAGS)
  THIRD_LIBS += $(SYS_LEPTONICA_LIBS)
else
  THIRD_CFLAGS += $(LEPTONICA_CFLAGS)
  THIRD_LIBS += $(LEPTONICA_LIBS)
  THIRD_SRC += $(LEPTONICA_SRC)
$(OUT)/thirdparty/leptonica/%.o: thirdparty/leptonica/%.c
	$(CC_CMD) $(LIB_CFLAGS) $(LEPTONICA_CFLAGS) $(LEPTONICA_BUILD_CFLAGS)
endif
endif

# --- TESSERACT ---

ifeq ($(HAVE_TESSERACT),yes)
  THIRD_CFLAGS += -DHAVE_TESSERACT
ifeq ($(USE_SYSTEM_TESSERACT),yes)
  THIRD_CFLAGS += $(SYS_TESSERACT_CFLAGS)
  THIRD_LIBS += $(SYS_TESSERACT_LIBS) -lstdc++
else
  THIRD_CFLAGS += $(TESSERACT_CFLAGS)
  THIRD_LIBS += $(TESSERACT_LIBS)
  THIRD_SRC += $(TESSERACT_SRC)
  THIRD_SRC += $(TESSERACT_SRC)
$(OUT)/thirdparty/tesseract/%.o: thirdparty/tesseract/%.cpp
	$(CXX_CMD) $(LIB_CFLAGS) $(TESSERACT_CFLAGS) $(TESSERACT_BUILD_CFLAGS)
endif
endif

# --- EXTRACT ---

THIRD_CFLAGS += $(EXTRACT_CFLAGS)
THIRD_LIBS += $(EXTRACT_LIBS)
THIRD_SRC += $(EXTRACT_SRC)
$(OUT)/thirdparty/extract/%.o: thirdparty/extract/%.c
	$(CC_CMD) $(WARNING_CFLAGS) $(LIB_CFLAGS) $(EXTRACT_CFLAGS) $(ZLIB_CFLAGS) $(EXTRACT_BUILD_CFLAGS)

# The first python3 command in the next two rules returns 0 (success) iff
# python3 exists and is version 3.6 or later.

thirdparty/extract/src/docx_template.c: thirdparty/extract/src/template.docx thirdparty/extract/src/docx_template_build.py
	$(QUIET_GEN) if python3 -c 'import sys; sys.exit(sys.version_info < (3,6))'; \
		then python3 thirdparty/extract/src/docx_template_build.py \
			-i thirdparty/extract/src/template.docx \
			-n docx \
			-o thirdparty/extract/src/docx_template ; \
		else echo python3 is older than 3.6 so not running docx_template_build.py; \
		fi
	@touch $@

thirdparty/extract/src/odt_template.c: thirdparty/extract/src/template.odt thirdparty/extract/src/docx_template_build.py
	$(QUIET_GEN) if python3 -c 'import sys; sys.exit(sys.version_info < (3,6))'; \
		then python3 thirdparty/extract/src/docx_template_build.py \
			-i thirdparty/extract/src/template.odt \
			-n odt \
			-o thirdparty/extract/src/odt_template ; \
		else echo python3 is older than 3.6 so not running docx_template_build.py; \
		fi
	@touch $@

generate: thirdparty/extract/src/docx_template.c
