# Modern CMake requires CMake version 3.
#
# Ubuntu Focal (20.04 LTS) provides CMake 3.16, 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.
cmake_minimum_required(VERSION 3.16...3.26 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})

    # Enable testing of pyGPlates (either via running 'ctest', or 'make test', or building 'RUN_TESTS' in Visual Studio).
    enable_testing()
    # Print test output when test fails - when running tests with 'make test' or building 'RUN_TESTS' (in Visual Studio).
    list(APPEND CMAKE_CTEST_ARGUMENTS "--output-on-failure")  # Ignored if CMake < 3.17
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)

#
# GPlates or pyGPlates specific sub-directories.
#
if (GPLATES_BUILD_GPLATES)
    #
    # Recurse into C++ documentation sub-directory.
    #
    add_subdirectory(doc)
else()
    #
    # Recurse into the sub-directory for testing/installing the pyGPlates Python module.
    #
    add_subdirectory(pygplates)

    #
    # Recurse into the sub-directory for generating the Python API documentation.
    #
    add_subdirectory(doc-python-api)
endif()
