Skip to content

Commit 2ec6099

Browse files
authored
Only generate coverage targets if the COVERAGE option is enabled (#384)
* Only generate coverage targets if the COVERAGE option is enabled Fixes #383 * Add changelog entry
1 parent cc954fd commit 2ec6099

File tree

3 files changed

+62
-57
lines changed

3 files changed

+62
-57
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Template:
55
Next
66
---------------------
77

8+
- [Only generate CMake coverage build targets when explicitly enabled](https://github.com/PJK/libcbor/issues/383)
9+
810
0.13.0 (2025-08-30)
911
---------------------
1012

CMakeLists.txt

Lines changed: 56 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ cmake_minimum_required(VERSION 3.5)
22

33
project(libcbor LANGUAGES C CXX)
44
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH}
5-
"${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules/")
5+
"${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules/")
66
include(CTest)
77
include(GNUInstallDirs) # Provides CMAKE_INSTALL_ variables
88

99
set(CBOR_VERSION_MAJOR "0")
1010
set(CBOR_VERSION_MINOR "13")
1111
set(CBOR_VERSION_PATCH "0")
1212
set(CBOR_VERSION
13-
${CBOR_VERSION_MAJOR}.${CBOR_VERSION_MINOR}.${CBOR_VERSION_PATCH})
13+
${CBOR_VERSION_MAJOR}.${CBOR_VERSION_MINOR}.${CBOR_VERSION_PATCH})
1414

1515
option(CMAKE_SKIP_INSTALL_ALL_DEPENDENCY
16-
"cmake --build --target install does not depend on cmake --build" true)
16+
"cmake --build --target install does not depend on cmake --build" true)
1717
option(BUILD_SHARED_LIBS "Build as a shared library" false)
1818

1919
include(CheckIncludeFiles)
@@ -28,19 +28,19 @@ option(CBOR_CUSTOM_ALLOC "Custom, dynamically defined allocator support" OFF)
2828
if(CBOR_CUSTOM_ALLOC)
2929
message(
3030
WARNING
31-
"CBOR_CUSTOM_ALLOC has been deprecated. \
31+
"CBOR_CUSTOM_ALLOC has been deprecated. \
3232
Custom allocators are now enabled by default. \
3333
The flag is a no-op and will be removed in the next version. \
3434
Please remove CBOR_CUSTOM_ALLOC from your build configuration.")
3535
endif()
3636

3737
option(CBOR_PRETTY_PRINTER "Include a pretty-printing routine" ON)
3838
set(CBOR_BUFFER_GROWTH
39-
"2"
40-
CACHE STRING "Factor for buffer growth & shrinking")
39+
"2"
40+
CACHE STRING "Factor for buffer growth & shrinking")
4141
set(CBOR_MAX_STACK_SIZE
42-
"2048"
43-
CACHE STRING "maximum size for decoding context stack")
42+
"2048"
43+
CACHE STRING "maximum size for decoding context stack")
4444

4545
option(WITH_TESTS "[TEST] Build unit tests (requires CMocka)" OFF)
4646
if(WITH_TESTS)
@@ -50,14 +50,14 @@ endif()
5050
option(WITH_EXAMPLES "Build examples" ON)
5151

