##############################################################################
# Copyright (C) 2026 Eclipse ThreadX contributors
#
# This program and the accompanying materials are made available under the
# terms of the MIT License which is available at
# https://opensource.org/licenses/MIT.
#
# AI Disclosure: This file was largely AI-generated by Claude Code (Opus 5).
# The AI-generated portions may be considered public domain (CC0-1.0)
# and not subject to the project's licence. The human contributor has
# reviewed and verified that the code is correct.
#
# SPDX-License-Identifier: MIT and CC0-1.0
##############################################################################

cmake_minimum_required(VERSION 3.13 FATAL_ERROR)
cmake_policy(SET CMP0057 NEW)

project(freertos_regression_test LANGUAGES C)

set(SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/../../regression)
set(FIXTURE_DIR ${CMAKE_CURRENT_LIST_DIR}/../../fixtures)
set(REPO_ROOT ${CMAKE_CURRENT_LIST_DIR}/../../../..)
set(LAYER_SOURCE ${REPO_ROOT}/utility/rtos_compatibility_layers/FreeRTOS/tx_freertos.c)
set(LAYER_INCLUDE_DIR ${REPO_ROOT}/utility/rtos_compatibility_layers/FreeRTOS)

set(freertos_test_cases
    ${SOURCE_DIR}/txfr_queue_create_test.c
    ${SOURCE_DIR}/txfr_task_create_test.c
    ${SOURCE_DIR}/txfr_sync_create_test.c)

# tx_api.h maps the public API onto the error checking entry points, so those
# are the symbols that exist at link time and the ones the harness wraps.  A
# name that does not resolve is silently ignored by the linker, so this list
# must be kept in step with the __wrap_ functions in txfr_test_harness.c.
set(wrapped_symbols
    _txe_byte_allocate
    _txe_byte_release
    _txe_semaphore_create
    _txe_semaphore_delete
    _txe_mutex_create
    _txe_mutex_delete
    _txe_event_flags_create
    _txe_event_flags_delete
    _txe_timer_create
    _txe_timer_delete
    _txe_thread_create
    _txe_thread_delete
    _txe_thread_resume)

set(wrap_link_options "")
foreach(symbol ${wrapped_symbols})
  list(APPEND wrap_link_options "-Wl,--wrap=${symbol}")
endforeach()

# The layer is compiled from source into each test rather than linked from the
# freertos-threadx target, so that the test's FreeRTOSConfig.h applies.
add_library(freertos_layer_under_test OBJECT ${LAYER_SOURCE})
target_include_directories(freertos_layer_under_test
                           PUBLIC ${LAYER_INCLUDE_DIR} ${FIXTURE_DIR})
target_link_libraries(freertos_layer_under_test PUBLIC azrtos::threadx)

set(test_warning_options -Wall -Wextra -Werror)

add_library(freertos_test_harness OBJECT ${SOURCE_DIR}/txfr_test_harness.c)
target_include_directories(freertos_test_harness
                           PUBLIC ${SOURCE_DIR} ${LAYER_INCLUDE_DIR} ${FIXTURE_DIR})
target_link_libraries(freertos_test_harness PUBLIC azrtos::threadx)
target_compile_options(freertos_test_harness PRIVATE ${test_warning_options})

foreach(test_case ${freertos_test_cases})
  get_filename_component(test_name ${test_case} NAME_WE)

  add_executable(${test_name} ${test_case}
                 $<TARGET_OBJECTS:freertos_test_harness>
                 $<TARGET_OBJECTS:freertos_layer_under_test>)
  target_include_directories(${test_name}
                             PRIVATE ${SOURCE_DIR} ${LAYER_INCLUDE_DIR} ${FIXTURE_DIR})
  target_link_libraries(${test_name} PRIVATE azrtos::threadx)
  target_link_options(${test_name} PRIVATE ${wrap_link_options})
  target_compile_options(${test_name} PRIVATE ${test_warning_options})

  add_test(${CMAKE_BUILD_TYPE}::${test_name} ${test_name})
endforeach()
