Skip to content

Commit 8fc5da5

Browse files
committed
Update code-coverage 'OBJECTS' for static libs
In addition to the original ability for 'OBJECTS' to import and present coverage information for shared libraries, it has now been updated to do the same for detected static library targets.
1 parent dbf6bb5 commit 8fc5da5

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

code-coverage.cmake

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright (C) 2018-2020 by George Cave - gcave@stablecoder.ca
2+
# Copyright (C) 2018-2024 by George Cave - gcave@stablecoder.ca
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
@@ -222,7 +222,7 @@ endif()
222222
# EXTERNAL - For GCC's lcov, allows the profiling of 'external' files from the processing directory
223223
# COVERAGE_TARGET_NAME - For executables ONLY, changes the outgoing target name so instead of `ccov-${TARGET_NAME}` it becomes `ccov-${COVERAGE_TARGET_NAME}`.
224224
# EXCLUDE <PATTERNS> - Excludes files of the patterns provided from coverage. Note that GCC/lcov excludes by glob pattern, and clang/LLVM excludes via regex! **These do not copy to the 'all' targets.**
225-
# OBJECTS <TARGETS> - For executables ONLY, if the provided targets are shared libraries, adds coverage information to the output
225+
# OBJECTS <TARGETS> - For executables ONLY, if the provided targets are static or shared libraries, adds coverage information to the output
226226
# PRE_ARGS <ARGUMENTS> - For executables ONLY, prefixes given arguments to the associated ccov-* executable call ($<PRE_ARGS> ccov-*)
227227
# ARGS <ARGUMENTS> - For executables ONLY, appends the given arguments to the associated ccov-* executable call (ccov-* $<ARGS>)
228228
# ~~~
@@ -304,14 +304,16 @@ function(target_code_coverage TARGET_NAME)
304304
if(CMAKE_C_COMPILER_ID MATCHES "(Apple)?[Cc]lang"
305305
OR CMAKE_CXX_COMPILER_ID MATCHES "(Apple)?[Cc]lang")
306306

307-
# If there are shared objects to also work with, generate the string to
308-
# add them here
309-
foreach(SO_TARGET ${target_code_coverage_OBJECTS})
307+
# If there are static or shared objects to also work with, generate the
308+
# string to add them here
309+
foreach(LINKED_OBJECT ${target_code_coverage_OBJECTS})
310310
# Check to see if the target is a shared object
311-
if(TARGET ${SO_TARGET})
312-
get_target_property(SO_TARGET_TYPE ${SO_TARGET} TYPE)
313-
if(${SO_TARGET_TYPE} STREQUAL "SHARED_LIBRARY")
314-
set(SO_OBJECTS ${SO_OBJECTS} -object=$<TARGET_FILE:${SO_TARGET}>)
311+
if(TARGET ${_TARGET})
312+
get_target_property(LINKED_OBJECT_TYPE ${LINKED_OBJECT} TYPE)
313+
if(${LINKED_OBJECT_TYPE} STREQUAL "STATIC_LIBRARY"
314+
OR ${LINKED_OBJECT_TYPE} STREQUAL "SHARED_LIBRARY")
315+
set(LINKED_OBJECTS ${LINKED_OBJECTS}
316+
-object=$<TARGET_FILE:${LINKED_OBJECT}>)
315317
endif()
316318
endif()
317319
endforeach()
@@ -328,7 +330,8 @@ function(target_code_coverage TARGET_NAME)
328330
$<TARGET_FILE:${TARGET_NAME}> ${target_code_coverage_ARGS}
329331
COMMAND
330332
${CMAKE_COMMAND} -E echo "-object=$<TARGET_FILE:${TARGET_NAME}>"
331-
${SO_OBJECTS} >> ${CMAKE_COVERAGE_OUTPUT_DIRECTORY}/binaries.list
333+
${LINKED_OBJECTS} >>
334+
${CMAKE_COVERAGE_OUTPUT_DIRECTORY}/binaries.list
332335
COMMAND
333336
${CMAKE_COMMAND} -E echo
334337
"${CMAKE_CURRENT_BINARY_DIR}/${target_code_coverage_COVERAGE_TARGET_NAME}.profraw"
@@ -357,40 +360,40 @@ function(target_code_coverage TARGET_NAME)
357360
add_custom_target(
358361
ccov-show-${target_code_coverage_COVERAGE_TARGET_NAME}
359362
COMMAND
360-
${LLVM_COV_PATH} show $<TARGET_FILE:${TARGET_NAME}> ${SO_OBJECTS}
363+
${LLVM_COV_PATH} show $<TARGET_FILE:${TARGET_NAME}>
361364
-instr-profile=${target_code_coverage_COVERAGE_TARGET_NAME}.profdata
362-
-show-line-counts-or-regions ${EXCLUDE_REGEX}
365+
-show-line-counts-or-regions ${LINKED_OBJECTS} ${EXCLUDE_REGEX}
363366
DEPENDS ccov-processing-${target_code_coverage_COVERAGE_TARGET_NAME})
364367