5252
option(HUGE_FUZZ "[TEST] Fuzz through 8GB of data in the test.\
53-
Do not use with memory instrumentation!" OFF)
53+
Do not use with memory instrumentation!" OFF)
5454
if(HUGE_FUZZ)
5555
add_definitions(-DHUGE_FUZZ)
5656
endif()
5757

5858
option(SANE_MALLOC
59-
"[TEST] Assume that malloc will not allocate multi-GB blocks.\
60-
Tests only, platform specific" OFF)
59+
"[TEST] Assume that malloc will not allocate multi-GB blocks.\
60+
Tests only, platform specific" OFF)
6161
if(SANE_MALLOC)
6262
add_definitions(-DSANE_MALLOC)
6363
endif()
@@ -89,9 +89,9 @@ check_c_source_compiles("
8989
#include <stdio.h>
9090
[[nodiscard]] int f(void) { return 42; }
9191
int main(void) { return f(); }
92-
" HAS_NODISCARD_ATTRIBUTE)
92+
" HAS_NODISCARD_ATTRIBUTE)
9393

94-
if (HAS_NODISCARD_ATTRIBUTE)
94+
if(HAS_NODISCARD_ATTRIBUTE)
9595
message(STATUS "[[nodiscard]] is supported.")
9696
add_definitions(-D_CBOR_HAS_NODISCARD_ATTRIBUTE)
9797
# Assume that if we have [[nodiscard]], we have some C23 support. May fail.
@@ -129,12 +129,12 @@ else()
129129
set(CBOR_RESTRICT_SPECIFIER "restrict")
130130

131131
set(CMAKE_C_FLAGS_DEBUG
132-
"${CMAKE_C_FLAGS_DEBUG} -O0 -Wall -Wextra -g -ggdb -DDEBUG=true")
132+
"${CMAKE_C_FLAGS_DEBUG} -O0 -Wall -Wextra -g -ggdb -DDEBUG=true")
133133
set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -O3 -Wall -Wextra -DNDEBUG")
134134

135135
if(SANITIZE)
136136
set(CMAKE_C_FLAGS_DEBUG
137-
"${CMAKE_C_FLAGS_DEBUG} \
137+
"${CMAKE_C_FLAGS_DEBUG} \
138138
-fsanitize=undefined -fsanitize=address \
139139
-fsanitize=bounds -fsanitize=alignment")
140140
endif()
@@ -147,7 +147,7 @@ check_type_size("size_t" SIZEOF_SIZE_T)
147147
if(SIZEOF_SIZE_T LESS 8)
148148
message(
149149
WARNING
150-
"Your size_t is less than 8 bytes. \
150+
"Your size_t is less than 8 bytes. \
151151
Decoding of huge items that would exceed the memory address space \
152152
will always fail. Consider implementing a custom streaming \
153153
decoder if you need to deal with huge items.")
@@ -160,9 +160,9 @@ check_c_source_compiles("
160160
__builtin_unreachable();
161161
return 0;
162162
}
163-
" HAS_BUILTIN_UNREACHABLE)
163+
" HAS_BUILTIN_UNREACHABLE)
164164

165-
if (HAS_BUILTIN_UNREACHABLE)
165+
if(HAS_BUILTIN_UNREACHABLE)
166166
add_definitions(-D_CBOR_HAS_BUILTIN_UNREACHABLE)
167167
endif()
168168

@@ -172,7 +172,7 @@ endif()
172172
# mandatory with CMP0069 set to NEW.
173173
set(LTO_SUPPORTED FALSE)
174174
if(${CMAKE_VERSION} VERSION_GREATER "3.9.0" OR ${CMAKE_VERSION} VERSION_EQUAL
175-
"3.9.0")
175+
"3.9.0")
176176
cmake_policy(SET CMP0069 NEW)
177177
# Require LTO support to build libcbor with newer CMake versions
178178
include(CheckIPOSupported)
@@ -186,7 +186,7 @@ endif()
186186
if(LTO_SUPPORTED)
187187
message(
188188
STATUS
189-
"LTO is supported and CMAKE_INTERPROCEDURAL_OPTIMIZATION=${CMAKE_INTERPROCEDURAL_OPTIMIZATION}"
189+
"LTO is supported and CMAKE_INTERPROCEDURAL_OPTIMIZATION=${CMAKE_INTERPROCEDURAL_OPTIMIZATION}"
190190
)
191191
else()
192192
message(STATUS "LTO is not supported")
@@ -200,64 +200,65 @@ enable_testing()
200200

201201
set(CTEST_MEMORYCHECK_COMMAND "/usr/bin/valgrind")
202202
set(MEMORYCHECK_COMMAND_OPTIONS
203-
"--tool=memcheck --track-origins=yes --leak-check=full --error-exitcode=1")
204-
205-
add_custom_target(
206-
coverage
207-
COMMAND ctest
208-
COMMAND lcov --capture --directory . --output-file coverage.info
209-
COMMAND genhtml coverage.info --highlight --legend --output-directory
210-
coverage_html
211-
COMMAND
203+
"--tool=memcheck --track-origins=yes --leak-check=full --error-exitcode=1")
204+
205+
# TODO: Rename the option to be in line with the rest (CBOR_...)
206+
option(COVERAGE "Enable code coverage instrumentation" OFF)
207+
if(COVERAGE)
208+
add_custom_target(
209+
coverage
210+
COMMAND ctest
211+
COMMAND lcov --capture --directory . --output-file coverage.info
212+
COMMAND genhtml coverage.info --highlight --legend --output-directory
213+
coverage_html
214+
COMMAND
212215
echo
213216
"Coverage report ready: ${CMAKE_CURRENT_BINARY_DIR}/coverage_html/index.html"
214-
COMMENT "Generate coverage report using the GNU toolchain"
215-
)
216-
217-
add_custom_target(
218-
llvm-coverage
219-
COMMAND make -j 16
220-
COMMAND rm -rf coverage_profiles
221-
COMMAND mkdir coverage_profiles
222-
COMMAND
217+
COMMENT "Generate coverage report using the GNU toolchain"
218+
)
219+
220+
add_custom_target(
221+
llvm-coverage
222+
COMMAND make -j 16
223+
COMMAND rm -rf coverage_profiles
224+
COMMAND mkdir coverage_profiles
225+
COMMAND
223226
bash -c
224227
[[ for TEST in $(ls test/*_test); do LLVM_PROFILE_FILE="coverage_profiles/$(basename -- ${TEST}).profraw" ./${TEST}; done ]]
225-
# VERBATIM makes escaping working, but breaks shell expansions, so we need to
226-
# explicitly use bash
227-
COMMAND
228+
# VERBATIM makes escaping working, but breaks shell expansions, so we need to
229+
# explicitly use bash
230+
COMMAND
228231
bash -c
229232
[[ llvm-profdata merge -sparse $(ls coverage_profiles/*.profraw) -o coverage_profiles/combined.profdata ]]
230-
COMMAND
233+
COMMAND
231234
bash -c
232235
[[ llvm-cov show -instr-profile=coverage_profiles/combined.profdata test/*_test -format=html > coverage_profiles/report.html ]]
233-
COMMAND
236+
COMMAND
234237
bash -c
235238
[[ llvm-cov report -instr-profile=coverage_profiles/combined.profdata test/*_test ]]
236-
COMMAND
239+
COMMAND
237240
echo
238241
"Coverage report ready: ${CMAKE_CURRENT_BINARY_DIR}/coverage_profiles/report.html"
239-
VERBATIM
240-
COMMENT "Generate coverage report using the LLVM toolchain")
242+
VERBATIM
243+
COMMENT "Generate coverage report using the LLVM toolchain")
241244

242-
option(COVERAGE "Enable code coverage instrumentation" OFF)
243-
if(COVERAGE)
244245
message("Configuring code coverage instrumentation")
245246
if(CMAKE_C_COMPILER_ID MATCHES "GNU")
246247
# https://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html
247248
set(CMAKE_C_FLAGS
248-
"${CMAKE_C_FLAGS} -g -fprofile-arcs -ftest-coverage --coverage")
249+
"${CMAKE_C_FLAGS} -g -fprofile-arcs -ftest-coverage --coverage")
249250
set(CMAKE_EXE_LINKER_FLAGS_DEBUG
250-
"${CMAKE_EXE_LINKER_FLAGS_DEBUG} -g -fprofile-arcs -ftest-coverage --coverage"
251+
"${CMAKE_EXE_LINKER_FLAGS_DEBUG} -g -fprofile-arcs -ftest-coverage --coverage"
251252
)
252253
elseif(CMAKE_C_COMPILER_ID MATCHES "Clang")
253254
set(CMAKE_C_FLAGS
254-
"${CMAKE_C_FLAGS} -fprofile-instr-generate -fcoverage-mapping")
255+
"${CMAKE_C_FLAGS} -fprofile-instr-generate -fcoverage-mapping")
255256
set(CMAKE_EXE_LINKER_FLAGS_DEBUG
256-
"${CMAKE_EXE_LINKER_FLAGS_DEBUG} -fprofile-instr-generate")
257+
"${CMAKE_EXE_LINKER_FLAGS_DEBUG} -fprofile-instr-generate")
257258
else()
258259
message(
259260
WARNING
260-
"Code coverage build not implemented for compiler ${CMAKE_C_COMPILER_ID}"
261+
"Code coverage build not implemented for compiler ${CMAKE_C_COMPILER_ID}"
261262
)
262263
endif()
263264
endif()
@@ -272,9 +273,9 @@ include_directories(src)
272273
# is accessible using the same path during both library build and installed
273274
# header use, without littering the source dir.
274275
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/src/cbor/configuration.h.in
275-
${PROJECT_BINARY_DIR}/cbor/configuration.h)
276+
${PROJECT_BINARY_DIR}/cbor/configuration.h)
276277
install(FILES ${PROJECT_BINARY_DIR}/cbor/configuration.h
277-
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/cbor)
278+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/cbor)
278279

279280
add_subdirectory(src)
280281
if(LTO_SUPPORTED)

test/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@ foreach(test_file ${TESTS})
1212
string(REGEX REPLACE ".*/([^/]+).c" "\\1" NAME ${test_file})
1313
message("Adding test ${NAME}")
1414
add_executable(${NAME} "${NAME}.c" assertions.c stream_expectations.c
15-
test_allocator.c)
15+
test_allocator.c)
1616
target_link_libraries(${NAME} ${CMOCKA_LIBRARIES})
1717
target_link_libraries(${NAME} cbor)
1818
if(MATH_LIBRARY)
1919
target_link_libraries(${NAME} ${MATH_LIBRARY})
2020
endif()
2121
target_include_directories(${NAME} PUBLIC ${CMOCKA_INCLUDE_DIR})
2222
add_test(NAME ${NAME} COMMAND ${NAME})
23-
add_dependencies(coverage ${NAME})
23+
if(COVERAGE)
24+
add_dependencies(coverage ${NAME})
25+
endif()
2426
endforeach()
2527

2628
add_executable(cpp_linkage_test cpp_linkage_test.cpp)

0 commit comments

Comments
 (0)