# To build locally get a copy of the dependancies (http://windows.php.net/downloads/php-sdk/deps/)
# These are the dependencies used by PHP and largely used and tested. These binaries are built using
# the sources available at https://github.com/winlibs
# Then run 'nmake /f windows\Makefile.vc' in MS dev console
# To build tests as well 'nmake /f windows\Makefile.vc all'
# To build tests and execute them 'nmake /f windows\Makefile.vc check'

CC=cl
LD=link
LB=lib

!IFNDEF DEBUG
DEBUG=0
!ENDIF

!IFNDEF WITH_DEVEL
WITH_DEVEL=..\deps
!ENDIF

!IFNDEF WITH_BUILD
WITH_BUILD=..\gdbuild
!ENDIF

LIBGD_SRC_DIR=src
LIBGD_OBJ_DIR=$(WITH_BUILD)
LIBGD_BIN_DIR=$(WITH_BUILD)\bin
LIBGD_TEST_DIR=test

!IFNDEF PLATFORM
PLATFORM=x86
!ENDIF
# detect x64 cross-compiler env
!IF "$(PREFERREDTOOLARCHITECTURE)" == "x64"
PLATFORM=x64
!ENDIF
PLATFORM=$(PLATFORM:X=x)

BUILD_DIR=$(WITH_BUILD)

!MESSAGE library source:  $(LIBGD_SRC_DIR)

GD_DLL=libgd.dll
GD_LIB=libgd_a.lib
GD_CONFIG_H=$(LIBGD_SRC_DIR)\config.h


SDR=$(BUILD_DIR)^\
TDR=$(BUILD_DIR)\tests^\
TARGETDLL=$(BUILD_DIR)\$(GD_DLL)
TARGETLIB=$(BUILD_DIR)\$(GD_LIB)

LIB_OBJS= \
  $(LIBGD_OBJ_DIR)\gd.obj \
  $(LIBGD_OBJ_DIR)\gdcache.obj \
  $(LIBGD_OBJ_DIR)\gd_color.obj \
  $(LIBGD_OBJ_DIR)\gd_color_match.obj \
  $(LIBGD_OBJ_DIR)\gdfontg.obj \
  $(LIBGD_OBJ_DIR)\gdfontl.obj \
  $(LIBGD_OBJ_DIR)\gdfontmb.obj \
  $(LIBGD_OBJ_DIR)\gdfonts.obj \
  $(LIBGD_OBJ_DIR)\gdfontt.obj \
  $(LIBGD_OBJ_DIR)\gdft.obj \
  $(LIBGD_OBJ_DIR)\gdfx.obj \
  $(LIBGD_OBJ_DIR)\gd_filename.obj \
  $(LIBGD_OBJ_DIR)\gd_filter.obj \
  $(LIBGD_OBJ_DIR)\gd_bmp.obj \
  $(LIBGD_OBJ_DIR)\gd_gd2.obj \
  $(LIBGD_OBJ_DIR)\gd_gd.obj \
  $(LIBGD_OBJ_DIR)\gd_gif_in.obj \
  $(LIBGD_OBJ_DIR)\gd_gif_out.obj \
  $(LIBGD_OBJ_DIR)\gdhelpers.obj \
  $(LIBGD_OBJ_DIR)\gd_io.obj \
  $(LIBGD_OBJ_DIR)\gd_io_dp.obj \
  $(LIBGD_OBJ_DIR)\gd_io_file.obj \
  $(LIBGD_OBJ_DIR)\gd_io_ss.obj \
  $(LIBGD_OBJ_DIR)\gd_jpeg.obj \
  $(LIBGD_OBJ_DIR)\gd_security.obj \
  $(LIBGD_OBJ_DIR)\gd_tga.obj \
  $(LIBGD_OBJ_DIR)\gd_tiff.obj \
  $(LIBGD_OBJ_DIR)\gd_xbm.obj \
  $(LIBGD_OBJ_DIR)\gdkanji.obj \
  $(LIBGD_OBJ_DIR)\gd_nnquant.obj \
  $(LIBGD_OBJ_DIR)\gd_png.obj \
  $(LIBGD_OBJ_DIR)\gd_ss.obj \
  $(LIBGD_OBJ_DIR)\gdtables.obj \
  $(LIBGD_OBJ_DIR)\gd_topal.obj \
  $(LIBGD_OBJ_DIR)\gd_transform.obj \
  $(LIBGD_OBJ_DIR)\gd_wbmp.obj \
  $(LIBGD_OBJ_DIR)\gdxpm.obj \
  $(LIBGD_OBJ_DIR)\wbmp.obj \
  $(LIBGD_OBJ_DIR)\gd_interpolation.obj \
  $(LIBGD_OBJ_DIR)\gd_matrix.obj \
  $(LIBGD_OBJ_DIR)\gd_rotate.obj \
  $(LIBGD_OBJ_DIR)\gd_version.obj \
  $(LIBGD_OBJ_DIR)\gd_crop.obj \
  $(LIBGD_OBJ_DIR)\gd_color_map.obj \
  $(LIBGD_OBJ_DIR)\gd_heif.obj \
  $(LIBGD_OBJ_DIR)\gd_avif.obj \
  $(LIBGD_OBJ_DIR)\gd_webp.obj

