CMAKE_MINIMUM_REQUIRED(VERSION 3.7 FATAL_ERROR)

SET(PACKAGE GD)
SET(PACKAGE_NAME GD)

PROJECT(GD)

SET(CMAKE_MODULE_PATH "${GD_SOURCE_DIR}/cmake/modules")

include(gd)

OPTION(ENABLE_GD_FORMATS "Enable GD image formats" 0)
OPTION(ENABLE_PNG "Enable PNG support" 0)
OPTION(ENABLE_LIQ "Enable libimagequant support" 0)
OPTION(ENABLE_JPEG "Enable JPEG support" 0)
OPTION(ENABLE_TIFF "Enable TIFF support" 0)
OPTION(ENABLE_ICONV "Enable ICONV support" 0)
OPTION(ENABLE_XPM "Enable XPM support" 0)
OPTION(ENABLE_FREETYPE "Enable Freetype2 support" 0)
OPTION(ENABLE_FONTCONFIG "Enable FontConfig support" 0)
OPTION(ENABLE_WEBP "Enable WebP support" 0)
OPTION(ENABLE_HEIF "Enable HEIF support" 0)
OPTION(ENABLE_AVIF "Enable AVIF support" 0)
OPTION(ENABLE_RAQM "Enable RAQM support" 0)

if (BUILD_TEST)
	ENABLE_TESTING()
endif(BUILD_TEST)

# if you would like to pass C flags to the compiler, then just
# - specify -DCMAKE_C_FLAGS=... on invocation of cmake, or
# - specify CFLAGS=... on invocation of make
IF(CMAKE_COMPILER_IS_GNUCC)
  SET(CMAKE_C_FLAGS_DEBUG "-g -Wall -Wextra -O0") # will be added to CMAKE_C_FLAGS when CMAKE_BUILD_TYPE is "Debug"
ENDIF(CMAKE_COMPILER_IS_GNUCC)
SET (CMAKE_LIBRARY_OUTPUT_DIRECTORY
  ${PROJECT_BINARY_DIR}/Bin
  CACHE PATH
  "Single Directory for all Libraries"
  )

# --------- Setup the Executable output Directory -------------
SET (CMAKE_RUNTIME_OUTPUT_DIRECTORY
  ${PROJECT_BINARY_DIR}/Bin
  CACHE PATH
  "Single Directory for all Executables."
  )

# --------- Setup the Executable output Directory -------------
SET (CMAKE_ARCHIVE_OUTPUT_DIRECTORY
  ${PROJECT_BINARY_DIR}/Bin
  CACHE PATH
  "Single Directory for all static libraries."
  )

SET(PKG_REQUIRES_PRIVATES "")

if (USE_EXT_GD)
	message("Using GD at: ${USE_EXT_GD}")
	INCLUDE_DIRECTORIES(BEFORE ${GD_INCLUDE_DIR})
	FIND_PACKAGE(GD)
	if (GD_FOUND)
		INCLUDE_DIRECTORIES(BEFORE ${GD_INCLUDE_DIR})
		LINK_DIRECTORIES(${GD_LIBRARIES})
		SET(GD_LIB ${GD_LIBRARIES})
		SET(GD_LIBS_DIR ${GD_LIBRARY})

		message("GD libs #: ${GD_LIBRARIES}")
		message("GD lib #: ${GD_LIBRARY}")
		message("GD include: ${GD_INCLUDE_DIR}")
	else (GD_FOUND)
		message("No gd found")
	endif (GD_FOUND)
