Blame build/cmake/LibarchiveCodeCoverage.cmake

Packit Service 1d0348
#################################################################
Packit Service 1d0348
# Adds a build target called "coverage" for code coverage.
Packit Service 1d0348
#
Packit Service 1d0348
# This compiles the code using special GCC flags, run the tests,
Packit Service 1d0348
# and then generates a nice HTML output. This new "coverage" make
Packit Service 1d0348
# target will only be available if you build using GCC in Debug
Packit Service 1d0348
# mode. If any of the required programs (lcov and genhtml) were
Packit Service 1d0348
# not found, a FATAL_ERROR message is printed.
Packit Service 1d0348
#
Packit Service 1d0348
# If not already done, this code will set ENABLE_TEST to ON.
Packit Service 1d0348
#
Packit Service 1d0348
# To build the code coverage and open it in your browser do this:
Packit Service 1d0348
#
Packit Service 1d0348
#    mkdir debug
Packit Service 1d0348
#    cd debug
Packit Service 1d0348
#    cmake -DCMAKE_BUILD_TYPE=Debug -DENABLE_COVERAGE=ON ..
Packit Service 1d0348
#    make -j4
Packit Service 1d0348
#    make coverage
Packit Service 1d0348
#    xdg-open coverage/index.html
Packit Service 1d0348
#################################################################
Packit Service 1d0348
Packit Service 1d0348
# Find programs we need 
Packit Service 1d0348
FIND_PROGRAM(LCOV_EXECUTABLE lcov DOC "Full path to lcov executable")
Packit Service 1d0348
FIND_PROGRAM(GENHTML_EXECUTABLE genhtml DOC "Full path to genhtml executable")
Packit Service 1d0348
MARK_AS_ADVANCED(LCOV_EXECUTABLE GENHTML_EXECUTABLE)
Packit Service 1d0348
Packit Service 1d0348
# Check, compiler, build types and programs are available
Packit Service 1d0348
IF(NOT CMAKE_COMPILER_IS_GNUCC)
Packit Service 1d0348
MESSAGE(FATAL_ERROR "Coverage can only be built on GCC")
Packit Service 1d0348
ELSEIF(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
Packit Service 1d0348
MESSAGE(FATAL_ERROR "Coverage can only be built in Debug mode")
Packit Service 1d0348
ELSEIF(NOT LCOV_EXECUTABLE)
Packit Service 1d0348
MESSAGE(FATAL_ERROR "lcov executable not found")
Packit Service 1d0348
ELSEIF(NOT GENHTML_EXECUTABLE)
Packit Service 1d0348
MESSAGE(FATAL_ERROR "genhtml executable not found")
Packit Service 1d0348
ENDIF(NOT CMAKE_COMPILER_IS_GNUCC)
Packit Service 1d0348
Packit Service 1d0348
# Enable testing if not already done
Packit Service 1d0348
SET(ENABLE_TEST ON)
Packit Service 1d0348
Packit Service 1d0348
#################################################################
Packit Service 1d0348
# Set special compiler and linker flags for test coverage
Packit Service 1d0348
#################################################################
Packit Service 1d0348
# 0. Enable debug: -g
Packit Service 1d0348
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g")
Packit Service 1d0348
# 1. Disable optimizations: -O0
Packit Service 1d0348
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0")
Packit Service 1d0348
# 2. Enable all kind of warnings:
Packit Service 1d0348
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -W")
Packit Service 1d0348
# 3. Enable special coverage flag (HINT: --coverage is a synonym for -fprofile-arcs -ftest-coverage)
Packit Service 1d0348
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --coverage")
Packit Service 1d0348
SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} --coverage")
Packit Service 1d0348
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
Packit Service 1d0348
#################################################################
Packit Service 1d0348
Packit Service 1d0348
ADD_CUSTOM_TARGET(coverage
Packit Service 1d0348
COMMAND ${CMAKE_COMMAND} -E echo "Beginning test coverage. Output is written to coverage.log."
Packit Service 1d0348
COMMAND ${CMAKE_COMMAND} -E echo "COVERAGE-STEP-1/5: Reset all execution counts to zero"
Packit Service 1d0348
COMMAND ${LCOV_EXECUTABLE} --directory . --zerocounters > coverage.log 2>&1
Packit Service 1d0348
COMMAND ${CMAKE_COMMAND} -E echo "COVERAGE-STEP-2/5: Run testrunner"
Packit Service 1d0348
COMMAND ${CMAKE_CTEST_COMMAND} >> coverage.log 2>&1
Packit Service 1d0348
COMMAND ${CMAKE_COMMAND} -E echo "COVERAGE-STEP-3/5: Collect coverage data"
Packit Service 1d0348
COMMAND ${LCOV_EXECUTABLE} --capture --directory . --output-file "./coverage.info" >> coverage.log 2>&1
Packit Service 1d0348
COMMAND ${CMAKE_COMMAND} -E echo "COVERAGE-STEP-4/5: Generate HTML from coverage data"
Packit Service 1d0348
COMMAND ${GENHTML_EXECUTABLE} "coverage.info" --title="libarchive-${LIBARCHIVE_VERSION_STRING}" --show-details --legend --output-directory "./coverage"  >> coverage.log 2>&1
Packit Service 1d0348
COMMAND ${CMAKE_COMMAND} -E echo "COVERAGE-STEP-5/5: Open test coverage HTML output in browser: xdg-open ./coverage/index.html"
Packit Service 1d0348
COMMENT "Runs testrunner and generates coverage output (formats: .info and .html)")
Packit Service 1d0348