From a0ed9d5b519b636ed0351db13bb1c3f7469d4e94 Mon Sep 17 00:00:00 2001 From: Mathias Preiner Date: Thu, 24 Jun 2021 14:04:13 -0700 Subject: [PATCH] cmake: Add new code coverage targets. (#6796) This commit adds the following new code coverage targets: - coverage-reset: Resets the code coverage counters - coverage: Generates code coverage report for all cvc5 executions since the last coverage-reset - coverage-test: This was previously the coverage target that runs the tests and generates the coverage report (as used for nightlies). By using `make coverage-reset` and `make coverage` it is now possible to generate coverage reports for arbitrary executions of cvc5. --- CMakeLists.txt | 12 ++++++++- cmake/CodeCoverage.cmake | 57 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3780b0b5b..f5f571046 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -365,12 +365,22 @@ if(ENABLE_COVERAGE) # prevent this we always return with exit code 0 after the ctest command has # finished. setup_target_for_coverage_gcovr_html( - NAME coverage + NAME coverage-test EXECUTABLE ctest -j${CTEST_NTHREADS} -LE "example" --output-on-failure $$ARGS || exit 0 DEPENDS build-tests) + + # Adds targets `coverage` and `coverage-reset` for manually generating + # coverage reports for specific executions. + # + # Target coverage-reset resets all the coverage counters to zero, while + # target coverage will generate a coverage report for all executions since + # the last coverage-reset. + setup_target_for_coverage_lcov_no_executable( + NAME coverage + DEPENDENCIES cvc5-bin) endif() if(ENABLE_DEBUG_CONTEXT_MM) diff --git a/cmake/CodeCoverage.cmake b/cmake/CodeCoverage.cmake index 932c3d066..035c2aa24 100644 --- a/cmake/CodeCoverage.cmake +++ b/cmake/CodeCoverage.cmake @@ -185,6 +185,63 @@ function(SETUP_TARGET_FOR_COVERAGE_LCOV) endfunction() # SETUP_TARGET_FOR_COVERAGE_LCOV +function(SETUP_TARGET_FOR_COVERAGE_LCOV_NO_EXECUTABLE) + + set(options NONE) + set(oneValueArgs NAME) + cmake_parse_arguments(Coverage "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) + + if(NOT LCOV_PATH) + message(FATAL_ERROR "lcov not found! Aborting...") + endif() # NOT LCOV_PATH + + if(NOT GENHTML_PATH) + message(FATAL_ERROR "genhtml not found! Aborting...") + endif() # NOT GENHTML_PATH + + set(DIRECTORIES -d .) + foreach(LPATH ${COVERAGE_LCOV_PATHS}) + list(APPEND DIRECTORIES "-d") + list(APPEND DIRECTORIES "${LPATH}") + endforeach() + + + add_custom_target(${Coverage_NAME}-reset + # Cleanup lcov + COMMAND ${LCOV_PATH} ${DIRECTORIES} --zerocounters + # Create baseline to make sure untouched files show up in the report + COMMAND ${LCOV_PATH} -c -i ${DIRECTORIES} -o ${Coverage_NAME}.base + COMMENT "Resetting code coverage counters to zero." + ) + + # Setup target + add_custom_target(${Coverage_NAME} + # Capturing lcov counters and generating report + COMMAND ${LCOV_PATH} ${DIRECTORIES} --capture --output-file ${Coverage_NAME}.info + # add baseline counters + COMMAND ${LCOV_PATH} -a ${Coverage_NAME}.base -a ${Coverage_NAME}.info --output-file ${Coverage_NAME}.total + COMMAND ${LCOV_PATH} --remove ${Coverage_NAME}.total ${COVERAGE_LCOV_EXCLUDES} --output-file ${PROJECT_BINARY_DIR}/${Coverage_NAME}.info.cleaned + COMMAND ${GENHTML_PATH} -o ${Coverage_NAME} ${PROJECT_BINARY_DIR}/${Coverage_NAME}.info.cleaned + + WORKING_DIRECTORY ${PROJECT_BINARY_DIR} + DEPENDS ${Coverage_DEPENDENCIES} + COMMENT "Processing code coverage counters and generating report." + ) + + # Show where to find the lcov info report + add_custom_command(TARGET ${Coverage_NAME} POST_BUILD + COMMAND ; + COMMENT "Lcov code coverage info report saved in ${Coverage_NAME}.info." + ) + + # Show info where to find the report + add_custom_command(TARGET ${Coverage_NAME} POST_BUILD + COMMAND ; + COMMENT "Open ./${Coverage_NAME}/index.html in your browser to view the coverage report." + ) + +endfunction() # SETUP_TARGET_FOR_COVERAGE_LCOV_NO_EXECUTABLE + # Defines a target for running and collection code coverage information # Builds dependencies, runs the given executable and outputs reports. # NOTE! The executable should always have a ZERO as exit code otherwise -- 2.30.2