LIBS=kernel32.lib ole32.lib user32.lib advapi32.lib shell32.lib ws2_32.lib Dnsapi.lib Gdi32.Lib
LIBS_GD=libjpeg_a.lib freetype_a.lib libpng_a.lib libiconv_a.lib  zlib_a.lib libheif_a.lib libwebp_a.lib libxpm_a.lib libavif_a.lib libtiff.lib

PROG_EXES= \
	$(LIBGD_OBJ_DIR)\gdcmpgif.exe \
	$(LIBGD_OBJ_DIR)\gdtopng.exe \
	$(LIBGD_OBJ_DIR)\pngtogd.exe \
	$(LIBGD_OBJ_DIR)\gdparttopng.exe \
	$(LIBGD_OBJ_DIR)\gd2topng.exe \
	$(LIBGD_OBJ_DIR)\pngtogd2.exe \
	$(LIBGD_OBJ_DIR)\annotate.exe \
	$(LIBGD_OBJ_DIR)\gd2copypal.exe \
	$(LIBGD_OBJ_DIR)\gd2togif.exe \
	$(LIBGD_OBJ_DIR)\giftogd2.exe
PROG_OBJS=$(PROG_EXES:exe=obj)

INCLUDES=/Isrc /I$(WITH_DEVEL)\include -I$(WITH_DEVEL)\include\libpng16 -I$(WITH_DEVEL)\include\freetype -I$(WITH_DEVEL)\include\freetype2 /Itests /Itests\gdtest -I$(WITH_DEVEL)\include\

LDFLAGS=/nologo /nodefaultlib:libcmt /libpath:$(WITH_DEVEL)\lib /MACHINE:$(PLATFORM)
!IF $(DEBUG)
LDFLAGS=$(LDFLAGS) /DEBUG
!ENDIF

!IF $(DEBUG)
CFLAGS=/nologo /Qvec-report:1 /MD /TC /Zi
!ELSE
CFLAGS=/nologo /O2 /Qvec-report:1 /MD /TC
!ENDIF

CFLAGS= $(CFLAGS) \
       /DWIN32=1\
       /D_WIN32=1\
       /DMSWIN32=1\
       /DBGDWIN32=1\
       /DENABLE_GD_FORMATS=1\
       /DHAVE_FT2BUILD_H=1\
       /DHAVE_GD_H=1\
       /DHAVE_ICONV_H=1\
       /DHAVE_LIBAVIF=1\
       /DHAVE_LIBFREETYPE=1\
       /DHAVE_LIBJPEG=1\
       /DHAVE_LIBPNG=1\
       /DHAVE_LIBWEBP=1\
       /DHAVE_LIBHEIF=1\
       /DHAVE_LIBAVIF=1\
       /DHAVE_LIBZ=1\
       /DHAVE_LIBXPM=1\
       /DHAVE_LIBTIFF=1\
       $(INCLUDES)

