From 1241e4f0a6556144a7b856bb31804b4d27efd7c0 Mon Sep 17 00:00:00 2001 From: Lucas Chollet Date: Sat, 2 May 2026 21:23:27 +0200 Subject: [PATCH] Meta+CMake: Enable coverage for test binaries This will allow everything defined in headers to be instrumented. As an example, the function `AK::any_of` isn't instantiated inside AK, that means that no coverage is reported if we don't consider the instantiation in the test binary itself. --- CMakeLists.txt | 16 ++++++++++++++++ Meta/analyze-qemu-coverage.sh | 2 +- Userland/CMakeLists.txt | 16 ---------------- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2541106b3f5..3ce7113e705 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -261,6 +261,22 @@ elseif("${SERENITY_ARCH}" STREQUAL "x86_64") add_link_options(LINKER:-z,pack-relative-relocs) endif() +# Escape hatch target to prevent runtime startup libraries from having coverage enabled +# at awkward points in program initialization +add_library(NoCoverage INTERFACE) + +if (ENABLE_USERSPACE_COVERAGE_COLLECTION) + if (CMAKE_CXX_COMPILER_ID MATCHES "Clang$") + add_compile_options(-fprofile-instr-generate -fcoverage-mapping) + add_link_options(-fprofile-instr-generate -fcoverage-mapping) + + target_compile_options(NoCoverage INTERFACE -fno-profile-generate -fno-profile-instr-use -fno-profile-instr-generate -fno-coverage-mapping) + target_link_options(NoCoverage INTERFACE -fno-profile-generate -fno-profile-instr-use -fno-profile-instr-generate -fno-coverage-mapping) + else() + message(FATAL_ERROR "ENABLE_USERSPACE_COVERAGE_COLLECTION not supported yet for ${CMAKE_CXX_COMPILER_ID}") + endif() +endif() + add_subdirectory(Userland) add_subdirectory(Tests) diff --git a/Meta/analyze-qemu-coverage.sh b/Meta/analyze-qemu-coverage.sh index c205927d840..8c2c7e374b3 100755 --- a/Meta/analyze-qemu-coverage.sh +++ b/Meta/analyze-qemu-coverage.sh @@ -35,7 +35,7 @@ echo "[ELEVATED] Making sure profile data files are owned by the current user, t echo "Discovering all binaries and shared libraries in $BUILD_DIR/Root" # shellcheck disable=SC2156 # The recommended fix on the Shellcheck github page for this warning causes the script to not find any files at all -all_binaries=$(find "$BUILD_DIR"/Root -type f -executable -exec sh -c "file {} | grep -vi relocatable | grep -Eiq ': elf (32|64)-bit'" \; -printf "-object %p\n" | grep -Ev '(usr\/Tests|usr\/local|boot\/|Loader.so)') +all_binaries=$(find "$BUILD_DIR"/Root -type f -executable -exec sh -c "file {} | grep -vi relocatable | grep -Eiq ': elf (32|64)-bit'" \; -printf "-object %p\n" | grep -Ev '(usr\/local|boot\/|Loader.so)') CLANG_BINDIR="${SERENITY_ROOT}/Toolchain/Local/clang/bin" LLVM_PROFDATA="$CLANG_BINDIR/llvm-profdata" diff --git a/Userland/CMakeLists.txt b/Userland/CMakeLists.txt index 6539a889985..ab857980019 100644 --- a/Userland/CMakeLists.txt +++ b/Userland/CMakeLists.txt @@ -1,21 +1,5 @@ add_compile_options(-O2) -# Escape hatch target to prevent runtime startup libraries from having coverage enabled -# at awkward points in program initialization -add_library(NoCoverage INTERFACE) - -if (ENABLE_USERSPACE_COVERAGE_COLLECTION) - if (CMAKE_CXX_COMPILER_ID MATCHES "Clang$") - add_compile_options(-fprofile-instr-generate -fcoverage-mapping) - add_link_options(-fprofile-instr-generate -fcoverage-mapping) - - target_compile_options(NoCoverage INTERFACE -fno-profile-generate -fno-profile-instr-use -fno-profile-instr-generate -fno-coverage-mapping) - target_link_options(NoCoverage INTERFACE -fno-profile-generate -fno-profile-instr-use -fno-profile-instr-generate -fno-coverage-mapping) - else() - message(FATAL_ERROR "ENABLE_USERSPACE_COVERAGE_COLLECTION not supported yet for ${CMAKE_CXX_COMPILER_ID}") - endif() -endif() - add_subdirectory(Applications) add_subdirectory(Demos) add_subdirectory(DevTools)