# GNU Makefile for third party libraries used by MuPDF

ifeq ($(USE_SYSTEM_LIBS),yes)
  USE_SYSTEM_FREETYPE := 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
endif

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

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

# --- FREETYPE 2 ---

ifeq ($(USE_SYSTEM_FREETYPE),yes)
  FREETYPE_CFLAGS += $(SYS_FREETYPE_CFLAGS)
  THIRD_CFLAGS += $(FREETYPE_CFLAGS)
  THIRD_LIBS += $(SYS_FREETYPE_LIBS)
else

FREETYPE_CFLAGS += -Iscripts/freetype -Ithirdparty/freetype/include

THIRD_SRC += thirdparty/freetype/src/base/ftbase.c
THIRD_SRC += thirdparty/freetype/src/base/ftbbox.c
THIRD_SRC += thirdparty/freetype/src/base/ftbitmap.c
THIRD_SRC += thirdparty/freetype/src/base/ftdebug.c
THIRD_SRC += thirdparty/freetype/src/base/ftgasp.c
THIRD_SRC += thirdparty/freetype/src/base/ftglyph.c
THIRD_SRC += thirdparty/freetype/src/base/ftinit.c
THIRD_SRC += thirdparty/freetype/src/base/ftstroke.c
THIRD_SRC += thirdparty/freetype/src/base/ftsynth.c
THIRD_SRC += thirdparty/freetype/src/base/ftsystem.c
THIRD_SRC += thirdparty/freetype/src/base/fttype1.c
THIRD_SRC += thirdparty/freetype/src/cff/cff.c
THIRD_SRC += thirdparty/freetype/src/cid/type1cid.c
THIRD_SRC += thirdparty/freetype/src/psaux/psaux.c
THIRD_SRC += thirdparty/freetype/src/pshinter/pshinter.c
THIRD_SRC += thirdparty/freetype/src/psnames/psnames.c
THIRD_SRC += thirdparty/freetype/src/raster/raster.c
THIRD_SRC += thirdparty/freetype/src/sfnt/sfnt.c
THIRD_SRC += thirdparty/freetype/src/smooth/smooth.c
THIRD_SRC += thirdparty/freetype/src/truetype/truetype.c
THIRD_SRC += thirdparty/freetype/src/type1/type1.c

THIRD_CFLAGS += $(FREETYPE_CFLAGS)

$(OUT)/thirdparty/freetype/%.o: thirdparty/freetype/%.c
	$(CC_CMD) $(FREETYPE_CFLAGS) \
		-DFT_CONFIG_MODULES_H=\"slimftmodules.h\" \
		-DFT_CONFIG_OPTIONS_H=\"slimftoptions.h\" \
		-DFT2_BUILD_LIBRARY

endif

# --- HARFBUZZ ---

ifeq ($(USE_SYSTEM_HARFBUZZ),yes)
  THIRD_CFLAGS += $(SYS_HARFBUZZ_CFLAGS)
  THIRD_LIBS += $(SYS_HARFBUZZ_LIBS)
else

THIRD_SRC += thirdparty/harfbuzz/src/hb-aat-layout.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-aat-map.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-blob.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-buffer.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-buffer-serialize.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-common.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-face.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-fallback-shape.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-font.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-ft.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-map.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-number.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-ot-cff1-table.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-ot-cff2-table.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-ot-color.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-ot-face.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-ot-font.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-ot-layout.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-ot-map.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-ot-math.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-ot-meta.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-ot-metrics.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-ot-name.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-ot-shape.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-ot-shape-complex-arabic.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-ot-shape-complex-default.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-ot-shape-complex-hangul.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-ot-shape-complex-hebrew.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-ot-shape-complex-indic.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-ot-shape-complex-indic-table.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-ot-shape-complex-khmer.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-ot-shape-complex-myanmar.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-ot-shape-complex-thai.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-ot-shape-complex-use.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-ot-shape-complex-use-table.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-ot-shape-complex-vowel-constraints.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-ot-shape-fallback.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-ot-shape-normalize.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-ot-tag.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-ot-var.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-set.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-shape.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-shape-plan.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-shaper.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-static.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-subset.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-subset-cff1.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-subset-cff2.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-subset-cff-common.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-subset-input.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-subset-plan.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-ucd.cc
THIRD_SRC += thirdparty/harfbuzz/src/hb-unicode.cc

THIRD_CFLAGS += -Ithirdparty/harfbuzz/src

