cmake_minimum_required(VERSION 3.20)

project(openroller VERSION 0.1.5 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

option(OPENROLLER_BUILD_DESKTOP "Build the SDL/OpenGL desktop player" ON)
option(OPENROLLER_BUILD_TOOLS "Build host-side research and PSP packaging tools" ON)
option(OPENROLLER_BUILD_PSP_RUNTIME_PROBE "Build the host PSP runtime probe" ON)

set(VECTORAIL_CORE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../vectorail-core"
    CACHE PATH "Path to a vectorail-core source checkout")
set(VECTORAIL_GC_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../vectorail-gc"
    CACHE PATH "Path to a vectorail-gc source checkout")

if(OPENROLLER_BUILD_DESKTOP AND NOT TARGET Vectorail::Core)
    find_package(VectorailCore CONFIG QUIET)
    if(NOT VectorailCore_FOUND)
        if(EXISTS "${VECTORAIL_CORE_SOURCE_DIR}/CMakeLists.txt")
            add_subdirectory("${VECTORAIL_CORE_SOURCE_DIR}"
                "${CMAKE_CURRENT_BINARY_DIR}/_deps/vectorail-core")
        else()
            message(FATAL_ERROR
                "vectorail-core was not found. Install VectorailCore or set "
                "VECTORAIL_CORE_SOURCE_DIR to its source checkout.")
        endif()
    endif()
endif()

if(NOT TARGET Vectorail::GC)
    find_package(VectorailGC CONFIG QUIET)
    if(NOT VectorailGC_FOUND)
        if(EXISTS "${VECTORAIL_GC_SOURCE_DIR}/CMakeLists.txt")
            set(VECTORAIL_GC_BUILD_TOOLS OFF CACHE BOOL "" FORCE)
            add_subdirectory("${VECTORAIL_GC_SOURCE_DIR}"
                "${CMAKE_CURRENT_BINARY_DIR}/_deps/vectorail-gc")
        else()
            message(FATAL_ERROR
                "vectorail-gc was not found. Install VectorailGC or set "
                "VECTORAIL_GC_SOURCE_DIR to its source checkout.")
        endif()
    endif()
endif()

function(openroller_enable_warnings target)
    if(MSVC)
        target_compile_options("${target}" PRIVATE /W4)
    else()
        target_compile_options("${target}" PRIVATE -Wall -Wextra -Wpedantic)
    endif()
endfunction()

if(OPENROLLER_BUILD_DESKTOP)
    add_subdirectory(apps/desktop)
endif()

if(OPENROLLER_BUILD_TOOLS)
    add_executable(openroller-stage-probe src/main.cpp)
    target_link_libraries(openroller-stage-probe PRIVATE Vectorail::GC)
    openroller_enable_warnings(openroller-stage-probe)

    add_executable(openroller-psp-pack src/psp_pack.cpp)
    target_include_directories(openroller-psp-pack PRIVATE include)
    target_link_libraries(openroller-psp-pack PRIVATE Vectorail::GC)
    openroller_enable_warnings(openroller-psp-pack)

    add_executable(openroller-psp-catalog src/psp_catalog.cpp)
    target_include_directories(openroller-psp-catalog PRIVATE include)
    target_link_libraries(openroller-psp-catalog PRIVATE Vectorail::GC)
    openroller_enable_warnings(openroller-psp-catalog)
endif()

if(OPENROLLER_BUILD_PSP_RUNTIME_PROBE)
    add_executable(openroller-psp-runtime-probe
        src/psp_runtime_probe.cpp
        psp/src/StageRuntime.cpp
        psp/src/Gameplay.cpp
    )
    target_include_directories(openroller-psp-runtime-probe PRIVATE psp/include include)
    openroller_enable_warnings(openroller-psp-runtime-probe)
endif()
