Skip to content

Commit 5b6c6ef

Browse files
committed
More passthrough options for coverage backends
A bunch of options that allow users to specify options to passthrough to the backend programs.
1 parent f4431e7 commit 5b6c6ef

File tree

1 file changed

+85
-26
lines changed

1 file changed

+85
-26
lines changed

code-coverage.cmake

Lines changed: 85 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,8 @@ endif()
218218
# Targets added (executables only):
219219
# ccov-run-${TARGET_NAME} : Re-runs the executable, collecting fresh coverage data
220220
# ccov-html-${TARGET_NAME} : Generates HTML code coverage report for the associated named target.
221-
# ccov-${TARGET_NAME} : Generates HTML code coverage report for the associated named target. (same as ccov-html-${TARGET_NAME})
222221
# ccov-html : Generates HTML code coverage report for every target added with 'AUTO' parameter.
222+
# ccov-${TARGET_NAME} : Generates HTML code coverage report for the associated named target. (same as ccov-html-${TARGET_NAME})
223223
# ccov : Generates HTML code coverage report for every target added with 'AUTO' parameter. (same as ccov-html)
224224
#
225225
# LLVM-based coverage targets added (executables only):
@@ -239,12 +239,23 @@ endif()
239239
# ALL - Adds the target to the 'ccov-all-*' targets created by a prior call to `add_code_coverage_all_targets` Effective on executable targets.
240240
# EXTERNAL - For GCC's lcov, allows the profiling of 'external' files from the processing directory
241241
# COVERAGE_TARGET_NAME - For executables ONLY, changes the outgoing target name so instead of `ccov-${TARGET_NAME}` it becomes `ccov-${COVERAGE_TARGET_NAME}`.
242-
# EXCLUDE <PATTERNS> - Excludes files of the patterns provided from coverage. Added to any lcov/llvm specific excludes. sNote that GCC/lcov excludes by glob pattern, and clang/LLVM excludes via regex! **These do not copy to the 'all' targets.**
243-
# LLVM_EXCLUDE <PATTERNS> - Excludes files that match the provided patterns, LLVM excludes by regex patterns.
244-
# LCOV_EXCLUDE <PATTERNS> - Excludes files that match the provided patterns. LCOV exclude by glob patterns.
245242
# OBJECTS <TARGETS> - For executables ONLY, if the provided targets are static or shared libraries, adds coverage information to the output
246243
# PRE_ARGS <ARGUMENTS> - For executables ONLY, prefixes given arguments to the associated ccov-run-${TARGET_NAME} executable call ($<PRE_ARGS> ccov-*)
247244
# ARGS <ARGUMENTS> - For executables ONLY, appends the given arguments to the associated ccov-run-${TARGET_NAME} executable call (ccov-* $<ARGS>)
245+
# EXCLUDE <PATTERNS> - Excludes files of the patterns provided from coverage. Added to any LLVM/LCOV specified excludes. (These do not copy to the 'all' targets)
246+
#
247+
# Optional Parameters effective with the clang/LLVM backend:
248+
# LLVM_EXCLUDE <PATTERNS> - Excludes files that match the provided patterns, LLVM excludes by regex patterns.
249+
# LLVM_PROFDATA_OPTIONS <OPTIONS> - Options are passed verbatim to the `llvm-profdata` call that merges/processes raw profile data. (.profraw -> .profdata)
250+
# LLVM_COV_SHOW_OPTIONS <OPTIONS> - Options are passed verbatim to the `llvm-cov show` call for the `ccov-show-${TARGET_NAME}` target.
251+
# LLVM_COV_REPORT_OPTIONS <OPTIONS> - Options are passed verbatim to the `llvm-cov report` call for the `ccov-report-${TARGET_NAME}` target.
252+
# LLVM_COV_EXPORT_OPTIONS <OPTIONS> - Options are passed verbatim to the `llvm-cov export` call for the `ccov-export-${TARGET_NAME}` target.
253+
# LLVM_COV_HTML_OPTIONS <OPTIONS> - Options are passed verbatim to the `llvm-cov show -format="html"` call for the `ccov-html-${TARGET_NAME}`/`ccov-${TARGET_NAME}` targets.
254+
#
255+
# Optional Parameters effective with the GCC/lcov backend:
256+
# LCOV_EXCLUDE <PATTERNS> - Excludes files that match the provided patterns. LCOV exclude by glob patterns.
257+
# LCOV_OPTIONS <OPTIONS> - Options are passed verbatim to the `lcov` call when capturing/filtering capture data
258+
# GENHTML_OPTIONS <OPTIONS> - Options are passed verbatim to the `genhtml` call when generating the HTML report from lcov data for the `ccov-html-${TARGET_NAME}`/`ccov-${TARGET_NAME}` targets.
248259
# ~~~
249260
function(target_code_coverage TARGET_NAME)
250261
if(NOT CODE_COVERAGE)
@@ -254,8 +265,23 @@ function(target_code_coverage TARGET_NAME)
254265
# Argument parsing
255266
set(options AUTO ALL EXTERNAL PUBLIC INTERFACE PLAIN)
256267
set(single_value_keywords COVERAGE_TARGET_NAME)
257-
set(multi_value_keywords EXCLUDE LLVM_EXCLUDE LCOV_EXCLUDE OBJECTS PRE_ARGS
258-
ARGS)
268+
set(multi_value_keywords
269+
# common
270+
EXCLUDE
271+
OBJECTS
272+
PRE_ARGS
273+
ARGS
274+
# llvm
275+
LLVM_EXCLUDE
276+
LLVM_PROFDATA_OPTIONS
277+
LLVM_COV_SHOW_OPTIONS
278+
LLVM_COV_REPORT_OPTIONS
279+
LLVM_COV_EXPORT_OPTIONS
280+
LLVM_COV_HTML_OPTIONS
281+
# lcov
282+
LCOV_EXCLUDE
283+
LCOV_OPTIONS
284+
GENHTML_OPTIONS)
259285
cmake_parse_arguments(
260286
target_code_coverage "${options}" "${single_value_keywords}"
261287
"${multi_value_keywords}" ${ARGN})
@@ -394,7 +420,8 @@ function(target_code_coverage TARGET_NAME)
394420
add_custom_command(
395421
OUTPUT ${target_code_coverage_COVERAGE_TARGET_NAME}.profdata
396422
COMMAND
397-
${LLVM_PROFDATA_PATH} merge -sparse
423+
${LLVM_PROFDATA_PATH} merge
424+
${target_code_coverage_LLVM_PROFDATA_OPTIONS} -sparse
398425
${target_code_coverage_COVERAGE_TARGET_NAME}.profraw -o
399426
${target_code_coverage_COVERAGE_TARGET_NAME}.profdata
400427
DEPENDS ${target_code_coverage_COVERAGE_TARGET_NAME}.profraw)
@@ -419,6 +446,7 @@ function(target_code_coverage TARGET_NAME)
419446
${LLVM_COV_PATH} show $<TARGET_FILE:${TARGET_NAME}>
420447
-instr-profile=${target_code_coverage_COVERAGE_TARGET_NAME}.profdata
421448
-show-line-counts-or-regions ${LINKED_OBJECTS} ${EXCLUDE_REGEX}
449+
${target_code_coverage_LLVM_COV_SHOW_OPTIONS}
422450
DEPENDS ${target_code_coverage_COVERAGE_TARGET_NAME}.profdata)
423451

