From a9f8f27b216876e1aadfc1c3588c905173a5f9d7 Mon Sep 17 00:00:00 2001 From: Anonymous Maarten Date: Sat, 28 Oct 2017 08:15:34 +0200 Subject: [PATCH] cmake: add coverage support --- cmake/ctest/experimental_script.ctest | 23 +++++++++++------------ cmake_configure.cmake | 22 ++++++++++++++++++++++ cmake_options.cmake | 4 +++- tests/CMakeLists.txt | 4 +++- 4 files changed, 39 insertions(+), 14 deletions(-) diff --git a/cmake/ctest/experimental_script.ctest b/cmake/ctest/experimental_script.ctest index d0d12c67..f767aac5 100644 --- a/cmake/ctest/experimental_script.ctest +++ b/cmake/ctest/experimental_script.ctest @@ -2,16 +2,15 @@ cmake_minimum_required(VERSION 3.2.2) list(APPEND CMAKE_MODULE_PATH "${CTEST_SCRIPT_DIRECTORY}/../modules") -get_filename_component(SRC_DIR "${CTEST_SCRIPT_DIRECTORY}/../.." ABSOLUTE) -get_filename_component(BIN_DIR "${SRC_DIR}/build/experimental" ABSOLUTE) +get_filename_component(CTEST_SOURCE_DIRECTORY "${CTEST_SCRIPT_DIRECTORY}/../.." ABSOLUTE) +get_filename_component(CTEST_BINARY_DIRECTORY "${CTEST_SOURCE_DIRECTORY}/build/experimental" ABSOLUTE) -set(CTEST_SOURCE_DIRECTORY "${SRC_DIR}") -set(CTEST_BINARY_DIRECTORY "${BIN_DIR}") set(CTEST_CMAKE_GENERATOR "Unix Makefiles") -ctest_empty_binary_directory("${BIN_DIR}") +set(CTEST_COVERAGE_COMMAND "gcov") -set(CMAKE_BINARY_DIR "${BIN_DIR}") -set(CMAKE_CURRENT_BINARY_DIR "${BIN_DIR}") +if(EXISTS "${CTEST_BINARY_DIRECTORY}/CMakeCache.txt") + ctest_empty_binary_directory("${CTEST_BINARY_DIRECTORY}") +endif() include(GetGitRevisionDescription) get_git_head_revision(GIT_REFSPEC GIT_SHA1) @@ -23,12 +22,12 @@ set(CTEST_BUILD_NAME "experimental_script-${SHORT_COMMIT}") ctest_start("Experimental") ctest_configure( OPTIONS - "-DBUILD_VIEWER=1;-DBUILD_TESTS=1" + "-DBUILD_VIEWER=1;-DBUILD_TESTS=1;-DTEST_COVERAGE=1;-DRW_VERBOSE_DEBUG_MESSAGES=0" ) ctest_build() -ctest_test( - RETURN_VALUE TEST_RESULT - ) +ctest_test() +ctest_coverage() +ctest_coverage() ctest_submit( RETURN_VALUE SUBMIT_RESULT ) @@ -39,5 +38,5 @@ message("Build Name: ${CTEST_BUILD_NAME}") if(NOT SUBMIT_RESULT EQUAL "0") message("Uploading results failed.") message("Execute the next command to submit later.") - message("make -C \"CTEST_BINARY_DIRECTORY\" ExperimentalSubmit") + message("make -C \"${CTEST_BINARY_DIRECTORY}\" ExperimentalSubmit") endif() diff --git a/cmake_configure.cmake b/cmake_configure.cmake index 4ae859e6..a8cfb11b 100644 --- a/cmake_configure.cmake +++ b/cmake_configure.cmake @@ -68,3 +68,25 @@ elseif(FAILED_CHECK_ACTION STREQUAL "BREAKPOINT") else() message(FATAL_ERROR "Illegal FAILED_CHECK_ACTION option. (was '${FAILED_CHECK_ACTION}')") endif() + +if(TEST_COVERAGE) + if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug") + message("Test Coverage enabled. Setting CMAKE_BUILD_TYPE to Debug.") + set(CMAKE_BUILD_TYPE "Debug") + endif() + if(NOT BUILD_TESTS) + message("TestCoverage enabled. Enabling BUILD_TESTS.") + set(BUILD_TESTS "ON") + endif() + target_compile_options(rw_interface + INTERFACE + "-O0" + "-fprofile-arcs" + "-ftest-coverage" + ) + set_target_properties(rw_interface + PROPERTIES + INTERFACE_LINK_LIBRARIES + gcov + ) +endif() diff --git a/cmake_options.cmake b/cmake_options.cmake index 95368fbf..32b4dd03 100644 --- a/cmake_options.cmake +++ b/cmake_options.cmake @@ -15,5 +15,7 @@ set(BIN_DIR "bin" CACHE STRING "Prefix subdirectory to put the binaries in.") set(DOC_DIR "share/doc/openrw" CACHE STRING "Prefix subdirectory to put the documentation in.") if(NOT CMAKE_BUILD_TYPE) - set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build, options are: Debug Release" FORCE) + set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build, options are: Debug Release") endif() + +option(TEST_COVERAGE "Enable coverage analysis (implies CMAKE_BUILD_TYPE=Debug)") diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 315ca70e..a8accb9b 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -77,4 +77,6 @@ target_link_libraries(run_tests ${Boost_SYSTEM_LIBRARY} ) -add_test(UnitTests run_tests) +add_test(NAME UnitTests + COMMAND "$" + )