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