424452
# Print out a summary of the coverage information to the command line
@@ -428,6 +456,7 @@ function(target_code_coverage TARGET_NAME)
428456
${LLVM_COV_PATH} report $<TARGET_FILE:${TARGET_NAME}>
429457
-instr-profile=${target_code_coverage_COVERAGE_TARGET_NAME}.profdata
430458
${LINKED_OBJECTS} ${EXCLUDE_REGEX}
459+
${target_code_coverage_LLVM_COV_REPORT_OPTIONS}
431460
DEPENDS ${target_code_coverage_COVERAGE_TARGET_NAME}.profdata)
432461

433462
# Export coverage information so continuous integration tools (e.g.
@@ -437,7 +466,8 @@ function(target_code_coverage TARGET_NAME)
437466
COMMAND
438467
${LLVM_COV_PATH} export $<TARGET_FILE:${TARGET_NAME}>
439468
-instr-profile=${target_code_coverage_COVERAGE_TARGET_NAME}.profdata
440-
-format="text" ${LINKED_OBJECTS} ${EXCLUDE_REGEX} >
469+
-format="text" ${LINKED_OBJECTS} ${EXCLUDE_REGEX}
470+
${target_code_coverage_LLVM_COV_EXPORT_OPTIONS} >
441471
${CMAKE_COVERAGE_OUTPUT_DIRECTORY}/${target_code_coverage_COVERAGE_TARGET_NAME}.json
442472
DEPENDS ${target_code_coverage_COVERAGE_TARGET_NAME}.profdata)
443473

