##############################################################################
# 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 CMP0054 NEW)
cmake_policy(SET CMP0057 NEW)

project(threadx_freertos_test LANGUAGES C)

set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)

# The suite intercepts the ThreadX entry points with the linker's --wrap, which
# is a GNU ld and lld feature.  There is no MSVC equivalent, so the suite is
# built on Linux only and the build stops early elsewhere rather than failing
# later with confusing link errors.
if(NOT CMAKE_C_COMPILER_ID MATCHES "GNU|Clang")
  message(FATAL_ERROR
          "The FreeRTOS compatibility layer tests require GNU ld or lld for "
          "--wrap support and are supported on Linux only.")
endif()

set(BUILD_CONFIGURATIONS default_build)
set(CMAKE_CONFIGURATION_TYPES
    ${BUILD_CONFIGURATIONS}
    CACHE STRING "list of supported configuration types" FORCE)
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS
                                             ${CMAKE_CONFIGURATION_TYPES})
list(GET CMAKE_CONFIGURATION_TYPES 0 BUILD_TYPE)
if((NOT CMAKE_BUILD_TYPE) OR (NOT ("${CMAKE_BUILD_TYPE}" IN_LIST
                                   CMAKE_CONFIGURATION_TYPES)))
  set(CMAKE_BUILD_TYPE
      "${BUILD_TYPE}"
      CACHE STRING "Build Type of the project" FORCE)
endif()

message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
message(STATUS "Using toolchain file: ${CMAKE_TOOLCHAIN_FILE}.")

# Built 32 bit, as the ThreadX and SMP suites are.  This is not incidental for
# this suite: the Linux port defines ULONG as unsigned int on x86_64, while the
# compatibility layer passes pointers through ULONG arguments, for instance the
# task argument and the timer identifier.  A 64 bit build therefore truncates
# those pointers, which the compiler reports as -Wpointer-to-int-cast and which
# would crash the timer callback wrapper.
add_compile_options(-m32)
add_link_options(-m32)

# Applied to everything, including the ThreadX build pulled in below.  The
# strict warning set is applied to the test code alone, in the regression
# CMakeLists, since neither ThreadX nor the layer under test builds clean
# under -Wextra today and that is not this suite's to fix.
add_compile_options(-ggdb -g3 -fdiagnostics-color)

enable_testing()

# The compatibility layer stores a back pointer in each thread control block,
# so ThreadX has to be built with the matching user extension.
set(TX_USER_FILE
    ${CMAKE_CURRENT_LIST_DIR}/../fixtures/tx_user.h
    CACHE FILEPATH "tx_user.h used for the FreeRTOS layer tests" FORCE)

add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../../.. threadx)

add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/regression)
