########################################################################
#                                                                      #
# This file creates targets for building, installing and packaging.    #
#                                                                      #
########################################################################


#####################
# Find dependencies #
#####################

###########################################
# Find OpenGL and GLEW (GPlates only)     #
#
# The pygplates module compiles none of the rendering code (its source list is the include
# closure of the pyGPlates API - see 'cmake/pygplates_source_closure.py'), so it needs
# neither OpenGL nor GLEW.
#
# Note: On Linux systems when the *legacy* OpenGL libraries are not found, the default (for OpenGL_GL_PREFERENCE)
#       is to utilize GLVND - in this case using the OpenGL::GL target will instead use the GLVND targets OpenGL::OpenGL and OpenGL::GLX.
if (GPLATES_BUILD_GPLATES)
	find_package(OpenGL REQUIRED)
	find_package(GLEW REQUIRED)
endif()

#############
# Find ZLIB #
#
# Use CMake's built-in FindZLIB module (which defines the ZLIB::ZLIB imported target).
# Note: Windows and macOS get zlib from conda; only Linux also offers it as a system package.
find_package(ZLIB REQUIRED)

#############
# Find CGAL #
#
# According to this link, starting with CGAL 4.12 a call to 'find_package' is all that's needed
# (along with "target_link_libraries(... CGAL::CGAL)"), and so we no longer need to "include(${CGAL_USE_FILE})":
#   https://github.com/CGAL/cgal/wiki/How-to-use-CGAL-with-CMake-or-your-own-build-system
#
# However we don't specify a required version because currently (eg, with CGAL 4.14.3) we get the error message:
#   'Could not find a configuration file for package "CGAL" that is compatible with requested version "4.12"'.
#
# NOTE: We find CGAL before Boost because CGAL also finds Boost and ends up overriding
#       Boost variables (like 'Boost_LIBRARIES') if we find CGAL after.
find_package(CGAL REQUIRED)

###############
# Find Python #
#
# And put the Python major/minor versions in variables GPLATES_PYTHON_VERSION_MAJOR/GPLATES_PYTHON_VERSION_MINOR
# since needed by Boost Python.
#

# Python 'Development' sub-components:
# - 'Development.Embed' for Python embedding developments (eg, GPlates), and
# - 'Development.Module' for Python module developments (eg, pyGPlates).
#
# Note: Using 'Development.Module' is necessary when building pyGPlates "manylinux" wheels
#       (because the manylinux docker image does not have the Python libraries, since you don't link to them on Unix).
#       See https://scikit-build-core.readthedocs.io/en/latest/faqs.html#finding-python.
if (GPLATES_BUILD_GPLATES)
	set(GPLATES_PYTHON_DEVELOPMENT_COMPONENT_NAME Development.Embed)
else()  # pygplates ...
	set(GPLATES_PYTHON_DEVELOPMENT_COMPONENT_NAME Development.Module)
endif()

# The 'Development' component provides the Python headers and library, and the 'Interpreter' component
# provides Python3_EXECUTABLE, which we use below (except under conda, which tells us
# which interpreter to use).
#
# Note: Both components are requested in a *single* find_package() call, because that is what makes
#       find_package() look up the development artifacts *from the interpreter*, and so keeps the two
#       consistent. From 'Support.cmake' in CMake's FindPython module: "if python interpreter is found, use
#       it to look-up for artifacts to ensure consistency between interpreter and development environments".
#       Ask for them in two calls instead and each search stands alone (every call also resets all the
#       Python3_<component>_FOUND flags first), so on a machine with more than one Python
#       - and especially with a '-DPython3_EXECUTABLE=...' pointing at one of them, which
#       the error message further below tells users to pass - you can silently end up with one Python's
#       headers and library and another Python's version and interpreter. Together in one call that same
#       mismatch is instead a hard "Could NOT find Python3 (missing: Development.Module)".
#
#       Cross-compiling is the one exception, and there the call *must* be split: CMake policy CMP0190
#       (CMake 4.1) makes requesting 'Interpreter' (or 'Compiler') together with any 'Development' component
#       a hard error while cross-compiling, unless CMAKE_CROSSCOMPILING_EMULATOR is set:
#         https://github.com/Kitware/CMake/blob/bda0b36155a63134811c8ec754fba4fa85dfcd65/Modules/FindPython/Support.cmake#L1601
#       And a conda-forge cross build (as pyGPlates 1.0's 'linux-aarch64', 'linux-ppc64le' and 'osx-arm64'
#       were) is exactly that situation: the compiler activation passes '-DCMAKE_SYSTEM_NAME' (so
#       CMAKE_CROSSCOMPILING is TRUE) and sets no emulator anywhere, while the policy range in the top-level
#       'cmake_minimum_required(VERSION 3.22...4.4)' opts us into CMP0190. So a combined call fails to
#       *configure* there. pyGPlates 1.0.0 escaped this only because its policy range stopped at 3.31, before
#       CMP0190 existed.
#
#       Splitting the call gives up nothing on that path, because find_package() declines to use the
#       interpreter for the artifact lookup in precisely that situation anyway (an interpreter built for the
#       build platform cannot describe the target). We ask for 'Interpreter' first and 'Development' second
#       so that it is the *target* artifacts that determine Python3_VERSION.
#
#       NumPy is deliberately not requested as a component in either form - see the NumPy section further
#       below for why.
if (CMAKE_CROSSCOMPILING AND NOT CMAKE_CROSSCOMPILING_EMULATOR)
	find_package(Python3 REQUIRED COMPONENTS Interpreter)
	find_package(Python3 REQUIRED COMPONENTS ${GPLATES_PYTHON_DEVELOPMENT_COMPONENT_NAME})
else()
	find_package(Python3 REQUIRED COMPONENTS Interpreter ${GPLATES_PYTHON_DEVELOPMENT_COMPONENT_NAME})
endif()

# Get the Python major/minor versions.
set(GPLATES_PYTHON_VERSION_MAJOR ${Python3_VERSION_MAJOR})
set(GPLATES_PYTHON_VERSION_MINOR ${Python3_VERSION_MINOR})

# Get the Python interpreter executable (GPLATES_PYTHON_EXECUTABLE).
if (GPLATES_CONDA_BUILD)
	# This build is part of a conda build (eg, a "conda build ..." command) so we use the conda-build
	# environment variable PYTHON as the Python installation.
	#
	# This is important when cross-compiling (eg, when conda-forge is building for the 'osx-arm64' platform) because
	# we want to install into the conda 'host' Python prefix (ie, for the target platform) instead of installing into
	# the conda 'build' Python prefix (since find_package() finds the Python in the build prefix), otherwise we'll get the error:
	#
	#   "Empty package; python present in build and host deps.  You probably picked up the build environment's python  executable.
	#    You need to alter your recipe to  use the PYTHON env var in your recipe to run that executable."
	#
	# Note: On a cross-compile the conda recipe's 'cross-python_<target_platform>' build dependency replaces
	#       $PREFIX/bin/python with a build-architecture shim (a 'crossenv' wrapper pointed at the host's
	#       '_sysconfigdata_*.py'), so running PYTHON here is a native process that reports the *host* prefix's
	#       paths - which is how this works without emulation. See 'pygplates/conda/README.md' (section
	#       "Cross builds run the host interpreter"). Dropping that build dependency from the recipe would
	#       break the execute_process() calls below.
	#
	# Note: Cross-compiling is also why the find_package() call above is split in two when CMAKE_CROSSCOMPILING is set
	#       (CMake policy CMP0190). See the comment there.
	#
	# When not cross-compiling there's only a 'host' prefix (conda only installs Python into the 'host' prefix, not also the 'build' prefix).
	# In this situation the PYTHON environment variable should be the same as that found with 'find_package(Python3)'.
	#
	# Note: The Python major.minor versions will match between the 'build' and 'host' prefixes, so we can still use
	#       'GPLATES_PYTHON_VERSION_MAJOR' and 'GPLATES_PYTHON_VERSION_MINOR' (obtained from 'build' prefix when cross-compiling).

	# Python interpreter executable from conda-build PYTHON environment variable ('host' prefix).
	set(GPLATES_PYTHON_EXECUTABLE $ENV{PYTHON})
