Skip to content

Commit 09e7760

Browse files
authored
Update CodeCoverage.cmake module (#450)
1 parent fe8ea21 commit 09e7760

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

cmake/Modules/CodeCoverage.cmake

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@
5959
# 2019-12-19, FeRD (Frank Dana)
6060
# - Rename Lcov outputs, make filtered file canonical, fix cleanup for targets
6161
#
62+
# 2020-01-19, Bob Apthorpe
63+
# - Added gfortran support
64+
#
65+
# 2020-02-17, FeRD (Frank Dana)
66+
# - Make all add_custom_target()s VERBATIM to auto-escape wildcard characters
67+
# in EXCLUDEs, and remove manual escaping from gcovr targets
68+
#
6269
# USAGE:
6370
#
6471
# 1. Copy this file into your cmake modules path.
@@ -122,12 +129,22 @@ if("${CMAKE_CXX_COMPILER_ID}" MATCHES "(Apple)?[Cc]lang")
122129
message(FATAL_ERROR "Clang version must be 3.0.0 or greater! Aborting...")
123130
endif()
124131
elseif(NOT CMAKE_COMPILER_IS_GNUCXX)
125-
message(FATAL_ERROR "Compiler is not GNU gcc! Aborting...")
132+
if("${CMAKE_Fortran_COMPILER_ID}" MATCHES "[Ff]lang")
133+
# Do nothing; exit conditional without error if true
134+
elseif("${CMAKE_Fortran_COMPILER_ID}" MATCHES "GNU")
135+
# Do nothing; exit conditional without error if true
136+
else()
137+
message(FATAL_ERROR "Compiler is not GNU gcc! Aborting...")
138+
endif()
126139
endif()
127140

128141
set(COVERAGE_COMPILER_FLAGS "-g -fprofile-arcs -ftest-coverage"
129142
CACHE INTERNAL "")
130143

144+
set(CMAKE_Fortran_FLAGS_COVERAGE
145+
${COVERAGE_COMPILER_FLAGS}
146+
CACHE STRING "Flags used by the Fortran compiler during coverage builds."
147+
FORCE )
131148
set(CMAKE_CXX_FLAGS_COVERAGE
132149
${COVERAGE_COMPILER_FLAGS}
133150
CACHE STRING "Flags used by the C++ compiler during coverage builds."
@@ -145,6 +162,7 @@ set(CMAKE_SHARED_LINKER_FLAGS_COVERAGE
145162
CACHE STRING "Flags used by the shared libraries linker during coverage builds."
146163
FORCE )
147164
mark_as_advanced(
165+
CMAKE_Fortran_FLAGS_COVERAGE
148166
CMAKE_CXX_FLAGS_COVERAGE
149167
CMAKE_C_FLAGS_COVERAGE
150168
CMAKE_EXE_LINKER_FLAGS_COVERAGE
@@ -154,7 +172,7 @@ if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
154172
message(WARNING "Code coverage results with an optimised (non-Debug) build may be misleading")
155173
endif() # NOT CMAKE_BUILD_TYPE STREQUAL "Debug"
156174

157-
if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
175+
if(CMAKE_C_COMPILER_ID STREQUAL "GNU" OR CMAKE_Fortran_COMPILER_ID STREQUAL "GNU")
158176
link_libraries(gcov)
159177
endif()
160178

@@ -242,6 +260,7 @@ function(setup_target_for_coverage_lcov)
242260

243261
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
244262
DEPENDS ${Coverage_DEPENDENCIES}
263+
VERBATIM # Protect arguments to commands
245264
COMMENT "Resetting code coverage counters to zero.\nProcessing code coverage counters and generating report."
246265
)
247266

@@ -304,9 +323,8 @@ function(setup_target_for_coverage_gcovr_xml)
304323
# Combine excludes to several -e arguments
305324
set(GCOVR_EXCLUDE_ARGS "")
306325
foreach(EXCLUDE ${GCOVR_EXCLUDES})
307-
string(REPLACE "*" "\\*" EXCLUDE_REPLACED ${EXCLUDE})
308326
list(APPEND GCOVR_EXCLUDE_ARGS "-e")
309-
list(APPEND GCOVR_EXCLUDE_ARGS "${EXCLUDE_REPLACED}")
327+
list(APPEND GCOVR_EXCLUDE_ARGS "${EXCLUDE}")
310328
endforeach()
311329

312330
add_custom_target(${Coverage_NAME}
@@ -321,6 +339,7 @@ function(setup_target_for_coverage_gcovr_xml)
321339
BYPRODUCTS ${Coverage_NAME}.xml
322340
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
323341
DEPENDS ${Coverage_DEPENDENCIES}
342+
VERBATIM # Protect arguments to commands
324343
COMMENT "Running gcovr to produce Cobertura code coverage report."
325344
)
326345

@@ -376,9 +395,8 @@ function(setup_target_for_coverage_gcovr_html)
376395
# Combine excludes to several -e arguments
377396
set(GCOVR_EXCLUDE_ARGS "")
378397
foreach(EXCLUDE ${GCOVR_EXCLUDES})
379-
string(REPLACE "*" "\\*" EXCLUDE_REPLACED ${EXCLUDE})
380398
list(APPEND GCOVR_EXCLUDE_ARGS "-e")
381-
list(APPEND GCOVR_EXCLUDE_ARGS "${EXCLUDE_REPLACED}")
399+
list(APPEND GCOVR_EXCLUDE_ARGS "${EXCLUDE}")
382400
endforeach()
383401

384402
add_custom_target(${Coverage_NAME}
@@ -393,9 +411,11 @@ function(setup_target_for_coverage_gcovr_html)
393411
-r ${BASEDIR} ${GCOVR_EXCLUDE_ARGS}
394412
--object-directory=${PROJECT_BINARY_DIR}
395413
-o ${Coverage_NAME}/index.html
414+
396415
BYPRODUCTS ${PROJECT_BINARY_DIR}/${Coverage_NAME} # report directory
397416
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
398417
DEPENDS ${Coverage_DEPENDENCIES}
418+
VERBATIM # Protect arguments to commands
399419
COMMENT "Running gcovr to produce HTML code coverage report."
400420
)
401421

@@ -410,5 +430,6 @@ endfunction() # setup_target_for_coverage_gcovr_html
410430
function(append_coverage_compiler_flags)
411431
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COVERAGE_COMPILER_FLAGS}" PARENT_SCOPE)
412432
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COVERAGE_COMPILER_FLAGS}" PARENT_SCOPE)
433+
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} ${COVERAGE_COMPILER_FLAGS}" PARENT_SCOPE)
413434
message(STATUS "Appending code coverage compiler flags: ${COVERAGE_COMPILER_FLAGS}")
414435
endfunction() # append_coverage_compiler_flags

0 commit comments

Comments
 (0)