# -----------------------------------------------------------------------------
# Relevant source files and their virtual folders for IDE (source groups)
# -----------------------------------------------------------------------------
set(NO_GROUP_SRCS
    "${CMAKE_CURRENT_SOURCE_DIR}/mcds_hl_decoder_test_main.cpp"
    "${CMAKE_CURRENT_SOURCE_DIR}/mcds_hl_decoder_test.cpp"
    "${CMAKE_CURRENT_SOURCE_DIR}/mcds_hl_decoder_test.h"
)

target_sources(mcds_hl_decoder_test
    PRIVATE ${NO_GROUP_SRCS}
)


# -----------------------------------------------------------------------------
# Find relevant dependencies
# -----------------------------------------------------------------------------

# -----------------------------------------------------------------------------
# includes, and libraries
# -----------------------------------------------------------------------------
target_include_directories(mcds_hl_decoder_test
    PRIVATE
        "${CMAKE_CURRENT_SOURCE_DIR}"
        "${CMAKE_SOURCE_DIR}/src"
        "${CMAKE_SOURCE_DIR}/src/mcds"
        "${CMAKE_SOURCE_DIR}/src/mcds_tools"
        "${CMAKE_SOURCE_DIR}/src/mcds_lib"
        "${CMAKE_SOURCE_DIR}/include/mcds_libs"
        # Retained: test sources chain-include internal headers that transitively
        # include elf_disasm and capstone headers.  These deps are PRIVATE in
        # mcds_decoder so they do not propagate to consumers.  Making them PUBLIC
        # would widen the library's public API contract -- kept explicit here as a
        # justified plan deviation (W1-03-01).
        ${elf_disasm_INCLUDE_DIRS}
        ${capstone_INCLUDE_DIRS}
        "${CMAKE_BINARY_DIR}"
)

# Link only mcds_decoder (not mcds_configurator): both libraries share many
# source files (mcds_config_regs, mcds_control, reg_name, ...).  Linking both
# static libs causes the MSVC linker to pull in object files from each for
# different symbols, resulting in duplicated/split global state and a runtime
# crash.  mcds_decoder is a superset that contains all symbols the test needs.
target_link_libraries(mcds_hl_decoder_test PRIVATE mcds_decoder)

# -----------------------------------------------------------------------------
# Dependencies
# -----------------------------------------------------------------------------

# -----------------------------------------------------------------------------
# Compile definitions
# -----------------------------------------------------------------------------
if (MSVC)
    target_compile_definitions(mcds_hl_decoder_test PRIVATE
        "$<$<CONFIG:Debug>:"
            "_DEBUG"
        ">"
        "$<$<CONFIG:Release>:"
            "NDEBUG"
        ">"
        "_CRT_SECURE_NO_WARNINGS"
        "_WIN32"
    )
elseif (UNIX)
    target_compile_definitions(mcds_hl_decoder_test PRIVATE
        "UNIX"
    )
endif()

# -----------------------------------------------------------------------------
# Compile and link options
# -----------------------------------------------------------------------------
if (MSVC)
    target_compile_options(mcds_hl_decoder_test PRIVATE
        "$<$<CONFIG:Debug>:"
            "/MDd"
            "/Od"
        ">"
        "$<$<CONFIG:Release>:"
            "/MD"
            "/O2"
        ">"
        /JMC
        /analyze
        /Zi
        /W3
        /MP
        /nologo
    )

    target_link_options(mcds_hl_decoder_test PRIVATE
        /INCREMENTAL
        /SUBSYSTEM:CONSOLE
    )
elseif (UNIX)
    target_compile_options(mcds_hl_decoder_test PRIVATE
        -Wall;
    )
endif()

# -----------------------------------------------------------------------------
# CTest registration
# -----------------------------------------------------------------------------
add_test(
    NAME mcds_hl_decoder_test
    COMMAND mcds_hl_decoder_test
    WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/test/decoder"
)
set_tests_properties(mcds_hl_decoder_test PROPERTIES
    TIMEOUT 1800
    LABELS "long;integration"
)

# -----------------------------------------------------------------------------
# Install
# -----------------------------------------------------------------------------
install(TARGETS mcds_hl_decoder_test DESTINATION bin COMPONENT applications)
