Skip to content

Commit c71e0d0

Browse files
committed
Update code-coverage documentation
1 parent 75c5ff2 commit c71e0d0

File tree

2 files changed

+68
-39
lines changed

2 files changed

+68
-39
lines changed

README.md

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -156,27 +156,42 @@ To enable, turn on the `CODE_COVERAGE` variable.
156156

157157
### Added Targets
158158

159-
- GCOV/LCOV:
160-
- ccov : Generates HTML code coverage report for every target added with 'AUTO' parameter.
161-
- ccov-${TARGET_NAME} : Generates HTML code coverage report for the associated named target.
162-
- ccov-all : Generates HTML code coverage report, merging every target added with 'ALL' parameter into a single detailed report.
163-
- ccov-all-capture : Generates an all-merged.info file, for use with coverage dashboards (e.g. codecov.io, coveralls).
164-
- LLVM-COV:
165-
- ccov : Generates HTML code coverage report for every target added with 'AUTO' parameter.
166-
- ccov-report : Generates HTML code coverage report for every target added with 'AUTO' parameter.
167-
- ccov-${TARGET_NAME} : Generates HTML code coverage report.
168-
- ccov-rpt-${TARGET_NAME} : Prints to command line summary per-file coverage information.
169-
- ccov-show-${TARGET_NAME} : Prints to command line detailed per-line coverage information.
170-
- ccov-all : Generates HTML code coverage report, merging every target added with 'ALL' parameter into a single detailed report.
171-
- ccov-all-report : Prints summary per-file coverage information for every target added with ALL' parameter to the command line.
159+
#### Via `target_code_coverage`
160+
161+
Targets added (executables only):
162+
- ccov-run-${TARGET_NAME} : Re-runs the executable, collecting fresh coverage data
163+
- ccov-html-${TARGET_NAME} : Generates HTML code coverage report for the associated named target.
164+
- ccov-${TARGET_NAME} : Generates HTML code coverage report for the associated named target. (same as ccov-html-${TARGET_NAME})
165+
- ccov-html : Generates HTML code coverage report for every target added with 'AUTO' parameter.
166+
- ccov : Generates HTML code coverage report for every target added with 'AUTO' parameter. (same as ccov-html)
167+
168+
LLVM-based coverage targets added (executables only):
169+
- ccov-report-${TARGET_NAME} : Prints to command line summary per-file coverage information.
170+
- ccov-export-${TARGET_NAME} : Exports the coverage report to a JSON file.
171+
- ccov-show-${TARGET_NAME} : Prints to command line detailed per-line coverage information.
172+
- ccov-report : Generates HTML code coverage report for every target added with 'AUTO' parameter.
173+
174+
#### Via `add_code_coverage_all_targets`
175+
176+
Targets added:
177+
- ccov-all-run : Re-runs all tagged executables, collecting fresh coverage data
178+
- ccov-all-html : Generates an HTML report of all tagged executable coverage data merged into one
179+
- ccov-all : Generates an HTML report of all tagged executable coverage data merged into one (same as ccov-all-html)
180+
181+
LLVM-based coverage targets added:
182+
- ccov-all-report : Generates an HTML report of all tagged executable coverage data merged into one and displays it in the CLI
183+
- ccov-all-export : Exports coverage data in JSON format for use in CI environments or similar
184+
185+
GCC-based coverage targets added:
186+
- ccov-all-capture : Generates an all-merged.info file, for use with coverage dashboards (e.g. codecov.io, coveralls).
172187

173188
### Usage
174189

175190
To enable any code coverage instrumentation/targets, the single CMake option of `CODE_COVERAGE` needs to be set to 'ON', either by GUI, ccmake, or on the command line ie `-DCODE_COVERAGE=ON`.
176191