else()
	# Python interpreter executable from 'find_package(Python3)'.
	set(GPLATES_PYTHON_EXECUTABLE ${Python3_EXECUTABLE})
endif()

# Make sure Python interpreter exists before we use it.
if (NOT EXISTS ${GPLATES_PYTHON_EXECUTABLE})
	message(FATAL_ERROR "Python interpreter was not found")
endif()

# Get the Python standard library location (GPLATES_PYTHON_STDLIB_DIR).
if (GPLATES_CONDA_BUILD)
	# Get the 'host' prefix Python to print the standard library location.
	execute_process(COMMAND ${GPLATES_PYTHON_EXECUTABLE} "-c" "import sysconfig; print(sysconfig.get_path('stdlib'));"
		RESULT_VARIABLE _PYTHON_STDLIB_RESULT
		OUTPUT_VARIABLE GPLATES_PYTHON_STDLIB_DIR
		ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
	if (_PYTHON_STDLIB_RESULT)
		message(FATAL_ERROR "Unable to find Python standard library location")
	endif()
else()
	# Python standard library location from 'find_package(Python3)' (provides Python3_STDLIB variable).
	set(GPLATES_PYTHON_STDLIB_DIR ${Python3_STDLIB})
endif()

# Get the Python prefix directory (GPLATES_PYTHON_PREFIX_DIR).
#
# Note: When building with conda this will be the 'host' prefix Python.
execute_process(COMMAND ${GPLATES_PYTHON_EXECUTABLE} "-c" "import sys; print(sys.prefix);"
	RESULT_VARIABLE _PYTHON_PREFIX_RESULT
	OUTPUT_VARIABLE GPLATES_PYTHON_PREFIX_DIR
	ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
if (_PYTHON_PREFIX_RESULT)
	message(FATAL_ERROR "Unable to find Python prefix location")
endif()

# Convert '\' to '/' in paths.
file(TO_CMAKE_PATH ${GPLATES_PYTHON_EXECUTABLE} GPLATES_PYTHON_EXECUTABLE)
file(TO_CMAKE_PATH ${GPLATES_PYTHON_STDLIB_DIR} GPLATES_PYTHON_STDLIB_DIR)
file(TO_CMAKE_PATH ${GPLATES_PYTHON_PREFIX_DIR} GPLATES_PYTHON_PREFIX_DIR)

# Reject a virtual environment (venv/virtualenv) when building the embedded-Python GPlates application.
#
# GPlates embeds a Python interpreter and, for a standalone install, copies the Python standard library
# wholesale from GPLATES_PYTHON_STDLIB_DIR. A venv has no standard library of its own - on Windows
# 'venv/Lib' holds only 'site-packages', while the real stdlib (eg, the 'encodings' module) stays in the
# base Python and is reached at runtime only via the venv's redirection. Since we derive the prefix/stdlib
# from sys.prefix / Python_STDLIB (which point at the venv) and the embedding test forces PYTHONHOME to
# that prefix, the embedded interpreter can't find its stdlib and dies with an obscure
# "ModuleNotFoundError: No module named 'encodings'". Detect it here and fail with a clear message.
#
# This is gated on GPLATES_BUILD_GPLATES (like the embedding test below): building the pyGPlates *module*
# with 'pip install .' is a normal, supported use of a venv - only the embedded application needs a full
# Python. A venv is detected by sys.prefix differing from sys.base_prefix (conda environments, being full
# prefixes, are not flagged).
if (GPLATES_BUILD_GPLATES)
	execute_process(COMMAND ${GPLATES_PYTHON_EXECUTABLE} "-c" "import sys; print(sys.prefix != sys.base_prefix);"
		RESULT_VARIABLE _PYTHON_VENV_RESULT
		OUTPUT_VARIABLE _PYTHON_IS_VENV
		ERROR_QUIET OUTPUT_STRIP_TRAILING_WHITESPACE)
	if (NOT _PYTHON_VENV_RESULT AND _PYTHON_IS_VENV STREQUAL "True")
		message(FATAL_ERROR
			"The Python interpreter selected for the embedded GPlates build is a virtual environment "
			"(venv/virtualenv):\n"
			"    ${GPLATES_PYTHON_PREFIX_DIR}\n"
			"A venv has no standard library of its own, so the embedded interpreter cannot locate its "
			"stdlib (you would get \"ModuleNotFoundError: No module named 'encodings'\") and the standalone "
			"install has no stdlib to bundle. Build GPlates against a full Python installation instead - "
			"point CMake at it with, eg, -DPython3_EXECUTABLE=/path/to/python "
			"- and curate that installation's site-packages (eg, numpy only) to keep the standalone "
			"dependency scan clean. (A venv is fine for building the pyGPlates module via 'pip install .'.)")
	endif()
endif()

# Find the Python NumPy C-API include directories (and store in GPLATES_PYTHON_NUMPY_INCLUDE_DIRS if found).
#
# Note: We ask the Python interpreter for its NumPy include directory rather than using the 'NumPy' component of
#       find_package(Python3), for two reasons:
#       - GPLATES_PYTHON_EXECUTABLE is the interpreter we are actually building against (under conda that is the
#         'host' prefix Python, whereas the component would report the 'build' prefix when cross-compiling), and
#       - requesting the 'NumPy' component implicitly adds 'Interpreter' to that find_package() call, and on
#         CMake 4.1 'Development.Module' along with it, which re-triggers CMP0190 when cross-compiling no matter
#         how the calls above are split. CMake >= 4.2 drops that second implication (policy CMP0201), but there
#         is no reason to depend on the CMake version here when the interpreter can just be asked directly.
execute_process(COMMAND "${GPLATES_PYTHON_EXECUTABLE}" "-c" "import numpy as np; print(np.get_include());"
	RESULT_VARIABLE _NUMPY_RESULT
	OUTPUT_VARIABLE _NUMPY_OUTPUT
	ERROR_VARIABLE _NUMPY_ERROR
	OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_STRIP_TRAILING_WHITESPACE)
if (NOT _NUMPY_RESULT)  # success
	file(TO_CMAKE_PATH ${_NUMPY_OUTPUT} GPLATES_PYTHON_NUMPY_INCLUDE_DIRS)  # Convert '\' to '/' in paths.

	# Make sure the NumPy C-API header is actually in there.
	#
	# Note: 'Config_h.cmake' only defines GPLATES_HAVE_NUMPY_C_API when it finds this header, so an include
	#       directory without it leaves us just as badly off as not finding NumPy at all.
	if (NOT EXISTS "${GPLATES_PYTHON_NUMPY_INCLUDE_DIRS}/numpy/arrayobject.h")
		set(_NUMPY_ERROR "There is no 'numpy/arrayobject.h' in '${GPLATES_PYTHON_NUMPY_INCLUDE_DIRS}'.")
		unset(GPLATES_PYTHON_NUMPY_INCLUDE_DIRS)
	endif()
endif()

# NumPy is required when building pyGPlates, but only recommended when building GPlates.
#
# Without the NumPy C-API everything still builds, but silently loses the ability to accept NumPy int/float
# scalars as arguments to Python functions (see GPLATES_HAVE_NUMPY_C_API in the source). For pyGPlates that is a
# broken package rather than a reduced one: 'pyproject.toml' declares numpy as an unconditional *runtime*
# dependency, so every pyGPlates user has NumPy and can hand us its scalars. And the failure is invisible until
# one of them does, which is far too late - hence a fatal error here.
#
# GPlates merely embeds Python, where NumPy is genuinely optional, so there it stays a warning (which also keeps
# a from-source build working in a minimal Python installation).
if (NOT GPLATES_PYTHON_NUMPY_INCLUDE_DIRS)
	set(_NUMPY_FAILURE
		"Unable to get the NumPy C-API include directory from the Python interpreter:\n"
		"    ${GPLATES_PYTHON_EXECUTABLE}\n"
		"${_NUMPY_ERROR}\n")
	if (GPLATES_BUILD_GPLATES)
		message(WARNING ${_NUMPY_FAILURE}
			"You will not be able to pass NumPy int/float scalars as arguments to Python functions. "
			"To enable this functionality please install NumPy into the above Python installation and then "
			"re-run CMake (to enable use of the NumPy C-API).")
	else()
		message(FATAL_ERROR ${_NUMPY_FAILURE}
			"NumPy is required when building pyGPlates: it is an unconditional runtime dependency of the "
			"pyGPlates package (see 'dependencies' in 'pyproject.toml') and its C-API is what lets pyGPlates "
			"accept NumPy int/float scalars as arguments. Please install NumPy into the above Python "
			"installation and then re-run CMake.")
	endif()
endif()

# Set the Python executable in the parent scope (parent directory).
#
# When building pyGPlates it is used to test/install the pyGPlates Python module and to generate the pyGPlates API documentation.
set(GPLATES_PYTHON_EXECUTABLE ${GPLATES_PYTHON_EXECUTABLE} PARENT_SCOPE)

##############
# Find Boost #
#
# Use dynamic linking for all Boost libraries.
# This includes the optional unit test framework, since it's error prone to switch static linking off and on, and
# note that we set the compiler flag BOOST_TEST_DYN_LINK later below (required by code using unit test framework).
set(Boost_USE_STATIC_LIBS FALSE)
#
# Starting with version 1.70 Boost provides a CMake package configuration file so that CMake does not have to update
# its builtin FindBoost module for every Boost release. For backward compatibility CMake (< 3.30) provides a FindBoost module but
# it will use a Boost CMake configuration file if it finds one. However, on Windows, the Boost config file can give the error
# "No suitable build variant has been found" (if you compile GPlates/pyGPlates with a different version of Visual Studio
# than was used to compile Boost). So we'll disable the search for the Boost config file (for CMake < 3.30) but just on Windows.
# This also means that, on Windows, the CMake version (when < 3.30) should be high enough that it supports the Boost version
# (since each CMake version updates the supported Boost versions).
#
# CMake 3.30 removed the FindBoost module (thus requiring boost-cmake), which means only CMake < 3.30 can disable search for boost-cmake.
if (CMAKE_VERSION VERSION_LESS 3.30)
	if (WIN32)
		set(Boost_NO_BOOST_CMAKE ON)
	endif()
endif()

# Minimum required Boost version.
#
if (CMAKE_VERSION VERSION_LESS 3.30)
	# Version 1.69 is when the boost::system became header-only and no longer required adding the boost::system module (which was removed in 1.89).
	# Version 1.55 is when boost::is_copy_constructible<T> was introduced (although can use std version in C++11 compilers now).
	# Version 1.39 is when <boost/bind/bind.hpp> was added (before that there was just <boost/bind.hpp>).
	set(_GPLATES_MIN_BOOST_VERSION 1.69)
else()  # CMake >= 3.30 ...
	# CMake 3.30 removed the FindBoost module, which means Boost needs to provide a CMake package configuration file and
	# only Boost >= 1.70 provides that.
	set(_GPLATES_MIN_BOOST_VERSION 1.70)
endif()

# First find the Boost library version (it decides how the Boost Python component is named below).
# We find the library version by finding Boost without any library components (this just finds the Boost headers).
find_package(Boost ${_GPLATES_MIN_BOOST_VERSION} REQUIRED)

# Boost Python components have Python version suffixes (eg, 'python37' and 'numpy37').
if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.30 AND NOT WIN32 AND Boost_VERSION VERSION_LESS 1.71)
	# CMake 3.30 removed the FindBoost module (thus requiring boost-cmake). (This also means our Boost version must be >= 1.70).
	# We could use Python version suffixes but Boost 1.70's boost-cmake scripts have an error (fixed in Boost 1.71 by
	# https://github.com/boostorg/boost_install/commit/e193f08) where it fails to detect 3-digits Python version (ie, boost_python310).
	# However specifying NO version works, so we'll just specify no version (for this old Boost).
	set(GPLATES_BOOST_PYTHON_COMPONENT_NAME python)
	set(GPLATES_BOOST_PYTHON_NUMPY_COMPONENT_NAME numpy)
else()
	# Match Boost Python with the Python version we found earlier.
	#
	# Note: An unversioned component (eg, 'python' rather than 'python313') would also work via boost-cmake when
	#       only one Boost.Python variant is installed. But when multiple variants are installed (eg, the manylinux
	#       wheel image installs boost_python39...boost_python314 side by side) the unversioned config just picks
	#       the first variant it finds - which is the wrong Python version for all but one wheel. So always request
	#       the exact variant. Also, on Windows, the boost-python target WITHOUT a version (eg, Boost::python)
	#       has trouble finding the DLL at runtime.
	set(GPLATES_BOOST_PYTHON_COMPONENT_NAME python${GPLATES_PYTHON_VERSION_MAJOR}${GPLATES_PYTHON_VERSION_MINOR})
	set(GPLATES_BOOST_PYTHON_NUMPY_COMPONENT_NAME numpy${GPLATES_PYTHON_VERSION_MAJOR}${GPLATES_PYTHON_VERSION_MINOR})
endif()

# Now find the Boost library components.
find_package(Boost ${_GPLATES_MIN_BOOST_VERSION} REQUIRED
		# These components are required (including boost python)...
		COMPONENTS program_options thread ${GPLATES_BOOST_PYTHON_COMPONENT_NAME}
		#
		# These components are optional...
		OPTIONAL_COMPONENTS
			#
			# Boost.Python.Numpy is optional because we actually haven't started using it yet and
			# it requires Boost >= 1.63 (which is above our current minimum Boost requirement).
			#
			# UPDATE: Since NumPy 2.0 has been released, and we're not yet using Boost.Python.Numpy, we'll just disable it for now.
			#
			#         To support both NumPy 1.x and 2.x at *runtime* we need to build pyGPlates against NumPy 2.0
			#         (note that pyGPlates accesses the NumPy C API directly - see 'GPLATES_PYTHON_NUMPY_INCLUDE_DIRS').
			#         For building pyGPlates wheels, that's taken care of in our 'pyproject.toml' (with 'numpy>=2.0').
			#         But for accessing the NumPy C API via Boost.Python.NumPy (which we don't do yet) it means
			#         Boost.Python itself must be built against NumPy 2.0. And ensuring this is a bit more work.
			#         For example, on Windows you'd need to make sure you install NumPy 2.0 into your Python versions
			#         prior to building Boost from source code. And on macOS/Linux you'd need to ensure that the
			#         NumPy installed into your build environment (eg, conda) is updated (to 2.0).
			#
			#         So, since we're not yet using Boost.Python.NumPy we'll just disable it to avoid the possible runtime error:
			#           "A module that was compiled using NumPy 1.x cannot be run in NumPy 2.0.0 as it may crash."
			#
			#${GPLATES_BOOST_PYTHON_NUMPY_COMPONENT_NAME}
		)

############
# Find Qt #
#
# CMake automatically runs Qt's moc, uic and rcc code generation tools.
# See https://doc.qt.io/qt-5/cmake-get-started.html
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
#
# Require Qt 5.15 (but use Qt6 when available since it has bug-fixes beyond the Qt5.15 non-commercial version).
#
# Our minimum supported Ubuntu is Jammy 22.04 which uses Qt 5.15.
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core)
#
# The pygplates module needs only Qt Core. In particular it must not link Qt Gui: on Linux and
# macOS 'Qt6::Gui' propagates Qt's own OpenGL dependency, which is what used to make the Linux
# wheel require libGL. The Gui, Network, Svg, Widgets and OpenGL modules serve code the module
# does not compile (see 'cmake/pygplates_source_closure.py' and the pygplates-linkage test).
# All of the module's XML work, the GeoSciML reader included, is QXmlStreamReader/Writer, which
# lives in Core under both Qt5 and Qt6.
#
if (QT_VERSION_MAJOR EQUAL 6)
	# Note: We don't need Qt6::Core5Compat since we no longer use any Qt5-only APIs
	# (the last was QRegExp, now QRegularExpression, which is in Qt Core under both Qt5 and Qt6).
	#
	# Qt6 moved the QOpenGL* classes from (Gui and Widgets modules in Qt5) into OpenGL (and new OpenGLWidgets) modules.
	if (GPLATES_BUILD_GPLATES)
		find_package(Qt6 REQUIRED COMPONENTS Core Gui Network Svg Widgets Xml OpenGL OpenGLWidgets)
	else()
		find_package(Qt6 REQUIRED COMPONENTS Core)
	endif()
