cmake_minimum_required(VERSION 3.22)
cmake_policy(VERSION 3.22)

# IPO warning.
cmake_policy(SET CMP0069 NEW) 
set(CMAKE_POLICY_DEFAULT_CMP0069 NEW)

set (PROJECT openloco)

set(VCPKG_GIT_TAG 2fb57e1c4738d4b4484d2234c886caf006612222)

# Store the top level source directory in a variable to be passed down to targets.
set(OPENLOCO_PROJECT_PATH ${CMAKE_SOURCE_DIR})

# Note: Searching for CCache must be before project() so project() can use CCache too
# if it is available
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
find_package(CCache)

if (CCache_FOUND)
    option(OPENLOCO_USE_CCACHE "Use CCache to improve recompilation speed (optional)" ON)
    if (OPENLOCO_USE_CCACHE)
        # Use e.g. "ccache clang++" instead of "clang++"
        set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCache_EXECUTABLE}")
        set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK    "${CCache_EXECUTABLE}")
    endif (OPENLOCO_USE_CCACHE)
endif (CCache_FOUND)

include(FetchContent)

if (WIN32)
    set(OPENLOCO_USE_VCPKG_DEFAULT YES)
else()
    set(OPENLOCO_USE_VCPKG_DEFAULT NO)
endif()
option(OPENLOCO_USE_VCPKG "Use vcpkg for dependencies and toolchain" ${OPENLOCO_USE_VCPKG_DEFAULT})


# On windows VCPKG is used to get all dependencies other platforms can use it as well by setting OPENLOCO_USE_VCPKG
if (OPENLOCO_USE_VCPKG)
    if (EXISTS ${VCPKG_ROOT})
        set(CMAKE_TOOLCHAIN_FILE "${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake")
    else()
        # Provide a message for first time invocations
        if (NOT CMAKE_TOOLCHAIN_FILE)
            message("VCPKG not installed, fetching and building VCPKG. Warning may take a while on slow connections.")
        endif ()
        FetchContent_Declare(vcpkg
            GIT_REPOSITORY https://github.com/microsoft/vcpkg.git
            GIT_TAG ${VCPKG_GIT_TAG}
        )
        FetchContent_MakeAvailable(vcpkg)

        set(CMAKE_TOOLCHAIN_FILE "${vcpkg_SOURCE_DIR}/scripts/buildsystems/vcpkg.cmake"
            CACHE STRING "CMake toolchain file")
    endif()
endif ()

project(${PROJECT} LANGUAGES CXX VERSION 26.11)

# Enable IPO for all Release targets by default
include(CheckIPOSupported)
check_ipo_supported(RESULT ipo_supported OUTPUT ipo_error)

if(ipo_supported)
    # Enable IPO globally for all Release builds
    set(CMAKE_INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE)
    message(STATUS "IPO/LTO enabled for all Release targets.")
else()
    message(WARNING "IPO/LTO not supported: ${ipo_error}")
endif()

# libstdc++ doesn't have standalone <execution> support so if using libstdc++ we need to
# link against TBB
include(CheckCXXSymbolExists)
check_cxx_symbol_exists(__GLIBCXX__  version HAS_LIBSTDCPP)
if (HAS_LIBSTDCPP)
    message(STATUS "libstdc++ detected will require linking with TBB")
endif()

# Enable a compilation database for clang-tidy (honoured by Makefile/Ninja generators;
# the Visual Studio generator does not consume it).
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

if (NOT CMAKE_CXX_STANDARD)
    set(CMAKE_CXX_STANDARD 20)
endif()

if (CMAKE_BINARY_DIR STREQUAL CMAKE_SOURCE_DIR)
    message(FATAL_ERROR "Building in-source is not supported! Create a build dir and remove ${CMAKE_SOURCE_DIR}/CMakeCache.txt")
endif()

include(FindPkgConfig)
include(CheckCXXCompilerFlag)
include(GNUInstallDirs)
include(OpenLocoVersion)
include(OpenLocoCommon)
include(OpenLocoUtility)

option(STRICT "Build with warnings as errors" YES)
option(OPENLOCO_BUILD_TESTS "Build tests" YES)
option(OPENLOCO_HEADER_CHECK "Verify all public interfaces are standalone" NO)

if (APPLE)
    # Enable the use of e.g. std::execution::par
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fexperimental-library")
endif ()

set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake;${CMAKE_MODULE_PATH}")

# Display source files nicely in IDEs
set_property(GLOBAL PROPERTY USE_FOLDERS ON)

# System dependencies
set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)

# Thirdparty dependencies
include(thirdparty/CMakeLists.txt)

if (${OPENLOCO_BUILD_TESTS})
    enable_testing()
endif()

FetchContent_Declare(openloco_objects
            URL      https://github.com/OpenLoco/OpenGraphics/releases/download/v0.1.13/objects.zip
            URL_HASH SHA256=5af3dbf660ed0e20b9f425a46fbbd618a52a48fe96fa3a4dfea1fdf138950b0a)
FetchContent_MakeAvailable(openloco_objects)

add_subdirectory(src)

if (NOT APPLE)
    if (${CMAKE_VERSION} VERSION_GREATER 3.26)
        # Uses the copy_directory_if_different to avoid copying objects/language every time (3.26 feature)
        add_custom_target(OpenLocoAssets ALL
            COMMAND ${CMAKE_COMMAND} -E copy_directory_if_different "${CMAKE_CURRENT_SOURCE_DIR}/data" "$<TARGET_FILE_DIR:App>/data"
            COMMAND ${CMAKE_COMMAND} -E copy_directory_if_different "${openloco_objects_SOURCE_DIR}" "$<TARGET_FILE_DIR:App>/data/objects"
            COMMENT "Copying over data and objects folder"
        )
    else()
        add_custom_target(OpenLocoAssets ALL
            COMMAND ${CMAKE_COMMAND} -E copy_directory "${CMAKE_CURRENT_SOURCE_DIR}/data" "$<TARGET_FILE_DIR:App>/data"
            COMMAND ${CMAKE_COMMAND} -E copy_directory "${openloco_objects_SOURCE_DIR}" "$<TARGET_FILE_DIR:App>/data/objects"
            COMMENT "Copying over data and objects folder"
        )
    endif()
    add_dependencies(App OpenLocoAssets)
    set_target_properties(OpenLocoAssets PROPERTIES PROJECT_LABEL "Assets")
endif()

set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT App)

install(TARGETS App
    RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}"
    BUNDLE DESTINATION ".")
