]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Move code coverage detection into its own cmake file.
authorNathan Moinvaziri <nathan@nathanm.com>
Mon, 15 Feb 2021 03:01:21 +0000 (19:01 -0800)
committerHans Kristian Rosbach <hk-github@circlestorm.org>
Wed, 17 Feb 2021 13:59:16 +0000 (14:59 +0100)
CMakeLists.txt
cmake/detect-coverage.cmake [new file with mode: 0644]

index 75806443a2dc34482126a9ce3c0c380b61235b74..c4c4343b0fc5c2f7e33a8d1c0a36c84fa7273115 100644 (file)
@@ -53,6 +53,7 @@ include(CMakeDependentOption)
 include(FeatureSummary)
 
 include(cmake/detect-arch.cmake)
+include(cmake/detect-coverage.cmake)
 include(cmake/detect-sanitizer.cmake)
 
 if(CMAKE_TOOLCHAIN_FILE)
@@ -303,26 +304,7 @@ endif()
 
 # Set code coverage compiler flags
 if(WITH_CODE_COVERAGE)
-    set(CMAKE_REQUIRED_LINK_OPTIONS -coverage)
-    check_c_compiler_flag(-coverage HAVE_COVERAGE)
-    set(CMAKE_REQUIRED_LINK_OPTIONS)
-    if(HAVE_COVERAGE)
-        set(CMAKE_C_FLAGS "-O0 ${CMAKE_C_FLAGS} -coverage")
-        set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -coverage")
-        set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -coverage")
-    else()
-        # 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)
-        if(HAVE_TEST_COVERAGE)
-            set(CMAKE_C_FLAGS "-O0 ${CMAKE_C_FLAGS} -ftest-coverage -fprofile-arcs -fprofile-values")
-            set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lgcov -fprofile-arcs")
-            set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -lgcov -fprofile-arcs")
-        else()
-            message(WARNING "Compiler does not support code coverage")
-        endif()
-        set(CMAKE_REQUIRED_LINK_OPTIONS)
-    endif()
+    add_code_coverage()
 endif()
 
 # Set native instruction set compiler flag
diff --git a/cmake/detect-coverage.cmake b/cmake/detect-coverage.cmake
new file mode 100644 (file)
index 0000000..dc51404
--- /dev/null
@@ -0,0 +1,28 @@
+# detect-coverage.cmake -- Detect supported compiler coverage flags
+# Licensed under the Zlib license, see LICENSE.md for details
+
+macro(add_code_coverage)
+    # Check for -coverage flag support for Clang/GCC
+    set(CMAKE_REQUIRED_LINK_OPTIONS -coverage)
+    check_c_compiler_flag(-coverage HAVE_COVERAGE)
+    set(CMAKE_REQUIRED_LINK_OPTIONS)
+
+    if(HAVE_COVERAGE)
+        set(CMAKE_C_FLAGS "-O0 ${CMAKE_C_FLAGS} -coverage")
+        set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -coverage")
+        set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -coverage")
+    else()
+        # 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)
+        set(CMAKE_REQUIRED_LINK_OPTIONS)
+
+        if(HAVE_TEST_COVERAGE)
+            set(CMAKE_C_FLAGS "-O0 ${CMAKE_C_FLAGS} -ftest-coverage -fprofile-arcs -fprofile-values")
+            set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lgcov -fprofile-arcs")
+            set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -lgcov -fprofile-arcs")
+        else()
+            message(WARNING "Compiler does not support code coverage")
+        endif()
+    endif()
+endmacro()