@@ -36,25 +36,68 @@ get_all_catch_test_targets(all_targets ${PROJECT_SOURCE_DIR})
3636
3737# Create a custom command for each test target to run it
3838# Make sure that coverage data is written with paths relative to the source directory
39- unset (reports)
39+ unset (report_outputs)
40+ unset (coverage_reports)
4041foreach (target ${all_targets} )
4142
42- set (sonarqube_report_file "${PROJECT_BINARY_DIR} /reports/tests/${target} .sonarqube.xml" )
43+ unset (command_prefix)
44+ if (ENABLE_COVERAGE AND CMAKE_CXX_COMPILER_ID STREQUAL "Clang" )
45+ # Extra output files for the profile data
46+ set (raw_coverage "${PROJECT_BINARY_DIR} /reports/tests/${target} .profraw" )
47+ set (indexed_coverage "${PROJECT_BINARY_DIR} /reports/tests/${target} .profdata" )
48+ set (coverage_report "${PROJECT_BINARY_DIR} /reports/tests/${target} .txt" )
49+ set (command_prefix "cmake" "-E" "env" "LLVM_PROFILE_FILE=${raw_coverage} " )
50+ list (APPEND coverage_reports ${coverage_report} )
51+ endif ()
52+
4353 set (junit_report_file "${PROJECT_BINARY_DIR} /reports/tests/${target} .junit.xml" )
44- list (APPEND reports ${sonarqube_report_file} ${junit_report_file} )
54+ list (APPEND report_outputs ${junit_report_file} )
4555 add_custom_command (
46- OUTPUT ${sonarqube_report_file} ${junit_report_file}
47- COMMAND $<TARGET_FILE :${target} > --reporter console --reporter SonarQube::out=${sonarqube_report_file} --reporter
48- JUnit::out=${junit_report_file}
56+ OUTPUT ${junit_report_file} ${raw_coverage}
57+ COMMAND ${command_prefix} $<TARGET_FILE :${target} > --reporter console --reporter JUnit::out=${junit_report_file}
4958 WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
59+ DEPENDS ${target}
5060 USES_TERMINAL
5161 COMMENT "Running test ${target} "
5262 )
63+
64+ if (ENABLE_COVERAGE AND CMAKE_CXX_COMPILER_ID STREQUAL "Clang" )
65+ add_custom_command (
66+ OUTPUT ${indexed_coverage}
67+ COMMAND llvm-profdata merge -sparse ${raw_coverage} -o ${indexed_coverage}
68+ DEPENDS ${raw_coverage}
69+ )
70+
71+ add_custom_command (
72+ OUTPUT ${coverage_report}
73+ COMMAND llvm-cov show --show-branches=count -Xdemangler c++filt
74+ -instr-profile=${CMAKE_CURRENT_BINARY_DIR}/coverage.profdata $<TARGET_FILE :${target} > > ${coverage_report}
75+ DEPENDS ${indexed_coverage}
76+ )
77+ endif ()
78+
5379endforeach ()
5480
5581# Create a custom target that depends on all test targets
5682add_custom_target (
5783 run_all_tests
58- DEPENDS ${reports }
84+ DEPENDS ${report_outputs} ${all_targets }
5985 COMMENT "Running all Catch2 tests"
6086)
87+
88+ # Concatenate all the coverage reports together
89+ if (ENABLE_COVERAGE AND CMAKE_CXX_COMPILER_ID STREQUAL "Clang" )
90+
91+ add_custom_command (
92+ OUTPUT ${PROJECT_BINARY_DIR} /reports/coverage.txt
93+ COMMAND cat ${coverage_reports} > ${PROJECT_BINARY_DIR} /reports/coverage.txt
94+ DEPENDS ${coverage_reports}
95+ )
96+
97+ add_custom_target (
98+ coverage
99+ DEPENDS ${PROJECT_BINARY_DIR} /reports/coverage.txt
100+ COMMENT "Running all Catch2 tests with coverage"
101+ )
102+
103+ endif ()
0 commit comments