Coveralls.cmake 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 <[email protected]>
  23. #
  24. set(_CMAKE_SCRIPT_PATH ${CMAKE_CURRENT_LIST_DIR}) # must be outside coveralls_setup() to get correct path
  25. #
  26. # Param _COVERAGE_SRCS A list of source files that coverage should be collected for.
  27. # Param _COVERALLS_UPLOAD Upload the result to coveralls?
  28. #
  29. function(coveralls_setup _COVERAGE_SRCS _COVERALLS_UPLOAD)
  30. if (ARGC GREATER 2)
  31. set(_CMAKE_SCRIPT_PATH ${ARGN})
  32. message(STATUS "Coveralls: Using alternate CMake script dir: ${_CMAKE_SCRIPT_PATH}")
  33. endif()
  34. if (NOT EXISTS "${_CMAKE_SCRIPT_PATH}/CoverallsClear.cmake")
  35. message(FATAL_ERROR "Coveralls: Missing ${_CMAKE_SCRIPT_PATH}/CoverallsClear.cmake")
  36. endif()
  37. if (NOT EXISTS "${_CMAKE_SCRIPT_PATH}/CoverallsGenerateGcov.cmake")
  38. message(FATAL_ERROR "Coveralls: Missing ${_CMAKE_SCRIPT_PATH}/CoverallsGenerateGcov.cmake")
  39. endif()
  40. # When passing a CMake list to an external process, the list
  41. # will be converted from the format "1;2;3" to "1 2 3".
  42. # This means the script we're calling won't see it as a list
  43. # of sources, but rather just one long path. We remedy this
  44. # by replacing ";" with "*" and then reversing that in the script
  45. # that we're calling.
  46. # http://cmake.3232098.n2.nabble.com/Passing-a-CMake-list-quot-as-is-quot-to-a-custom-target-td6505681.html
  47. set(COVERAGE_SRCS_TMP ${_COVERAGE_SRCS})
  48. set(COVERAGE_SRCS "")
  49. foreach (COVERAGE_SRC ${COVERAGE_SRCS_TMP})
  50. set(COVERAGE_SRCS "${COVERAGE_SRCS}*${COVERAGE_SRC}")
  51. endforeach()
  52. #message("Coverage sources: ${COVERAGE_SRCS}")
  53. set(COVERALLS_FILE ${PROJECT_BINARY_DIR}/coveralls.json)
  54. add_custom_target(coveralls_generate
  55. # Zero the coverage counters.
  56. COMMAND ${CMAKE_COMMAND} -DPROJECT_BINARY_DIR="${PROJECT_BINARY_DIR}" -P "${_CMAKE_SCRIPT_PATH}/CoverallsClear.cmake"
  57. # Run regress tests.
  58. COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure
  59. # Generate Gcov and translate it into coveralls JSON.
  60. # We do this by executing an external CMake script.
  61. # (We don't want this to run at CMake generation time, but after compilation and everything has run).
  62. COMMAND ${CMAKE_COMMAND}
  63. -DCOVERAGE_SRCS="${COVERAGE_SRCS}" # TODO: This is passed like: "a b c", not "a;b;c"
  64. -DCOVERALLS_OUTPUT_FILE="${COVERALLS_FILE}"
  65. -DCOV_PATH="${PROJECT_BINARY_DIR}"
  66. -DPROJECT_ROOT="${PROJECT_SOURCE_DIR}"
  67. -P "${_CMAKE_SCRIPT_PATH}/CoverallsGenerateGcov.cmake"
  68. WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
  69. COMMENT "Generating coveralls output..."
  70. )
  71. if (_COVERALLS_UPLOAD)
  72. message("COVERALLS UPLOAD: ON")
  73. find_program(CURL_EXECUTABLE curl)
  74. if (NOT CURL_EXECUTABLE)
  75. message(FATAL_ERROR "Coveralls: curl not found! Aborting")
  76. endif()
  77. add_custom_target(coveralls_upload
  78. # Upload the JSON to coveralls.
  79. COMMAND ${CURL_EXECUTABLE}
  80. -S -F json_file=@${COVERALLS_FILE}
  81. https://coveralls.io/api/v1/jobs
  82. DEPENDS coveralls_generate
  83. WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
  84. COMMENT "Uploading coveralls output...")
  85. add_custom_target(coveralls DEPENDS coveralls_upload)
  86. else()
  87. message("COVERALLS UPLOAD: OFF")
  88. add_custom_target(coveralls DEPENDS coveralls_generate)
  89. endif()
  90. endfunction()
  91. macro(coveralls_turn_on_coverage)
  92. if(NOT (CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
  93. AND (NOT "${CMAKE_C_COMPILER_ID}" STREQUAL "Clang"))
  94. 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> ..")
  95. endif()
  96. if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
  97. message(FATAL_ERROR "Coveralls: Code coverage results with an optimised (non-Debug) build may be misleading! Add -DCMAKE_BUILD_TYPE=Debug")
  98. endif()
  99. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0 -fprofile-arcs -ftest-coverage")
  100. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 -fprofile-arcs -ftest-coverage")
  101. endmacro()