]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Improve compiler detection of code coverage flags.
authorNathan Moinvaziri <nathan@nathanm.com>
Sat, 13 Feb 2021 20:51:02 +0000 (12:51 -0800)
committerHans Kristian Rosbach <hk-github@circlestorm.org>
Wed, 17 Feb 2021 13:59:16 +0000 (14:59 +0100)
Pass -coverage or flag to linker for mingw-gcc.

CMakeLists.txt

index 006ff1b3a019d80d61295d4869e8160a1812a0b2..75806443a2dc34482126a9ce3c0c380b61235b74 100644 (file)
@@ -48,6 +48,7 @@ include(CheckFunctionExists)
 include(CheckIncludeFile)
 include(CheckCSourceCompiles)
 include(CheckCSourceRuns)
+include(CheckCCompilerFlag)
 include(CMakeDependentOption)
 include(FeatureSummary)
 
@@ -302,13 +303,25 @@ endif()
 
 # Set code coverage compiler flags
 if(WITH_CODE_COVERAGE)
-    if(CMAKE_C_COMPILER_ID MATCHES "Clang")
+    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")
-    elseif(__GNUC__)
+        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_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")
+        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()
 endif()