else (USE_EXT_GD)

	MACRO(GV VER VAR)
		execute_process(
			COMMAND perl ${CMAKE_CURRENT_SOURCE_DIR}/config/getver.pl ${VER}
			OUTPUT_VARIABLE ${VAR}
		)
	ENDMACRO(GV)

	GV(MAJOR GDLIB_MAJOR)
	GV(MINOR GDLIB_MINOR)
	GV(RELEASE GDLIB_REVISION)
	GV(EXTRA GDLIB_EXTRA)
	SET(GDLIB_VERSION "${GDLIB_MAJOR}.${GDLIB_MINOR}.${GDLIB_REVISION}${GDLIB_EXTRA}")
	MESSAGE(STATUS "gd version ${GDLIB_VERSION}")

	MACRO(GV_LT VER VAR)
		execute_process(
			COMMAND sh ${CMAKE_CURRENT_SOURCE_DIR}/config/getlib.sh ${VER}
			OUTPUT_VARIABLE ${VAR}
		)
	ENDMACRO(GV_LT)

	GV_LT(SONAME GDLIB_LIB_SOVERSION)
	GV_LT(VERSION GDLIB_LIB_VERSION)
	MESSAGE(STATUS "gd shared lib version ${GDLIB_LIB_SOVERSION} (${GDLIB_LIB_VERSION})")

	include(CheckIncludeFiles)
	include(CheckIncludeFile)

	include(AC_HEADER_STDC)
	include(CheckPrototypeExists)

	if (ENABLE_GD_FORMATS)
		FIND_PACKAGE(ZLIB REQUIRED)
	endif (ENABLE_GD_FORMATS)

	if (ENABLE_PNG)
		FIND_PACKAGE(ZLIB REQUIRED)
		FIND_PACKAGE(PNG REQUIRED)
	endif (ENABLE_PNG)

	if (ENABLE_ICONV)
		FIND_PACKAGE(ICONV REQUIRED)
	endif (ENABLE_ICONV)

	IF (ENABLE_WEBP)
		FIND_PACKAGE(WEBP REQUIRED)
	ENDIF (ENABLE_WEBP)

	IF (ENABLE_HEIF)
		FIND_PACKAGE(HEIF REQUIRED)
	ENDIF (ENABLE_HEIF)

	IF (ENABLE_AVIF)
		FIND_PACKAGE(libavif 0.8.2 REQUIRED CONFIG)
		SET(HAVE_LIBAVIF 1)
		SET(AVIF_LIBRARIES avif)
		SET(AVIF_FOUND 1)
	ENDIF (ENABLE_AVIF)

	IF (ENABLE_LIQ)
		FIND_PACKAGE(LIQ REQUIRED)
	ENDIF (ENABLE_LIQ)

	IF (NOT WIN32)
		FIND_PACKAGE(PTHREAD)
	ENDIF (NOT WIN32)

	if (ENABLE_JPEG)
		FIND_PACKAGE(JPEG REQUIRED)
	endif (ENABLE_JPEG)

	if (ENABLE_TIFF)
		FIND_PACKAGE(TIFF REQUIRED)
	endif (ENABLE_TIFF)

	if (ENABLE_FREETYPE)
		FIND_PACKAGE(Freetype REQUIRED)
	endif (ENABLE_FREETYPE)

	if (ENABLE_XPM)
		FIND_PACKAGE(XPM REQUIRED)
	endif (ENABLE_XPM)

	if (ENABLE_FONTCONFIG)
		FIND_PACKAGE(FontConfig REQUIRED)
	endif (ENABLE_FONTCONFIG)

	if (ENABLE_RAQM)
		FIND_PACKAGE(RAQM REQUIRED)
	endif (ENABLE_RAQM)

	SET(ENABLE_GD_FORMATS ${ENABLE_GD_FORMATS})

	if (FREETYPE_FOUND)
		INCLUDE_DIRECTORIES(${FREETYPE_INCLUDE_DIRS})
		SET(HAVE_FT2BUILD_H 1)
		SET(HAVE_LIBFREETYPE 1)
		LIST(APPEND PKG_REQUIRES_PRIVATES freetype2)
	ENDIF(FREETYPE_FOUND)

	IF(ZLIB_FOUND)
		INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIR})
		SET(HAVE_LIBZ 1)
		LIST(APPEND PKG_REQUIRES_PRIVATES zlib)
	ENDIF(ZLIB_FOUND)

	IF(WEBP_FOUND)
		INCLUDE_DIRECTORIES(${WEBP_INCLUDE_DIR})
		SET(HAVE_LIBWEBP 1)
	ENDIF(WEBP_FOUND)

	IF(HEIF_FOUND)
		INCLUDE_DIRECTORIES(${HEIF_INCLUDE_DIR})
		SET(HAVE_LIBHEIF 1)
	ENDIF(HEIF_FOUND)

	IF(PNG_FOUND)
		INCLUDE_DIRECTORIES(${PNG_INCLUDE_DIR})
		SET(HAVE_LIBPNG 1)
		LIST(APPEND PKG_REQUIRES_PRIVATES libpng)
	ENDIF(PNG_FOUND)

	IF(ICONV_FOUND)
		INCLUDE_DIRECTORIES(${ICONV_INCLUDE_DIR})
		SET(HAVE_ICONV 1)
		SET(HAVE_ICONV_H 1)
	ENDIF(ICONV_FOUND)

	IF(LIQ_FOUND)
		INCLUDE_DIRECTORIES(${LIQ_INCLUDE_DIR})
		SET(HAVE_LIBIMAGEQUANT 1)
	ENDIF(LIQ_FOUND)

	IF(XPM_FOUND)
		INCLUDE_DIRECTORIES(${XPM_INCLUDE_DIR})
		SET(HAVE_LIBXPM 1)
		LIST(APPEND PKG_REQUIRES_PRIVATES xpm)
	ENDIF(XPM_FOUND)

	IF(JPEG_FOUND)
		INCLUDE_DIRECTORIES(${JPEG_INCLUDE_DIR})
		SET(HAVE_LIBJPEG 1)
		LIST(APPEND PKG_REQUIRES_PRIVATES libjpeg)
	ENDIF(JPEG_FOUND)

	IF(TIFF_FOUND)
		INCLUDE_DIRECTORIES(${TIFF_INCLUDE_DIR})
		SET(HAVE_LIBTIFF 1)
		LIST(APPEND PKG_REQUIRES_PRIVATES libtiff-4)
	ENDIF(TIFF_FOUND)

	IF(FONTCONFIG_FOUND)
		INCLUDE_DIRECTORIES(${FONTCONFIG_INCLUDE_DIR})
		SET(HAVE_LIBFONTCONFIG 1)
		LIST(APPEND PKG_REQUIRES_PRIVATES fontconfig)
	ELSE (FONTCONFIG_FOUND)
		SET(FONTCONFIG_LIBRARY "")
		SET(FONTCONFIG_INCLUDE_DIR "")
		SET(FONTCONFIG_LIB_DIR "")
	ENDIF(FONTCONFIG_FOUND)

	IF(RAQM_FOUND)
		INCLUDE_DIRECTORIES(${RAQM_INCLUDE_DIR})
		SET(HAVE_RAQM 1)
		LIST(APPEND PKG_REQUIRES_PRIVAES raqm)
	ENDIF(RAQM_FOUND)

	string(REPLACE ";" ", " PKG_REQUIRES_PRIVATES "${PKG_REQUIRES_PRIVATES}")
	SET(HAVE_CONFIG_H 1)

	ADD_DEFINITIONS(-DHAVE_CONFIG_H)

	CHECK_INCLUDE_FILE("stdint.h"	HAVE_STDINT_H)
	CHECK_INCLUDE_FILE("inttypes.h"	HAVE_INTTYPES_H)

	CONFIGURE_FILE(${GD_SOURCE_DIR}/src/config.h.cmake ${GD_SOURCE_DIR}/src/config.h ESCAPE_QUOTES)

	option(BUILD_SHARED_LIBS "Build shared libs" ON)
	option(BUILD_STATIC_LIBS "Build static libs" OFF)

	if (WIN32)
		SET(GD_LIB libgd)
		ADD_DEFINITIONS( -DWIN32 -D_WIN32 -DMSWIN32 -DBGDWIN32 -DWINVER=0x0500  -D_WIN32_WINNT=0x0500 -D_WIN32_IE=0x0600)

		if(NOT MINGW AND MSVC_VERSION GREATER 1399)
			ADD_DEFINITIONS("/D_CRT_SECURE_NO_DEPRECATE")
		endif(NOT MINGW AND MSVC_VERSION GREATER 1399)
		if (MINGW OR MSYS)
			ADD_DEFINITIONS("-mms-bitfields") # -m32
		endif (MINGW OR MSYS)
	else (WIN32)
		SET(GD_LIB gd)
	endif (WIN32)

	SET(GD_LIB_STATIC "${GD_LIB}_static")

	IF(PROFILE)
		add_definitions("-pg")
		set(CMAKE_EXE_LINKER_FLAGS ${LINK_FLAGS} "-pg")
	ENDIF(PROFILE)

	SET(GD_INCLUDE_DIR "${GD_SOURCE_DIR}/src" "${GD_SOURCE_DIR}")
	INCLUDE_DIRECTORIES(before ${FONTCONFIG_INCLUDE_DIR})

	add_subdirectory(src)
