Skip to content

Commit d6fe5fc

Browse files
committed
Updated target_code_coverage visibility options
When calling target_code_coverage, one can now use the extra single argument options to change the visibility of added compile/link options to PUBLIC or INTERFACE from the default PRIVATE.
1 parent 6c11e34 commit d6fe5fc

File tree

1 file changed

+35
-15
lines changed

1 file changed

+35
-15
lines changed

Diff for: code-coverage.cmake

+35-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright (C) 2018 by George Cave - [email protected]
2+
# Copyright (C) 2018-2020 by George Cave - [email protected]
33
#
44
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
55
# use this file except in compliance with the License. You may obtain a copy of
@@ -191,6 +191,8 @@ endif()
191191
# Required:
192192
# TARGET_NAME - Name of the target to generate code coverage for.
193193
# Optional:
194+
# PUBLIC - Sets the visibility for added compile options to targets to PUBLIC instead of the default of PRIVATE.
195+
# PUBLIC - Sets the visibility for added compile options to targets to INTERFACE instead of the default of PRIVATE.
194196
# AUTO - Adds the target to the 'ccov' target so that it can be run in a batch with others easily. Effective on executable targets.
195197
# 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.
196198
# EXTERNAL - For GCC's lcov, allows the profiling of 'external' files from the processing directory
@@ -201,13 +203,28 @@ endif()
201203
# ~~~
202204
function(target_code_coverage TARGET_NAME)
203205
# Argument parsing
204-
set(options AUTO ALL EXTERNAL)
206+
set(options
207+
AUTO
208+
ALL
209+
EXTERNAL
210+
PUBLIC
211+
INTERFACE)
205212
set(single_value_keywords COVERAGE_TARGET_NAME)
206213
set(multi_value_keywords EXCLUDE OBJECTS ARGS)
207214
cmake_parse_arguments(
208215
target_code_coverage "${options}" "${single_value_keywords}"
209216
"${multi_value_keywords}" ${ARGN})
210217

218+
# Set the visibility of target functions to PUBLIC, INTERFACE or default to
219+
# PRIVATE.
220+
if(target_code_coverage_PUBLIC)
221+
set(TARGET_VISIBILITY PUBLIC)
222+
elseif(target_code_coverage_INTERFACE)
223+
set(TARGET_VISIBILITY INTERFACE)
224+
else()
225+
set(TARGET_VISIBILITY PRIVATE)
226+
endif()
227+
211228
if(NOT target_code_coverage_COVERAGE_TARGET_NAME)
212229
# If a specific name was given, use that instead.
213230
set(target_code_coverage_COVERAGE_TARGET_NAME ${TARGET_NAME})
@@ -218,20 +235,23 @@ function(target_code_coverage TARGET_NAME)
218235
# Add code coverage instrumentation to the target's linker command
219236
if(CMAKE_C_COMPILER_ID MATCHES "(Apple)?[Cc]lang"
220237
OR CMAKE_CXX_COMPILER_ID MATCHES "(Apple)?[Cc]lang")
221-
target_compile_options(${TARGET_NAME} PRIVATE -fprofile-instr-generate
222-
-fcoverage-mapping)
223-
set_property(
224-
TARGET ${TARGET_NAME}
225-
APPEND_STRING
226-
PROPERTY LINK_FLAGS "-fprofile-instr-generate ")
227-
set_property(
228-
TARGET ${TARGET_NAME}
229-
APPEND_STRING
230-
PROPERTY LINK_FLAGS "-fcoverage-mapping ")
238+
target_compile_options(
239+
${TARGET_NAME}
240+
${TARGET_VISIBILITY}
241+
-fprofile-instr-generate
242+
-fcoverage-mapping)
243+
target_link_options(
244+
${TARGET_NAME}
245+
${TARGET_VISIBILITY}
246+
-fprofile-instr-generate
247+
-fcoverage-mapping)
231248
elseif(CMAKE_COMPILER_IS_GNUCXX)
232-
target_compile_options(${TARGET_NAME} PRIVATE -fprofile-arcs
233-
-ftest-coverage)
234-
target_link_libraries(${TARGET_NAME} PRIVATE gcov)
249+
target_compile_options(
250+
${TARGET_NAME}
251+
${TARGET_VISIBILITY}
252+
-fprofile-arcs
253+
-ftest-coverage)
254+
target_link_libraries(${TARGET_NAME} ${TARGET_VISIBILITY} gcov)
235255
endif()
236256

237257
# Targets

0 commit comments

Comments
 (0)