Skip to content

Commit 8ee19e0

Browse files
committed
Rework GCC/LCOV-based code coverage
Change the chaining from just named targets to now use a mix of custom commands chained by generated files.
1 parent dd115d4 commit 8ee19e0

File tree

1 file changed

+31
-24
lines changed

1 file changed

+31
-24
lines changed

code-coverage.cmake

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -471,14 +471,6 @@ function(target_code_coverage TARGET_NAME)
471471
"${CMAKE_COVERAGE_DATA_DIRECTORY}/${target_code_coverage_COVERAGE_TARGET_NAME}.info"
472472
)
473473

474-
# Run the executable, generating coverage information
475-
add_custom_target(
476-
ccov-run-${target_code_coverage_COVERAGE_TARGET_NAME}
477-
COMMAND
478-
${CMAKE_CROSSCOMPILING_EMULATOR} ${target_code_coverage_PRE_ARGS}
479-
$<TARGET_FILE:${TARGET_NAME}> ${target_code_coverage_ARGS}
480-
DEPENDS ${TARGET_NAME})
481-
482474
# Generate exclusion string for use
483475
foreach(EXCLUDE_ITEM ${target_code_coverage_EXCLUDE})
484476
set(EXCLUDE_REGEX ${EXCLUDE_REGEX} --remove ${COVERAGE_INFO}
@@ -509,14 +501,32 @@ function(target_code_coverage TARGET_NAME)
509501
COMMAND ${LCOV_PATH} --directory ${CMAKE_BINARY_DIR} --zerocounters)
510502
endif()
511503

504+
# Run the executable, generating coverage information
505+
add_custom_command(
506+
OUTPUT ${target_code_coverage_COVERAGE_TARGET_NAME}.ccov-run
507+
COMMAND
508+
${CMAKE_CROSSCOMPILING_EMULATOR} ${target_code_coverage_PRE_ARGS}
509+
$<TARGET_FILE:${TARGET_NAME}> ${target_code_coverage_ARGS}
510+
COMMAND # add a dummy file to use as a dependency to indicate the target
511+
# has been run and data collected
512+
${CMAKE_COMMAND} -E touch
513+
${target_code_coverage_COVERAGE_TARGET_NAME}.ccov-run
514+
DEPENDS ${TARGET_NAME})
512515
add_custom_target(
513-
ccov-capture-${target_code_coverage_COVERAGE_TARGET_NAME}
516+
ccov-run-${target_code_coverage_COVERAGE_TARGET_NAME}
517+
DEPENDS ${target_code_coverage_COVERAGE_TARGET_NAME}.ccov-run)
518+
519+
add_custom_command(
520+
OUTPUT ${COVERAGE_INFO}
514521
COMMAND
515522
${LCOV_PATH} --directory ${CMAKE_BINARY_DIR} --base-directory
516523
${CMAKE_SOURCE_DIR} --capture ${EXTERNAL_OPTION} --output-file
517524
${COVERAGE_INFO}
518525
COMMAND ${EXCLUDE_COMMAND}
519-
DEPENDS)
526+
DEPENDS ${target_code_coverage_COVERAGE_TARGET_NAME}.ccov-run)
527+
add_custom_target(
528+
ccov-capture-${target_code_coverage_COVERAGE_TARGET_NAME}
529+
DEPENDS ${COVERAGE_INFO})
520530

521531
# Only generates HTML output of the coverage information for perusal
522532
add_custom_target(
@@ -528,15 +538,12 @@ function(target_code_coverage TARGET_NAME)
528538
COMMAND
529539
${CMAKE_COMMAND} -E echo
530540
"Open ${CMAKE_COVERAGE_OUTPUT_DIRECTORY}/${target_code_coverage_COVERAGE_TARGET_NAME}/index.html in your browser to view the coverage report."
531-
DEPENDS ccov-capture-${target_code_coverage_COVERAGE_TARGET_NAME})
541+
DEPENDS ${COVERAGE_INFO})
532542

533543
# Generates HTML output of the coverage information for perusal
534544
add_custom_target(
535545
ccov-${target_code_coverage_COVERAGE_TARGET_NAME}
536-
COMMAND
537-
DEPENDS ccov-clean-${target_code_coverage_COVERAGE_TARGET_NAME}
538-
ccov-run-${target_code_coverage_COVERAGE_TARGET_NAME}
539-
ccov-html-${target_code_coverage_COVERAGE_TARGET_NAME})
546+
DEPENDS ccov-html-${target_code_coverage_COVERAGE_TARGET_NAME})
540547
endif()
541548

542549
# AUTO
@@ -737,8 +744,7 @@ function(add_code_coverage_all_targets)
737744
"GNU")
738745
set(COVERAGE_INFO "${CMAKE_COVERAGE_OUTPUT_DIRECTORY}/ccov-all.info")
739746

740-
# Nothing required for gcov
741-
add_custom_target(ccov-all-processing COMMAND ;)
747+
add_custom_target(ccov-all-run)
742748

743749
# Exclusion regex string creation
744750
set(EXCLUDE_REGEX)
@@ -769,12 +775,13 @@ function(add_code_coverage_all_targets)
769775
DEPENDS ccov-all-processing)
770776
endif()
771777

772-
add_custom_target(
773-
ccov-all-capture
774-
COMMAND ${LCOV_PATH} --directory ${CMAKE_BINARY_DIR} --capture
775-
--output-file ${COVERAGE_INFO}
778+
add_custom_command(
779+
OUTPUT ${COVERAGE_INFO}
780+
COMMAND ${LCOV_PATH} --ignore-errors unused --directory
781+
${CMAKE_BINARY_DIR} --capture --output-file ${COVERAGE_INFO}
776782
COMMAND ${EXCLUDE_COMMAND}
777-
DEPENDS)
783+
DEPENDS ccov-all-run)
784+
add_custom_target(ccov-all-capture DEPENDS ${COVERAGE_INFO})
778785

779786
# Only generates HTML output of all targets for perusal
780787
add_custom_target(
@@ -784,13 +791,13 @@ function(add_code_coverage_all_targets)
784791
COMMAND
785792
${CMAKE_COMMAND} -E echo
786793
"Open ${CMAKE_COVERAGE_OUTPUT_DIRECTORY}/all-merged/index.html in your browser to view the coverage report."
787-
DEPENDS ccov-all-capture)
794+
DEPENDS ${COVERAGE_INFO})
788795

789796
# Generates HTML output of all targets for perusal
790797
add_custom_target(
791798
ccov-all
792799
COMMAND
793-
DEPENDS ccov-all-processing ccov-all-capture ccov-all-html)
800+
DEPENDS ccov-all-html)
794801

795802
endif()
796803
endfunction()

0 commit comments

Comments
 (0)