2018-09-29 08:55:33 -04:00
|
|
|
# Copyright (c) 2012 - 2017, Lars Bilke
|
2021-09-26 06:35:42 -04:00
|
|
|
# Copyright (c) 2021 KeePassXC Team
|
2016-11-04 00:47:04 -04:00
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
# are permitted provided that the following conditions are met:
|
|
|
|
#
|
|
|
|
# 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
# list of conditions and the following disclaimer.
|
|
|
|
#
|
|
|
|
# 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions and the following disclaimer in the documentation
|
|
|
|
# and/or other materials provided with the distribution.
|
|
|
|
#
|
|
|
|
# 3. Neither the name of the copyright holder nor the names of its contributors
|
|
|
|
# may be used to endorse or promote products derived from this software without
|
|
|
|
# specific prior written permission.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
|
|
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
2018-09-29 08:55:33 -04:00
|
|
|
include(CMakeParseArguments)
|
|
|
|
|
2016-11-04 00:47:04 -04:00
|
|
|
# Check prereqs
|
2021-09-26 06:35:42 -04:00
|
|
|
find_program(GCOV_PATH gcov)
|
|
|
|
find_program(LLVM_COV_PATH llvm-cov)
|
|
|
|
find_program(LLVM_PROFDATA_PATH llvm-profdata)
|
|
|
|
find_program(XCRUN_PATH xcrun)
|
|
|
|
find_program(GENHTML_PATH NAMES genhtml genhtml.perl genhtml.bat)
|
|
|
|
find_program(GCOVR_PATH gcovr PATHS ${CMAKE_SOURCE_DIR}/scripts/test)
|
|
|
|
|
|
|
|
set(COVERAGE_COMPILER_FLAGS "-g -O0" CACHE INTERNAL "")
|
|
|
|
if(CMAKE_COMPILER_IS_GNUCXX)
|
|
|
|
set(COVERAGE_COMPILER_FLAGS "${COVERAGE_COMPILER_FLAGS} --coverage -fprofile-arcs -ftest-coverage")
|
|
|
|
elseif(CMAKE_COMPILER_IS_CLANGXX)
|
|
|
|
set(COVERAGE_COMPILER_FLAGS "${COVERAGE_COMPILER_FLAGS} -fprofile-instr-generate -fcoverage-mapping")
|
2018-09-29 08:55:33 -04:00
|
|
|
endif()
|
|
|
|
|
2021-09-26 06:35:42 -04:00
|
|
|
set(CMAKE_COVERAGE_FORMAT
|
|
|
|
"html" "txt"
|
|
|
|
CACHE STRING "Coverage report output format.")
|
|
|
|
set_property(CACHE CMAKE_COVERAGE_FORMAT PROPERTY STRINGS "html" "txt")
|
2018-09-29 08:55:33 -04:00
|
|
|
|
|
|
|
set(CMAKE_CXX_FLAGS_COVERAGE
|
|
|
|
${COVERAGE_COMPILER_FLAGS}
|
2021-09-26 06:35:42 -04:00
|
|
|
CACHE STRING "Flags used by the C++ compiler during coverage builds.")
|
2018-09-29 08:55:33 -04:00
|
|
|
set(CMAKE_C_FLAGS_COVERAGE
|
|
|
|
${COVERAGE_COMPILER_FLAGS}
|
2021-09-26 06:35:42 -04:00
|
|
|
CACHE STRING "Flags used by the C compiler during coverage builds.")
|
2018-09-29 08:55:33 -04:00
|
|
|
set(CMAKE_EXE_LINKER_FLAGS_COVERAGE
|
2016-11-04 00:47:04 -04:00
|
|
|
""
|
2021-09-26 06:35:42 -04:00
|
|
|
CACHE STRING "Flags used for linking binaries during coverage builds.")
|
2018-09-29 08:55:33 -04:00
|
|
|
set(CMAKE_SHARED_LINKER_FLAGS_COVERAGE
|
2016-11-04 00:47:04 -04:00
|
|
|
""
|
2021-09-26 06:35:42 -04:00
|
|
|
CACHE STRING "Flags used by the shared libraries linker during coverage builds.")
|
2018-09-29 08:55:33 -04:00
|
|
|
mark_as_advanced(
|
2021-09-26 06:35:42 -04:00
|
|
|
CMAKE_COVERAGE_FORMAT
|
2016-11-04 00:47:04 -04:00
|
|
|
CMAKE_CXX_FLAGS_COVERAGE
|
|
|
|
CMAKE_C_FLAGS_COVERAGE
|
|
|
|
CMAKE_EXE_LINKER_FLAGS_COVERAGE
|
2021-09-26 06:35:42 -04:00
|
|
|
CMAKE_SHARED_LINKER_FLAGS_COVERAGE)
|
2016-11-04 00:47:04 -04:00
|
|
|
|
2019-04-20 12:12:00 -04:00
|
|
|
if(NOT CMAKE_BUILD_TYPE_LOWER STREQUAL "debug")
|
2018-09-29 08:55:33 -04:00
|
|
|
message(WARNING "Code coverage results with an optimised (non-Debug) build may be misleading")
|
|
|
|
endif() # NOT CMAKE_BUILD_TYPE STREQUAL "Debug"
|
|
|
|
|
2021-09-26 06:35:42 -04:00
|
|
|
if(CMAKE_COMPILER_IS_GNUCXX)
|
|
|
|
if(NOT GCOV_PATH)
|
|
|
|
message(FATAL_ERROR "gcov not found! Aborting...")
|
|
|
|
endif() # NOT GCOV_PATH
|
2018-09-29 08:55:33 -04:00
|
|
|
link_libraries(gcov)
|
|
|
|
endif()
|
|
|
|
|
|
|
|
|
|
|
|
# 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
|
|
|
|
# the coverage generation will not complete.
|
|
|
|
#
|
2021-09-26 06:35:42 -04:00
|
|
|
# SETUP_TARGET_FOR_COVERAGE_GCOVR(
|
2018-09-29 08:55:33 -04:00
|
|
|
# NAME ctest_coverage # New target name
|
|
|
|
# EXECUTABLE ctest -j ${PROCESSOR_COUNT} # Executable in PROJECT_BINARY_DIR
|
|
|
|
# DEPENDENCIES executable_target # Dependencies to build first
|
|
|
|
# )
|
2021-09-26 06:35:42 -04:00
|
|
|
function(SETUP_TARGET_FOR_COVERAGE_GCOVR)
|
2018-09-29 08:55:33 -04:00
|
|
|
|
|
|
|
set(options NONE)
|
|
|
|
set(oneValueArgs NAME)
|
|
|
|
set(multiValueArgs EXECUTABLE EXECUTABLE_ARGS DEPENDENCIES)
|
|
|
|
cmake_parse_arguments(Coverage "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
|
|
|
|
|
|
|
|
if(NOT GCOVR_PATH)
|
|
|
|
message(FATAL_ERROR "gcovr not found! Aborting...")
|
|
|
|
endif() # NOT GCOVR_PATH
|
|
|
|
|
|
|
|
# Combine excludes to several -e arguments
|
|
|
|
set(GCOVR_EXCLUDES "")
|
2021-09-26 06:35:42 -04:00
|
|
|
foreach(EXCLUDE ${COVERAGE_EXCLUDES})
|
2018-09-29 08:55:33 -04:00
|
|
|
list(APPEND GCOVR_EXCLUDES "-e")
|
|
|
|
list(APPEND GCOVR_EXCLUDES "${EXCLUDE}")
|
|
|
|
endforeach()
|
|
|
|
|
|
|
|
add_custom_target(${Coverage_NAME}
|
|
|
|
# Run tests
|
2021-09-26 06:35:42 -04:00
|
|
|
COMMAND $(MAKE)
|
|
|
|
COMMAND ${Coverage_EXECUTABLE}
|
2018-09-29 08:55:33 -04:00
|
|
|
|
2021-09-26 06:35:42 -04:00
|
|
|
# Create folder
|
|
|
|
COMMAND ${CMAKE_COMMAND} -E make_directory ${PROJECT_BINARY_DIR}/${Coverage_NAME}
|
2018-09-29 08:55:33 -04:00
|
|
|
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
|
|
|
|
DEPENDS ${Coverage_DEPENDENCIES}
|
|
|
|
)
|
|
|
|
|
2021-09-26 06:35:42 -04:00
|
|
|
if("html" IN_LIST CMAKE_COVERAGE_FORMAT)
|
|
|
|
add_custom_command(TARGET ${Coverage_NAME} POST_BUILD
|
|
|
|
# Running gcovr HTML
|
|
|
|
COMMAND ${GCOVR_PATH} --html --html-details
|
|
|
|
-r ${PROJECT_SOURCE_DIR} ${GCOVR_EXCLUDES}
|
|
|
|
--object-directory=${PROJECT_BINARY_DIR}
|
|
|
|
-o ${Coverage_NAME}-html/index.html
|
|
|
|
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
|
|
|
|
COMMENT "Running gcovr to produce HTML code coverage report ${Coverage_NAME}-html."
|
|
|
|
)
|
|
|
|
endif()
|
|
|
|
|
|
|
|
if("txt" IN_LIST CMAKE_COVERAGE_FORMAT)
|
|
|
|
add_custom_command(TARGET ${Coverage_NAME} POST_BUILD
|
|
|
|
# Running gcovr XML
|
|
|
|
COMMAND ${GCOVR_PATH}
|
|
|
|
-r ${PROJECT_SOURCE_DIR} ${GCOVR_EXCLUDES}
|
|
|
|
--object-directory=${PROJECT_BINARY_DIR}
|
|
|
|
-o ${Coverage_NAME}.txt
|
|
|
|
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
|
|
|
|
COMMENT "Running gcovr to produce TXT code coverage report ${Coverage_NAME}.txt."
|
|
|
|
)
|
|
|
|
endif()
|
2018-09-29 08:55:33 -04:00
|
|
|
|
2021-09-26 06:35:42 -04:00
|
|
|
endfunction() # SETUP_TARGET_FOR_COVERAGE_GCOVR
|
2018-09-29 08:55:33 -04:00
|
|
|
|
|
|
|
# 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
|
|
|
|
# the coverage generation will not complete.
|
|
|
|
#
|
2021-09-26 06:35:42 -04:00
|
|
|
# SETUP_TARGET_FOR_COVERAGE_LLVM(
|
2018-09-29 08:55:33 -04:00
|
|
|
# NAME ctest_coverage # New target name
|
|
|
|
# EXECUTABLE ctest -j ${PROCESSOR_COUNT} # Executable in PROJECT_BINARY_DIR
|
|
|
|
# DEPENDENCIES executable_target # Dependencies to build first
|
|
|
|
# )
|
2021-09-26 06:35:42 -04:00
|
|
|
function(SETUP_TARGET_FOR_COVERAGE_LLVM)
|
2018-09-29 08:55:33 -04:00
|
|
|
|
|
|
|
set(options NONE)
|
2021-09-26 06:35:42 -04:00
|
|
|
set(oneValueArgs NAME PROF_FILE)
|
|
|
|
set(multiValueArgs EXECUTABLE BINARY EXECUTABLE_ARGS SOURCES DEPENDENCIES)
|
2018-09-29 08:55:33 -04:00
|
|
|
cmake_parse_arguments(Coverage "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
|
|
|
|
|
2021-09-26 06:35:42 -04:00
|
|
|
if(XCRUN_PATH)
|
|
|
|
set(LLVM_COV_PATH ${XCRUN_PATH} llvm-cov)
|
|
|
|
set(LLVM_PROFDATA_PATH ${XCRUN_PATH} llvm-profdata)
|
|
|
|
else()
|
|
|
|
if(NOT LLVM_COV_PATH)
|
|
|
|
message(FATAL_ERROR "llvm-cov not found! Aborting...")
|
|
|
|
endif() # NOT LLVM_COV_PATH
|
|
|
|
if(NOT LLVM_PROFDATA_PATH)
|
|
|
|
message(FATAL_ERROR "llvm-profdata not found! Aborting...")
|
|
|
|
endif() # NOT LLVM_PROFDATA_PATH
|
|
|
|
endif() # XCRUN_PATH
|
|
|
|
|
|
|
|
set(LLVM_PROFILE_DIR ${PROJECT_BINARY_DIR}/llvm_profile)
|
|
|
|
file(REMOVE_RECURSE ${LLVM_PROFILE_DIR})
|
|
|
|
|
|
|
|
set(COV_EXCLUDES "")
|
|
|
|
foreach(EXCLUDE ${COVERAGE_EXCLUDES})
|
|
|
|
list(APPEND COV_EXCLUDES "-ignore-filename-regex=${EXCLUDE}")
|
2018-09-29 08:55:33 -04:00
|
|
|
endforeach()
|
|
|
|
|
2021-09-26 06:35:42 -04:00
|
|
|
list(GET Coverage_BINARY 0 COV_BINARY)
|
|
|
|
if(Coverage_BINARY)
|
|
|
|
list(REMOVE_AT Coverage_BINARY 0)
|
|
|
|
foreach(BIN ${Coverage_BINARY})
|
|
|
|
list(APPEND COV_BINARY -object ${BIN})
|
|
|
|
endforeach()
|
|
|
|
endif()
|
|
|
|
|
2018-09-29 08:55:33 -04:00
|
|
|
add_custom_target(${Coverage_NAME}
|
2021-09-26 06:35:42 -04:00
|
|
|
COMMAND $(MAKE)
|
|
|
|
COMMAND ${CMAKE_COMMAND} -E env LLVM_PROFILE_FILE=${LLVM_PROFILE_DIR}/profile-%p.profraw ${Coverage_EXECUTABLE}
|
|
|
|
|
|
|
|
COMMAND ${LLVM_PROFDATA_PATH} merge -sparse ${LLVM_PROFILE_DIR}/* -o coverage.profdata
|
|
|
|
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
|
|
|
|
DEPENDS ${Coverage_DEPENDENCIES})
|
|
|
|
|
|
|
|
if("html" IN_LIST CMAKE_COVERAGE_FORMAT)
|
|
|
|
add_custom_command(TARGET ${Coverage_NAME} POST_BUILD
|
|
|
|
COMMAND ${LLVM_COV_PATH} show -instr-profile=coverage.profdata ${COV_BINARY}
|
|
|
|
--format=html --output-dir=${Coverage_NAME}-html ${COV_EXCLUDES} ${Coverage_SOURCES}
|
|
|
|
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
|
|
|
|
COMMENT "Running llvm-cov to produce HTML code coverage report ${Coverage_NAME}-html")
|
|
|
|
endif()
|
2018-09-29 08:55:33 -04:00
|
|
|
|
2021-09-26 06:35:42 -04:00
|
|
|
if("txt" IN_LIST CMAKE_COVERAGE_FORMAT)
|
|
|
|
add_custom_command(TARGET ${Coverage_NAME} POST_BUILD
|
|
|
|
COMMAND ${LLVM_COV_PATH} show -instr-profile=coverage.profdata ${COV_BINARY}
|
|
|
|
--format=text ${COV_EXCLUDES} ${Coverage_SOURCES} > ${Coverage_NAME}.txt
|
2018-09-29 08:55:33 -04:00
|
|
|
|
2021-09-26 06:35:42 -04:00
|
|
|
WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
|
|
|
|
COMMENT "Running llvm-cov to produce TXT code coverage report ${Coverage_NAME}.txt.")
|
|
|
|
endif()
|
2018-09-29 08:55:33 -04:00
|
|
|
|
2021-09-26 06:35:42 -04:00
|
|
|
endfunction() # SETUP_TARGET_FOR_COVERAGE_LLVM
|
2018-09-29 08:55:33 -04:00
|
|
|
|
|
|
|
|
|
|
|
function(APPEND_COVERAGE_COMPILER_FLAGS)
|
2021-09-26 06:35:42 -04:00
|
|
|
message(STATUS "Appending code coverage compiler flags: ${COVERAGE_COMPILER_FLAGS}")
|
2018-09-29 08:55:33 -04:00
|
|
|
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${COVERAGE_COMPILER_FLAGS}" PARENT_SCOPE)
|
|
|
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COVERAGE_COMPILER_FLAGS}" PARENT_SCOPE)
|
2021-09-21 00:17:46 -04:00
|
|
|
endfunction() # APPEND_COVERAGE_COMPILER_FLAGS
|