Skip to content

Commit 4b3bd66

Browse files
committed
cmake: Use SECP256K1_COVERAGE instead of CMAKE_BUILD_TYPE=Coverage
This change fixes coverage-enabled builds for multi-configuration generators, e.g., "Ninja Multi-Config".
1 parent 014e063 commit 4b3bd66

File tree

2 files changed

+20
-32
lines changed

2 files changed

+20
-32
lines changed

CMakeLists.txt

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,20 @@ if(SECP256K1_VALGRIND)
156156
endif()
157157

158158
option(SECP256K1_BUILD_BENCHMARK "Build benchmarks." ON)
159+
160+
include(CMakeDependentOption)
161+
cmake_dependent_option(SECP256K1_COVERAGE "Enable coverage analysis support." OFF "NOT MSVC" OFF)
162+
include(TryAppendCFlags)
163+
if(SECP256K1_COVERAGE)
164+
add_compile_definitions(COVERAGE)
165+
try_append_c_flags(-O0 --coverage)
166+
add_link_options(--coverage)
167+
endif()
168+
159169
option(SECP256K1_BUILD_TESTS "Build tests." ON)
160170
option(SECP256K1_BUILD_EXHAUSTIVE_TESTS "Build exhaustive tests." ON)
161171
option(SECP256K1_BUILD_CTIME_TESTS "Build constant-time tests." ${SECP256K1_VALGRIND})
172+
162173
option(SECP256K1_BUILD_EXAMPLES "Build examples." OFF)
163174

164175
include(ProcessConfigurations)
@@ -170,37 +181,14 @@ if(MSVC)
170181
remove_flag_from_all_configs(/DNDEBUG)
171182
else()
172183
remove_flag_from_all_configs(-DNDEBUG)
173-
# Prefer -O2 optimization level. (-O3 is CMake's default for Release for many compilers.)
174-
replace_flag_in_config(Release -O3 -O2)
175-
endif()
176-
177-
# Define custom "Coverage" build type.
178-
set(CMAKE_C_FLAGS_COVERAGE "${CMAKE_C_FLAGS_RELWITHDEBINFO} -O0 -DCOVERAGE=1 --coverage" CACHE STRING
179-
"Flags used by the C compiler during \"Coverage\" builds."
180-
FORCE
181-
)
182-
set(CMAKE_EXE_LINKER_FLAGS_COVERAGE "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} --coverage" CACHE STRING
183-
"Flags used for linking binaries during \"Coverage\" builds."
184-
FORCE
185-
)
186-
set(CMAKE_SHARED_LINKER_FLAGS_COVERAGE "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} --coverage" CACHE STRING
187-
"Flags used by the shared libraries linker during \"Coverage\" builds."
188-
FORCE
189-
)
190-
mark_as_advanced(
191-
CMAKE_C_FLAGS_COVERAGE
192-
CMAKE_EXE_LINKER_FLAGS_COVERAGE
193-
CMAKE_SHARED_LINKER_FLAGS_COVERAGE
194-
)
195-
196-
get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
197-
if(is_multi_config)
198-
list(APPEND CMAKE_CONFIGURATION_TYPES Coverage)
199-
else()
200-
set_property(CACHE CMAKE_BUILD_TYPE APPEND PROPERTY STRINGS Coverage)
184+
if(SECP256K1_COVERAGE)
185+
remove_flag_from_all_configs(-O[s0-9])
186+
else()
187+
# Prefer -O2 optimization level. (-O3 is CMake's default for Release for many compilers.)
188+
replace_flag_in_config(Release -O3 -O2)
189+
endif()
201190
endif()
202191

203-
include(TryAppendCFlags)
204192
if(MSVC)
205193
# Keep the following commands ordered lexicographically.
206194
try_append_c_flags(/W3) # Production quality warning level.
@@ -275,7 +263,7 @@ message("Optional binaries:")
275263
message(" benchmark ........................... ${SECP256K1_BUILD_BENCHMARK}")
276264
message(" noverify_tests ...................... ${SECP256K1_BUILD_TESTS}")
277265
set(tests_status "${SECP256K1_BUILD_TESTS}")
278-
if(CMAKE_BUILD_TYPE STREQUAL "Coverage")
266+
if(SECP256K1_COVERAGE)
279267
set(tests_status OFF)
280268
endif()
281269
message(" tests ............................... ${tests_status}")

src/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ if(SECP256K1_BUILD_TESTS)
8888
add_executable(noverify_tests tests.c)
8989
target_link_libraries(noverify_tests secp256k1_precomputed secp256k1_asm)
9090
add_test(NAME noverify_tests COMMAND noverify_tests)
91-
if(NOT CMAKE_BUILD_TYPE STREQUAL "Coverage")
91+
if(NOT SECP256K1_COVERAGE)
9292
add_executable(tests tests.c)
9393
target_compile_definitions(tests PRIVATE VERIFY)
9494
target_link_libraries(tests secp256k1_precomputed secp256k1_asm)
@@ -100,7 +100,7 @@ if(SECP256K1_BUILD_EXHAUSTIVE_TESTS)
100100
# Note: do not include secp256k1_precomputed in exhaustive_tests (it uses runtime-generated tables).
101101
add_executable(exhaustive_tests tests_exhaustive.c)
102102
target_link_libraries(exhaustive_tests secp256k1_asm)
103-
target_compile_definitions(exhaustive_tests PRIVATE $<$<NOT:$<CONFIG:Coverage>>:VERIFY>)
103+
target_compile_definitions(exhaustive_tests PRIVATE $<$<NOT:$<BOOL:${SECP256K1_COVERAGE}>>:VERIFY>)
104104
add_test(NAME exhaustive_tests COMMAND exhaustive_tests)
105105
endif()
106106

0 commit comments

Comments
 (0)