Skip to content

Commit 1ef4557

Browse files
committed
Added ability to designate custom ccov targets
The new parameter for target_code_coverage function allows for custom naming of ccov targets, so rather than being limited to just the TARGET_NAME, and thus a single ccov target per executable, now multiple variants can be generated using differing names.
1 parent 83aca43 commit 1ef4557

File tree

1 file changed

+38
-31
lines changed

1 file changed

+38
-31
lines changed

code-coverage.cmake

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -192,16 +192,23 @@ endif()
192192
# AUTO - Adds the target to the 'ccov' target so that it can be run in a batch with others easily. Effective on executable targets.
193193
# 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.
194194
# EXTERNAL - For GCC's lcov, allows the profiling of 'external' files from the processing directory
195+
# COVERAGE_TARGET_NAME - Changes the outgoing target name so instead of `ccov-${TARGET_NAME}` it becomes `ccov-${COVERAGE_TARGET_NAME}`.
195196
# EXCLUDE <REGEX_PATTERNS> - Excludes files of the patterns provided from coverage. **These do not copy to the 'all' targets.**
196197
# OBJECTS <TARGETS> - For executables ONLY, if the provided targets are shared libraries, adds coverage information to the output
197198
# ~~~
198199
function(target_code_coverage TARGET_NAME)
199200
# Argument parsing
200201
set(options AUTO ALL EXTERNAL)
202+
set(single_value_keywords COVERAGE_TARGET_NAME)
201203
set(multi_value_keywords EXCLUDE OBJECTS)
202-
cmake_parse_arguments(target_code_coverage "${options}" ""
204+
cmake_parse_arguments(target_code_coverage "${options}" "${single_value_keywords}"
203205
"${multi_value_keywords}" ${ARGN})
204206

207+
if(NOT target_code_coverage_COVERAGE_TARGET_NAME)
208+
# If a specific name was given, use that instead.
209+
set(target_code_coverage_COVERAGE_TARGET_NAME ${TARGET_NAME})
210+
endif()
211+
205212
if(CODE_COVERAGE)
206213

207214
# Add code coverage instrumentation to the target's linker command
@@ -231,7 +238,7 @@ function(target_code_coverage TARGET_NAME)
231238
if(CMAKE_C_COMPILER_ID MATCHES "(Apple)?[Cc]lang"
232239
OR CMAKE_CXX_COMPILER_ID MATCHES "(Apple)?[Cc]lang")
233240
add_custom_target(
234-
ccov-run-${TARGET_NAME}
241+
ccov-run-${target_code_coverage_COVERAGE_TARGET_NAME}
235242
COMMAND echo "-object=$<TARGET_FILE:${TARGET_NAME}>" >>
236243
${CMAKE_COVERAGE_OUTPUT_DIRECTORY}/binaries.list
237244
DEPENDS ccov-preprocessing ${TARGET_NAME})
@@ -243,7 +250,7 @@ function(target_code_coverage TARGET_NAME)
243250
)
244251
endif()
245252

246-
add_dependencies(ccov-libs ccov-run-${TARGET_NAME})
253+
add_dependencies(ccov-libs ccov-run-${target_code_coverage_COVERAGE_TARGET_NAME})
247254
endif()
248255
endif()
249256

@@ -266,21 +273,21 @@ function(target_code_coverage TARGET_NAME)
266273

267274
# Run the executable, generating raw profile data
268275
add_custom_target(
269-
ccov-run-${TARGET_NAME}
270-
COMMAND LLVM_PROFILE_FILE=${TARGET_NAME}.profraw
276+
ccov-run-${target_code_coverage_COVERAGE_TARGET_NAME}
277+
COMMAND LLVM_PROFILE_FILE=${target_code_coverage_COVERAGE_TARGET_NAME}.profraw
271278
$<TARGET_FILE:${TARGET_NAME}>
272279
COMMAND echo "-object=$<TARGET_FILE:${TARGET_NAME}>" >>
273280
${CMAKE_COVERAGE_OUTPUT_DIRECTORY}/binaries.list
274-
COMMAND echo "${CMAKE_CURRENT_BINARY_DIR}/${TARGET_NAME}.profraw " >>
281+
COMMAND echo "${CMAKE_CURRENT_BINARY_DIR}/${target_code_coverage_COVERAGE_TARGET_NAME}.profraw " >>
275282
${CMAKE_COVERAGE_OUTPUT_DIRECTORY}/profraw.list
276283
DEPENDS ccov-preprocessing ccov-libs ${TARGET_NAME})
277284