else()
	# In Qt5, the modern QOpenGL* classes are in the Gui and Widgets modules (not OpenGL and OpenGLWidgets modules).
	if (GPLATES_BUILD_GPLATES)
		find_package(Qt5 5.15 REQUIRED COMPONENTS Core Gui Network Svg Widgets Xml)
	else()
		find_package(Qt5 5.15 REQUIRED COMPONENTS Core)
	endif()
endif()

############
# Find Qwt (GPlates only) #
#
# Note that we find this *after* finding Qt since we use the Qt include directory to help find Qwt.
# And we use our own "FindQwt.cmake" module, which also defines the Qwt::Qwt imported target.
#
if (GPLATES_BUILD_GPLATES)
	find_package(Qwt REQUIRED)
endif()

#############
# Find GDAL #
#
find_package(GDAL 2.0 REQUIRED)

#############
# Find PROJ #
#
# GPlates requires PROJ 6 or above (see README.md) and uses only the modern 'proj.h' API.
# We use our own FindPROJ.cmake module: it prefers PROJ's CMake config package (eg, conda) but
# falls back to a manual library search for distributions that ship no config package. Either way
# it provides the PROJ::proj target (linked below).
#
# Note: FindPROJ.cmake is kept only for Ubuntu 22.04, whose 'libproj-dev' ships no CMake config
# package (and CMake has no built-in FindPROJ), so the manual fallback is the only way to locate
# PROJ there. Remove this module once our minimum Ubuntu LTS provides a PROJ config package.
find_package(PROJ REQUIRED)