endif (USE_EXT_GD)

add_subdirectory(tests)
add_subdirectory(examples)

add_custom_target(distclean ${GD_SOURCE_DIR}/cmake/distclean.sh)

SET(CPACK_PACKAGE_NAME "libgd")
SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "libGD, powerful and easy to use graphic library")
SET(CPACK_PACKAGE_VENDOR "https://www.libgd.org")
SET(CPACK_PACKAGE_VERSION_MAJOR ${GDLIB_MAJOR})
SET(CPACK_PACKAGE_VERSION_MINOR ${GDLIB_MINOR})
SET(CPACK_PACKAGE_VERSION_PATCH ${GDLIB_REVISION}${GDLIB_EXTRA})

if(WIN32)
	set(CPACK_GENERATOR ZIP)
else(WIN32)
	set(CPACK_GENERATOR TGZ)
endif(WIN32)


INSTALL(FILES docs/INSTALL DESTINATION share/doc/gd-${GDLIB_MAJOR}.${GDLIB_MINOR})
INSTALL(FILES docs/README.JPN DESTINATION share/doc/gd-${GDLIB_MAJOR}.${GDLIB_MINOR})
INSTALL(FILES docs/README.CMAKE DESTINATION share/doc/gd-${GDLIB_MAJOR}.${GDLIB_MINOR})
INSTALL(FILES docs/README.TESTING DESTINATION share/doc/gd-${GDLIB_MAJOR}.${GDLIB_MINOR})
INSTALL(FILES docs/README.TXT DESTINATION share/doc/gd-${GDLIB_MAJOR}.${GDLIB_MINOR})


