From e37d1834655dfd1decf8b306d8e56302319c1cf4 Mon Sep 17 00:00:00 2001 From: Konrad Kleine Date: Fri, 13 Dec 2013 09:13:01 +0100 Subject: [PATCH] Moved local code coverage code separate script --- CMakeLists.txt | 51 +----------------- build/cmake/LibarchiveCodeCoverage.cmake | 68 ++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 50 deletions(-) create mode 100644 build/cmake/LibarchiveCodeCoverage.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index c6763469a..fd2576763 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -156,56 +156,7 @@ SET(POSIX_REGEX_LIB "AUTO" CACHE STRING "Choose what library should provide POSI SET(ENABLE_SAFESEH "AUTO" CACHE STRING "Enable use of /SAFESEH linker flag (MSVC only)") SET(WINDOWS_VERSION "" CACHE STRING "Set Windows version to use (Windows only)") -################################################################# -# Add build target for code coverage (GCC only) -################################################################# -IF(ENABLE_COVERAGE) - # Find programs we need - FIND_PROGRAM(LCOV_EXECUTABLE lcov DOC "Full path to lcov executable") - FIND_PROGRAM(GENHTML_EXECUTABLE genhtml DOC "Full path to genhtml executable") - MARK_AS_ADVANCED(LCOV_EXECUTABLE GENHTML_EXECUTABLE) - - IF(NOT CMAKE_COMPILER_IS_GNUCC) - MESSAGE(FATAL_ERROR "Coverage can only be built on GCC") - ELSEIF(NOT CMAKE_BUILD_TYPE STREQUAL "Debug") - MESSAGE(FATAL_ERROR "Coverage can only be built in Debug mode") - ELSEIF(NOT LCOV_EXECUTABLE) - MESSAGE(FATAL_ERROR "lcov executable not found") - ELSEIF(NOT GENHTML_EXECUTABLE) - MESSAGE(FATAL_ERROR "genhtml executable not found") - ENDIF(NOT CMAKE_COMPILER_IS_GNUCC) - - # Enable testing if not already done - SET(ENABLE_TEST ON) - - ################################################################# - # Set special compiler and linker flags for test coverage - ################################################################# - # 0. Enable debug: -g - SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g") - # 1. Disable optimizations: -O0 - SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0") - # 2. Enable all kind of warnings: - SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -W") - # 3. Enable special coverage flag (HINT: --coverage is a synonym for -fprofile-arcs -ftest-coverage) - SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --coverage") - SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} --coverage") - SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage") - ################################################################# - - ADD_CUSTOM_TARGET(coverage - COMMAND ${CMAKE_COMMAND} -E echo "Beginning test coverage. Output is written to coverage.log." - COMMAND ${CMAKE_COMMAND} -E echo "COVERAGE-STEP-1/5: Reset all execution counts to zero" - COMMAND ${LCOV_EXECUTABLE} --directory . --zerocounters > coverage.log 2>&1 - COMMAND ${CMAKE_COMMAND} -E echo "COVERAGE-STEP-2/5: Run testrunner" - COMMAND ${CMAKE_CTEST_COMMAND} >> coverage.log 2>&1 - COMMAND ${CMAKE_COMMAND} -E echo "COVERAGE-STEP-3/5: Collect coverage data" - COMMAND ${LCOV_EXECUTABLE} --capture --directory . --output-file "./coverage.info" >> coverage.log 2>&1 - COMMAND ${CMAKE_COMMAND} -E echo "COVERAGE-STEP-4/5: Generate HTML from coverage data" - COMMAND ${GENHTML_EXECUTABLE} "coverage.info" --title="libarchive-${LIBARCHIVE_VERSION_STRING}" --show-details --legend --output-directory "./coverage" >> coverage.log 2>&1 - COMMAND ${CMAKE_COMMAND} -E echo "COVERAGE-STEP-5/5: Open test coverage HTML output in browser: ./coverage/index.html" - COMMENT "Runs testrunner and generates coverage output (formats: .info and .html)") -ENDIF(ENABLE_COVERAGE) +include(LibarchiveCodeCoverage) IF(ENABLE_TEST) ENABLE_TESTING() diff --git a/build/cmake/LibarchiveCodeCoverage.cmake b/build/cmake/LibarchiveCodeCoverage.cmake new file mode 100644 index 000000000..ea7bb215a --- /dev/null +++ b/build/cmake/LibarchiveCodeCoverage.cmake @@ -0,0 +1,68 @@ +################################################################# +# Adds a build target called "coverage" for code coverage. +# +# This compiles the code using special GCC flags, run the tests, +# and then generates a nice HTML output. This new "coverage" make +# target will only be available if you build using GCC in Debug +# mode. If any of the required programs (lcov and genhtml) were +# not found, a FATAL_ERROR message is printed. +# +# If not already done, this code will set ENABLE_TEST to ON. +# +# To build the code coverage and open it in your browser do this: +# +# mkdir debug +# cd debug +# cmake -DCMAKE_BUILD_TYPE=Debug -DENABLE_COVERAGE=ON .. +# make -j4 +# make coverage +# xdg-open coverage/index.html +################################################################# +IF(ENABLE_COVERAGE) + # Find programs we need + FIND_PROGRAM(LCOV_EXECUTABLE lcov DOC "Full path to lcov executable") + FIND_PROGRAM(GENHTML_EXECUTABLE genhtml DOC "Full path to genhtml executable") + MARK_AS_ADVANCED(LCOV_EXECUTABLE GENHTML_EXECUTABLE) + + # Check, compiler, build types and programs are available + IF(NOT CMAKE_COMPILER_IS_GNUCC) + MESSAGE(FATAL_ERROR "Coverage can only be built on GCC") + ELSEIF(NOT CMAKE_BUILD_TYPE STREQUAL "Debug") + MESSAGE(FATAL_ERROR "Coverage can only be built in Debug mode") + ELSEIF(NOT LCOV_EXECUTABLE) + MESSAGE(FATAL_ERROR "lcov executable not found") + ELSEIF(NOT GENHTML_EXECUTABLE) + MESSAGE(FATAL_ERROR "genhtml executable not found") + ENDIF(NOT CMAKE_COMPILER_IS_GNUCC) + + # Enable testing if not already done + SET(ENABLE_TEST ON) + + ################################################################# + # Set special compiler and linker flags for test coverage + ################################################################# + # 0. Enable debug: -g + SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g") + # 1. Disable optimizations: -O0 + SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0") + # 2. Enable all kind of warnings: + SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -W") + # 3. Enable special coverage flag (HINT: --coverage is a synonym for -fprofile-arcs -ftest-coverage) + SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} --coverage") + SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} --coverage") + SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage") + ################################################################# + + ADD_CUSTOM_TARGET(coverage + COMMAND ${CMAKE_COMMAND} -E echo "Beginning test coverage. Output is written to coverage.log." + COMMAND ${CMAKE_COMMAND} -E echo "COVERAGE-STEP-1/5: Reset all execution counts to zero" + COMMAND ${LCOV_EXECUTABLE} --directory . --zerocounters > coverage.log 2>&1 + COMMAND ${CMAKE_COMMAND} -E echo "COVERAGE-STEP-2/5: Run testrunner" + COMMAND ${CMAKE_CTEST_COMMAND} >> coverage.log 2>&1 + COMMAND ${CMAKE_COMMAND} -E echo "COVERAGE-STEP-3/5: Collect coverage data" + COMMAND ${LCOV_EXECUTABLE} --capture --directory . --output-file "./coverage.info" >> coverage.log 2>&1 + COMMAND ${CMAKE_COMMAND} -E echo "COVERAGE-STEP-4/5: Generate HTML from coverage data" + COMMAND ${GENHTML_EXECUTABLE} "coverage.info" --title="libarchive-${LIBARCHIVE_VERSION_STRING}" --show-details --legend --output-directory "./coverage" >> coverage.log 2>&1 + COMMAND ${CMAKE_COMMAND} -E echo "COVERAGE-STEP-5/5: Open test coverage HTML output in browser: xdg-open ./coverage/index.html" + COMMENT "Runs testrunner and generates coverage output (formats: .info and .html)") +ENDIF(ENABLE_COVERAGE) -- 2.47.2