#################
# Initial setup #
#################



# Generate the version source file.
configure_file(${PROJECT_SOURCE_DIR}/src/global/Version.cc.in ${PROJECT_BINARY_DIR}/src/global/Version.cc @ONLY)

# Generate the license source file.
configure_file(${PROJECT_SOURCE_DIR}/src/global/License.cc.in ${PROJECT_BINARY_DIR}/src/global/License.cc @ONLY)

# Generate 'global/config.h' from 'global/config.h.in'.
include (Config_h)
configure_file(${PROJECT_SOURCE_DIR}/src/global/config.h.in ${PROJECT_BINARY_DIR}/src/global/config.h @ONLY)


##################
# Create targets #
##################


if (GPLATES_BUILD_GPLATES)

	# The target to add source files (via target_sources()) and other target properties (eg, via target_link_libraries()).
	# This is the gplates library, which then propagates target properties to those depending on it (such as target 'gplates').
	set(SOURCE_TARGET gplates-lib)
	# The build target (to install and package).
	set(BUILD_TARGET gplates)

	#
	# Create a single static library containing all source code except the main source file(s) in current directory.
	#
	# Note that any target INTERFACE_* properties set on 'gplates-lib', via "target_*(gplates-lib PUBLIC ...)", are
	# inherited by targets linking to 'gplates-lib', such as 'gplates' and 'gplates-unit-test'.
	#
	# For now just add the generated version and license source files.
	# Below we'll add the remaining source files in 'add_subdirectory()' calls.
	add_library(gplates-lib STATIC EXCLUDE_FROM_ALL
		${PROJECT_BINARY_DIR}/src/global/Version.cc
		${PROJECT_BINARY_DIR}/src/global/License.cc
		${PROJECT_BINARY_DIR}/src/global/config.h)

	#
	# Add 'gplates' executable target (linked to gplates-lib).
	#
	add_executable(gplates gplates_main.cc ScribeExportGPlates.cc)
	target_link_libraries(gplates PRIVATE gplates-lib)

	#
	# Add 'gplates-unit-test' executable (linked to gplates-lib).
	#
	# It's only added if GoogleTest is available (eg, the conda-forge 'gtest' package, which provides
	# 'GTestConfig.cmake' with imported targets GTest::gtest and GTest::gtest_main).
	# GoogleTest remains optional so that GPlates can still be built without it, but CI builds the
	# 'gplates-unit-test' target explicitly and therefore fails loudly if GoogleTest is missing.
	# It will be populated by source files from the 'unit-test/' sub-directory.
	find_package(GTest QUIET)
	if (TARGET GTest::gtest)
		add_executable(gplates-unit-test EXCLUDE_FROM_ALL gplates_unit_test_main.cc ScribeExportGPlatesUnitTest.cc)
		# Note: We link GTest::gtest (not GTest::gtest_main) because we provide our own main()
		#       (in 'gplates_unit_test_main.cc') that sets up Qt before running the tests.
		target_link_libraries(gplates-unit-test PRIVATE gplates-lib GTest::gtest)

		# Tests that read data files from 'src/unit-test/data/' get its absolute path from this
		# compile definition, so the tests do not depend on the current working directory
		# (they can be run from anywhere, and CTest does not need to set a working directory).
		target_compile_definitions(gplates-unit-test PRIVATE
			"GPLATES_UNIT_TEST_DATA_DIR=\"${CMAKE_CURRENT_SOURCE_DIR}/unit-test/data\"")

		# Register each GoogleTest test case as an individual CTest test.
		#
		# The tests are registered in every build configuration. A failed GPlatesGlobal::Assert
		# aborts by default in a GPLATES_DEBUG (Debug or RelWithDebInfo) build of GPlates, which
		# would kill the tests that exercise error paths, so 'gplates_unit_test_main.cc' makes it
		# throw instead (see 'global/GPlatesAssert.h'). Note: gtest_discover_tests() cannot attach
		# CONFIGURATIONS to the discovered tests, so for multi-config generators (Visual Studio/Xcode)
		# the tests are discovered for whichever configuration was built - pass that configuration
		# to 'ctest -C'.
		#
		# Note: Do not pass WORKING_DIRECTORY (or a PROPERTIES WORKING_DIRECTORY - the keyword
		# would be re-parsed as the WORKING_DIRECTORY argument): discovery leaves a
		# 'cmake_test_discovery_*.json' scratch file in its working directory, which must
		# therefore remain the build directory. The tests themselves are working-directory
		# independent (see GPLATES_UNIT_TEST_DATA_DIR above).
		include(GoogleTest)
		gtest_discover_tests(gplates-unit-test
			# Discovery (running the executable with '--gtest_list_tests' after building it) can
			# be slow on a busy CI runner since the executable links the entire gplates-lib.
			DISCOVERY_TIMEOUT 120
			PROPERTIES TIMEOUT 600)
	else()
		# Only print status message if this is not a public release.
		# Because it'll only confuse users (because gplates-unit-test is not part of public release).
		if (NOT GPLATES_PUBLIC_RELEASE)
			message(STATUS "Warning: GoogleTest (gtest) not found so GPlates unit-test executable gplates-unit-test will not be available.")
		endif()
	endif()

