From 9a544f2d987df940625465129a5f00bea01ad4eb Mon Sep 17 00:00:00 2001 From: Nils Gladitz Date: Thu, 2 Apr 2015 17:02:08 +0200 Subject: [PATCH] CTestCoverageCollectGCOV: Support CTEST_CUSTOM_COVERAGE_EXCLUDE --- Modules/CTestCoverageCollectGCOV.cmake | 31 +++++++++++++- Tests/CMakeLists.txt | 3 +- .../TestProject/3rdparty/foo.cpp | 1 + .../TestProject/CMakeLists.txt | 41 +++++++++++++++++++ .../TestProject/extra/extra.cpp | 1 + .../TestProject/fake_compile_time_gcno.cmake | 7 ++++ .../TestProject/fake_run_time_gcda.cmake | 12 ++++++ .../TestProject/main.cpp | 1 + Tests/CTestCoverageCollectGCOV/fakegcov.cmake | 12 ++++-- Tests/CTestCoverageCollectGCOV/test.cmake.in | 37 +++++++++++------ 10 files changed, 128 insertions(+), 18 deletions(-) create mode 100644 Tests/CTestCoverageCollectGCOV/TestProject/3rdparty/foo.cpp create mode 100644 Tests/CTestCoverageCollectGCOV/TestProject/CMakeLists.txt create mode 100644 Tests/CTestCoverageCollectGCOV/TestProject/extra/extra.cpp create mode 100644 Tests/CTestCoverageCollectGCOV/TestProject/fake_compile_time_gcno.cmake create mode 100644 Tests/CTestCoverageCollectGCOV/TestProject/fake_run_time_gcda.cmake create mode 100644 Tests/CTestCoverageCollectGCOV/TestProject/main.cpp diff --git a/Modules/CTestCoverageCollectGCOV.cmake b/Modules/CTestCoverageCollectGCOV.cmake index 4519627e3..8659a6916 100644 --- a/Modules/CTestCoverageCollectGCOV.cmake +++ b/Modules/CTestCoverageCollectGCOV.cmake @@ -147,8 +147,37 @@ function(ctest_coverage_collect_gcov) \"Binary\": \"${binary_dir}\" }") # collect the gcov files + set(unfiltered_gcov_files) + file(GLOB_RECURSE unfiltered_gcov_files RELATIVE ${binary_dir} "${coverage_dir}/*.gcov") + set(gcov_files) - file(GLOB_RECURSE gcov_files RELATIVE ${binary_dir} "${coverage_dir}/*.gcov") + foreach(gcov_file ${unfiltered_gcov_files}) + file(STRINGS ${binary_dir}/${gcov_file} first_line LIMIT_COUNT 1 ENCODING UTF-8) + + set(is_excluded false) + if(first_line MATCHES "^ -: 0:Source:(.*)$") + set(source_file ${CMAKE_MATCH_1}) + elseif(NOT GCOV_QUIET) + message(STATUS "Could not determine source file corresponding to: ${gcov_file}") + endif() + + foreach(exclude_entry ${CTEST_CUSTOM_COVERAGE_EXCLUDE}) + if(source_file MATCHES "${exclude_entry}") + set(is_excluded true) + + if(NOT GCOV_QUIET) + message("Excluding coverage for: ${source_file} which matches ${exclude_entry}") + endif() + + break() + endif() + endforeach() + + if(NOT is_excluded) + list(APPEND gcov_files ${gcov_file}) + endif() + endforeach() + # tar up the coverage info with the same date so that the md5 # sum will be the same for the tar file independent of file time # stamps diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt index 5944d081e..d9988b200 100644 --- a/Tests/CMakeLists.txt +++ b/Tests/CMakeLists.txt @@ -2345,12 +2345,13 @@ ${CMake_BINARY_DIR}/bin/cmake -DDIR=dev -P ${CMake_SOURCE_DIR}/Utilities/Release "${CMake_BINARY_DIR}/Tests/CTestCoverageCollectGCOV/test.cmake" @ONLY ESCAPE_QUOTES) add_test(CTestCoverageCollectGCOV ${CMAKE_CTEST_COMMAND} + -C \${CTEST_CONFIGURATION_TYPE} -S "${CMake_BINARY_DIR}/Tests/CTestCoverageCollectGCOV/test.cmake" -VV --output-log "${CMake_BINARY_DIR}/Tests/CTestCoverageCollectGCOV/testOut.log" ) set_tests_properties(CTestCoverageCollectGCOV PROPERTIES PASS_REGULAR_EXPRESSION - "PASSED with correct output.*Testing/CoverageInfo/echoargs.gcov") + "PASSED with correct output.*Testing/CoverageInfo/main.cpp.gcov") configure_file( "${CMake_SOURCE_DIR}/Tests/CTestTestEmptyBinaryDirectory/test.cmake.in" diff --git a/Tests/CTestCoverageCollectGCOV/TestProject/3rdparty/foo.cpp b/Tests/CTestCoverageCollectGCOV/TestProject/3rdparty/foo.cpp new file mode 100644 index 000000000..85e6cd8c3 --- /dev/null +++ b/Tests/CTestCoverageCollectGCOV/TestProject/3rdparty/foo.cpp @@ -0,0 +1 @@ +void foo() {} diff --git a/Tests/CTestCoverageCollectGCOV/TestProject/CMakeLists.txt b/Tests/CTestCoverageCollectGCOV/TestProject/CMakeLists.txt new file mode 100644 index 000000000..ce6fac435 --- /dev/null +++ b/Tests/CTestCoverageCollectGCOV/TestProject/CMakeLists.txt @@ -0,0 +1,41 @@ +cmake_minimum_required(VERSION 3.2) + +project(TestProject CXX) + +include(CTest) + +set(SOURCES + main.cpp + 3rdparty/foo.cpp + extra/extra.cpp +) + +add_executable(myexecutable ${SOURCES}) + +set_property(SOURCE main.cpp APPEND PROPERTY LABELS SourceLabel) +set_property(TARGET myexecutable APPEND PROPERTY LABELS TargetLabel) + +set(MYEXECUTABLE_INFO_FILE "${CMAKE_CURRENT_BINARY_DIR}/myexecutable_info.cmake") + +file(WRITE "${MYEXECUTABLE_INFO_FILE}" " + set(TARGET myexecutable) + set(SOURCE_DIR \"${CMAKE_CURRENT_SOURCE_DIR}\") + set(SOURCES \"${SOURCES}\") +") + +add_custom_command(TARGET myexecutable + POST_BUILD + COMMAND ${CMAKE_COMMAND} + "-DINFO_FILE=${MYEXECUTABLE_INFO_FILE}" + -P "${CMAKE_CURRENT_SOURCE_DIR}/fake_compile_time_gcno.cmake" + VERBATIM +) + +add_test(NAME mytest + COMMAND ${CMAKE_COMMAND} + "-DMYEXECUTABLE=$" + "-DTARGETDIR=${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/myexecutable.dir" + -P "${CMAKE_CURRENT_SOURCE_DIR}/fake_run_time_gcda.cmake" +) + +set_property(TEST mytest APPEND PROPERTY LABELS TestLabel) diff --git a/Tests/CTestCoverageCollectGCOV/TestProject/extra/extra.cpp b/Tests/CTestCoverageCollectGCOV/TestProject/extra/extra.cpp new file mode 100644 index 000000000..c3a2c129d --- /dev/null +++ b/Tests/CTestCoverageCollectGCOV/TestProject/extra/extra.cpp @@ -0,0 +1 @@ +void extra() {} diff --git a/Tests/CTestCoverageCollectGCOV/TestProject/fake_compile_time_gcno.cmake b/Tests/CTestCoverageCollectGCOV/TestProject/fake_compile_time_gcno.cmake new file mode 100644 index 000000000..881460b3c --- /dev/null +++ b/Tests/CTestCoverageCollectGCOV/TestProject/fake_compile_time_gcno.cmake @@ -0,0 +1,7 @@ +include("${INFO_FILE}") + +foreach(source ${SOURCES}) + file(WRITE "CMakeFiles/${TARGET}.dir/${source}.gcno" + "${SOURCE_DIR}/${source}" + ) +endforeach() diff --git a/Tests/CTestCoverageCollectGCOV/TestProject/fake_run_time_gcda.cmake b/Tests/CTestCoverageCollectGCOV/TestProject/fake_run_time_gcda.cmake new file mode 100644 index 000000000..26ce2bd76 --- /dev/null +++ b/Tests/CTestCoverageCollectGCOV/TestProject/fake_run_time_gcda.cmake @@ -0,0 +1,12 @@ +execute_process(COMMAND ${MYEXECUTABLE} RESULT_VARIABLE RESULT) + +if(NOT RESULT_VARIABLE STREQUAL "0") + message("Test failure") +endif() + +file(GLOB_RECURSE gcno_files "${TARGETDIR}/*.gcno") + +foreach(gcno_file ${gcno_files}) + string(REPLACE ".gcno" ".gcda" gcda_file "${gcno_file}") + configure_file(${gcno_file} ${gcda_file} COPYONLY) +endforeach() diff --git a/Tests/CTestCoverageCollectGCOV/TestProject/main.cpp b/Tests/CTestCoverageCollectGCOV/TestProject/main.cpp new file mode 100644 index 000000000..237c8ce18 --- /dev/null +++ b/Tests/CTestCoverageCollectGCOV/TestProject/main.cpp @@ -0,0 +1 @@ +int main() {} diff --git a/Tests/CTestCoverageCollectGCOV/fakegcov.cmake b/Tests/CTestCoverageCollectGCOV/fakegcov.cmake index e704f14ca..b0c3a9b9f 100644 --- a/Tests/CTestCoverageCollectGCOV/fakegcov.cmake +++ b/Tests/CTestCoverageCollectGCOV/fakegcov.cmake @@ -3,6 +3,12 @@ foreach(I RANGE 0 ${CMAKE_ARGC}) set(gcda_file "${CMAKE_ARGV${I}}") endif() endforeach() -get_filename_component(gcda_file ${gcda_file} NAME_WE) -file(WRITE "${CMAKE_SOURCE_DIR}/${gcda_file}.gcov" -"fake gcov file") + +get_filename_component(gcda_name ${gcda_file} NAME) +string(REPLACE ".gcda" ".gcov" gcov_name "${gcda_name}") + +file(STRINGS "${gcda_file}" source_file LIMIT_COUNT 1 ENCODING UTF-8) + +file(WRITE "${CMAKE_SOURCE_DIR}/${gcov_name}" + " -: 0:Source:${source_file}" +) diff --git a/Tests/CTestCoverageCollectGCOV/test.cmake.in b/Tests/CTestCoverageCollectGCOV/test.cmake.in index 4bdcb10e4..29f7e7f1a 100644 --- a/Tests/CTestCoverageCollectGCOV/test.cmake.in +++ b/Tests/CTestCoverageCollectGCOV/test.cmake.in @@ -1,16 +1,21 @@ cmake_minimum_required(VERSION 2.8.12) -set(CTEST_PROJECT_NAME "SmallAndFast") -set(CTEST_SOURCE_DIRECTORY "@CMake_SOURCE_DIR@/Tests/CTestTest/SmallAndFast") -set(CTEST_BINARY_DIRECTORY "@CMake_BINARY_DIR@/Tests/CTestCoverageCollectGCOV") +set(CTEST_PROJECT_NAME "TestProject") +set(CTEST_SOURCE_DIRECTORY "@CMake_SOURCE_DIR@/Tests/CTestCoverageCollectGCOV/TestProject") +set(CTEST_BINARY_DIRECTORY "@CMake_BINARY_DIR@/Tests/CTestCoverageCollectGCOV/TestProject") set(CTEST_CMAKE_GENERATOR "@CMAKE_GENERATOR@") + +ctest_empty_binary_directory(${CTEST_BINARY_DIRECTORY}) + ctest_start(Experimental) ctest_configure() ctest_build() ctest_test() -file(WRITE ${CTEST_BINARY_DIRECTORY}/CMakeFiles/echoargs.dir/echoargs.gcda -"dummy -") +list(APPEND CTEST_CUSTOM_COVERAGE_EXCLUDE + "/foo/something" + "/3rdparty/" + "/bar/somethingelse" +) include(CTestCoverageCollectGCOV) set(tar_file ${CTEST_BINARY_DIRECTORY}/gcov.tar) @@ -22,15 +27,21 @@ ctest_coverage_collect_gcov( GCOV_OPTIONS -P "@CMake_SOURCE_DIR@/Tests/CTestCoverageCollectGCOV/fakegcov.cmake") execute_process(COMMAND - ${CMAKE_COMMAND} -E tar tf ${tar_file} - OUTPUT_VARIABLE out - WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}) + ${CMAKE_COMMAND} -E tar tf ${tar_file} + OUTPUT_VARIABLE out + WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} + OUTPUT_STRIP_TRAILING_WHITESPACE +) + +string(REPLACE "\n" ";" out "${out}") +list(SORT out) set(expected_out -"Testing/CoverageInfo/echoargs.gcov -Testing/CoverageInfo/data.json -CMakeFiles/echoargs.dir/Labels.json -") + CMakeFiles/myexecutable.dir/Labels.json + Testing/CoverageInfo/data.json + Testing/CoverageInfo/extra.cpp.gcov + Testing/CoverageInfo/main.cpp.gcov +) if("${out}" STREQUAL "${expected_out}") message("PASSED with correct output: ${out}")