INSTALL(FILES examples/arc.c DESTINATION share/doc/gd-${GDLIB_MAJOR}.${GDLIB_MINOR})
INSTALL(FILES examples/copyrotated.c DESTINATION share/doc/gd-${GDLIB_MAJOR}.${GDLIB_MINOR})
INSTALL(FILES examples/crop.c DESTINATION share/doc/gd-${GDLIB_MAJOR}.${GDLIB_MINOR})
INSTALL(FILES examples/flip.c DESTINATION share/doc/gd-${GDLIB_MAJOR}.${GDLIB_MINOR})
INSTALL(FILES examples/gif.c DESTINATION share/doc/gd-${GDLIB_MAJOR}.${GDLIB_MINOR})
INSTALL(FILES examples/nnquant.c DESTINATION share/doc/gd-${GDLIB_MAJOR}.${GDLIB_MINOR})
INSTALL(FILES examples/noIcon.pic DESTINATION share/doc/gd-${GDLIB_MAJOR}.${GDLIB_MINOR})
INSTALL(FILES examples/noIcon.sgi DESTINATION share/doc/gd-${GDLIB_MAJOR}.${GDLIB_MINOR})
INSTALL(FILES examples/noIcon.tga DESTINATION share/doc/gd-${GDLIB_MAJOR}.${GDLIB_MINOR})
INSTALL(FILES examples/noIconAlpha.tga DESTINATION share/doc/gd-${GDLIB_MAJOR}.${GDLIB_MINOR})
INSTALL(FILES examples/test_crop_threshold.png DESTINATION share/doc/gd-${GDLIB_MAJOR}.${GDLIB_MINOR})
INSTALL(FILES examples/tgaread.c DESTINATION share/doc/gd-${GDLIB_MAJOR}.${GDLIB_MINOR})
INSTALL(FILES examples/tiffread.c DESTINATION share/doc/gd-${GDLIB_MAJOR}.${GDLIB_MINOR})
INSTALL(FILES examples/windows.c DESTINATION share/doc/gd-${GDLIB_MAJOR}.${GDLIB_MINOR})


set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_SOURCE_DIR}/COPYING")

set(CPACK_SOURCE_GENERATOR TGZ)
set(CPACK_SOURCE_IGNORE_FILES
"~$"
"\\\\.swp$"
"\\\\.gitignore$"
"^${PROJECT_SOURCE_DIR}/debian/"
"^${PROJECT_SOURCE_DIR}/old/"
"^${PROJECT_SOURCE_DIR}/bld/"
)
install(FILES ${top_level_DOCFILES} DESTINATION ${DOC_DIR})
INCLUDE(CPack)