TEST_CFLAGS=$(CFLAGS)
TEST_LDFLAGS=$(LDFLAGS) $(SDR)readdir.obj $(SDR)gdtest.obj $(BUILD_DIR)\libgd.lib
TEST_LIST=$(TDR)testlist.txt
TEST_MK=$(TDR)Maketests.vc

TEST_OBJS=$(TEST_EXES:tests=obj)
TEST_OBJS=$(TEST_OBJS:exe=obj)

# create buil dir and test exe dir. test ext dir may not be called but easier like this
# so we can include the gentest file
!IF [if not exist $(BUILD_DIR) mkdir $(BUILD_DIR)]
!ENDIF

!IF [if not exist $(TDR) mkdir $(TDR)]
!ENDIF

!IF [$(MAKEDIR)\windows\gentest.bat tests $(BUILD_DIR) "$(TEST_CFLAGS)" "$(TEST_LDFLAGS)" $(CC) $(LD) "$(TEST_MK)" "$(TEST_LIST)"]
!ENDIF

# pseudotargets
build_libs: make_dirs $(TARGETDLL) $(TARGETLIB)
build_progs: build_libs $(PROG_OBJS) $(PROG_EXES)
all: build_libs build_tests build_progs
check: all run_tests

clean:
  @if exist $(SDR) rd $(SDR) /s /q
  @if exist $(TDR) rd $(TDR) /s /q
  @if exist $(BUILD_DIR)\*.exe del $(BUILD_DIR)\*.exe /q

# helpers

make_dirs:
	@if not exist $(BUILD_DIR) mkdir $(BUILD_DIR)
	@if not exist $(SDR) mkdir $(SDR)
	@echo #ifndef HAVE_FT2BUILD_H> $(GD_CONFIG_H)
	@echo #define HAVE_FT2BUILD_H>> $(GD_CONFIG_H)
	@echo #endif>> $(GD_CONFIG_H)
	@echo #ifndef HAVE_ICONV_H>> $(GD_CONFIG_H)
	@echo #define HAVE_ICONV_H>> $(GD_CONFIG_H)
	@echo #endif>> $(GD_CONFIG_H)
	@echo #ifndef HAVE_LIBAVIF>> $(GD_CONFIG_H)
	@echo #define HAVE_LIBAVIF>> $(GD_CONFIG_H)
	@echo #endif>> $(GD_CONFIG_H)
	@echo #ifndef HAVE_LIBFREETYPE>> $(GD_CONFIG_H)
	@echo #define HAVE_LIBFREETYPE>> $(GD_CONFIG_H)
	@echo #endif>> $(GD_CONFIG_H)
	@echo #ifndef HAVE_LIBJPEG>> $(GD_CONFIG_H)
	@echo #define HAVE_LIBJPEG>> $(GD_CONFIG_H)
	@echo #endif>> $(GD_CONFIG_H)
	@echo #ifndef HAVE_LIBPNG>> $(GD_CONFIG_H)
	@echo #define HAVE_LIBPNG>> $(GD_CONFIG_H)
	@echo #endif>> $(GD_CONFIG_H)
	@echo #ifndef HAVE_LIBWEBP>> $(GD_CONFIG_H)
	@echo #define HAVE_LIBWEBP>> $(GD_CONFIG_H)
	@echo #endif>> $(GD_CONFIG_H)
	@echo #ifndef HAVE_LIBHEIF>> $(GD_CONFIG_H)
	@echo #define HAVE_LIBHEIF>> $(GD_CONFIG_H)
	@echo #endif>> $(GD_CONFIG_H)
	@echo #ifndef HAVE_LIBAVIF>> $(GD_CONFIG_H)
	@echo #define HAVE_LIBAVIF>> $(GD_CONFIG_H)
	@echo #endif>> $(GD_CONFIG_H)
	@echo #ifndef HAVE_LIBZ>> $(GD_CONFIG_H)
	@echo #define HAVE_LIBZ>> $(GD_CONFIG_H)
	@echo #endif>> $(GD_CONFIG_H)
	@echo #ifndef HAVE_LIBXPM>> $(GD_CONFIG_H)
	@echo #define HAVE_LIBXPM>> $(GD_CONFIG_H)
	@echo #endif>> $(GD_CONFIG_H)
	@echo #ifndef HAVE_INTTYPES_H>> $(GD_CONFIG_H)
	@echo #define HAVE_INTTYPES_H>> $(GD_CONFIG_H)
	@echo #endif>> $(GD_CONFIG_H)
	@echo #ifndef HAVE_SYS_STAT_H>> $(GD_CONFIG_H)
	@echo #define HAVE_SYS_STAT_H>> $(GD_CONFIG_H)
	@echo #endif>> $(GD_CONFIG_H)
	@echo #ifndef HAVE_LIBTIFF>> $(GD_CONFIG_H)
	@echo #define HAVE_LIBTIFF>> $(GD_CONFIG_H)
	@echo #endif>> $(GD_CONFIG_H)