else()  # pygplates ...

	# The target to add source files (via target_sources()) and other target properties (eg, via target_link_libraries()).
	set(SOURCE_TARGET pygplates)
	# The build target (to install and package).
	set(BUILD_TARGET pygplates)
	
	#
	# Add the 'pygplates' Python extension module.
	#
	# Note this is the external Python library that is not embedded inside GPlates.
	Python3_add_library(pygplates MODULE ScribeExportPyGPlates.cc)
	# All sources files compiled in pygplates need to be position independent and hence compiled differently than 'gplates-lib'.
	# This is why it's not linked to 'gplates-lib' and why source files get added to both 'pygplates' and 'gplates-lib'.
	#
	# It's also why any target INTERFACE_* properties set on 'gplates-lib', via "target_*(gplates-lib PUBLIC ...)", are *not*
	# inherited by target 'pygplates' (because 'pygplates' does not link to 'gplates-lib'), and hence also need to be set on
	# 'pygplates' via "target_*(pygplates ...)".
	#
	# Note that module libraries default to position-independent code, but we'll set it anyway.
	set_target_properties(pygplates PROPERTIES POSITION_INDEPENDENT_CODE ON)

	#
	# Build the pygplates module library into a 'pygplates/' sub-directory of the build output directory.
	#
	# This gives the build tree the same shape as the installed Python package, which is a 'pygplates/' package
	# directory containing the pygplates module library and an '__init__.py' (see 'cmake/modules/Install.cmake',
	# which also generates that '__init__.py', and the type stub, into this same directory).
	#
	# So '<build_dir>/bin' (or '<build_dir>/bin/<config>' for a multi-config generator) is a directory you can
	# put on 'sys.path' (or PYTHONPATH) to "import pygplates" exactly as an end user would - which is what our
	# tests, and the type stub generator, do. Note this is the same directory as before this became a package,
	# so an existing "PYTHONPATH=<build_dir>/bin/Release" still works (it now finds the package, not the library).
	#
	# Note: a multi-config generator (eg, Visual Studio) normally appends a per-configuration sub-directory to
	#       LIBRARY_OUTPUT_DIRECTORY, but it does *not* do that when the value contains a generator expression -
	#       so we have to interpose '$<CONFIG>' ourselves. And only for a multi-config generator, since a
	#       single-config generator (eg, Ninja, Unix Makefiles) has no per-configuration sub-directory.
	#
	# Note: a MODULE library uses LIBRARY_OUTPUT_DIRECTORY on *all* platforms (unlike a SHARED library, whose
	#       DLL on Windows uses RUNTIME_OUTPUT_DIRECTORY), and has no import library, so this is the only
	#       output directory that needs redirecting.
	#
	if (CMAKE_CONFIGURATION_TYPES)  # multi-config generator (eg, Visual Studio, Xcode) ...
		set(_pygplates_package_build_dir "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/$<CONFIG>/pygplates")
	else()  # single-config generator (eg, Ninja, Unix Makefiles) ...
		set(_pygplates_package_build_dir "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/pygplates")
	endif()
	set_target_properties(pygplates PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${_pygplates_package_build_dir}")
	unset(_pygplates_package_build_dir)
	target_sources(pygplates PRIVATE
		${PROJECT_BINARY_DIR}/src/global/Version.cc
		${PROJECT_BINARY_DIR}/src/global/License.cc
		${PROJECT_BINARY_DIR}/src/global/config.h)

	#
	# Write the module's full source list at generate time, for the pygplates-source-closure test
	# (which diffs it against the include closure of the pyGPlates API - the module must compile
	# exactly that closure; see 'cmake/pygplates_source_closure.py').
	#
	# Note: a multi-config generator (eg, Visual Studio) evaluates 'file(GENERATE)' once per
	#       configuration, and the lists differ (AUTOMOC/AUTORCC name their generated sources
	#       after the configuration, eg 'mocs_compilation_Release.cpp'), so writing them all to
	#       one file is an error. So write one file per configuration. They differ only in
	#       generated sources, which the test ignores.
	#
	# The test ('pygplates/test/CMakeLists.txt') reads the file name from the global property
	# GPLATES_PYGPLATES_SOURCES_FILE, so that it is decided only here. For a multi-config
	# generator, it contains '$<CONFIG>' (which 'add_test()' evaluates for the configuration
	# being tested).
	#
	get_property(_generator_is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
	if (_generator_is_multi_config)  # multi-config generator (eg, Visual Studio, Xcode) ...
		set(_pygplates_sources_file "${PROJECT_BINARY_DIR}/pygplates_sources_$<CONFIG>.txt")
	else()  # single-config generator (eg, Ninja, Unix Makefiles) ...
		set(_pygplates_sources_file "${PROJECT_BINARY_DIR}/pygplates_sources.txt")
	endif()
	file(GENERATE OUTPUT "${_pygplates_sources_file}"
		CONTENT "$<JOIN:$<TARGET_PROPERTY:pygplates,SOURCES>,\n>")
	set_property(GLOBAL PROPERTY GPLATES_PYGPLATES_SOURCES_FILE "${_pygplates_sources_file}")
	unset(_pygplates_sources_file)
	unset(_generator_is_multi_config)

endif()


##########################
# Source sub-directories #
##########################


#
# Recurse into source sub-directories.
#

# Specify source sub-directories.
set(source_sub_directories 
	api
	app-logic
	canvas-tools
	cli
	data-mining
	file-io
	global
	gui
	maths
	model
	opengl
	presentation
	property-values
	qt-resources
	qt-widgets
	scribe
	unit-test
	utils
	view-operations)

# Specify the default grouping of source files (for display in Visual Studio and XCode IDEs).
# This will apply to the top-level source directories.
#
# Note: The last regular expression that matches a source file applies.
#       So we need to specify the default/general source groups first.
source_group(sources REGULAR_EXPRESSION "\\.(cc|cpp|cxx)$")
source_group(headers REGULAR_EXPRESSION "\\.hh?$")
source_group(ui REGULAR_EXPRESSION "\\.ui$")
source_group(qrc REGULAR_EXPRESSION "\\.qrc$")
source_group(rc REGULAR_EXPRESSION "\\.rc$")

foreach(sub_dir ${source_sub_directories})
	# Traverse into sub-directory "CMakeLists.txt".
	add_subdirectory(${sub_dir})
	
	# Specify specific grouping of source files specific to the current sub-directory (for display in Visual Studio and XCode IDEs).
	#
	# Here we give each sub-directory its own folder group.
	# And under each sub-directory folder we have folders for each source file category (".cc", ".h", ".ui", ".qrc").
	#
	# Note: The groups defined by 'source_group' are scoped in the directory where it is called.
	#       So, since we're matching subdirectories in the regular expressions, we cannot call 'source_group'
	#       in the sub-directory "CMakeLists.txt" files.
	source_group(${sub_dir}\\sources REGULAR_EXPRESSION "/${sub_dir}/[a-zA-Z0-9_-]+\\.(cc|cpp|cxx)$")
	source_group(${sub_dir}\\headers REGULAR_EXPRESSION "/${sub_dir}/[a-zA-Z0-9_-]+\\.hh?$")
	source_group(${sub_dir}\\ui REGULAR_EXPRESSION "/${sub_dir}/[a-zA-Z0-9_-]+\\.ui$")
	source_group(${sub_dir}\\qrc REGULAR_EXPRESSION "/${sub_dir}/[a-zA-Z0-9_-]+\\.qrc$")
	source_group(${sub_dir}\\rc REGULAR_EXPRESSION "/${sub_dir}/[a-zA-Z0-9_-]+\\.rc$")
endforeach()

# Enable the location of each target to be specified using the FOLDER target property (for Visual Studio and XCode).
# This also enables the default placement of some targets created by CMake under a folder called "CMakePredefinedTargets".
set_property(GLOBAL PROPERTY USE_FOLDERS ON)


#############################
# General target properties #
#############################



#
# General COMPILE_FEATURES (target property).
#

# Set the minimum C++ language standard to C++17.
#
# Qt 6 and CGAL 6 both require C++17 (and pass that requirement on through their imported targets), so nearly
# every build was already compiled as C++17. Requiring it here makes the Qt5 builds that use an older CGAL
# (eg, Ubuntu's own packages) compile with the same standard, and lets shared code rely on C++17.
#
# Note: A compile feature (rather than the CXX_STANDARD property) is a minimum, so a dependency that needs a
#       later standard still gets it.
target_compile_features(${SOURCE_TARGET} PUBLIC cxx_std_17)
#
# Disable compiler-specific extensions (eg, '-std=c++17' rather than '-std=gnu++17' on GNU compilers).
# With CMake policy CMP0128 (CMake 3.22) this is honoured without also setting CXX_STANDARD.
set_target_properties(${SOURCE_TARGET} PROPERTIES CXX_EXTENSIONS OFF)


#
# General PRECOMPILE_HEADERS (target property).
#

# Use pre-compiled headers for targets 'gplates-lib' and 'pygplates'.
#
# These targets have the largest amount of source code.
# Each target has its own pre-compiled "_pch.h" header that currently lists only external headers.
# Some internal headers that are used a lot but infrequently changed (such as utils) could be added also.
if (GPLATES_USE_PRECOMPILED_HEADERS)
	target_precompile_headers(${SOURCE_TARGET} PRIVATE ${PROJECT_SOURCE_DIR}/src/${SOURCE_TARGET}_pch.h)

	# Exclude specific source files from including pre-compiled header.
	#
	# NOTE: Source file properties are visible only to targets added in the same directory.
	#       So unfortunately we must set them here (we cannot set them in the source sub-directories).
	#
	set(_EXCLUDE_PCH_SOURCE_FILES
			# HellingerThread.cc redefines BOOST_PYTHON_MAX_ARITY, so Boost headers (eg, pre-compiled header) cannot be included before that...
			qt-widgets/HellingerThread.cc)
	foreach(_SOURCE_FILE IN LISTS _EXCLUDE_PCH_SOURCE_FILES)
		set_source_files_properties(${_SOURCE_FILE} PROPERTIES SKIP_PRECOMPILE_HEADERS ON)
	endforeach()
	unset(_EXCLUDE_PCH_SOURCE_FILES)
endif()


#
# General INCLUDE_DIRECTORIES (target property).
#

# A lot of "#include" statements are relative to the 'src' directory.
target_include_directories(${SOURCE_TARGET} PUBLIC ${PROJECT_SOURCE_DIR}/src)
# The 'src' directory exists in both the CMake source and binary trees (which, while same for in-place builds,
# are different for out-of-place builds). This is so generated source files (like 'global/config.h') can be found.
target_include_directories(${SOURCE_TARGET} PUBLIC ${PROJECT_BINARY_DIR}/src)

# External library include directories are treated as system include directories.
# 'src/system-fixes' contains files copied from external libraries.
target_include_directories(${SOURCE_TARGET} SYSTEM PUBLIC ${PROJECT_SOURCE_DIR}/src/system-fixes)


#
# General COMPILE_DEFINITIONS (target property).
#

# Add GPLATES_DEBUG preprocessor define to DEBUG and RELWITHDEBINFO configurations.
target_compile_definitions(${SOURCE_TARGET} PUBLIC $<$<CONFIG:DEBUG>:GPLATES_DEBUG> $<$<CONFIG:RELWITHDEBINFO>:GPLATES_DEBUG>)
# Boost 1.58 introduced a breaking change in boost::variant that does compile-time with boost::get<U>(variant)
# to see if U is one of the variant types. However it seems to generate compile errors for references and boost::optional.
# So we'll default to using the old relaxed (run-time) method.
target_compile_definitions(${SOURCE_TARGET} PUBLIC BOOST_VARIANT_USE_RELAXED_GET_BY_DEFAULT)
# Temporary avoidance of warning in Boost due to bug in version 1.69 caused by using deprecated "boost/pending/integer_log2.hpp".
# Apparently it wasn't fixed in 1.69 (only 1.70 and above).
target_compile_definitions(${SOURCE_TARGET} PUBLIC BOOST_ALLOW_DEPRECATED_HEADERS)

#
# COMPILE_DEFINITIONS (target property) for MSVC compiler only.
#
if (CMAKE_CXX_COMPILER_ID MATCHES MSVC)
	# Disable Visual Studio deprecation warnings like:
	# "warning C4996: '_strcpy': This function or variable may be unsafe. Consider using _strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. ".
	target_compile_definitions(${SOURCE_TARGET} PUBLIC _CRT_SECURE_NO_DEPRECATE)
	# Disable deprecation warnings about renamed POSIX functions (eg, 'dup' renamed to '_dup' but we want to use 'dup' across all platforms).
	target_compile_definitions(${SOURCE_TARGET} PUBLIC _CRT_NONSTDC_NO_WARNINGS)
	# Disable warning C4996 / STL4043 (eg, "'stdext::checked_array_iterator<const T *>::_Verify_offset': warning STL4043:
	# stdext::checked_array_iterator, stdext::unchecked_array_iterator, and related factory functions are non-Standard
	# extensions and will be removed in the future...").
	#
	# This comes from Qt5's own qcompilerdetection.h using the MSVC-specific stdext::checked_array_iterator extension
	# (via QT_MAKE_CHECKED_ARRAY_ITERATOR/QT_MAKE_UNCHECKED_ARRAY_ITERATOR), which newer MSVC STL versions deprecate.
	# We have no control over Qt5's internals, so just silence it.
	target_compile_definitions(${SOURCE_TARGET} PUBLIC _SILENCE_STDEXT_ARR_ITERS_DEPRECATION_WARNING)
endif()

#
# General COMPILE_OPTIONS (target property).
#

#
# COMPILE_OPTIONS (target property) for MSVC compiler only.
#
if (CMAKE_CXX_COMPILER_ID MATCHES MSVC)
	# If we've been asked to do parallel builds in Visual Studio within a project.
	if (GPLATES_MSVC_PARALLEL_BUILD)
		if (GPLATES_MSVC_PARALLEL_BUILD_PROCESSES)
			# User specified the number of parallel build processes.
			target_compile_options(${SOURCE_TARGET} PUBLIC /MP${GPLATES_MSVC_PARALLEL_BUILD_PROCESSES})
		else()
			# Use all available CPUs.
			target_compile_options(${SOURCE_TARGET} PUBLIC /MP)
		endif()
	endif()
	# Some C++ object files (like "PyPropertyValues.obj") contain a lot of sections (enough to require increasing the limit).
	target_compile_options(${SOURCE_TARGET} PUBLIC /bigobj)
	
	#
	# Warnings
	#
	if (GPLATES_PUBLIC_RELEASE)
		# Disable all warnings when releasing source code to non-developers.
		target_compile_options(${SOURCE_TARGET} PUBLIC /W0)
	else()
		# Default warning level /W3 seems sufficient (/W4 generates informational warnings which are not necessary to write good code).
		# But avoid /WX - we don't want to treat all warnings as errors.
		target_compile_options(${SOURCE_TARGET} PUBLIC /W3)
		
		# Disable warning C4267: 'var' : conversion from 'size_t' to 'type', possible loss of data
		#
		# When compiling with Visual Studio in 64-bit mode this warning shows up in a very large number of places.
		# Mostly because std::vector<Type>::size(), etc, return 'size_t' (which is 64 bits) and we store the result
		# in an 'unsigned int' loop counter (which is 32 bits). However, in pretty much all cases we do not
		# need more than 32 bits. We could explicitly tell the compiler this by using 'static_cast<unsigned int>()'
		# (or use size_t for the loop counter) but it just becomes far too cumbersome to change this everywhere.
		#
		# Also this warning is not produced by gcc, even with "-Wall" and "-Wextra" turned on (just checked this with gcc 8.3).
		# The only way to enable this warning with gcc is with "-Wconversion" (and then use "-W-no-float-conversion" to
		# disable the extra 'double->float' warnings also created by this).
		#
		# So we'll disable it for Visual Studio also.
		target_compile_options(${SOURCE_TARGET} PUBLIC /wd4267)
		# Disable warning C4503: 'identifier' : decorated name length exceeded, name was truncated
		#
		# Apparently a hash is applied to truncated names, so program correctness is unaffected.
		# However debugging and linking are possibly affected.
		# But this warning no longer occurs in Visual Studio 2017 and later compilers.
		target_compile_options(${SOURCE_TARGET} PUBLIC /wd4503)
	endif()
endif()

#
# COMPILE_OPTIONS (target property) common to GNU and Clang compilers.
#
if (CMAKE_CXX_COMPILER_ID MATCHES GNU OR
	CMAKE_CXX_COMPILER_ID MATCHES Clang)
	#
	# Warnings
	#
	if (GPLATES_PUBLIC_RELEASE)
		# Disable all warnings when releasing source code to non-developers.
		target_compile_options(${SOURCE_TARGET} PUBLIC -w)
	else()
		# Compile warnings.
		set(_WARNINGS
				-Wall -Wcast-align -Wwrite-strings -Wfloat-equal
				-Wpointer-arith -Wshadow -Wnon-virtual-dtor
				-Woverloaded-virtual -Wold-style-cast)
		target_compile_options(${SOURCE_TARGET} PUBLIC ${_WARNINGS})
		unset(_WARNINGS)

		# Disable some warnings.
		set(_DISABLE_WARNINGS
				-Wno-long-long -Wno-unused-parameter -Wno-unused-const-variable
				# Prefer to keep unused functions available...
				-Wno-unused-function
				# Keep unused local typedefs - used by some compilers but not others...
				-Wno-unused-local-typedefs
				# Keep variables assigned to but otherwise unused...
				-Wno-unused-but-set-variable)
		target_compile_options(${SOURCE_TARGET} PUBLIC ${_DISABLE_WARNINGS})
		unset(_DISABLE_WARNINGS)
	endif()

	#
	# No floating-point contraction (fusing 'a * b + c' into one FMA instruction, rounded once).
	# Clang contracts by default wherever the target has FMA, which is every arm64 target (Apple
	# Silicon, Linux aarch64), and GCC does too when the target has it. The geometry code's exact
	# sign tests and tie orders assume each operation is rounded on its own, as on x86-64 and with
	# MSVC (whose default /fp:precise does not contract), so the results would differ by platform:
	# on arm64 the polygon overlay's comparisons against its prototype fail.
	#
	target_compile_options(${SOURCE_TARGET} PUBLIC -ffp-contract=off)
endif()

#
# COMPILE_OPTIONS (target property) for GNU compiler only.
#
if (CMAKE_CXX_COMPILER_ID MATCHES GNU)
	target_compile_options(${SOURCE_TARGET} PUBLIC -fno-strict-aliasing)

	#
	# Warnings
	#
	if (NOT GPLATES_PUBLIC_RELEASE)
		# Disable some warnings.
		set(_DISABLE_WARNINGS
				-Wno-clobbered -Wno-maybe-uninitialized
				# Keep the ABI-change notes out of the build log, eg "parameter passing for
				# argument of type 'GPlatesMaths::Real' when C++17 is enabled changed to match
				# C++14 in GCC 10.1". These fire on aarch64 (and ppc64le) for our small classes
				# wrapping one or two doubles, in the thousands - drowning out real diagnostics.
				#
				# They warn that GCC before 10.1 passed such an argument differently when
				# compiling as C++17, so they only matter if objects compiled by an older GCC
				# are linked with objects compiled by GCC 10.1 or later. That cannot happen when
				# the project and its dependencies come from a single toolchain (eg, the wheel
				# builds compile everything with the manylinux image's GCC), and any dependency
				# binaries we don't build ourselves (conda-forge, system packages) have long
				# since been built with a newer GCC - 10.1 was released in May 2020.
				-Wno-psabi)

		# Disable '-Wuninitialized' for g++ >= 12 and Boost <= 1.80.
		#
		# G++ 12 started treating some '-Wmaybe-uninitialized' warnings as '-Wuninitialized'
		# warnings creating a problem for boost::function in Boost 1.80 and ealier (but Boost fixed
		# it for 1.81 by disabling same warning: https://github.com/boostorg/function/pull/43)...
		if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 12)
			if (Boost_VERSION VERSION_LESS_EQUAL 1.80.0)
				list(APPEND _DISABLE_WARNINGS -Wno-uninitialized)
			endif()
		endif()

		target_compile_options(${SOURCE_TARGET} PUBLIC ${_DISABLE_WARNINGS})
		unset(_DISABLE_WARNINGS)
	endif()
endif()

#
# COMPILE_OPTIONS (target property) for Clang compiler only.
#
if (CMAKE_CXX_COMPILER_ID MATCHES Clang)
	#
	# Warnings
	#
	if (NOT GPLATES_PUBLIC_RELEASE)
		# Disable some warnings.
		set(_DISABLE_WARNINGS
				# Suppress redeclared-class-member warnings from boost (from boost 1.47 at least), related to BOOST_BIMAP...
				-Wno-redeclared-class-member
				# Suppress warnings about command-line arguments unused by Clang...
				-Qunused-arguments -Wno-unknown-warning-option
				# We don't always use private data members...
				-Wno-unused-private-field
				# We have a lot of boost-python 'bp::list' returned as 'bp::object', for example, which we won't std::move...
				-Wno-return-std-move)

		# Disable '-Wenum-constexpr-conversion' for clang++ >= 16 and Boost < 1.85.
		#
		# It looks like Boost is fixing it, so fix should be in the next release 1.85
		# (see https://github.com/boostorg/mpl/pull/77).
		if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 16)
			if (Boost_VERSION VERSION_LESS 1.85.0)
				list(APPEND _DISABLE_WARNINGS -Wno-enum-constexpr-conversion)
			endif()
		endif()

		target_compile_options(${SOURCE_TARGET} PUBLIC ${_DISABLE_WARNINGS})
		unset(_DISABLE_WARNINGS)
	endif()