177192
From this point, there are two primary methods for adding instrumentation to targets:
178193
1. A blanket instrumentation by calling `add_code_coverage()`, where all targets in that directory and all subdirectories are automatically instrumented.
179-
2. Per-target instrumentation by calling `target_code_coverage(<TARGET_NAME>)`, where the target is given and thus only that target is instrumented. This applies to both libraries and executables.
194+
2. Per-target instrumentation by calling `target_code_coverage(<TARGET_NAME>)`, where the target is given and thus only that target is instrumented and executables have ccov* targets created. This applies to both libraries and executables.
180195
3. Automatically add coverage for each target with `-DCCOV_TARGETS_HOOK=On` and `-DCCOV_TARGETS_HOOK_ARGS=...` for default values, requires `add_code_coverage()` or similar.
181196

182197
To add coverage targets, such as calling `make ccov` to generate the actual coverage information for perusal or consumption, call `target_code_coverage(<TARGET_NAME>)` on an *executable* target.

code-coverage.cmake

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright (C) 2018-2024 by George Cave - [email protected]
2+
# Copyright (C) 2018-2025 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
@@ -224,37 +224,34 @@ endif()
224224
# Adds code coverage instrumentation to a library, or instrumentation/targets
225225
# for an executable target.
226226
# ~~~
227-
# EXECUTABLE ADDED TARGETS:
228-
# GCOV/LCOV:
229-
# ccov : Generates HTML code coverage report for every target added with 'AUTO' parameter.
230-
# ccov-${TARGET_NAME} : Generates HTML code coverage report for the associated named target.
231-
# ccov-all : Generates HTML code coverage report, merging every target added with 'ALL' parameter into a single detailed report.
232-
#
233-
# LLVM-COV:
234-
# ccov : Generates HTML code coverage report for every target added with 'AUTO' parameter.
235-
# ccov-report : Generates HTML code coverage report for every target added with 'AUTO' parameter.
236-
# ccov-${TARGET_NAME} : Generates HTML code coverage report.
227+
# Targets added (executables only):
228+
# ccov-run-${TARGET_NAME} : Re-runs the executable, collecting fresh coverage data
229+
# ccov-html-${TARGET_NAME} : Generates HTML code coverage report for the associated named target.
230+
# ccov-${TARGET_NAME} : Generates HTML code coverage report for the associated named target. (same as ccov-html-${TARGET_NAME})
231+
# ccov-html : Generates HTML code coverage report for every target added with 'AUTO' parameter.
232+
# ccov : Generates HTML code coverage report for every target added with 'AUTO' parameter. (same as ccov-html)
233+
#
234+
# LLVM-based coverage targets added (executables only):
237235
# ccov-report-${TARGET_NAME} : Prints to command line summary per-file coverage information.
238236
# ccov-export-${TARGET_NAME} : Exports the coverage report to a JSON file.
239237
# ccov-show-${TARGET_NAME} : Prints to command line detailed per-line coverage information.
240-
# ccov-all : Generates HTML code coverage report, merging every target added with 'ALL' parameter into a single detailed report.
241-
# ccov-all-report : Prints summary per-file coverage information for every target added with ALL' parameter to the command line.
242-
# ccov-all-export : Exports the coverage report to a JSON file.
238+
# ccov-report : Generates HTML code coverage report for every target added with 'AUTO' parameter.
243239
#
244-
# Required:
240+
# Required Parameters:
245241
# TARGET_NAME - Name of the target to generate code coverage for.
246-
# Optional:
242+
#
243+
# Optional Parameters:
247244
# PUBLIC - Sets the visibility for added compile options to targets to PUBLIC instead of the default of PRIVATE.
248245
# INTERFACE - Sets the visibility for added compile options to targets to INTERFACE instead of the default of PRIVATE.
249246
# PLAIN - Do not set any target visibility (backward compatibility with old cmake projects)
250247
# AUTO - Adds the target to the 'ccov' target so that it can be run in a batch with others easily. Effective on executable targets.
251-
# 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.
248+
# ALL - Adds the target to the 'ccov-all-*' targets created by a prior call to `add_code_coverage_all_targets` Effective on executable targets.
252249
# EXTERNAL - For GCC's lcov, allows the profiling of 'external' files from the processing directory
253250
# COVERAGE_TARGET_NAME - For executables ONLY, changes the outgoing target name so instead of `ccov-${TARGET_NAME}` it becomes `ccov-${COVERAGE_TARGET_NAME}`.
254251
# 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.**
255252
# OBJECTS <TARGETS> - For executables ONLY, if the provided targets are static or shared libraries, adds coverage information to the output
256-
# PRE_ARGS <ARGUMENTS> - For executables ONLY, prefixes given arguments to the associated ccov-* executable call ($<PRE_ARGS> ccov-*)
257-
# ARGS <ARGUMENTS> - For executables ONLY, appends the given arguments to the associated ccov-* executable call (ccov-* $<ARGS>)
253+
# PRE_ARGS <ARGUMENTS> - For executables ONLY, prefixes given arguments to the associated ccov-run-${TARGET_NAME} executable call ($<PRE_ARGS> ccov-*)
254+
# ARGS <ARGUMENTS> - For executables ONLY, appends the given arguments to the associated ccov-run-${TARGET_NAME} executable call (ccov-* $<ARGS>)
258255
# ~~~
259256
function(target_code_coverage TARGET_NAME)
260257
if(NOT CODE_COVERAGE)
@@ -613,8 +610,13 @@ function(target_code_coverage TARGET_NAME)
613610
endfunction()
614611

