Skip to content

Commit 7934f77

Browse files
Merge pull request #386 from Infinoid/code-coverage
Add code coverage targets to cmake
2 parents 61969a6 + 604dca7 commit 7934f77

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

CMakeLists.txt

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ endif()
55
project(taco)
66
option(CUDA "Build for NVIDIA GPU (CUDA must be preinstalled)" OFF)
77
option(PYTHON "Build TACO for python environment" OFF)
8-
option(OPENMP" Build with OpenMP execution support" OFF)
8+
option(OPENMP "Build with OpenMP execution support" OFF)
9+
option(COVERAGE "Build with code coverage analysis" OFF)
910
if(CUDA)
1011
message("-- Searching for CUDA Installation")
1112
find_package(CUDA REQUIRED)
@@ -75,6 +76,15 @@ if(OPENMP)
7576
set(C_CXX_FLAGS "-fopenmp ${C_CXX_FLAGS}")
7677
endif(OPENMP)
7778

79+
if(COVERAGE)
80+
find_program(PATH_TO_GCOVR gcovr REQUIRED)
81+
# add coverage tooling to build flags
82+
set(C_CXX_FLAGS "${C_CXX_FLAGS} -g -fprofile-arcs -ftest-coverage")
83+
# name the coverage files "foo.gcno", not "foo.cpp.gcno"
84+
set(CMAKE_CXX_OUTPUT_EXTENSION_REPLACE 1)
85+
message("-- Code coverage analysis (gcovr) enabled")
86+
endif(COVERAGE)
87+
7888
set(C_CXX_FLAGS "${C_CXX_FLAGS}")
7989
set(CMAKE_C_FLAGS "${C_CXX_FLAGS}")
8090
set(CMAKE_CXX_FLAGS "${C_CXX_FLAGS} -std=c++14")
@@ -103,3 +113,23 @@ if(PYTHON)
103113
endif(PYTHON)
104114
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wmissing-declarations")
105115
add_custom_target(src DEPENDS apps)
116+
117+
if(COVERAGE)
118+
# code coverage analysis target
119+
add_custom_target(gcovr
120+
COMMAND mkdir -p coverage
121+
COMMAND ${CMAKE_MAKE_PROGRAM} test
122+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
123+
)
124+
add_custom_command(TARGET gcovr
125+
COMMAND echo "Running gcovr..."
126+
COMMAND ${PATH_TO_GCOVR} -r ${CMAKE_SOURCE_DIR} --html --html-details -o coverage/index.html ${CMAKE_BINARY_DIR}
127+
COMMAND echo "See coverage/index.html for coverage information."
128+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
129+
)
130+
add_dependencies(gcovr taco-test)
131+
if(PYTHON)
132+
add_dependencies(gcovr core_modules)
133+
endif(PYTHON)
134+
set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES coverage)
135+
endif(COVERAGE)

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,25 @@ To run the Python test suite individually:
8585
python3 build/python_bindings/unit_tests.py
8686

8787

88+
## Code coverage analysis
89+
90+
To enable code coverage analysis, configure with `-DCOVERAGE=ON`. This requires
91+
the `gcovr` tool to be installed in your PATH.
92+
93+
For best results, the build type should be set to `Debug`. For example:
94+
95+
cmake -DCMAKE_BUILD_TYPE=Debug -DCOVERAGE=ON ..
96+
97+
Then to run code coverage analysis:
98+
99+
make gcovr
100+
101+
This will run the test suite and produce some coverage analysis. This process
102+
requires that the tests pass, so any failures must be fixed first.
103+
If all goes well, coverage results will be output to the `coverage/` folder.
104+
See `coverage/index.html` for a high level report, and click individual files
105+
to see the line-by-line results.
106+
88107
# Library example
89108

90109
The following sparse tensor-times-vector multiplication example in C++

0 commit comments

Comments
 (0)