endif()


#
# General LINK_OPTIONS (target_property).

#
# LINK_OPTIONS (target_property) for MSVC compiler only.
#
if (CMAKE_CXX_COMPILER_ID MATCHES MSVC)
	if (GPLATES_BUILD_GPLATES)
		# Enable 4Gb of virtual address space instead of 2Gb (default for Windows).
		# This doubles addressable memory if GPlates is compiled as 32-bit but run on a 64-bit Windows OS.
		# On a 32-bit Windows OS this won't help because only 2Gb (by default) is accessible
		# (the 2-4Gb process address range is reserved for the system).
		#
		# Note: It seems not needed for pygplates (only the 'python.exe' that load the pygplates DLL).
		target_link_options(gplates-lib PRIVATE /LARGEADDRESSAWARE)
	endif()
endif()

#
# LINK_OPTIONS (target_property) for Apple compilers only.
#
if (APPLE)
	# 'bind_at_load' causes undefined symbols to be referenced at load/launch.
	target_link_options(${SOURCE_TARGET} PRIVATE LINKER:-bind_at_load)
endif(APPLE)


#
# Application (bundle, resources, etc) target properties.
#

if (GPLATES_BUILD_GPLATES)
	# Set the platform-dependent icon file.
	if (APPLE)
		set(GPlates_ICON "${GPLATES_SOURCE_DISTRIBUTION_DIR}/gplates_desktop_icon.icns")
	elseif (MSVC)
		set(GPlates_ICON "${GPLATES_SOURCE_DISTRIBUTION_DIR}/gplates_desktop_icon.rc")
	endif()
	if (GPlates_ICON)
		target_sources(gplates PRIVATE ${GPlates_ICON})
	endif()

	if (APPLE)
		# Copy icon file to 'Resources' directory inside application bundle.
		set_source_files_properties(${GPlates_ICON} PROPERTIES MACOSX_PACKAGE_LOCATION Resources)

		set_target_properties(gplates PROPERTIES
				# Tell cmake to build gplates as an application bundle.
				MACOSX_BUNDLE TRUE

				# Specify our own Info.plist template file.
				# We add a couple of keys to ensure Qt includes support Mac Retina displays.
				MACOSX_BUNDLE_INFO_PLIST "${GPLATES_SOURCE_DISTRIBUTION_DIR}/MacOSXBundleInfo.plist.in"

				MACOSX_BUNDLE_INFO_STRING "${PROJECT_DESCRIPTION}"
				MACOSX_BUNDLE_ICON_FILE gplates_desktop_icon.icns
				# A constant reverse-DNS bundle identifier - it identifies the *application*, not
				# the build, and macOS keys LaunchServices registration and app associations on it.
				# A version-bearing one would make every development build a distinct application.
				# This is also the domain and name QSettings already derives its preferences file
				# from (see 'GPlatesQApplication.cc'), so the two now agree.
				MACOSX_BUNDLE_GUI_IDENTIFIER "org.gplates.GPlates"
				MACOSX_BUNDLE_LONG_VERSION_STRING "GPlates ${GPLATES_VERSION_PRERELEASE_USER}"
				MACOSX_BUNDLE_BUNDLE_NAME "GPlates"
				MACOSX_BUNDLE_SHORT_VERSION_STRING "${GPLATES_VERSION}"
				MACOSX_BUNDLE_BUNDLE_VERSION "${GPLATES_VERSION_PRERELEASE}"
				MACOSX_BUNDLE_COPYRIGHT "${GPLATES_COPYRIGHT_STRING}")
	endif()

	if (WIN32)
		# UPDATE: Don't build GPlates as a GUI application on Windows (with a WinMain entry point).
		#
		# If GPlates is built as a GUI application (on Windows) then it detaches from the parent console and
		# all output to stdout and stderr are lost. We actually capture the stdout/stderr output in GPlates
		# (eg, generated from dependency libraries) and output them to the GPlates log window and log file,
		# but there's nothing to capture unless GPlates is built as a console application. In fact a simple
		# 'print()' statement in the embedded Python interpreter would have triggered an error.
		if (FALSE)
			# This also tells Qt${QT_VERSION_MAJOR}::Core to link in its WinMain.
			set_target_properties(gplates PROPERTIES WIN32_EXECUTABLE TRUE)
		endif()
	endif()
