# Minimum required version of CMake
cmake_minimum_required(VERSION 3.28)

# Project setup
project(main VERSION 1.0.0
    HOMEPAGE_URL "https://git.noorlander.info/E.Noorlander/CMake_SDL_CPP.git"
    DESCRIPTION "Demo embedded Linux C++ project"
    LANGUAGES CXX)

# Set C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Set binary name
set(BIN_NAME "test")
add_compile_definitions(APP_NAME="${BIN_NAME}")

# Set binary suffix based on OS
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
    set(BIN_SUFFIX ".exe")
elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
    set(BIN_SUFFIX ".app")
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
    set(BIN_SUFFIX ".elf")
else()
    set(BIN_SUFFIX ".bin")
endif()

# Compiler settings for Windows
if(MSVC)
    add_compile_options(/std:c++17)
    set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
endif()

# SDL2 & SDL2_ttf setup
if(BIN_STATIC)
    set(SDL2_USE_STATIC_LIBS ON)
endif()

# List of subprojects to include (e.g., external libraries or modules)
set(SubProjects
    myDynamicLibrary  # Example subproject, replace with actual subprojects
)

# Copy the font
file(COPY ${CMAKE_SOURCE_DIR}/include/Open_Sans/static/OpenSans-Regular.ttf DESTINATION ${CMAKE_BINARY_DIR}/assets)

# Add executable
add_executable(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp)

# Set target properties (output name & suffix)
set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME ${BIN_NAME} SUFFIX ${BIN_SUFFIX})

# Include SDL2 
find_package(SDL2 REQUIRED)
find_package(SDL2_ttf REQUIRED)

target_include_directories(${PROJECT_NAME} PRIVATE ${SDL2_INCLUDE_DIRS} ${SDL2_TTF_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} PRIVATE SDL2::SDL2 SDL2_ttf::SDL2_ttf)

# Loop over the subprojects and link them to the main executable
foreach(Project IN LISTS SubProjects)
    # Check if the target for this subproject has been added already
    if(NOT TARGET ${Project})
        # Add the subproject's directory to the build (if not already added)
        add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/lib/${Project})
    endif()

    target_link_libraries(${PROJECT_NAME} PRIVATE ${Project})
endforeach()

# Enable parallel build support
include(ProcessorCount)
ProcessorCount(N)
if(NOT N EQUAL 0)
  set(CTEST_BUILD_FLAGS -j${N})
  set(ctest_test_args ${PROJECT_NAME} PARALLEL_LEVEL ${N})
endif()
