cmake_minimum_required(VERSION 3.23)
project(
    ToyMaker
    VERSION 0.5.6
    DESCRIPTION "A game engine built using SDL3 and OpenGL."
    HOMEPAGE_URL https://www.github.com/raynmetal/toymaker
    LANGUAGES CXX
)

set(CopyDepsDefault OFF)
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
    set(CopyDepsDefault ON)
endif()

include(CMakeDependentOption)
option(TOYMAKER_BUILD_EXAMPLES "Whether projects under `Examples/` should be built" OFF)
option(TOYMAKER_INSTALL_COPYDEPS "Whether binary dependencies are copied during installation" ${CopyDepsDefault})
cmake_dependent_option(TOYMAKER_USE_INSTALLED "Use locally installed ToyMaker library for the basic example project" ON TOYMAKER_BUILD_EXAMPLE_CENTERTEXT OFF)

# Build toymaker examples and tests on top of engine built here
if(NOT TOYMAKER_USE_INSTALLED)
    message("ToyMaker library is configured to be built from source")
    add_library(ToyMakerCompilerFlags INTERFACE)
    add_library(ToyMaker::ToyMakerCompilerFlags ALIAS ToyMakerCompilerFlags)
    target_compile_features(ToyMakerCompilerFlags INTERFACE cxx_std_17)

    add_subdirectory(ToyMaker_Main)
    add_subdirectory(ToyMaker_Builtins)
else()
    message("Machine-local installation of ToyMaker will be used to build ToyMaker executables")
endif()

# Build example projects, if configured
if(TOYMAKER_BUILD_EXAMPLES)
    message("The ToyMaker example projects have been configured to be built")
    add_subdirectory(Examples)
else()
    message("The ToyMaker example projects will not be built in this configuration")
endif()

# Provide ctest related options IF the project being built is ToyMaker
# itself
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
    include(CTest)
endif()
# Prevent the compilation of test drivers when ToyMaker is a vendored
# library
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING)
    message("Test drivers have been configured to be built")
    enable_testing()
    add_subdirectory(tests)
endif()

include(InstallRequiredSystemLibraries)

# Build ToyMaker from source if not directed to use installed version (e.g., when compiling
# examples)
if(NOT TOYMAKER_USE_INSTALLED)
    include(GNUInstallDirs)
    include(CMakePackageConfigHelpers)
    configure_package_config_file(cmake/ToyMakerConfig.cmake.in
    ${CMAKE_CURRENT_BINARY_DIR}/cmake/ToyMakerConfig.cmake
    INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/ToyMaker)
    write_basic_package_version_file(
        ${CMAKE_CURRENT_BINARY_DIR}/cmake/ToyMakerConfigVersion.cmake
        COMPATIBILITY ExactVersion
    )
    install(
        TARGETS
            ToyMakerCompilerFlags
        EXPORT
            LibToyMakerTargets
    )
    install(
        EXPORT
            LibToyMakerTargets
        DESTINATION 
            ${CMAKE_INSTALL_LIBDIR}/cmake/ToyMaker
        NAMESPACE
            ToyMaker::
    )
    include(GNUInstallDirs)
    install(
        FILES
            ${CMAKE_CURRENT_BINARY_DIR}/cmake/ToyMakerConfig.cmake
            ${CMAKE_CURRENT_BINARY_DIR}/cmake/ToyMakerConfigVersion.cmake
            LICENSE.txt
            README.md
        DESTINATION
            ${CMAKE_INSTALL_LIBDIR}/cmake/ToyMaker
    )
endif()

# Includes and uses uninstall script to remove files (but NOT directories)
# created during installation from source.
if(NOT TARGET uninstall)
    configure_file(
        "${CMAKE_CURRENT_SOURCE_DIR}/cmake_uninstall.cmake.in"
        "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
        IMMEDIATE @ONLY
    )
    add_custom_target(uninstall
        COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake
    )
endif()

set(CPACK_PACKAGE_NAME "toymaker")
set(CPACK_PACKAGE_VENDOR "Zoheb Shujauddin")
set(CPACK_PACKAGE_DESCRIPTION "ToyMaker - A simple 3D game engine built on SDL, OpenGL, C++")
set(CPACK_PACKAGE_INSTALL_DIRECTORY "toymaker/toymaker-${ToyMaker_VERSION}")
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE.txt")
set(CPACK_RESOURCE_FILE_README "${CMAKE_CURRENT_SOURCE_DIR}/README.md")
include(CPack)