endif()


########################
# Dependency libraries #
########################

#
# OpenGL (and GLEW) dependency (GPlates only - the pygplates module does no rendering).
#
if (GPLATES_BUILD_GPLATES)
	target_link_libraries(${SOURCE_TARGET} PUBLIC OpenGL::GL OpenGL::GLU GLEW::GLEW)
	# Define GL_SILENCE_DEPRECATION to avoid a bunch of OpenGL deprecation warnings.
	# Currently happens when compiling on macOS mojave (10.14).
	# We will eventually replace OpenGL with Vulkan, but not for a while since
	# Apple are unlikely to 'remove' OpenGL in their drivers anytime soon.
	#
	# Set as usage requirements (INTERFACE) on the OpenGL imported library target.
	target_compile_definitions(OpenGL::GL INTERFACE GL_SILENCE_DEPRECATION)
endif()

#
# ZLIB dependency.
#
target_link_libraries(${SOURCE_TARGET} PUBLIC ZLIB::ZLIB)

#
# Boost dependency.
#
# Add required Boost components.
target_link_libraries(${SOURCE_TARGET} PUBLIC Boost::boost Boost::program_options Boost::thread Boost::${GPLATES_BOOST_PYTHON_COMPONENT_NAME})
# Disable Boost auto-linking.
# This solves the issue whereby, for example, Boost is compiled with Visual Studio 2015 but we're compiling GPlates/pyGPlates
# with Visual Studio 2019 (which causes auto-linking to look for Boost libs with 'vc142' in their name) and hence cannot find
# the Boost libs with 'vc140' in their name (built with the 2015 compiler).
target_link_libraries(${SOURCE_TARGET} PUBLIC Boost::disable_autolinking)
# Add optional Boost.Python.Numpy component (if found).
if (TARGET Boost::${GPLATES_BOOST_PYTHON_NUMPY_COMPONENT_NAME})
	target_link_libraries(${SOURCE_TARGET} PUBLIC Boost::${GPLATES_BOOST_PYTHON_NUMPY_COMPONENT_NAME})