$(OUT)/thirdparty/harfbuzz/%.o: thirdparty/harfbuzz/%.cc
	$(CXX_CMD) $(FREETYPE_CFLAGS) -Iinclude/mupdf \
		-DHAVE_FALLBACK=1 \
		-DHAVE_OT \
		-DHAVE_ROUND \
		-DHAVE_UCDN \
		-DHB_NO_MT \
		-Dhb_malloc_impl=fz_hb_malloc \
		-Dhb_calloc_impl=fz_hb_calloc \
		-Dhb_free_impl=fz_hb_free \
		-Dhb_realloc_impl=fz_hb_realloc \
		-DHAVE_FREETYPE \
		-fno-exceptions \
		-fno-rtti \
		-fno-threadsafe-statics\
		-fvisibility-inlines-hidden\
		-std=gnu++11

endif

# --- JPEG-XR ---

ifeq ($(HAVE_JPEGXR),yes)
ifeq ($(USE_SYSTEM_JPEGXR),yes)
  THIRD_CFLAGS += $(SYS_JPEGXR_CFLAGS) -DHAVE_JPEGXR
  THIRD_LIBS += $(SYS_JPEGXR_LIBS)
else

THIRD_SRC += thirdparty/jpegxr/Software/algo.c
THIRD_SRC += thirdparty/jpegxr/Software/api.c
THIRD_SRC += thirdparty/jpegxr/Software/cr_parse.c
THIRD_SRC += thirdparty/jpegxr/Software/flags.c
THIRD_SRC += thirdparty/jpegxr/Software/init.c
THIRD_SRC += thirdparty/jpegxr/Software/io.c
THIRD_SRC += thirdparty/jpegxr/Software/jpegxr_pixelformat.c
THIRD_SRC += thirdparty/jpegxr/Software/r_parse.c
THIRD_SRC += thirdparty/jpegxr/Software/r_strip.c
THIRD_SRC += thirdparty/jpegxr/Software/r_tile_frequency.c
THIRD_SRC += thirdparty/jpegxr/Software/r_tile_spatial.c
THIRD_SRC += thirdparty/jpegxr/Software/x_strip.c

THIRD_CFLAGS += -Ithirdparty/jpegxr
THIRD_CFLAGS += -Ithirdparty/jpegxr/Software
THIRD_CFLAGS += -DHAVE_JPEGXR

$(OUT)/thirdparty/jpegxr/%.o: thirdparty/jpegxr/%.c
	$(CC_CMD) -Ithirdparty/jpegxr -Ithirdparty/jpegxr/Software -Wno-tautological-compare

endif
endif

# --- LIBJPEG ---

ifeq ($(USE_SYSTEM_LIBJPEG),yes)
  THIRD_CFLAGS += $(SYS_LIBJPEG_CFLAGS) -DSHARE_JPEG
  THIRD_LIBS += $(SYS_LIBJPEG_LIBS)
else

THIRD_SRC += thirdparty/libjpeg/jaricom.c
THIRD_SRC += thirdparty/libjpeg/jcomapi.c
THIRD_SRC += thirdparty/libjpeg/jdapimin.c
THIRD_SRC += thirdparty/libjpeg/jdapistd.c
THIRD_SRC += thirdparty/libjpeg/jdarith.c
THIRD_SRC += thirdparty/libjpeg/jdatadst.c
THIRD_SRC += thirdparty/libjpeg/jdatasrc.c
THIRD_SRC += thirdparty/libjpeg/jdcoefct.c
THIRD_SRC += thirdparty/libjpeg/jdcolor.c
THIRD_SRC += thirdparty/libjpeg/jddctmgr.c
THIRD_SRC += thirdparty/libjpeg/jdhuff.c
THIRD_SRC += thirdparty/libjpeg/jdinput.c
THIRD_SRC += thirdparty/libjpeg/jdmainct.c
THIRD_SRC += thirdparty/libjpeg/jdmarker.c
THIRD_SRC += thirdparty/libjpeg/jdmaster.c
THIRD_SRC += thirdparty/libjpeg/jdmerge.c
THIRD_SRC += thirdparty/libjpeg/jdpostct.c
THIRD_SRC += thirdparty/libjpeg/jdsample.c
THIRD_SRC += thirdparty/libjpeg/jdtrans.c
THIRD_SRC += thirdparty/libjpeg/jerror.c
THIRD_SRC += thirdparty/libjpeg/jfdctflt.c
THIRD_SRC += thirdparty/libjpeg/jfdctfst.c
THIRD_SRC += thirdparty/libjpeg/jfdctint.c
THIRD_SRC += thirdparty/libjpeg/jidctflt.c
THIRD_SRC += thirdparty/libjpeg/jidctfst.c
THIRD_SRC += thirdparty/libjpeg/jidctint.c
THIRD_SRC += thirdparty/libjpeg/jmemmgr.c
THIRD_SRC += thirdparty/libjpeg/jquant1.c
THIRD_SRC += thirdparty/libjpeg/jquant2.c
THIRD_SRC += thirdparty/libjpeg/jutils.c

THIRD_CFLAGS += -Iscripts/libjpeg -Ithirdparty/libjpeg

