1# detect-coverage.cmake -- Detect supported compiler coverage flags 2# Licensed under the Zlib license, see LICENSE.md for details 3 4macro(add_code_coverage) 5 # Check for -coverage flag support for Clang/GCC 6 if(CMAKE_VERSION VERSION_LESS 3.14) 7 set(CMAKE_REQUIRED_LIBRARIES -lgcov) 8 else() 9 set(CMAKE_REQUIRED_LINK_OPTIONS -coverage) 10 endif() 11 check_c_compiler_flag(-coverage HAVE_COVERAGE) 12 set(CMAKE_REQUIRED_LIBRARIES) 13 set(CMAKE_REQUIRED_LINK_OPTIONS) 14 15 if(HAVE_COVERAGE) 16 set(CMAKE_C_FLAGS "-O0 ${CMAKE_C_FLAGS} -coverage") 17 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -coverage") 18 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -coverage") 19 else() 20 # Some versions of GCC don't support -coverage shorthand 21 if(CMAKE_VERSION VERSION_LESS 3.14) 22 set(CMAKE_REQUIRED_LIBRARIES -lgcov) 23 else() 24 set(CMAKE_REQUIRED_LINK_OPTIONS -lgcov -fprofile-arcs) 25 endif() 26 check_c_compiler_flag("-ftest-coverage -fprofile-arcs -fprofile-values" HAVE_TEST_COVERAGE) 27 set(CMAKE_REQUIRED_LIBRARIES) 28 set(CMAKE_REQUIRED_LINK_OPTIONS) 29 30 if(HAVE_TEST_COVERAGE) 31 set(CMAKE_C_FLAGS "-O0 ${CMAKE_C_FLAGS} -ftest-coverage -fprofile-arcs -fprofile-values") 32 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lgcov -fprofile-arcs") 33 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -lgcov -fprofile-arcs") 34 else() 35 message(WARNING "Compiler does not support code coverage") 36 endif() 37 endif() 38endmacro() 39