# Modern CMake requires CMake version 3.
#
# Ubuntu Bionic (18.04 LTS) provides CMake 3.10, so we'll make this the minimum requirement.
#
# Note: There's a "..." between min and max versions which means we need to use the minimum version
#       but are safe to use with policies (see 'cmake_policy' command) up to the maximum version.
#       Specifying a maximum version means we don't need 'cmake_policy' calls for running CMake versions
#       in that [min, max] range. Note that CMake version prior to 3.12 apparently ignore the "...<max>" part.
cmake_minimum_required(VERSION 3.10...3.23 FATAL_ERROR)

# Where to find our CMake modules.
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/cmake/modules)

#
# Include versioning.
#
include (Version)

#
# Build either GPlates or pyGPlates.
#
# We don't build both because CPack debian packages do not allow a separate version for each component package.
#
# NOTE: For versioning we always use MAJOR.MINOR.PATCH (to be compatible with Semantic Versioning 2.0 - https://semver.org/spec/v2.0.0.html)
#       CMake always allows an extra TWEAK component but we don't use it.
#       We do however support pre-releases which we set in our own variable PROJECT_VERSION_PRERELEASE (and human-readable PROJECT_VERSION_PRERELEASE_USER)
#       which has the same 'PROJECT_' prefix as the PROJECT_NAME and PROJECT_VERSION variables generated by the 'project()' command.
if (GPLATES_BUILD_GPLATES)
    project(GPlates VERSION ${GPLATES_VERSION_MAJOR}.${GPLATES_VERSION_MINOR}.${GPLATES_VERSION_PATCH} LANGUAGES C CXX)

    set(PROJECT_VERSION_PRERELEASE ${GPLATES_VERSION_PRERELEASE})
    set(PROJECT_VERSION_PRERELEASE_USER ${GPLATES_VERSION_PRERELEASE_USER})
    set(PROJECT_VERSION_PRERELEASE_SUFFIX ${GPLATES_VERSION_PRERELEASE_SUFFIX})
    set(PROJECT_VERSION_PRERELEASE_SUFFIX_USER ${GPLATES_VERSION_PRERELEASE_SUFFIX_USER})
else()
    project(PyGPlates VERSION ${PYGPLATES_VERSION_MAJOR}.${PYGPLATES_VERSION_MINOR}.${PYGPLATES_VERSION_PATCH} LANGUAGES C CXX)
    
    set(PROJECT_VERSION_PRERELEASE ${PYGPLATES_VERSION_PRERELEASE})
    set(PROJECT_VERSION_PRERELEASE_USER ${PYGPLATES_VERSION_PRERELEASE_USER})
    set(PROJECT_VERSION_PRERELEASE_SUFFIX ${PYGPLATES_VERSION_PRERELEASE_SUFFIX})
    set(PROJECT_VERSION_PRERELEASE_SUFFIX_USER ${PYGPLATES_VERSION_PRERELEASE_SUFFIX_USER})
endif()

#
# Include configuration options (default options and options overridden by user).
#
include (ConfigDefault)

#
# Add new custom build configurations (ProfileGprof and ProfileGplates) to
# existing Debug/Release/RelWithDebInfo/MinSizeRel configurations.
#
include (CustomBuildConfigs)

#
# Recurse into source code sub-directory.
#
add_subdirectory(src)

#
# Document either GPlates or pyGPlates.
#
if (GPLATES_BUILD_GPLATES)
    #
    # Recurse into C++ documentation sub-directory.
    #
    add_subdirectory(doc-cpp)
else()
    #
    # Recurse into python API documentation sub-directory.
    #
    # NOTE: We add this subdirectory after 'src' because it adds a custom target to build the
    #       Sphinx documentation and that target depends on the 'pygplates' target
    #       (to ensure pygplates gets built first since Sphinx imports pygplates to search its docstrings).
    #
    add_subdirectory(doc-python-api)
endif()
