cmake: Add new code coverage targets. (#6796)
authorMathias Preiner <mathias.preiner@gmail.com>
Thu, 24 Jun 2021 21:04:13 +0000 (14:04 -0700)
committerGitHub <noreply@github.com>
Thu, 24 Jun 2021 21:04:13 +0000 (14:04 -0700)
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
cmake/CodeCoverage.cmake

index 3780b0b5bfa72eba672cc82533b6218598fd4497..f5f57104628c7a9cd522562a086f029b873ef1ed 100644 (file)
@@ -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)
index 932c3d0660d9973fce6a1be13107003adf2d2d13..035c2aa24f7ca80e791b5009b9e26c69e4321a2b 100644 (file)
@@ -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