Skip to content

Commit 212b0df

Browse files
committedMay 6, 2022
Filter for target types and no-visibility option
IMPORTED and ALIAS targets cannot be add to coverage. Also, in old project where PUBLIC, PRIVATE or INTERFACE visibility was not specified on target, there is option PLAIN which do not set any for compile and link (this requires PUBLIC) option.
1 parent 1b03e94 commit 212b0df

File tree

1 file changed

+37
-8
lines changed

1 file changed

+37
-8
lines changed
 

‎code-coverage.cmake

+37-8
Original file line numberDiff line numberDiff line change
@@ -208,17 +208,39 @@ if(CODE_COVERAGE AND NOT CODE_COVERAGE_ADDED)
208208
if (COMMAND _add_executable)
209209
message(FATAL_ERROR "add_executable was already redefined. Only one redefinitions is allowed.")
210210
endif()
211-
macro(add_executable)
212-
_add_executable(${ARGV})
213-
target_code_coverage(${ARGV0} ${CCOV_TARGETS_HOOK_ARGS})
211+
212+
set(CCOV_TARGETS_HOOK_LIST ${CCOV_TARGETS_HOOK_ARGS})
213+
separate_arguments(CCOV_TARGETS_HOOK_LIST)
214+
215+
macro(add_executable target_name)
216+
_add_executable(${target_name} ${ARGN})
217+
if (${ARGC} GREATER 1)
218+
string(TOUPPER ${ARGV1} TARGET_TYPE)
219+
endif()
220+
string(COMPARE NOTEQUAL "${TARGET_TYPE}" IMPORTED IS_NOT_IMPORTED)
221+
222+
if (IS_NOT_IMPORTED)
223+
target_code_coverage(${target_name} ${CCOV_TARGETS_HOOK_LIST})
224+
endif()
214225
endmacro(add_executable)
215226

216227
if (COMMAND _add_library)
217228
message(FATAL_ERROR "add_library was already redefined. Only one redefinitions is allowed.")
218229
endif()
219-
macro(add_library)
220-
_add_library(${ARGV})
221-
target_code_coverage(${ARGV0} ${CCOV_TARGETS_HOOK_ARGS})
230+
macro(add_library target_name)
231+
_add_library(${target_name} ${ARGN})
232+
if (${ARGC} GREATER 1)
233+
string(TOUPPER "${ARGV1}" TARGET_TYPE)
234+
endif()
235+
if (${ARGC} GREATER 2)
236+
string(TOUPPER "${ARGV2}" IMPORTED_TYPE)
237+
endif()
238+
string(COMPARE NOTEQUAL "${TARGET_TYPE}" ALIAS IS_NOT_ALIAS)
239+
string(COMPARE NOTEQUAL "${IMPORTED_TYPE}" IMPORTED IS_NOT_IMPORTED)
240+
241+
if (IS_NOT_ALIAS AND IS_NOT_IMPORTED)
242+
target_code_coverage(${target_name} ${CCOV_TARGETS_HOOK_LIST})
243+
endif()
222244
endmacro(add_library)
223245
endif (CCOV_TARGETS_HOOK)
224246

@@ -249,6 +271,7 @@ endif()
249271
# Optional:
250272
# PUBLIC - Sets the visibility for added compile options to targets to PUBLIC instead of the default of PRIVATE.
251273
# INTERFACE - Sets the visibility for added compile options to targets to INTERFACE instead of the default of PRIVATE.
274+
# PLAIN - Do not set any target visibility (backward compatibility with old cmake projects)
252275
# AUTO - Adds the target to the 'ccov' target so that it can be run in a batch with others easily. Effective on executable targets.
253276
# ALL - Adds the target to the 'ccov-all' and 'ccov-all-report' targets, which merge several executable targets coverage data to a single report. Effective on executable targets.
254277
# EXTERNAL - For GCC's lcov, allows the profiling of 'external' files from the processing directory
@@ -259,7 +282,7 @@ endif()
259282
# ~~~
260283
function(target_code_coverage TARGET_NAME)
261284
# Argument parsing
262-
set(options AUTO ALL EXTERNAL PUBLIC INTERFACE)
285+
set(options AUTO ALL EXTERNAL PUBLIC INTERFACE PLAIN)
263286
set(single_value_keywords COVERAGE_TARGET_NAME)
264287
set(multi_value_keywords EXCLUDE OBJECTS ARGS)
265288
cmake_parse_arguments(
@@ -270,10 +293,16 @@ function(target_code_coverage TARGET_NAME)
270293
# PRIVATE.
271294
if(target_code_coverage_PUBLIC)
272295
set(TARGET_VISIBILITY PUBLIC)
296+
set(TARGET_LINK_VISIBILITY PUBLIC)
273297
elseif(target_code_coverage_INTERFACE)
274298
set(TARGET_VISIBILITY INTERFACE)
299+
set(TARGET_LINK_VISIBILITY INTERFACE)
300+
elseif(target_code_coverage_PLAIN)
301+
set(TARGET_VISIBILITY PUBLIC)
302+
set(TARGET_LINK_VISIBILITY)
275303
else()
276304
set(TARGET_VISIBILITY PRIVATE)
305+
set(TARGET_LINK_VISIBILITY PRIVATE)
277306
endif()
278307

279308
if(NOT target_code_coverage_COVERAGE_TARGET_NAME)
@@ -294,7 +323,7 @@ function(target_code_coverage TARGET_NAME)
294323
"GNU")
295324
target_compile_options(${TARGET_NAME} ${TARGET_VISIBILITY} -fprofile-arcs
296325
-ftest-coverage)
297-
target_link_libraries(${TARGET_NAME} ${TARGET_VISIBILITY} gcov)
326+
target_link_libraries(${TARGET_NAME} ${TARGET_LINK_VISIBILITY} gcov)
298327
endif()
299328

300329
# Targets

0 commit comments

Comments
 (0)
Failed to load comments.