endif()

#
# Qt dependency.
#
# The pygplates module links only Qt Core - see the Qt find_package split above for why.
# The pygplates-linkage test fails if Gui creeps back in.
target_link_libraries(${SOURCE_TARGET} PUBLIC Qt${QT_VERSION_MAJOR}::Core)
if (GPLATES_BUILD_GPLATES)
	target_link_libraries(${SOURCE_TARGET} PUBLIC
			Qt${QT_VERSION_MAJOR}::Gui
			Qt${QT_VERSION_MAJOR}::Network
			Qt${QT_VERSION_MAJOR}::Svg
			Qt${QT_VERSION_MAJOR}::Widgets
			Qt${QT_VERSION_MAJOR}::Xml)
endif()
if (QT_VERSION_MAJOR EQUAL 6)
	if (GPLATES_BUILD_GPLATES)
		# Qt6 moved the QOpenGL* classes from (Gui module in Qt5) back into OpenGL (and new OpenGLWidgets) modules.
		target_link_libraries(${SOURCE_TARGET} PUBLIC Qt6::OpenGL Qt6::OpenGLWidgets)
	endif()
endif()
# The directory where Qt AUTOIUC auto-generated files ('ui_*.h') get written is automatically added to the
# INCLUDE_DIRECTORIES target property of the 'gplates-lib' and 'pygplates' targets, but not INTERFACE_INCLUDE_DIRECTORIES.
# This is fine for 'pygplates' (because nothing links to it), but it means the include directories are not propagated to
# any executables linking to 'gplates-lib' (such as 'gplates').
#
# Hence we get compile errors when sources (added to any targets linked to 'gplates-lib') try to include 'ui_*.h' files.
#
# The workaround involves explicitly adding these include paths as PUBLIC (which is same as PRIVATE plus INTERFACE).
#
# The generated 'ui_*.h' files go in '${CMAKE_CURRENT_BINARY_DIR}/<target-name>_autogen/include' for single-configuration
# generators, and in '${CMAKE_CURRENT_BINARY_DIR}/<target-name>_autogen/include_$<CONFIG>' for multi-configuration generators.
target_include_directories(${SOURCE_TARGET} PUBLIC
	${CMAKE_CURRENT_BINARY_DIR}/${SOURCE_TARGET}_autogen/include
	${CMAKE_CURRENT_BINARY_DIR}/${SOURCE_TARGET}_autogen/include_$<CONFIG>)
# Define QT_NO_KEYWORDS so that Qt will not pollute namespaces.
# Must use "Q_SLOTS Q_EMIT Q_SIGNALS" instead of "slots emit signals".
#
# Set as usage requirements (INTERFACE) on the Qt imported QtCore library so that all consuming targets,
# including the other Qt imported libraries like QtGui as well as our targets, will inherit them.
target_compile_definitions(Qt${QT_VERSION_MAJOR}::Core INTERFACE QT_NO_KEYWORDS)

#
# Qwt dependency (GPlates only - Qwt serves the GUI widgets the module does not compile).
#
if (GPLATES_BUILD_GPLATES)
	target_link_libraries(${SOURCE_TARGET} PUBLIC Qwt::Qwt)
	# Define Qwt version keywords.
	#
	# Set as usage requirements (INTERFACE) on the Qwt imported library target.
	target_compile_definitions(Qwt::Qwt INTERFACE
		QWT_MAJOR_VERSION=${QWT_MAJOR_VERSION}
		QWT_MINOR_VERSION=${QWT_MINOR_VERSION}
		QWT_PATCH_VERSION=${QWT_PATCH_VERSION})
endif()

#
# CGAL dependency.
#
target_link_libraries(${SOURCE_TARGET} PUBLIC CGAL::CGAL)
# It appears "auxiliary/gmp/include" in base CGAL installation does not always get included by
# CGALConfig.cmake (found using 'find_package(CGAL)' above). Noticed this with CMake 4.13.2 when
# installing CGAL with a CMAKE_INSTALL_PREFIX different than base installation directory.
target_include_directories(${SOURCE_TARGET} SYSTEM PUBLIC ${GMP_INCLUDE_DIR} ${MPFR_INCLUDE_DIR})

#
# GDAL dependency.
#
target_link_libraries(${SOURCE_TARGET} PUBLIC GDAL::GDAL)

#
# PROJ dependency.
#
target_link_libraries(${SOURCE_TARGET} PUBLIC PROJ::proj)

#
# Python dependency.
#
if (GPLATES_BUILD_GPLATES)
	target_link_libraries(gplates-lib PUBLIC Python3::Python)
else()
	# Note that the Python3_add_library() call above has already linked pygplates to Python3::Module.
endif()
# Add the NumPy include directories if found.
if (GPLATES_PYTHON_NUMPY_INCLUDE_DIRS)
	target_include_directories(${SOURCE_TARGET} SYSTEM PUBLIC ${GPLATES_PYTHON_NUMPY_INCLUDE_DIRS})
endif()
if (GPLATES_BUILD_GPLATES)
	# Python is embedded in the GPlates application (as opposed to pygplates which is a Python extension library).
	target_compile_definitions(gplates-lib PUBLIC GPLATES_PYTHON_EMBEDDING)
endif()


# Test whether Python embedding works.
# This must be run after the include and library paths have all been set.
#
# Note: Only test Python embedding for GPlates. PyGPlates is a Python module (to be loaded into the Python interpreter)
#       and hence shouldn't link to the Python libraries (since Python symbols are already loaded in the interpreter).
if (GPLATES_BUILD_GPLATES)
	include(TestPythonEmbedding)
endif()

#
# Include code to *install* the ${BUILD_TARGET} target (either 'gplates' or 'pygplates').
#
# Note that, while 'include()' pulls in content from another directory, it pulls it into the *current* directory scope.
#
include(Install)

#
# Include code to *package* the ${BUILD_TARGET} target (either 'gplates' or 'pygplates').
#
# This should be included after code that installs the targets since packaging first installs to a staging area.
#
include(Package)