$(OUT)/thirdparty/libjpeg/%.o: thirdparty/libjpeg/%.c
	$(CC_CMD) -Iscripts/libjpeg

endif

# --- LCMS2 ---

ifeq ($(USE_SYSTEM_LCMS2),yes)
  THIRD_CFLAGS += $(SYS_LCMS2_CFLAGS)
  THIRD_LIBS += $(SYS_LCMS2_LIBS)
else

THIRD_SRC += $(sort $(wildcard thirdparty/lcms2/src/cms*.c))

THIRD_CFLAGS += -Ithirdparty/lcms2/include -DHAVE_LCMS2MT

$(OUT)/thirdparty/lcms2/%.o: thirdparty/lcms2/%.c
	$(CC_CMD) -Ithirdparty/lcms2/include

endif

# --- MuJS ---

ifeq ($(USE_SYSTEM_MUJS),yes)
  THIRD_CFLAGS += $(SYS_MUJS_CFLAGS)
  THIRD_LIBS += $(SYS_MUJS_LIBS)
else

THIRD_SRC += thirdparty/mujs/one.c
THIRD_CFLAGS += -Ithirdparty/mujs

$(OUT)/thirdparty/mujs/%.o: thirdparty/mujs/%.c
	$(CC_CMD)

endif

# --- ZLIB ---

ifeq ($(USE_SYSTEM_ZLIB),yes)
  THIRD_CFLAGS += $(SYS_ZLIB_CFLAGS)
  THIRD_LIBS += $(SYS_ZLIB_LIBS)
else

THIRD_SRC += thirdparty/zlib/adler32.c
THIRD_SRC += thirdparty/zlib/compress.c
THIRD_SRC += thirdparty/zlib/crc32.c
THIRD_SRC += thirdparty/zlib/deflate.c
THIRD_SRC += thirdparty/zlib/inffast.c
THIRD_SRC += thirdparty/zlib/inflate.c
THIRD_SRC += thirdparty/zlib/inftrees.c
THIRD_SRC += thirdparty/zlib/trees.c
THIRD_SRC += thirdparty/zlib/uncompr.c
THIRD_SRC += thirdparty/zlib/zutil.c

THIRD_CFLAGS += -Ithirdparty/zlib

$(OUT)/thirdparty/zlib/%.o: thirdparty/zlib/%.c
	$(CC_CMD) -DHAVE_UNISTD_H -DHAVE_STDARG_H

endif

# --- LURATECH ---

ifeq ($(HAVE_LURATECH),yes)

