Coveralls.cmake 4.2 KB

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