]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Improve detection of compiler code coverage support.
authorHans Kristian Rosbach <hk-git@circlestorm.org>
Sun, 11 Jan 2026 13:49:06 +0000 (14:49 +0100)
committerHans Kristian Rosbach <hk-git@circlestorm.org>
Tue, 20 Jan 2026 11:56:04 +0000 (12:56 +0100)
cmake/detect-coverage.cmake

index 86e59f0bad9a6089a14cf47102018508d4263ce2..1ebec71f676d86e494ffb648356a18819e02345d 100644 (file)
@@ -1,28 +1,42 @@
 # detect-coverage.cmake -- Detect supported compiler coverage flags
 # Licensed under the Zlib license, see LICENSE.md for details
 
+# Attempt to enable gcov-style code coverage
 macro(add_code_coverage)
-    # Check for -coverage flag support for Clang/GCC
+    # Check for --coverage flag support
     set(CMAKE_REQUIRED_LINK_OPTIONS -coverage)
-    check_c_compiler_flag(-coverage HAVE_COVERAGE)
+    check_c_compiler_flag("--coverage" HAVE_COVERAGE)
     set(CMAKE_REQUIRED_LINK_OPTIONS)
-
     if(HAVE_COVERAGE)
-        add_compile_options(-coverage)
-        add_link_options(-coverage)
-        message(STATUS "Code coverage enabled using: -coverage")
+        # Check for --coverage -fcondition-coverage flag support
+        set(CMAKE_REQUIRED_LINK_OPTIONS -coverage -fcondition-coverage)
+        check_c_compiler_flag("--coverage -fcondition-coverage -Wno-coverage-too-many-conditions" HAVE_CONDITION_COVERAGE)
+        set(CMAKE_REQUIRED_LINK_OPTIONS)
+
+        if(HAVE_CONDITION_COVERAGE)
+            # Both --coverage and -fcondition-coverage supported
+            add_link_options(-coverage -fcondition-coverage)
+            add_compile_options(--coverage -fcondition-coverage -Wno-coverage-too-many-conditions)
+            message(STATUS "Code coverage enabled using: --coverage -fcondition-coverage")
+        else()
+            # Only --coverage supported.
+            add_link_options(-coverage)
+            add_compile_options(--coverage)
+            message(STATUS "Code coverage enabled using: --coverage")
+        endif()
     else()
-        # Some versions of GCC don't support -coverage shorthand
+        # Some versions of GCC don't support --coverage shorthand
         set(CMAKE_REQUIRED_LINK_OPTIONS -lgcov -fprofile-arcs)
-        check_c_compiler_flag("-ftest-coverage -fprofile-arcs -fprofile-values" HAVE_TEST_COVERAGE)
+        check_c_compiler_flag("-ftest-coverage -fprofile-arcs" HAVE_TEST_COVERAGE)
         set(CMAKE_REQUIRED_LINK_OPTIONS)
 
         if(HAVE_TEST_COVERAGE)
-            add_compile_options(-ftest-coverage -fprofile-arcs -fprofile-values)
             add_link_options(-lgcov -fprofile-arcs)
-            message(STATUS "Code coverage enabled using: -ftest-coverage")
+            add_compile_options(-ftest-coverage -fprofile-arcs)
+            message(STATUS "Code coverage enabled using: -ftest-coverage -fprofile-arcs")
         else()
-            message(WARNING "Compiler does not support code coverage")
+            # Failed to enable coverage, this is fatal to avoid silent failures in CI
+            message(FATAL_ERROR "WITH_CODE_COVERAGE requested, but unable to turn on code coverage with compiler/linker")
             set(WITH_CODE_COVERAGE OFF)
         endif()
     endif()