# The game's launcher, "Aces of the Pacific": the game folder, the game speed, the display and sound
# options, then aotp.
#
#   cmake -S launcher -B launcher/build -G Ninja && cmake --build launcher/build
#
# or, from the main project, -DAOTP_LAUNCHER=ON builds it next to aotp.exe.
# Needs wxWidgets 3.2 (MSYS2: mingw-w64-x86_64-wxwidgets3.2-msw; Debian and Ubuntu: libwxgtk3.2-dev).
cmake_minimum_required(VERSION 3.24)
project(aotplauncher LANGUAGES C CXX)
if(WIN32)
    enable_language(RC)
endif()

# MSYS2 and wxWidgets' own CMake builds install a package config; distributions
# such as Debian only ship wx-config, which CMake's FindwxWidgets uses.
find_package(wxWidgets 3.2 CONFIG QUIET COMPONENTS core base)
if(wxWidgets_FOUND AND TARGET wx::wxcore)
    set(AOTPL_WX_LIBRARIES wx::wxcore wx::wxbase)
else()
    find_package(wxWidgets 3.2 REQUIRED COMPONENTS core base)
    include(${wxWidgets_USE_FILE})
    set(AOTPL_WX_LIBRARIES ${wxWidgets_LIBRARIES})
endif()

add_executable(aotplauncher WIN32
    app.cpp
    game.cpp
    icon.cpp
    keys.cpp
    launcher.cpp
    settings.cpp
    ../src/gamever.c   # which release the game folder holds, shared with the game
)
set_target_properties(aotplauncher PROPERTIES OUTPUT_NAME "Aces of the Pacific" CXX_EXTENSIONS OFF)
target_compile_features(aotplauncher PRIVATE cxx_std_17)
target_link_libraries(aotplauncher PRIVATE ${AOTPL_WX_LIBRARIES})
# Built from the main project: beside aotp.exe, where it looks for it.
if(NOT PROJECT_IS_TOP_LEVEL)
    set_target_properties(aotplauncher PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
endif()

if(WIN32)
    target_sources(aotplauncher PRIVATE app.rc)
    target_link_libraries(aotplauncher PRIVATE comctl32)
    if(MINGW)
        # The C++ runtime inside the exe, so the build needs fewer DLLs beside it.
        target_link_options(aotplauncher PRIVATE -static-libgcc -static-libstdc++ $<$<CONFIG:Release>:-s>)
    endif()
    # The wxWidgets DLLs and everything they load, found where the compiler's own DLLs are.
    get_filename_component(AOTPL_DLL_DIR "${CMAKE_CXX_COMPILER}" DIRECTORY)
    add_custom_command(TARGET aotplauncher POST_BUILD
        COMMAND ${CMAKE_COMMAND} -DEXE=$<TARGET_FILE:aotplauncher> "-DSEARCH=${AOTPL_DLL_DIR}"
                -DCMAKE_OBJDUMP=${CMAKE_OBJDUMP} -P ${CMAKE_CURRENT_SOURCE_DIR}/copy_dlls.cmake)
endif()

if(MSVC)
    target_compile_options(aotplauncher PRIVATE /W4 /utf-8)
else()
    target_compile_options(aotplauncher PRIVATE -Wall -Wextra)
endif()