THIRD_SRC += $(sort $(wildcard thirdparty/luratech/ldf_jb2/source/common/*.c))
THIRD_SRC += $(sort $(wildcard thirdparty/luratech/ldf_jb2/source/compress/*.c))
THIRD_SRC += $(sort $(wildcard thirdparty/luratech/lwf_jp2/library/source/*.c))

LURATECH_CFLAGS += -Ithirdparty/luratech/ldf_jb2/source/libraries
LURATECH_CFLAGS += -Ithirdparty/luratech/ldf_jb2/source/compress
LURATECH_CFLAGS += -Ithirdparty/luratech/ldf_jb2/source/common
LURATECH_CFLAGS += -Ithirdparty/luratech/lwf_jp2/library/source

THIRD_CFLAGS += $(LURATECH_CFLAGS) -DHAVE_LURATECH

$(OUT)/thirdparty/luratech/%.o: thirdparty/luratech/%.c
	$(CC_CMD) -DLINUX $(LURATECH_CFLAGS) -Wno-tautological-compare -Wno-absolute-value -Wno-sign-compare

else # HAVE_LURATECH

# --- JBIG2DEC ---

ifeq ($(USE_SYSTEM_JBIG2DEC),yes)
  THIRD_CFLAGS += $(SYS_JBIG2DEC_CFLAGS)
  THIRD_LIBS += $(SYS_JBIG2DEC_LIBS)
else

THIRD_SRC += thirdparty/jbig2dec/jbig2.c
THIRD_SRC += thirdparty/jbig2dec/jbig2_arith.c
THIRD_SRC += thirdparty/jbig2dec/jbig2_arith_iaid.c
THIRD_SRC += thirdparty/jbig2dec/jbig2_arith_int.c
THIRD_SRC += thirdparty/jbig2dec/jbig2_generic.c
THIRD_SRC += thirdparty/jbig2dec/jbig2_halftone.c
THIRD_SRC += thirdparty/jbig2dec/jbig2_huffman.c
THIRD_SRC += thirdparty/jbig2dec/jbig2_hufftab.c
THIRD_SRC += thirdparty/jbig2dec/jbig2_image.c
THIRD_SRC += thirdparty/jbig2dec/jbig2_mmr.c
THIRD_SRC += thirdparty/jbig2dec/jbig2_page.c
THIRD_SRC += thirdparty/jbig2dec/jbig2_refinement.c
THIRD_SRC += thirdparty/jbig2dec/jbig2_segment.c
THIRD_SRC += thirdparty/jbig2dec/jbig2_symbol_dict.c
THIRD_SRC += thirdparty/jbig2dec/jbig2_text.c

THIRD_CFLAGS += -Ithirdparty/jbig2dec

$(OUT)/thirdparty/jbig2dec/%.o: thirdparty/jbig2dec/%.c
	$(CC_CMD) -DHAVE_STDINT_H -DJBIG_EXTERNAL_MEMENTO_H=\"mupdf/memento.h\" -Wno-sign-compare

endif

# --- OPENJPEG ---

ifeq ($(USE_SYSTEM_OPENJPEG),yes)
  THIRD_CFLAGS += $(SYS_OPENJPEG_CFLAGS)
  THIRD_LIBS += $(SYS_OPENJPEG_LIBS)
else

OPENJPEG_CFLAGS += -Ithirdparty/openjpeg/src/lib/openjp2
OPENJPEG_CFLAGS += -DOPJ_STATIC
OPENJPEG_CFLAGS += -DOPJ_HAVE_STDINT_H -DOPJ_HAVE_INTTYPES_H
OPENJPEG_CFLAGS += -DMUTEX_pthread=0

THIRD_SRC += thirdparty/openjpeg/src/lib/openjp2/bio.c
THIRD_SRC += thirdparty/openjpeg/src/lib/openjp2/cio.c
THIRD_SRC += thirdparty/openjpeg/src/lib/openjp2/dwt.c
THIRD_SRC += thirdparty/openjpeg/src/lib/openjp2/event.c
THIRD_SRC += thirdparty/openjpeg/src/lib/openjp2/function_list.c
THIRD_SRC += thirdparty/openjpeg/src/lib/openjp2/image.c
THIRD_SRC += thirdparty/openjpeg/src/lib/openjp2/invert.c
THIRD_SRC += thirdparty/openjpeg/src/lib/openjp2/j2k.c
THIRD_SRC += thirdparty/openjpeg/src/lib/openjp2/jp2.c
THIRD_SRC += thirdparty/openjpeg/src/lib/openjp2/mct.c
THIRD_SRC += thirdparty/openjpeg/src/lib/openjp2/mqc.c
THIRD_SRC += thirdparty/openjpeg/src/lib/openjp2/openjpeg.c
THIRD_SRC += thirdparty/openjpeg/src/lib/openjp2/pi.c
THIRD_SRC += thirdparty/openjpeg/src/lib/openjp2/sparse_array.c
THIRD_SRC += thirdparty/openjpeg/src/lib/openjp2/t1.c
THIRD_SRC += thirdparty/openjpeg/src/lib/openjp2/t2.c
THIRD_SRC += thirdparty/openjpeg/src/lib/openjp2/tcd.c
THIRD_SRC += thirdparty/openjpeg/src/lib/openjp2/tgt.c
THIRD_SRC += thirdparty/openjpeg/src/lib/openjp2/thread.c

THIRD_CFLAGS += $(OPENJPEG_CFLAGS)

$(OUT)/thirdparty/openjpeg/%.o: thirdparty/openjpeg/%.c
	$(CC_CMD) $(OPENJPEG_CFLAGS)

endif

endif # HAVE_LURATECH

# --- FreeGLUT ---

ifeq ($(USE_SYSTEM_GLUT),yes)
  GLUT_CFLAGS := $(SYS_GLUT_CFLAGS)
  GLUT_LIBS := $(SYS_GLUT_LIBS)
else

GLUT_SRC += $(sort $(wildcard thirdparty/freeglut/src/fg_*.c))
GLUT_SRC += $(sort $(wildcard thirdparty/freeglut/src/x11/*.c))

GLUT_OBJ := $(GLUT_SRC:%.c=$(OUT)/%.o)

LOCAL_GLUT_CFLAGS += -Ithirdparty/freeglut/include
LOCAL_GLUT_CFLAGS += -Ithirdparty/freeglut/src
LOCAL_GLUT_CFLAGS += -DHAVE_UNISTD_H -DHAVE_STDINT_H -DHAVE_X11_EXTENSIONS_XRANDR_H
LOCAL_GLUT_CFLAGS += -Wno-sign-compare

GLUT_LIB := $(OUT)/libfreeglut.a

$(GLUT_LIB): $(GLUT_OBJ)

$(OUT)/thirdparty/freeglut/%.o: thirdparty/freeglut/%.c
	$(CC_CMD) $(LOCAL_GLUT_CFLAGS)

GLUT_CFLAGS := -Ithirdparty/freeglut/include
GLUT_LIBS := -lGL -lX11 -lXrandr

endif

# --- cURL ---

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