summaryrefslogtreecommitdiff
path: root/test/posix/cmake
diff options
context:
space:
mode:
Diffstat (limited to 'test/posix/cmake')
-rw-r--r--test/posix/cmake/CMakeLists.txt83
-rw-r--r--test/posix/cmake/regression/CMakeLists.txt65
-rwxr-xr-xtest/posix/cmake/run.sh189
3 files changed, 337 insertions, 0 deletions
diff --git a/test/posix/cmake/CMakeLists.txt b/test/posix/cmake/CMakeLists.txt
new file mode 100644
index 00000000..830e69ba
--- /dev/null
+++ b/test/posix/cmake/CMakeLists.txt
@@ -0,0 +1,83 @@
+cmake_minimum_required(VERSION 3.13 FATAL_ERROR)
+cmake_policy(SET CMP0054 NEW)
+cmake_policy(SET CMP0057 NEW)
+
+# Regression tests for the POSIX compatibility layer.
+#
+# The layer cannot run on the hosted simulation ports: it supplies its own
+# pthread.h, errno.h, time.h and friends, which collide with the C library's,
+# and the linux port emulates threads with the very pthread calls the layer
+# exports. These tests therefore run on emulated targets, reusing the RISC-V
+# BSP and QEMU setup that the ThreadX RISC-V regression suite already uses.
+
+project(threadx_posix_test LANGUAGES C ASM)
+
+set(BUILD_CONFIGURATIONS default_build disable_notify_callbacks_build
+ stack_checking_build trace_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}.")
+message(STATUS "THREADX_ARCH: ${THREADX_ARCH}")
+
+set(default_build -DTX_QUEUE_MESSAGE_MAX_SIZE=32)
+set(disable_notify_callbacks_build -DTX_QUEUE_MESSAGE_MAX_SIZE=32 -DTX_DISABLE_NOTIFY_CALLBACKS)
+set(stack_checking_build -DTX_QUEUE_MESSAGE_MAX_SIZE=32 -DTX_ENABLE_STACK_CHECKING)
+set(trace_build -DTX_QUEUE_MESSAGE_MAX_SIZE=32 -DTX_ENABLE_EVENT_TRACE)
+
+# These tests are 32-bit only for now, and the reason is a gap in the layer
+# rather than a choice about coverage.
+#
+# The layer passes a message by putting the address of a private buffer into the
+# queue. ULONG is 32 bits on every ThreadX port, including the 64-bit ones, so
+# on a 64-bit target that address only fits if TX_64_BIT is defined: the layer
+# then splits it across two message words and sizes the message accordingly.
+# Without it, px_mq_send.c truncates the pointer to 32 bits and px_mq_receive.c
+# casts the truncated value back, which the compiler reports only as a warning.
+#
+# Defining TX_64_BIT is not a way out either: tx_api.h then uses the extension
+# pointer macros, which need tx_thread_extension_ptr in the thread control
+# block, and no 64-bit port outside ports_smp and ports/linux declares it.
+# Closing that gap belongs with the layer, not with its tests.
+if(THREADX_ARCH STREQUAL "risc-v64")
+ message(FATAL_ERROR
+ "The POSIX layer cannot run on a 64-bit port yet: it needs TX_64_BIT to "
+ "carry a 64-bit message pointer, and TX_64_BIT needs a port that declares "
+ "tx_thread_extension_ptr. Use the riscv32 toolchain file for these tests.")
+endif()
+
+add_compile_options(
+ -std=c99
+ -O0
+ -g3
+ -fdiagnostics-color
+ -Werror
+ -ffunction-sections
+ -fdata-sections
+ -DTX_REGRESSION_TEST
+ -DTEST_STACK_SIZE_PRINTF=4096
+ -DEXTERNAL_EXIT
+ ${${CMAKE_BUILD_TYPE}}
+)
+
+enable_testing()
+
+# ThreadX itself, plus the posix-threadx target the top-level file defines.
+add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../../.. threadx)
+
+# Reuse the RISC-V BSP from the ThreadX regression suite rather than keeping a
+# second copy of the board support in this tree.
+add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../../tx/cmake/riscv/bsp riscv_bsp)
+
+add_subdirectory(regression)
diff --git a/test/posix/cmake/regression/CMakeLists.txt b/test/posix/cmake/regression/CMakeLists.txt
new file mode 100644
index 00000000..8f433bcb
--- /dev/null
+++ b/test/posix/cmake/regression/CMakeLists.txt
@@ -0,0 +1,65 @@
+cmake_minimum_required(VERSION 3.13 FATAL_ERROR)
+cmake_policy(SET CMP0057 NEW)
+
+project(posix_regression_test LANGUAGES C ASM)
+
+set(SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/../../regression)
+set(TX_TEST_DIR ${CMAKE_CURRENT_LIST_DIR}/../../../tx/regression)
+set(BSP_DIR ${CMAKE_CURRENT_LIST_DIR}/../../../tx/cmake/riscv/bsp)
+
+if(THREADX_ARCH STREQUAL "risc-v64")
+ set(QEMU_SYSTEM "qemu-system-riscv64")
+ set(XLEN_TAG "rv64")
+else()
+ set(QEMU_SYSTEM "qemu-system-riscv32")
+ set(XLEN_TAG "rv32")
+endif()
+
+set(posix_test_cases
+ ${SOURCE_DIR}/posix_mq_basic_test.c
+ ${SOURCE_DIR}/posix_mq_send_abort_test.c
+ ${SOURCE_DIR}/posix_pthread_basic_test.c
+ ${SOURCE_DIR}/posix_pthread_self_test.c
+)
+
+set(WEAK_DEFAULTS_SOURCE ${CMAKE_CURRENT_LIST_DIR}/../../../shared/regression/testcontrol_weak_defaults.c)
+
+# Weak defaults must be an OBJECT library so all objects are always linked into
+# each test executable. A STATIC library would have the linker skip them, since
+# weak definitions do not satisfy undefined strong references while scanning an
+# archive.
+add_library(posix_test_weak_defaults OBJECT ${WEAK_DEFAULTS_SOURCE})
+target_compile_definitions(posix_test_weak_defaults PRIVATE
+ CTEST BATCH_TEST EXTERNAL_EXIT TEST_STACK_SIZE_PRINTF=4096)
+target_compile_options(posix_test_weak_defaults PRIVATE -include stdlib.h)
+target_link_libraries(posix_test_weak_defaults PRIVATE azrtos::threadx)
+
+# testcontrol.c supplies main(), tx_application_define() and
+# test_control_return(), and is shared with the ThreadX regression suite.
+add_library(posix_test_utility ${TX_TEST_DIR}/testcontrol.c)
+target_link_libraries(posix_test_utility PUBLIC riscv_bsp azrtos::threadx)
+target_compile_definitions(posix_test_utility PUBLIC
+ CTEST BATCH_TEST EXTERNAL_EXIT TEST_STACK_SIZE_PRINTF=4096)
+target_compile_options(posix_test_utility PUBLIC -include stdlib.h)
+
+function(register_posix_test test_name)
+ target_link_options(${test_name} PRIVATE
+ -T ${BSP_DIR}/link.lds -nostartfiles
+ -Wl,--gc-sections)
+ add_test(
+ NAME ${CMAKE_BUILD_TYPE}::${XLEN_TAG}::${test_name}
+ COMMAND ${QEMU_SYSTEM} -nographic -smp 1 -bios none -m 128M
+ -machine virt -kernel $<TARGET_FILE:${test_name}>
+ )
+ set_tests_properties(${CMAKE_BUILD_TYPE}::${XLEN_TAG}::${test_name}
+ PROPERTIES TIMEOUT 120)
+endfunction()
+
+foreach(test_case ${posix_test_cases})
+ get_filename_component(test_name ${test_case} NAME_WE)
+ add_executable(${test_name} ${test_case} $<TARGET_OBJECTS:posix_test_weak_defaults>)
+ target_link_libraries(${test_name} PRIVATE
+ -Wl,--whole-archive riscv_bsp -Wl,--no-whole-archive
+ posix_test_utility posix-threadx)
+ register_posix_test(${test_name})
+endforeach()
diff --git a/test/posix/cmake/run.sh b/test/posix/cmake/run.sh
new file mode 100755
index 00000000..76536cf7
--- /dev/null
+++ b/test/posix/cmake/run.sh
@@ -0,0 +1,189 @@
+#!/bin/bash
+##############################################################################
+# 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
+##############################################################################
+
+
+set -e
+
+# POSIX compatibility layer regression test build/test runner.
+# Usage: run.sh <arch> <build|test> [all|<config> ...]
+# arch: riscv32
+#
+# Only riscv32 is available: the layer cannot carry a 64-bit message pointer
+# without TX_64_BIT, and TX_64_BIT needs a port that declares
+# tx_thread_extension_ptr. See the comment in ../CMakeLists.txt.
+
+function help() {
+ echo "Usage: $0 <arch> <command> [all|<build_configuration> ...]"
+ echo ""
+ echo " arch: riscv32"
+ echo " command: build | test"
+ echo ""
+ echo "Available build configurations:"
+ for build in ${build_configurations[*]}; do
+ echo " $build"
+ done
+ exit 1
+}
+
+function validate() {
+ for build in ${build_configurations[*]}; do
+ if [ "$1" == "$build" ]; then
+ return
+ fi
+ done
+ help
+}
+
+function generate() {
+ local arch=$1
+ local build=$2
+ local toolchain_file
+ local build_dir
+
+ toolchain_file="${REPO_ROOT}/cmake/riscv32_gnu.cmake"
+
+ build_dir="build/${arch}_${build}"
+ cmake -B"$build_dir" -GNinja \
+ -DCMAKE_TOOLCHAIN_FILE="$toolchain_file" \
+ -DCMAKE_BUILD_TYPE="$build" \
+ .
+}
+
+function build() {
+ local arch=$1
+ local build=$2
+ # -k 0 keeps Ninja going after a target fails, so one broken target no
+ # longer decides whether the targets after it exist. ctest reports a
+ # missing binary as a failing test, which turned a single link error into
+ # a failure count that varied with build scheduling order.
+ cmake --build "build/${arch}_${build}" -- -k 0
+}
+
+function run_test() {
+ local arch=$1
+ local build=$2
+ local parallel_jobs=$3
+
+ # Guard the pushd: with the caller capturing this function's status, set -e
+ # no longer aborts here, so a missing build directory would otherwise let
+ # ctest run in the source tree and report "no tests" as success.
+ pushd "build/${arch}_${build}" || return 1
+ [ -z "${CTEST_PARALLEL_LEVEL}" ] && parallel="-j${parallel_jobs}"
+ if [ -z "${CTEST_REPEAT_FAIL}" ]; then
+ repeat_fail=2
+ else
+ repeat_fail=${CTEST_REPEAT_FAIL}
+ fi
+ # ctest's status is captured and returned past the popd and the summary
+ # steps, which all succeed. Without this the function would return 0 and a
+ # failing configuration would be reported as a pass.
+ local status=0
+ ctest $parallel --timeout 1000 \
+ -O "${arch}_${build}.txt" \
+ -T test --no-compress-output \
+ --test-output-size-passed 4194304 \
+ --test-output-size-failed 4194304 \
+ --output-on-failure \
+ --repeat until-pass:${repeat_fail} \
+ --output-junit "${arch}_${build}.xml" || status=$?
+ popd
+ return $status
+}
+
+# Determine repo root and script directory
+SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
+REPO_ROOT="$(cd "${SCRIPT_DIR}/../../.." && pwd)"
+cd "$SCRIPT_DIR"
+
+# Parse build configurations from CMakeLists.txt
+result=$(sed -n "/(BUILD_CONFIGURATIONS/,/)/p" CMakeLists.txt | sed ':label;N;s/\n/ /;b label' | grep -Pzo "[a-zA-Z0-9_]*build[a-zA-Z0-9_]*\s*" | tr -d '\0')
+IFS=' '
+read -ra build_configurations <<< "$result"
+
+if [ $# -lt 2 ]; then
+ help
+fi
+
+arch=$1
+shift
+
+if [ "$arch" != "riscv32" ]; then
+ echo "Error: arch must be 'riscv32'"
+ help
+fi
+
+command=$1
+shift
+
+if [ "$#" == "0" ]; then
+ builds=${build_configurations[0]}
+elif [ "$*" == "all" ]; then
+ builds=${build_configurations[@]}
+else
+ for item in $*; do
+ validate "$item"
+ done
+ builds=$*
+fi
+
+if [ "$command" == "build" ]; then
+ for item in $builds; do
+ generate "$arch" "$item"
+ echo ""
+ done
+
+ # A failing configuration must not stop the ones after it: under set -e
+ # the loop would abort and leave them unbuilt, which then reads as a wall
+ # of missing-binary test failures. The status is accumulated and returned.
+ build_status=0
+ for item in $builds; do
+ echo "Building ${arch} ${item}"
+ build "$arch" "$item" || build_status=$?
+ echo ""
+ done
+ [ $build_status -eq 0 ] || exit $build_status
+elif [ "$command" == "test" ]; then
+ cores=$(nproc)
+ if [ -z "${CTEST_PARALLEL_LEVEL}" ]; then
+ build_counts=$(echo $builds | wc -w)
+ parallel_jobs=$(($cores / $build_counts))
+ parallel_jobs=$(($parallel_jobs + 2))
+ pids=""
+ for item in $builds; do
+ echo "Testing ${arch} ${item}"
+ run_test "$arch" "$item" "$parallel_jobs" &
+ pids+=" $!"
+ done
+ exit_code=0
+ for p in $pids; do
+ wait $p || exit_code=$?
+ done
+ exit $exit_code
+ else
+ # ctest's status is accumulated rather than allowed to abort the loop.
+ # set -e would otherwise stop at the first failing configuration and
+ # leave every configuration after it untested. This mirrors what the
+ # parallel branch above already does with wait.
+ exit_code=0
+ for item in $builds; do
+ echo "Testing ${arch} ${item}"
+ run_test "$arch" "$item" "$parallel_jobs" || exit_code=$?
+ done
+ exit $exit_code
+ fi
+else
+ help
+fi