From: Nathan Moinvaziri Date: Mon, 15 Feb 2021 03:01:21 +0000 (-0800) Subject: Move code coverage detection into its own cmake file. X-Git-Tag: 2.0.0~21 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0a3ab73fa834786f7de6363e25f10450e43c5602;p=thirdparty%2Fzlib-ng.git Move code coverage detection into its own cmake file. --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 75806443a..c4c4343b0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 index 000000000..dc5140447 --- /dev/null +++ b/cmake/detect-coverage.cmake @@ -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()