|
| 1 | +# |
| 2 | +# The MIT License (MIT) |
| 3 | +# |
| 4 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +# of this software and associated documentation files (the "Software"), to deal |
| 6 | +# in the Software without restriction, including without limitation the rights |
| 7 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +# copies of the Software, and to permit persons to whom the Software is |
| 9 | +# furnished to do so, subject to the following conditions: |
| 10 | +# |
| 11 | +# The above copyright notice and this permission notice shall be included in all |
| 12 | +# copies or substantial portions of the Software. |
| 13 | +# |
| 14 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | +# SOFTWARE. |
| 21 | +# |
| 22 | +# Copyright (C) 2014 Joakim Söderberg <joakim.soderberg@gmail.com> |
| 23 | +# |
| 24 | + |
| 25 | +set(_CMAKE_SCRIPT_PATH ${CMAKE_CURRENT_LIST_DIR}) # must be outside coveralls_setup() to get correct path |
| 26 | + |
| 27 | +# |
| 28 | +# Param _COVERAGE_SRCS A list of source files that coverage should be collected for. |
| 29 | +# Param _COVERALLS_UPLOAD Upload the result to coveralls? |
| 30 | +# |
| 31 | + |
| 32 | +function(coveralls_setup _COVERAGE_SRCS _COVERALLS_UPLOAD) |
| 33 | + |
| 34 | + if (ARGC GREATER 2) |
| 35 | + set(_CMAKE_SCRIPT_PATH ${ARGN}) |
| 36 | + message(STATUS "Coveralls: Using alternate CMake script dir: ${_CMAKE_SCRIPT_PATH}") |
| 37 | + endif() |
| 38 | + |
| 39 | + if (NOT EXISTS "${_CMAKE_SCRIPT_PATH}/CoverallsClear.cmake") |
| 40 | + message(FATAL_ERROR "Coveralls: Missing ${_CMAKE_SCRIPT_PATH}/CoverallsClear.cmake") |
| 41 | + endif() |
| 42 | + |
| 43 | + if (NOT EXISTS "${_CMAKE_SCRIPT_PATH}/CoverallsGenerateGcov.cmake") |
| 44 | + message(FATAL_ERROR "Coveralls: Missing ${_CMAKE_SCRIPT_PATH}/CoverallsGenerateGcov.cmake") |
| 45 | + endif() |
| 46 | + |
| 47 | + # When passing a CMake list to an external process, the list |
| 48 | + # will be converted from the format "1;2;3" to "1 2 3". |
| 49 | + # This means the script we're calling won't see it as a list |
| 50 | + # of sources, but rather just one long path. We remedy this |
| 51 | + # by replacing ";" with "*" and then reversing that in the script |
| 52 | + # that we're calling. |
| 53 | + # http://cmake.3232098.n2.nabble.com/Passing-a-CMake-list-quot-as-is-quot-to-a-custom-target-td6505681.html |
| 54 | + set(COVERAGE_SRCS_TMP ${_COVERAGE_SRCS}) |
| 55 | + set(COVERAGE_SRCS "") |
| 56 | + foreach (COVERAGE_SRC ${COVERAGE_SRCS_TMP}) |
| 57 | + set(COVERAGE_SRCS "${COVERAGE_SRCS}*${COVERAGE_SRC}") |
| 58 | + endforeach() |
| 59 | + |
| 60 | + #message("Coverage sources: ${COVERAGE_SRCS}") |
| 61 | + set(COVERALLS_FILE ${PROJECT_BINARY_DIR}/coveralls.json) |
| 62 | + |
| 63 | + add_custom_target(coveralls_generate |
| 64 | + |
| 65 | + # Zero the coverage counters. |
| 66 | + COMMAND ${CMAKE_COMMAND} -DPROJECT_BINARY_DIR="${PROJECT_BINARY_DIR}" -P "${_CMAKE_SCRIPT_PATH}/CoverallsClear.cmake" |
| 67 | + |
| 68 | + # Run regress tests. |
| 69 | + COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure |
| 70 | + |
| 71 | + # Generate Gcov and translate it into coveralls JSON. |
| 72 | + # We do this by executing an external CMake script. |
| 73 | + # (We don't want this to run at CMake generation time, but after compilation and everything has run). |
| 74 | + COMMAND ${CMAKE_COMMAND} |
| 75 | + -DCOVERAGE_SRCS="${COVERAGE_SRCS}" # TODO: This is passed like: "a b c", not "a;b;c" |
| 76 | + -DCOVERALLS_OUTPUT_FILE="${COVERALLS_FILE}" |
| 77 | + -DCOV_PATH="${PROJECT_BINARY_DIR}" |
| 78 | + -DPROJECT_ROOT="${PROJECT_SOURCE_DIR}" |
| 79 | + -P "${_CMAKE_SCRIPT_PATH}/CoverallsGenerateGcov.cmake" |
| 80 | + |
| 81 | + WORKING_DIRECTORY ${PROJECT_BINARY_DIR} |
| 82 | + COMMENT "Generating coveralls output..." |
| 83 | + ) |
| 84 | + |
| 85 | + if (_COVERALLS_UPLOAD) |
| 86 | + message("COVERALLS UPLOAD: ON") |
| 87 | + |
| 88 | + find_program(CURL_EXECUTABLE curl) |
| 89 | + |
| 90 | + if (NOT CURL_EXECUTABLE) |
| 91 | + message(FATAL_ERROR "Coveralls: curl not found! Aborting") |
| 92 | + endif() |
| 93 | + |
| 94 | + add_custom_target(coveralls_upload |
| 95 | + # Upload the JSON to coveralls. |
| 96 | + COMMAND ${CURL_EXECUTABLE} |
| 97 | + -S -F json_file=@${COVERALLS_FILE} |
| 98 | + https://coveralls.io/api/v1/jobs |
| 99 | + |
| 100 | + DEPENDS coveralls_generate |
| 101 | + |
| 102 | + WORKING_DIRECTORY ${PROJECT_BINARY_DIR} |
| 103 | + COMMENT "Uploading coveralls output...") |
| 104 | + |
| 105 | + add_custom_target(coveralls DEPENDS coveralls_upload) |
| 106 | + else() |
| 107 | + message("COVERALLS UPLOAD: OFF") |
| 108 | + add_custom_target(coveralls DEPENDS coveralls_generate) |
| 109 | + endif() |
| 110 | + |
| 111 | +endfunction() |
| 112 | + |
| 113 | +macro(coveralls_turn_on_coverage) |
| 114 | + if(NOT (CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX) |
| 115 | + AND (NOT "${CMAKE_C_COMPILER_ID}" STREQUAL "Clang")) |
| 116 | + message(FATAL_ERROR "Coveralls: Compiler ${CMAKE_C_COMPILER_ID} is not GNU gcc! Aborting... You can set this on the command line using CC=/usr/bin/gcc CXX=/usr/bin/g++ cmake <options> ..") |
| 117 | + endif() |
| 118 | + |
| 119 | + if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug") |
| 120 | + message(FATAL_ERROR "Coveralls: Code coverage results with an optimised (non-Debug) build may be misleading! Add -DCMAKE_BUILD_TYPE=Debug") |
| 121 | + endif() |
| 122 | + |
| 123 | + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0 -fprofile-arcs -ftest-coverage") |
| 124 | + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 -fprofile-arcs -ftest-coverage") |
| 125 | +endmacro() |
| 126 | + |
| 127 | + |
| 128 | + |
0 commit comments