278285
# Merge the generated profile data so llvm-cov can process it
279286
add_custom_target(
280-
ccov-processing-${TARGET_NAME}
281-
COMMAND ${LLVM_PROFDATA_PATH} merge -sparse ${TARGET_NAME}.profraw -o
282-
${TARGET_NAME}.profdata
283-
DEPENDS ccov-run-${TARGET_NAME})
287+
ccov-processing-${target_code_coverage_COVERAGE_TARGET_NAME}
288+
COMMAND ${LLVM_PROFDATA_PATH} merge -sparse ${target_code_coverage_COVERAGE_TARGET_NAME}.profraw -o
289+
${target_code_coverage_COVERAGE_TARGET_NAME}.profdata
290+
DEPENDS ccov-run-${target_code_coverage_COVERAGE_TARGET_NAME})
284291

285292
# Ignore regex only works on LLVM >= 7
286293
if(LLVM_COV_VERSION VERSION_GREATER_EQUAL "7.0.0")
@@ -292,38 +299,38 @@ function(target_code_coverage TARGET_NAME)
292299

293300
# Print out details of the coverage information to the command line
294301
add_custom_target(
295-
ccov-show-${TARGET_NAME}
302+
ccov-show-${target_code_coverage_COVERAGE_TARGET_NAME}
296303
COMMAND
297304
${LLVM_COV_PATH} show $<TARGET_FILE:${TARGET_NAME}> ${SO_OBJECTS}
298-
-instr-profile=${TARGET_NAME}.profdata -show-line-counts-or-regions
305+
-instr-profile=${target_code_coverage_COVERAGE_TARGET_NAME}.profdata -show-line-counts-or-regions
299306
${EXCLUDE_REGEX}
300-
DEPENDS ccov-processing-${TARGET_NAME})
307+
DEPENDS ccov-processing-${target_code_coverage_COVERAGE_TARGET_NAME})
301308

302309
# Print out a summary of the coverage information to the command line
303310
add_custom_target(
304-
ccov-report-${TARGET_NAME}
311+
ccov-report-${target_code_coverage_COVERAGE_TARGET_NAME}
305312
COMMAND ${LLVM_COV_PATH} report $<TARGET_FILE:${TARGET_NAME}>
306-
${SO_OBJECTS} -instr-profile=${TARGET_NAME}.profdata
313+
${SO_OBJECTS} -instr-profile=${target_code_coverage_COVERAGE_TARGET_NAME}.profdata
307314
${EXCLUDE_REGEX}
308-
DEPENDS ccov-processing-${TARGET_NAME})
315+
DEPENDS ccov-processing-${target_code_coverage_COVERAGE_TARGET_NAME})
309316

310317
# Generates HTML output of the coverage information for perusal
311318
add_custom_target(
312-
ccov-${TARGET_NAME}
319+
ccov-${target_code_coverage_COVERAGE_TARGET_NAME}
313320
COMMAND
314321
${LLVM_COV_PATH} show $<TARGET_FILE:${TARGET_NAME}> ${SO_OBJECTS}
315-
-instr-profile=${TARGET_NAME}.profdata -show-line-counts-or-regions
316-
-output-dir=${CMAKE_COVERAGE_OUTPUT_DIRECTORY}/${TARGET_NAME}
322+
-instr-profile=${target_code_coverage_COVERAGE_TARGET_NAME}.profdata -show-line-counts-or-regions
323+
-output-dir=${CMAKE_COVERAGE_OUTPUT_DIRECTORY}/${target_code_coverage_COVERAGE_TARGET_NAME}
317324
-format="html" ${EXCLUDE_REGEX}
318-
DEPENDS ccov-processing-${TARGET_NAME})
325+
DEPENDS ccov-processing-${target_code_coverage_COVERAGE_TARGET_NAME})
319326