365368
# Print out a summary of the coverage information to the command line
366369
add_custom_target(
367370
ccov-report-${target_code_coverage_COVERAGE_TARGET_NAME}
368371
COMMAND
369-
${LLVM_COV_PATH} report $<TARGET_FILE:${TARGET_NAME}> ${SO_OBJECTS}
372+
${LLVM_COV_PATH} report $<TARGET_FILE:${TARGET_NAME}>
370373
-instr-profile=${target_code_coverage_COVERAGE_TARGET_NAME}.profdata
371-
${EXCLUDE_REGEX}
374+
${LINKED_OBJECTS} ${EXCLUDE_REGEX}
372375
DEPENDS ccov-processing-${target_code_coverage_COVERAGE_TARGET_NAME})
373376

374377
# Export coverage information so continuous integration tools (e.g.
375378
# Jenkins) can consume it
376379
add_custom_target(
377380
ccov-export-${target_code_coverage_COVERAGE_TARGET_NAME}
378381
COMMAND
379-
${LLVM_COV_PATH} export $<TARGET_FILE:${TARGET_NAME}> ${SO_OBJECTS}
382+
${LLVM_COV_PATH} export $<TARGET_FILE:${TARGET_NAME}>
380383
-instr-profile=${target_code_coverage_COVERAGE_TARGET_NAME}.profdata
381-
-format="text" ${EXCLUDE_REGEX} >
384+
-format="text" ${LINKED_OBJECTS} ${EXCLUDE_REGEX} >
382385
${CMAKE_COVERAGE_OUTPUT_DIRECTORY}/${target_code_coverage_COVERAGE_TARGET_NAME}.json
383386
DEPENDS ccov-processing-${target_code_coverage_COVERAGE_TARGET_NAME})
384387

385388
# Generates HTML output of the coverage information for perusal
386389
add_custom_target(
387390
ccov-${target_code_coverage_COVERAGE_TARGET_NAME}
388391
COMMAND
389-
${LLVM_COV_PATH} show $<TARGET_FILE:${TARGET_NAME}> ${SO_OBJECTS}
392+
${LLVM_COV_PATH} show $<TARGET_FILE:${TARGET_NAME}>
390393
-instr-profile=${target_code_coverage_COVERAGE_TARGET_NAME}.profdata
391394
-show-line-counts-or-regions
392395
-output-dir=${CMAKE_COVERAGE_OUTPUT_DIRECTORY}/${target_code_coverage_COVERAGE_TARGET_NAME}
393-
-format="html" ${EXCLUDE_REGEX}
396+
-format="html" ${LINKED_OBJECTS} ${EXCLUDE_REGEX}
394397
DEPENDS ccov-processing-${target_code_coverage_COVERAGE_TARGET_NAME})
395398

396399
elseif(CMAKE_C_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES

0 commit comments

Comments
 (0)