# Copyright (c) 2026 Kvaser AB
#
# SPDX-License-Identifier: MIT-0
#
# The MIT-0 (No Attribution) gives the user of this code
# the rights to use this code, without limitation, and with
# any requirements of attribution. See the full license here:
#   https://spdx.org/licenses/MIT-0.html

cmake_minimum_required(VERSION 3.20)

project(c_test)

include (InitializeCLLibraries.cmake)

if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
    set(SHARED_C_FLAGS
        /W4 # High warning level
        /WX # Warning as error
        /wd4820 # Allow padding
    )

    add_definitions(
        -D_CRT_SECURE_NO_WARNINGS # Don't show warnings for using memcpy, strncpy, etc
        -D_CRT_DECLARE_NONSTDC_NAMES=0 # Don't generate non-standard macros (min/max)
    )
    set(SYSTEM_LIBRARIES
        ntdll
        userenv
    )
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
    set(SHARED_C_FLAGS
        -Wall
        -Wpedantic
        -Werror
    )
    set(SYSTEM_LIBRARIES
    )
else()
    message(FATAL_ERROR "Unsupported compiler ${CMAKE_CXX_COMPILER_ID}")
endif()

# Add executable
add_executable(c_test_static)
target_sources(c_test_static
    PRIVATE
        src/main.c
)

target_compile_options(c_test_static
    PRIVATE
    ${SHARED_C_FLAGS}
)

target_link_libraries(c_test_static
    PRIVATE
    client_api_static
    ${SYSTEM_LIBRARIES}
)

add_executable(c_test_shared)
target_sources(c_test_shared
    PRIVATE
        src/main.c
)
target_compile_definitions(c_test_shared
    PRIVATE
    CL_API_USE_DLL
)

target_compile_options(c_test_static
    PRIVATE
    ${SHARED_C_FLAGS}
)

target_link_libraries(c_test_shared
    PRIVATE
    client_api_shared
    ${SYSTEM_LIBRARIES}
)

# add_custom_command(
#     TARGET c_test_shared POST_BUILD
#     COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_SOURCE_DIR}/data/arcus_98427_debug_app_internal.img"
#     "${CMAKE_CURRENT_BINARY_DIR}/arcus_98427_debug_app_internal.img"
# )


# On Windows we need to copy the dependencies over to the output directory
if (WIN32)
    add_custom_command(TARGET c_test_shared POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy -t $<TARGET_FILE_DIR:c_test_shared> $<TARGET_RUNTIME_DLLS:c_test_shared>
        COMMAND_EXPAND_LISTS
    )
endif()