320327
elseif(CMAKE_COMPILER_IS_GNUCXX)
321328
set(COVERAGE_INFO
322-
"${CMAKE_COVERAGE_OUTPUT_DIRECTORY}/${TARGET_NAME}.info")
329+
"${CMAKE_COVERAGE_OUTPUT_DIRECTORY}/${target_code_coverage_COVERAGE_TARGET_NAME}.info")
323330

324331
# Run the executable, generating coverage information
325332
add_custom_target(
326-
ccov-run-${TARGET_NAME}
333+
ccov-run-${target_code_coverage_COVERAGE_TARGET_NAME}
327334
COMMAND $<TARGET_FILE:${TARGET_NAME}>
328335
DEPENDS ccov-preprocessing ${TARGET_NAME})
329336

@@ -346,7 +353,7 @@ function(target_code_coverage TARGET_NAME)
346353

347354
# Capture coverage data
348355
add_custom_target(
349-
ccov-capture-${TARGET_NAME}
356+
ccov-capture-${target_code_coverage_COVERAGE_TARGET_NAME}
350357
COMMAND ${CMAKE_COMMAND} -E remove ${COVERAGE_INFO}
351358
COMMAND ${LCOV_PATH} --directory ${CMAKE_BINARY_DIR} --zerocounters
352359
COMMAND $<TARGET_FILE:${TARGET_NAME}>
@@ -359,33 +366,33 @@ function(target_code_coverage TARGET_NAME)
359366

360367
# Generates HTML output of the coverage information for perusal
361368
add_custom_target(
362-
ccov-${TARGET_NAME}
369+
ccov-${target_code_coverage_COVERAGE_TARGET_NAME}
363370
COMMAND ${GENHTML_PATH} -o
364-
${CMAKE_COVERAGE_OUTPUT_DIRECTORY}/${TARGET_NAME}
371+
${CMAKE_COVERAGE_OUTPUT_DIRECTORY}/${target_code_coverage_COVERAGE_TARGET_NAME}
365372
${COVERAGE_INFO}
366-
DEPENDS ccov-capture-${TARGET_NAME})
373+
DEPENDS ccov-capture-${target_code_coverage_COVERAGE_TARGET_NAME})
367374
endif()
368375

369376
add_custom_command(
370-
TARGET ccov-${TARGET_NAME}
377+
TARGET ccov-${target_code_coverage_COVERAGE_TARGET_NAME}
371378
POST_BUILD
372379
COMMAND ;
373380
COMMENT
374-
"Open ${CMAKE_COVERAGE_OUTPUT_DIRECTORY}/${TARGET_NAME}/index.html in your browser to view the coverage report."
381+
"Open ${CMAKE_COVERAGE_OUTPUT_DIRECTORY}/${target_code_coverage_COVERAGE_TARGET_NAME}/index.html in your browser to view the coverage report."
375382
)
376383

377384
# AUTO
378385
if(target_code_coverage_AUTO)
379386
if(NOT TARGET ccov)
380387
add_custom_target(ccov)
381388
endif()
382-
add_dependencies(ccov ccov-${TARGET_NAME})
389+
add_dependencies(ccov ccov-${target_code_coverage_COVERAGE_TARGET_NAME})
383390

384391
if(NOT CMAKE_COMPILER_IS_GNUCXX)
385392
if(NOT TARGET ccov-report)
386393
add_custom_target(ccov-report)
387394
endif()
388-
add_dependencies(ccov-report ccov-report-${TARGET_NAME})
395+
add_dependencies(ccov-report ccov-report-${target_code_coverage_COVERAGE_TARGET_NAME})
389396
endif()
390397
endif()
391398

@@ -398,7 +405,7 @@ function(target_code_coverage TARGET_NAME)
398405
)
399406
endif()
400407

401-
add_dependencies(ccov-all-processing ccov-run-${TARGET_NAME})
408+
add_dependencies(ccov-all-processing ccov-run-${target_code_coverage_COVERAGE_TARGET_NAME})
402409
endif()
403410
endif()
404411
endif()

0 commit comments

Comments
 (0)