615612
# Adds code coverage instrumentation to all targets in the current directory and
616-
# any subdirectories. To add coverage instrumentation to only specific targets,
613+
# any subdirectories.
614+
#
615+
# To add coverage instrumentation to only specific targets, or to add targets,
617616
# use `target_code_coverage`.
617+
#
618+
# @WARNING: Does not add targets to collect coverage data from executables, use
619+
# `target_code_coverage` to do so.
618620
function(add_code_coverage)
619621
if(NOT CODE_COVERAGE)
620622
return()
@@ -633,13 +635,23 @@ function(add_code_coverage)
633635
endif()
634636
endfunction()
635637

636-
# Adds the 'ccov-all' type targets that calls all targets added via
638+
# Adds several 'ccov-all-*' type targets that operates on all targets added via
637639
# `target_code_coverage` with the `ALL` parameter, but merges all the coverage
638-
# data from them into a single large report instead of the numerous smaller
639-
# reports. Also adds the ccov-all-capture Generates an all-merged.info file, for
640-
# use with coverage dashboards (e.g. codecov.io, coveralls).
640+
# data into a single large report instead of numerous smaller reports.
641641
# ~~~
642-
# Optional:
642+
# Targets added:
643+
# ccov-all-run : Re-runs all tagged executables, collecting fresh coverage data
644+
# ccov-all-html : Generates an HTML report of all tagged executable coverage data merged into one
645+
# ccov-all : Generates an HTML report of all tagged executable coverage data merged into one (same as ccov-all-html)
646+
#
647+
# LLVM-based coverage targets added:
648+
# ccov-all-report : Generates an HTML report of all tagged executable coverage data merged into one and displays it in the CLI
649+
# ccov-all-export : Exports coverage data in JSON format for use in CI environments or similar
650+
#
651+
# GCC-based coverage targets added:
652+
# ccov-all-capture : Generates an all-merged.info file, for use with coverage dashboards (e.g. codecov.io, coveralls).
653+
#
654+
# Optional Parameters:
643655
# EXCLUDE <PATTERNS> - Excludes files of the patterns provided from coverage. Note that GCC/lcov excludes by glob pattern, and clang/LLVM excludes via regex!
644656
# ~~~
645657
function(add_code_coverage_all_targets)
@@ -810,6 +822,8 @@ function(add_code_coverage_all_targets)
810822
COMMAND ${LCOV_PATH} --ignore-errors unused --directory
811823
${CMAKE_BINARY_DIR} --capture --output-file ${COVERAGE_INFO}
812824
COMMAND ${EXCLUDE_COMMAND}
825+
COMMAND ${CMAKE_COMMAND} -E echo
826+
"Generated coverage .info file at ${COVERAGE_INFO}"
813827
DEPENDS ccov-all-ran)
814828
add_custom_target(ccov-all-capture DEPENDS ${COVERAGE_INFO})
815829

0 commit comments

Comments
 (0)