Skip to content

Commit c821b10

Browse files
committed
Merge pull request #98 from jslee02/coveralls
Set up coveralls code coverage reporting
2 parents 15d306c + fee1a2f commit c821b10

7 files changed

+661
-3
lines changed

.travis.yml

+8-3
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@ compiler:
1414
- clang
1515

1616
env:
17-
- BUILD_TYPE=Debug
18-
- BUILD_TYPE=Release
17+
- BUILD_TYPE=Debug COVERALLS=OFF
18+
- BUILD_TYPE=Release COVERALLS=OFF
1919

2020
matrix:
21+
include:
22+
- os: linux
23+
compiler: gcc
24+
env: BUILD_TYPE=Debug COVERALLS=ON COVERALLS_REPO_TOKEN=PheK7dcpQFPXAUG0wTMVTnSVF349jEJGc
2125
exclude:
2226
- os: osx
2327
compiler: gcc
@@ -38,10 +42,11 @@ script:
3842
- cd build
3943

4044
# Configure
41-
- cmake -DCMAKE_BUILD_TYPE=$BUILD_TYPE ..
45+
- cmake -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DFCL_COVERALLS=$COVERALLS ..
4246

4347
# Build
4448
- make -j4
49+
- if [ $COVERALLS = ON ]; then make coveralls; fi
4550

4651
# Run unit tests
4752
- make test

CMakeLists.txt

+8
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ if(FCL_USE_SSE)
5656
# TODO: do something similar for other compilers
5757
endif()
5858

59+
# Coveralls support
60+
option(FCL_COVERALLS "Turn on coveralls support" OFF)
61+
option(FCL_COVERALLS_UPLOAD "Upload the generated coveralls json" ON)
62+
if(FCL_COVERALLS)
63+
include(Coveralls)
64+
coveralls_turn_on_coverage()
65+
endif()
66+
5967
# Find Octomap (optional)
6068
find_package(PkgConfig QUIET)
6169
set(FCL_HAVE_OCTOMAP 0)

CMakeModules/Coveralls.cmake

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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+

CMakeModules/CoverallsClear.cmake

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
# do not follow symlinks in file(GLOB_RECURSE ...)
26+
cmake_policy(SET CMP0009 NEW)
27+
28+
file(GLOB_RECURSE GCDA_FILES "${PROJECT_BINARY_DIR}/*.gcda")
29+
if(NOT GCDA_FILES STREQUAL "")
30+
file(REMOVE ${GCDA_FILES})
31+
endif()

0 commit comments

Comments
 (0)