@@ -450,6 +480,7 @@ function(target_code_coverage TARGET_NAME)
450480
-show-line-counts-or-regions
451481
-output-dir=${CMAKE_COVERAGE_OUTPUT_DIRECTORY}/${target_code_coverage_COVERAGE_TARGET_NAME}
452482
-format="html" ${LINKED_OBJECTS} ${EXCLUDE_REGEX}
483+
${target_code_coverage_LLVM_COV_HTML_OPTIONS}
453484
DEPENDS ${target_code_coverage_COVERAGE_TARGET_NAME}.profdata)
454485

455486
# Generates HTML output of the coverage information for perusal
@@ -547,7 +578,7 @@ function(target_code_coverage TARGET_NAME)
547578
COMMAND
548579
${LCOV_PATH} --directory ${CMAKE_BINARY_DIR} --base-directory
549580
${CMAKE_SOURCE_DIR} --capture ${EXTERNAL_OPTION} --output-file
550-
${COVERAGE_INFO}
581+
${COVERAGE_INFO} ${target_code_coverage_LCOV_OPTIONS}
551582
COMMAND ${EXCLUDE_COMMAND}
552583
DEPENDS ${target_code_coverage_COVERAGE_TARGET_NAME}.ccov-run)
553584
add_custom_target(
@@ -558,7 +589,7 @@ function(target_code_coverage TARGET_NAME)
558589
add_custom_target(
559590
ccov-html-${target_code_coverage_COVERAGE_TARGET_NAME}
560591
COMMAND
561-
${GENHTML_PATH} -o
592+
${GENHTML_PATH} ${target_code_coverage_GENHTML_OPTIONS} -o
562593
${CMAKE_COVERAGE_OUTPUT_DIRECTORY}/${target_code_coverage_COVERAGE_TARGET_NAME}
563594
${COVERAGE_INFO}
564595
COMMAND
@@ -639,9 +670,9 @@ function(add_code_coverage)
639670
endif()
640671
endfunction()
641672

642-
# Adds several 'ccov-all-*' type targets that operates on all targets added via
643-
# `target_code_coverage` with the `ALL` parameter, but merges all the coverage
644-
# data into a single large report instead of numerous smaller reports.
673+
# Adds several 'ccov-all-*' targets that operates runs all targets added via
674+
# `target_code_coverage` with the `ALL` parameter and merges all the coverage
675+
# data into a single large report instead of numerous smaller ones.
645676
# ~~~
646677
# Targets added:
647678
# ccov-all-run : Re-runs all tagged executables, collecting fresh coverage data
@@ -657,16 +688,38 @@ endfunction()
657688
#
658689
# Optional Parameters:
659690
# EXCLUDE <PATTERNS> - Excludes files of the patterns provided from coverage. Note that GCC/lcov excludes by glob pattern, and clang/LLVM excludes via regex!
691+
#
692+
# Optional Parameters effective with the clang/LLVM backend:
660693
# LLVM_EXCLUDE <PATTERNS> - Excludes files that match the provided patterns, LLVM excludes by regex patterns.
694+
# LLVM_PROFDATA_OPTIONS <OPTIONS> - Options are passed verbatim to the `llvm-profdata` call that merges/processes raw profile data. (.profraw -> .profdata)
695+
# LLVM_COV_REPORT_OPTIONS <OPTIONS> - Options are passed verbatim to the `llvm-cov report` call for the `ccov-report-all` target.
696+
# LLVM_COV_EXPORT_OPTIONS <OPTIONS> - Options are passed verbatim to the `llvm-cov export` call for the `ccov-export-all` target.
697+
# LLVM_COV_HTML_OPTIONS <OPTIONS> - Options are passed verbatim to the `llvm-cov show -format="html"` call for the `ccov-html-all`/`ccov-all` targets.
698+
#
699+
# Optional Parameters effective with the GCC/lcov backend:
661700
# LCOV_EXCLUDE <PATTERNS> - Excludes files that match the provided patterns. LCOV exclude by glob patterns.
701+
# LCOV_OPTIONS <OPTIONS> - Options are passed verbatim to the `lcov` call when capturing/filtering capture data
702+
# GENHTML_OPTIONS <OPTIONS> - Options are passed verbatim to the `genhtml` call when generating the HTML report from lcov data for the `ccov-html-all`/`ccov-all` targets.
662703
# ~~~
663704
function(add_code_coverage_all_targets)
664705
if(NOT CODE_COVERAGE)
665706
return()
666707
endif()
667708

668709
# Argument parsing
669-
set(multi_value_keywords EXCLUDE LLVM_EXCLUDE LCOV_EXCLUDE)
710+
set(multi_value_keywords
711+
# common
712+
EXCLUDE
713+
# llvm
714+
LLVM_EXCLUDE
715+
LLVM_PROFDATA_OPTIONS
716+
LLVM_COV_REPORT_OPTIONS
717+
LLVM_COV_EXPORT_OPTIONS
718+
LLVM_COV_HTML_OPTIONS
719+
# lcov
720+
LCOV_EXCLUDE
721+
LCOV_OPTIONS
722+
GENHTML_OPTIONS)
670723
cmake_parse_arguments(add_code_coverage_all_targets "" ""
671724
"${multi_value_keywords}" ${ARGN})
672725

@@ -691,8 +744,8 @@ function(add_code_coverage_all_targets)
691744
COMMAND
692745
powershell -Command $$FILELIST = Get-Content
693746
${CMAKE_COVERAGE_DATA_DIRECTORY}/all-profraw.list \; llvm-profdata.exe
694-
merge -o ${CMAKE_COVERAGE_DATA_DIRECTORY}/ccov-all.profdata -sparse
695-
$$FILELIST
747+
merge ${add_code_coverage_all_targets_LLVM_PROFDATA_OPTIONS} -o
748+
${CMAKE_COVERAGE_DATA_DIRECTORY}/ccov-all.profdata -sparse $$FILELIST
696749
DEPENDS ccov-all-ran)
697750
else()
698751
add_custom_command(
@@ -733,6 +786,7 @@ function(add_code_coverage_all_targets)
733786
report $$FILELIST
734787
-instr-profile=${CMAKE_COVERAGE_DATA_DIRECTORY}/ccov-all.profdata
735788
${EXCLUDE_REGEX}
789+
${add_code_coverage_all_targets_LLVM_COV_REPORT_OPTIONS}
736790
DEPENDS ${CMAKE_COVERAGE_DATA_DIRECTORY}/ccov-all.profdata)
737791
else()
738792
add_custom_target(
@@ -742,6 +796,7 @@ function(add_code_coverage_all_targets)
742796
${CMAKE_COVERAGE_DATA_DIRECTORY}/all-objects.list`
743797
-instr-profile=${CMAKE_COVERAGE_DATA_DIRECTORY}/ccov-all.profdata
744798
${EXCLUDE_REGEX}
799+
${add_code_coverage_all_targets_LLVM_COV_REPORT_OPTIONS}
745800
DEPENDS ${CMAKE_COVERAGE_DATA_DIRECTORY}/ccov-all.profdata)
746801
endif()
747802

@@ -755,7 +810,8 @@ function(add_code_coverage_all_targets)
755810
${CMAKE_COVERAGE_DATA_DIRECTORY}/all-objects.list \; llvm-cov.exe
756811
export $$FILELIST
757812
-instr-profile=${CMAKE_COVERAGE_DATA_DIRECTORY}/ccov-all.profdata
758-
-format="text" ${EXCLUDE_REGEX} >
813+
-format="text" ${EXCLUDE_REGEX}
814+
${add_code_coverage_all_targets_LLVM_COV_EXPORT_OPTIONS} >
759815
${CMAKE_COVERAGE_OUTPUT_DIRECTORY}/coverage.json
760816
DEPENDS ${CMAKE_COVERAGE_DATA_DIRECTORY}/ccov-all.profdata)
761817
else()
@@ -765,7 +821,8 @@ function(add_code_coverage_all_targets)
765821
${LLVM_COV_PATH} export `cat
766822
${CMAKE_COVERAGE_DATA_DIRECTORY}/all-objects.list`
767823
-instr-profile=${CMAKE_COVERAGE_DATA_DIRECTORY}/ccov-all.profdata
768-
-format="text" ${EXCLUDE_REGEX} >
824+
-format="text" ${EXCLUDE_REGEX}
825+
${add_code_coverage_all_targets_LLVM_COV_EXPORT_OPTIONS} >
769826
${CMAKE_COVERAGE_OUTPUT_DIRECTORY}/coverage.json
770827
DEPENDS ${CMAKE_COVERAGE_DATA_DIRECTORY}/ccov-all.profdata)
771828
endif()
@@ -782,6 +839,7 @@ function(add_code_coverage_all_targets)
782839
-show-line-counts-or-regions
783840
-output-dir=${CMAKE_COVERAGE_OUTPUT_DIRECTORY}/all-merged
784841
-format="html" ${EXCLUDE_REGEX}
842+
${add_code_coverage_all_targets_LLVM_COV_HTML_OPTIONS}
785843
DEPENDS ${CMAKE_COVERAGE_DATA_DIRECTORY}/ccov-all.profdata)
786844
else()
787845
add_custom_target(
@@ -793,6 +851,7 @@ function(add_code_coverage_all_targets)
793851
-show-line-counts-or-regions
794852
-output-dir=${CMAKE_COVERAGE_OUTPUT_DIRECTORY}/all-merged
795853
-format="html" ${EXCLUDE_REGEX}
854+
${add_code_coverage_all_targets_LLVM_COV_HTML_OPTIONS}
796855
DEPENDS ${CMAKE_COVERAGE_DATA_DIRECTORY}/ccov-all.profdata)
797856
endif()
798857

@@ -834,8 +893,9 @@ function(add_code_coverage_all_targets)
834893

835894
add_custom_command(
836895
OUTPUT ${COVERAGE_INFO}
837-
COMMAND ${LCOV_PATH} --ignore-errors unused --directory
838-
${CMAKE_BINARY_DIR} --capture --output-file ${COVERAGE_INFO}
896+
COMMAND
897+
${LCOV_PATH} ${add_code_coverage_all_targets_LCOV_OPTIONS} --directory
898+
${CMAKE_BINARY_DIR} --capture --output-file ${COVERAGE_INFO}
839899
COMMAND ${EXCLUDE_COMMAND}
840900
COMMAND ${CMAKE_COMMAND} -E echo
841901
"Generated coverage .info file at ${COVERAGE_INFO}"
@@ -845,18 +905,17 @@ function(add_code_coverage_all_targets)
845905
# Only generates HTML output of all targets for perusal
846906
add_custom_target(
847907
ccov-all-html
848-
COMMAND ${GENHTML_PATH} -o ${CMAKE_COVERAGE_OUTPUT_DIRECTORY}/all-merged
849-
${COVERAGE_INFO} -p ${CMAKE_SOURCE_DIR}
908+
COMMAND
909+
${GENHTML_PATH} ${add_code_coverage_all_targets_GENHTML_OPTIONS} -o
910+
${CMAKE_COVERAGE_OUTPUT_DIRECTORY}/all-merged ${COVERAGE_INFO} -p
911+
${CMAKE_SOURCE_DIR}
850912
COMMAND
851913
${CMAKE_COMMAND} -E echo
852914
"Open ${CMAKE_COVERAGE_OUTPUT_DIRECTORY}/all-merged/index.html in your browser to view the coverage report."
853915
DEPENDS ${COVERAGE_INFO})
854916

855917
# Generates HTML output of all targets for perusal
856-
add_custom_target(
857-
ccov-all
858-
COMMAND
859-
DEPENDS ccov-all-html)
918+
add_custom_target(ccov-all DEPENDS ccov-all-html)
860919

861920
endif()
862921
endfunction()

0 commit comments

Comments
 (0)