cmake_minimum_required(VERSION 3.28 FATAL_ERROR)

include(SetupVulkan)
include(Utils)
include(Filter)

cmake_path(GET CMAKE_CURRENT_SOURCE_DIR PARENT_PATH ROOT_FOLDER)
file(GLOB_RECURSE SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/*.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/*.h" "${CMAKE_CURRENT_SOURCE_DIR}/*.hpp")
FilterPlatformPaths(SOURCES)
if (NOT OPENVULKANO_ENABLE_CURL)
    list(FILTER SOURCES EXCLUDE REGEX "WebResourceLoader")
endif()

if (APPLE)
    list(FILTER SOURCES EXCLUDE REGEX "ExeAppendedZipResourceLoader")
endif()

if (LINUX)
    find_program(LSB_RELEASE_EXEC lsb_release)
    execute_process(COMMAND ${LSB_RELEASE_EXEC} -is
        OUTPUT_VARIABLE LSB_RELEASE_ID_SHORT
        OUTPUT_STRIP_TRAILING_WHITESPACE
    )
    if (NOT ${LSB_RELEASE_ID_SHORT} STREQUAL "Ubuntu")
        list(FILTER SOURCES EXCLUDE REGEX ".*Ubuntu.*")
    endif()
endif()

source_group(TREE "${CMAKE_CURRENT_SOURCE_DIR}" FILES ${SOURCES})
file(GLOB_RECURSE RESOURCES "${ROOT_FOLDER}/resources/*.rc" "${ROOT_FOLDER}/resources/*.h")
list(APPEND SOURCES ${RESOURCES})
add_executable(OpenVulkano_Tests ${SOURCES})

# append zip file at the end of executable file
if (WIN32 OR LINUX)
    set(ZIP_FILE ${ROOT_FOLDER}/resources/arch.zip)
    add_custom_command(TARGET OpenVulkano_Tests POST_BUILD COMMAND ${CMAKE_COMMAND} -E cat ${ZIP_FILE} >> $<TARGET_FILE:OpenVulkano_Tests>)
endif()

target_include_directories(OpenVulkano_Tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})

target_include_directories(OpenVulkano_Tests PRIVATE openVulkanoCpp)
target_link_libraries(OpenVulkano_Tests PRIVATE "$<LINK_LIBRARY:WHOLE_ARCHIVE,openVulkanoCpp>")

# add catch2 
target_link_libraries(OpenVulkano_Tests PRIVATE Catch2::Catch2WithMain)
SetupVulkan(OpenVulkano_Tests)
SetGlmDefines(OpenVulkano_Tests)
SetWarningSettings(OpenVulkano_Tests)

list(APPEND CMAKE_MODULE_PATH ${Catch2_SOURCE_DIR}/extras)
list(APPEND CMAKE_MODULE_PATH ${Catch2_SOURCE_DIR}/contrib)

include(CTest)
include(Catch)
catch_discover_tests(OpenVulkano_Tests)