run_tests: pretest
	@cd $(TDR)
	@$(MAKEDIR)\windows\run_tests

# libgd
$(LIBGD_OBJ_DIR):
	@if not exist $(LIBGD_OBJ_DIR) mkdir $(LIBGD_OBJ_DIR)

$(LIBGD_BIN_DIR):
	@if not exist $(LIBGD_BIN_DIR) mkdir $(LIBGD_BIN_DIR)

$(TARGETDLL): $(LIB_OBJS)
  @$(LD) /DLL /OUT:$@ $(LIB_OBJS) $(LIBS_GD) $(LIBS) $(LDFLAGS)

$(TARGETLIB): $(LIB_OBJS)
  @$(LB) /OUT:$@ $(LIB_OBJS) $(LDFLAGS)

{$(LIBGD_SRC_DIR)\}.c{$(LIBGD_OBJ_DIR)\}.obj:
	$(CC) $(CFLAGS) /c /Fo"$@" /Fd$*.pdb $<

all: $(LIBGD_OBJ_DIR) $(LIBGD_BIN_DIR) $(LIB_OBJS) $(STATICLIB)

.SUFFIXES: .c .obj .res .exe

# tests

!INCLUDE $(TEST_MK)
build_tests: $(TEST_LIST) $(SDR)readdir.obj $(SDR)gdtest.obj $(TEST_EXES)

$(SDR)gdtest.obj: $(TARGETDLL) tests\gdtest\gdtest.c
  @$(CC) $(CFLAGS) /DGDTEST_TOP_DIR=\"$(MAKEDIR:\=\\)\\tests\" /c tests\gdtest\gdtest.c /Fd$(SDR)gdtest.pdb /Fo$(SDR)gdtest.obj

$(SDR)readdir.obj: $(TARGETDLL) tests\gdtest\readdir.c
  @$(CC) $(CFLAGS) /c tests\gdtest\readdir.c /Fd$(SDR)readdir.pdb /Fo$(SDR)readdir.obj

pretest: $(TARGETDLL)
	if not exist $(TDR) mkdir $(TDR)
	copy $(TARGETDLL) $(TDR)$(GD_DLL)

# programs

{$(LIBGD_OBJ_DIR)}.obj{$(LIBGD_OBJ_DIR)}.exe:
	$(LD) $(LDFLAGS) $** $(BUILD_DIR)\libgd.lib /out:$@
