Files
threadx/scripts/cmake_bootstrap.sh
T
Frédéric Desbiens 3e85bbd431 Instrumented every build configuration and merged their coverage (#665)
Only default_build_coverage carried -fprofile-arcs, because the gate was the
build type and it is the only one of five whose name ends in _coverage. The
other four build and run all their tests and their coverage was discarded. That
is not redundancy thrown away: each configuration selects a different set of TX_
feature macros, so the code the other four compile is absent from the
denominator rather than uncovered in it.

TX_COVERAGE instruments a build regardless of its name, defaulting to OFF so a
single configuration built by hand behaves as before. coverage.sh gains a
--merge mode that unions the per-configuration JSON tracefiles, and
cmake_bootstrap.sh runs it after the test loop so a local run produces the same
merged report CI reads. The template sets TX_COVERAGE for build and test, and
coverage_name moves to the merged report.

Measured on the ThreadX suite, all 480 tests passing:

  default_build_coverage           3827 valid   3827 covered
  disable_notify_callbacks_build   3767         3766
  stack_checking_build             3857         3856
  stack_checking_rand_fill_build   3862         3861
  trace_build                      4123         4108
  merged                           4503         4487

The denominator grows by 676 lines, 17.7%, and the figure moves from 99.97% to
99.64%. The second one is honest, and the drop is the point rather than a
regression: the denominator now includes code the old report never counted. The
union also contains a file the old report did not contain at all --
tx_thread_stack_error_handler.c compiles only under TX_ENABLE_STACK_CHECKING, so
it was not listed at 0%, it was simply absent. 177 files becomes 178.

Coverage collection moved out of test() and now runs after the test loop, one
configuration at a time. gcov writes its intermediate gcov files into the
directory gcovr is rooted at, and coverage.sh roots every configuration at the
repository root so filenames come out repo-relative. Five concurrent gcovr
processes therefore share one scratch directory and delete each other's output:
the first full run of this change passed all 480 tests and produced no report
for three of the five configurations. Measured both ways -- two gcovr rooted at
the repository root fail concurrently and succeed in sequence. CI would not
have caught it, because test_tx.sh sets CTEST_PARALLEL_LEVEL=1 and takes the
serial branch.

Per-configuration output moved under coverage_report/per_configuration/ and is
excluded from the Pages artifact. The deploy job merges the ThreadX and SMP
artifacts into one tree and every configuration directory has the same name in
both, so left at the top level one suite's would overwrite the other's on the
published site.

On the SMP suite, an earlier run of this change saw trace_build fail
threadx_smp_time_slice_test and then hang, which raised the question of whether
-fprofile-arcs perturbs a timing-sensitive test. It does not. Sixteen runs
settle it, and the decisive one is that threadx_smp_time_slice_test failed
ERROR #31 -- twice in a row under --repeat until-pass:2 -- on an uninstrumented
build, in the exact shape CI runs, while three instrumented runs of that shape
passed 5 of 5. In the CI shape, CTEST_PARALLEL_LEVEL=1 run.sh test all:

  TX_COVERAGE=OFF   3 runs   2 green, one ERROR #31        310 s
  TX_COVERAGE=ON    3 runs   3 green, 5/5 each             325-329 s

So the test is a pre-existing flake on dev and instrumenting all five costs
about 5% of the suite's wall clock. Separately, and also in both instrumented
and uninstrumented builds, run.sh's parallel branch -- what a developer gets
typing run.sh test all with no CTEST_PARALLEL_LEVEL -- hangs under its own load,
four times in twelve runs. Several SMP tests create 1024 ThreadX threads by
construction and the Linux port backs each with a pthread, so five
configurations at once put on the order of 5000 threads on the machine. CI sets
CTEST_PARALLEL_LEVEL=1 and does not take that branch.

Assisted-by: Claude Opus 5 <noreply@anthropic.com>
2026-08-26 08:13:33 -04:00

254 lines
9.6 KiB
Bash
Executable File

#!/bin/bash
##############################################################################
# Copyright (c) 2024 Microsoft Corporation
# 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.
#
# SPDX-License-Identifier: MIT
##############################################################################
set -e
function help() {
echo "Usage: $0 [build|test] [all|<build_configuration> <build_configuration>...]"
echo "Available build_configuration:"
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
}
# CMake records the compiler it detected inside the build directory and keeps using it on
# every later configure. Changing CC would otherwise be ignored without a word: the build
# reports success while still using the compiler the directory was first configured with,
# so anyone verifying a change against a second compiler would be reading stale results.
# Only the C compiler is consulted, because these test trees declare LANGUAGES C.
function compiler_changed() {
local build=$1
local recorded requested
[ -d "build/$build" ] || return 1
recorded=$(sed -n 's/^set(CMAKE_C_COMPILER "\(.*\)")$/\1/p' build/$build/CMakeFiles/*/CMakeCCompiler.cmake 2>/dev/null | head -1)
[ -n "$recorded" ] || return 1
requested=$(command -v "${CC:-gcc}" 2>/dev/null)
[ -n "$requested" ] || return 1
[ "$recorded" != "$requested" ]
}
function generate() {
build=$1
if compiler_changed $build; then
echo "Compiler changed since build/$build was configured. Reconfiguring from scratch."
rm -rf build/$build
fi
cmake -Bbuild/$build -GNinja -DBUILD_SHARED_LIBS=ON -DCMAKE_TOOLCHAIN_FILE=$(dirname $(realpath $0))/../cmake/linux.cmake -DCMAKE_BUILD_TYPE=$build -DTX_COVERAGE=${TX_COVERAGE:-OFF} .
}
function build() {
cmake --build build/$1
}
function build_libs() {
if compiler_changed libs; then
echo "Compiler changed since build/libs was configured. Reconfiguring from scratch."
rm -rf build/libs
fi
cmake -Bbuild/libs -GNinja -DBUILD_SHARED_LIBS=ON -DCMAKE_TOOLCHAIN_FILE=$(dirname $(realpath $0))/../cmake/linux.cmake libs
cmake --build build/libs
}
function test() {
pushd build/$1
[ -z "${CTEST_PARALLEL_LEVEL}" ] && parallel="-j$2"
if [ -z "${CTEST_REPEAT_FAIL}" ];
then
repeat_fail=2
else
repeat_fail=${CTEST_REPEAT_FAIL}
fi
# ctest's status is captured rather than allowed to abort the function, and
# returned at the end. set -e would otherwise stop here on the first failing
# test, and every configuration after it would go untested -- and, before
# collection moved out of this function, uncovered as well. The gcda files
# exist by that point, so a failing run threw away coverage it had already
# collected, and the run whose behaviour changed is exactly the one whose
# coverage is worth reading. Measured: the failing run of 2026-08-18 produced
# test_reports artifacts and no coverage_report artifact at all. Collection
# itself now happens in collect_all_coverage, after every configuration has
# been tested, and it does not stop at the first failure either.
local status=0
ctest $parallel --timeout 1000 -O $1.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 $1.xml || status=$?
popd
# Tolerated because this is a summary for humans, and a ctest that died early
# enough to leave no matching line must not be what stops the coverage below.
grep -E "^(\s*[0-9]+|Total)" build/$1/$1.txt >build/$1.txt || true
sed -i "s/\x1B\[[0-9;]*[JKmsu]//g" build/$1.txt
return $status
}
# Coverage is collected for any configuration that was instrumented, which is
# what TX_COVERAGE decides. The build-type match is kept for the one
# configuration instrumented by its name, so building a single configuration by
# hand behaves exactly as it did before.
#
# Three of the test trees -- freertos, posix and tx/cmake/riscv -- have no
# coverage.sh at all, and this script serves them too. Say so rather than
# failing, so that TX_COVERAGE=ON is harmless anywhere it does not apply.
function collect_coverage() {
if [[ $1 = *"_coverage" ]] || [ "${TX_COVERAGE:-OFF}" = "ON" ]; then
if [ ! -x ./coverage.sh ]; then
echo "No coverage.sh in $(pwd); skipping coverage for $1."
return 0
fi
./coverage.sh $1
fi
}
# Collection runs after every configuration has been tested, and strictly one at
# a time.
#
# It used to sit inside test(), which was safe only while a single configuration
# was instrumented. gcov writes its intermediate .gcov files into the directory
# gcovr is rooted at, and coverage.sh roots every configuration at the repository
# root so that the report can name files the way the repository does. Five
# concurrent gcovr processes therefore share one scratch directory and delete
# each other's output. The symptom is a gcovr SanityCheckError naming a .gcov
# file that "doesn't exist but no error from GCOV detected", and it cost the
# report for three of the five configurations while all 480 tests still passed --
# a green suite with most of its coverage missing.
#
# Measured both ways: two gcovr invocations rooted at the repository root fail
# when run concurrently and both succeed when run in sequence. CI happens to be
# safe already, because test_tx.sh sets CTEST_PARALLEL_LEVEL=1 and takes the
# serial branch below, but that is a coincidence of one caller rather than a
# property of this script.
#
# Collection deliberately does not stop at the first failure, for the reason
# given in test(): the configurations that did produce data should still report.
#
# The trade this makes, recorded rather than discovered later: a configuration
# that hangs long enough to hit the job's own timeout now costs the reports for
# the configurations that already finished, where collecting inside test() would
# have kept them. That is judged the smaller risk -- a per-test timeout is a
# ctest failure and the loop carries on, the job timeout sits at 60 minutes
# against a suite that takes 5 to 25, and the alternative loses most of the
# coverage on every parallel run instead of on a hang.
function collect_all_coverage() {
local item status=0
for item in $builds; do
collect_coverage $item || status=$?
done
# Union the per-configuration reports. Only the union is a coverage figure:
# each configuration compiles a different set of TX_ feature macros, so a
# line one of them compiles out is absent from its denominator rather than
# uncovered in it, and an average of five percentages means nothing.
#
# Done here rather than as a separate workflow step so a local run produces
# the same merged report CI reads.
if [ "${TX_COVERAGE:-OFF}" = "ON" ] && [ -x ./coverage.sh ]; then
./coverage.sh --merge || status=$?
fi
return $status
}
cd $(dirname $0)
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 1 ]; then
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 $item
echo ""
done
for item in $builds; do
echo "Building $item"
build $item
echo ""
done
elif [ "$command" == "test" ]; then
cores=$(nproc)
if [ -z "${CTEST_PARALLEL_LEVEL}" ];
then
# Run builds in parallel
build_counts=$(echo $builds | wc -w)
parallel_jobs=$(($cores / $build_counts))
parallel_jobs=$(($parallel_jobs + 2))
pids=""
for item in $builds; do
echo "Testing $item"
test $item $parallel_jobs &
pids+=" $!"
done
exit_code=0
for p in $pids; do
wait $p || exit_code=$?
done
# A coverage failure turns the run red, but must not overwrite a test
# failure's status with its own.
coverage_status=0
collect_all_coverage || coverage_status=$?
[ $exit_code -ne 0 ] || exit_code=$coverage_status
exit $exit_code
else
# Run builds in serial. The status is collected the same way the parallel
# branch above collects it, so one failing configuration no longer stops
# the remaining ones from being tested. That mattered more after the
# suites moved to serial execution: a failure in the first configuration
# meant the other four never ran, and their coverage was never collected
# either.
exit_code=0
for item in $builds; do
echo "Testing $item"
test $item $parallel_jobs || exit_code=$?
done
# A coverage failure turns the run red, but must not overwrite a test
# failure's status with its own.
coverage_status=0
collect_all_coverage || coverage_status=$?
[ $exit_code -ne 0 ] || exit_code=$coverage_status
exit $exit_code
fi
elif [ "$command" == "build_libs" ]; then
build_libs
else
help
fi