|
104 | 104 | # 2025-08-28, Bronek Kozicki
|
105 | 105 | # - fix "At least one COMMAND must be given" CMake warning from policy CMP0175
|
106 | 106 | #
|
| 107 | +# 2025-09-03, Jingchen Wu |
| 108 | +# - remove the unused function append_coverage_compiler_flags and append_coverage_compiler_flags_to_target |
| 109 | +# - add a new function add_code_coverage_to_target |
| 110 | +# - remove some unused code |
| 111 | +# |
107 | 112 | # USAGE:
|
108 | 113 | #
|
109 | 114 | # 1. Copy this file into your cmake modules path.
|
|
112 | 117 | # using a CMake option() to enable it just optionally):
|
113 | 118 | # include(CodeCoverage)
|
114 | 119 | #
|
115 |
| -# 3. Append necessary compiler flags for all supported source files: |
116 |
| -# append_coverage_compiler_flags() |
117 |
| -# Or for specific target: |
118 |
| -# append_coverage_compiler_flags_to_target(YOUR_TARGET_NAME) |
| 120 | +# 3. Append necessary compiler flags and linker flags for all supported source files: |
| 121 | +# add_code_coverage_to_target(<target> <PRIVATE|PUBLIC|INTERFACE>) |
119 | 122 | #
|
120 | 123 | # 3.a (OPTIONAL) Set appropriate optimization flags, e.g. -O0, -O1 or -Og
|
121 | 124 | #
|
@@ -204,67 +207,69 @@ endforeach()
|
204 | 207 |
|
205 | 208 | set(COVERAGE_COMPILER_FLAGS "-g --coverage"
|
206 | 209 | CACHE INTERNAL "")
|
| 210 | + |
| 211 | +set(COVERAGE_CXX_COMPILER_FLAGS "") |
| 212 | +set(COVERAGE_C_COMPILER_FLAGS "") |
| 213 | +set(COVERAGE_CXX_LINKER_FLAGS "") |
| 214 | +set(COVERAGE_C_LINKER_FLAGS "") |
| 215 | + |
207 | 216 | if(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Clang)")
|
208 | 217 | include(CheckCXXCompilerFlag)
|
209 | 218 | include(CheckCCompilerFlag)
|
| 219 | + include(CheckLinkerFlag) |
| 220 | + |
| 221 | + set(COVERAGE_CXX_COMPILER_FLAGS ${COVERAGE_COMPILER_FLAGS}) |
| 222 | + set(COVERAGE_C_COMPILER_FLAGS ${COVERAGE_COMPILER_FLAGS}) |
| 223 | + set(COVERAGE_CXX_LINKER_FLAGS ${COVERAGE_COMPILER_FLAGS}) |
| 224 | + set(COVERAGE_C_LINKER_FLAGS ${COVERAGE_COMPILER_FLAGS}) |
210 | 225 |
|
211 | 226 | check_cxx_compiler_flag(-fprofile-abs-path HAVE_cxx_fprofile_abs_path)
|
212 | 227 | if(HAVE_cxx_fprofile_abs_path)
|
213 |
| - set(COVERAGE_CXX_COMPILER_FLAGS "${COVERAGE_COMPILER_FLAGS} -fprofile-abs-path") |
| 228 | + set(COVERAGE_CXX_COMPILER_FLAGS "${COVERAGE_CXX_COMPILER_FLAGS} -fprofile-abs-path") |
214 | 229 | endif()
|
215 | 230 |
|
216 | 231 | check_c_compiler_flag(-fprofile-abs-path HAVE_c_fprofile_abs_path)
|
217 | 232 | if(HAVE_c_fprofile_abs_path)
|
218 |
| - set(COVERAGE_C_COMPILER_FLAGS "${COVERAGE_COMPILER_FLAGS} -fprofile-abs-path") |
| 233 | + set(COVERAGE_C_COMPILER_FLAGS "${COVERAGE_C_COMPILER_FLAGS} -fprofile-abs-path") |
| 234 | + endif() |
| 235 | + |
| 236 | + check_linker_flag(CXX -fprofile-abs-path HAVE_cxx_linker_fprofile_abs_path) |
| 237 | + if(HAVE_cxx_linker_fprofile_abs_path) |
| 238 | + set(COVERAGE_CXX_LINKER_FLAGS "${COVERAGE_CXX_LINKER_FLAGS} -fprofile-abs-path") |
| 239 | + endif() |
| 240 | + |
| 241 | + check_linker_flag(C -fprofile-abs-path HAVE_c_linker_fprofile_abs_path) |
| 242 | + if(HAVE_c_linker_fprofile_abs_path) |
| 243 | + set(COVERAGE_C_LINKER_FLAGS "${COVERAGE_C_LINKER_FLAGS} -fprofile-abs-path") |
219 | 244 | endif()
|
220 | 245 |
|
221 | 246 | check_cxx_compiler_flag(-fprofile-update=atomic HAVE_cxx_fprofile_update)
|
222 | 247 | if(HAVE_cxx_fprofile_update)
|
223 |
| - set(COVERAGE_CXX_COMPILER_FLAGS "${COVERAGE_COMPILER_FLAGS} -fprofile-update=atomic") |
| 248 | + set(COVERAGE_CXX_COMPILER_FLAGS "${COVERAGE_CXX_COMPILER_FLAGS} -fprofile-update=atomic") |
224 | 249 | endif()
|
225 | 250 |
|
226 | 251 | check_c_compiler_flag(-fprofile-update=atomic HAVE_c_fprofile_update)
|
227 | 252 | if(HAVE_c_fprofile_update)
|
228 |
| - set(COVERAGE_C_COMPILER_FLAGS "${COVERAGE_COMPILER_FLAGS} -fprofile-update=atomic") |
| 253 | + set(COVERAGE_C_COMPILER_FLAGS "${COVERAGE_C_COMPILER_FLAGS} -fprofile-update=atomic") |
| 254 | + endif() |
| 255 | + |
| 256 | + check_linker_flag(CXX -fprofile-update=atomic HAVE_cxx_linker_fprofile_update) |
| 257 | + if(HAVE_cxx_linker_fprofile_update) |
| 258 | + set(COVERAGE_CXX_LINKER_FLAGS "${COVERAGE_CXX_LINKER_FLAGS} -fprofile-update=atomic") |
229 | 259 | endif()
|
230 |
| -endif() |
231 | 260 |
|
232 |
| -set(CMAKE_Fortran_FLAGS_COVERAGE |
233 |
| - ${COVERAGE_COMPILER_FLAGS} |
234 |
| - CACHE STRING "Flags used by the Fortran compiler during coverage builds." |
235 |
| - FORCE ) |
236 |
| -set(CMAKE_CXX_FLAGS_COVERAGE |
237 |
| - ${COVERAGE_COMPILER_FLAGS} |
238 |
| - CACHE STRING "Flags used by the C++ compiler during coverage builds." |
239 |
| - FORCE ) |
240 |
| -set(CMAKE_C_FLAGS_COVERAGE |
241 |
| - ${COVERAGE_COMPILER_FLAGS} |
242 |
| - CACHE STRING "Flags used by the C compiler during coverage builds." |
243 |
| - FORCE ) |
244 |
| -set(CMAKE_EXE_LINKER_FLAGS_COVERAGE |
245 |
| - "" |
246 |
| - CACHE STRING "Flags used for linking binaries during coverage builds." |
247 |
| - FORCE ) |
248 |
| -set(CMAKE_SHARED_LINKER_FLAGS_COVERAGE |
249 |
| - "" |
250 |
| - CACHE STRING "Flags used by the shared libraries linker during coverage builds." |
251 |
| - FORCE ) |
252 |
| -mark_as_advanced( |
253 |
| - CMAKE_Fortran_FLAGS_COVERAGE |
254 |
| - CMAKE_CXX_FLAGS_COVERAGE |
255 |
| - CMAKE_C_FLAGS_COVERAGE |
256 |
| - CMAKE_EXE_LINKER_FLAGS_COVERAGE |
257 |
| - CMAKE_SHARED_LINKER_FLAGS_COVERAGE ) |
| 261 | + check_linker_flag(C -fprofile-update=atomic HAVE_c_linker_fprofile_update) |
| 262 | + if(HAVE_c_linker_fprofile_update) |
| 263 | + set(COVERAGE_C_LINKER_FLAGS "${COVERAGE_C_LINKER_FLAGS} -fprofile-update=atomic") |
| 264 | + endif() |
| 265 | + |
| 266 | +endif() |
258 | 267 |
|
259 | 268 | get_property(GENERATOR_IS_MULTI_CONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
|
260 | 269 | if(NOT (CMAKE_BUILD_TYPE STREQUAL "Debug" OR GENERATOR_IS_MULTI_CONFIG))
|
261 | 270 | message(WARNING "Code coverage results with an optimised (non-Debug) build may be misleading")
|
262 | 271 | endif() # NOT (CMAKE_BUILD_TYPE STREQUAL "Debug" OR GENERATOR_IS_MULTI_CONFIG)
|
263 | 272 |
|
264 |
| -if(CMAKE_C_COMPILER_ID STREQUAL "GNU" OR CMAKE_Fortran_COMPILER_ID STREQUAL "GNU") |
265 |
| - link_libraries(gcov) |
266 |
| -endif() |
267 |
| - |
268 | 273 | # Defines a target for running and collection code coverage information
|
269 | 274 | # Builds dependencies, runs the given executable and outputs reports.
|
270 | 275 | # NOTE! The executable should always have a ZERO as exit code otherwise
|
@@ -454,18 +459,19 @@ function(setup_target_for_coverage_gcovr)
|
454 | 459 | )
|
455 | 460 | endfunction() # setup_target_for_coverage_gcovr
|
456 | 461 |
|
457 |
| -function(append_coverage_compiler_flags) |
458 |
| - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COVERAGE_COMPILER_FLAGS}" PARENT_SCOPE) |
459 |
| - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COVERAGE_COMPILER_FLAGS}" PARENT_SCOPE) |
460 |
| - set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} ${COVERAGE_COMPILER_FLAGS}" PARENT_SCOPE) |
461 |
| - message(STATUS "Appending code coverage compiler flags: ${COVERAGE_COMPILER_FLAGS}") |
462 |
| -endfunction() # append_coverage_compiler_flags |
463 |
| - |
464 |
| -# Setup coverage for specific library |
465 |
| -function(append_coverage_compiler_flags_to_target name) |
466 |
| - separate_arguments(_flag_list NATIVE_COMMAND "${COVERAGE_COMPILER_FLAGS}") |
467 |
| - target_compile_options(${name} PRIVATE ${_flag_list}) |
468 |
| - if(CMAKE_C_COMPILER_ID STREQUAL "GNU" OR CMAKE_Fortran_COMPILER_ID STREQUAL "GNU") |
469 |
| - target_link_libraries(${name} PRIVATE gcov) |
470 |
| - endif() |
471 |
| -endfunction() |
| 462 | +function(add_code_coverage_to_target name scope) |
| 463 | + separate_arguments(COVERAGE_CXX_COMPILER_FLAGS NATIVE_COMMAND "${COVERAGE_CXX_COMPILER_FLAGS}") |
| 464 | + separate_arguments(COVERAGE_C_COMPILER_FLAGS NATIVE_COMMAND "${COVERAGE_C_COMPILER_FLAGS}") |
| 465 | + separate_arguments(COVERAGE_CXX_LINKER_FLAGS NATIVE_COMMAND "${COVERAGE_CXX_LINKER_FLAGS}") |
| 466 | + separate_arguments(COVERAGE_C_LINKER_FLAGS NATIVE_COMMAND "${COVERAGE_C_LINKER_FLAGS}") |
| 467 | + |
| 468 | + # Add compiler options to the target |
| 469 | + target_compile_options(${name} ${scope} |
| 470 | + $<$<COMPILE_LANGUAGE:CXX>:${COVERAGE_CXX_COMPILER_FLAGS}> |
| 471 | + $<$<COMPILE_LANGUAGE:C>:${COVERAGE_C_COMPILER_FLAGS}>) |
| 472 | + |
| 473 | + target_link_libraries (${name} ${scope} |
| 474 | + $<$<LINK_LANGUAGE:CXX>:${COVERAGE_CXX_LINKER_FLAGS} gcov> |
| 475 | + $<$<LINK_LANGUAGE:C>:${COVERAGE_C_LINKER_FLAGS} gcov> |
| 476 | + ) |
| 477 | +endfunction() # add_code_coverage_to_target |
0 commit comments