From: Nathan Moinvaziri Date: Sat, 13 Feb 2021 20:51:02 +0000 (-0800) Subject: Improve compiler detection of code coverage flags. X-Git-Tag: 2.0.0~25 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9d4ca12f093a94514a9f747853f0243746353bc3;p=thirdparty%2Fzlib-ng.git Improve compiler detection of code coverage flags. Pass -coverage or flag to linker for mingw-gcc. --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 006ff1b3..75806443 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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()