Skip to content

Commit 45f5606

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 22435de commit 45f5606

File tree

2 files changed

+20
-32
lines changed

2 files changed

+20
-32
lines changed

CMakeLists.txt

+18-30
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,20 @@ if(SECP256K1_VALGRIND)
145145
endif()
146146

147147
option(SECP256K1_BUILD_BENCHMARK "Build benchmarks." ON)
148+
149+
include(CMakeDependentOption)
150+
cmake_dependent_option(SECP256K1_COVERAGE "Enable coverage analysis support." OFF "NOT MSVC" OFF)
151+
include(TryAppendCFlags)
152+
if(SECP256K1_COVERAGE)
153+
add_compile_definitions(COVERAGE)
154+
try_append_c_flags(-O0 --coverage)
155+
add_link_options(--coverage)
156+
endif()
157+
148158
option(SECP256K1_BUILD_TESTS "Build tests." ON)
149159
option(SECP256K1_BUILD_EXHAUSTIVE_TESTS "Build exhaustive tests." ON)
150160
option(SECP256K1_BUILD_CTIME_TESTS "Build constant-time tests." ${SECP256K1_VALGRIND})
161+
151162
option(SECP256K1_BUILD_EXAMPLES "Build examples." OFF)
152163

153164
include(ProcessConfigurations)
@@ -159,37 +170,14 @@ if(MSVC)
159170
remove_flag_from_all_configs(/DNDEBUG)
160171
else()
161172
remove_flag_from_all_configs(-DNDEBUG)
162-
# Prefer -O2 optimization level. (-O3 is CMake's default for Release for many compilers.)
163-
replace_flag_in_config(Release -O3 -O2)
164-
endif()
165-
166-
# Define custom "Coverage" build type.
167-
set(CMAKE_C_FLAGS_COVERAGE "${CMAKE_C_FLAGS_RELWITHDEBINFO} -O0 -DCOVERAGE=1 --coverage" CACHE STRING
168-
"Flags used by the C compiler during \"Coverage\" builds."
169-
FORCE
170-
)
171-
set(CMAKE_EXE_LINKER_FLAGS_COVERAGE "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} --coverage" CACHE STRING
172-
"Flags used for linking binaries during \"Coverage\" builds."
173-
FORCE
174-
)
175-
set(CMAKE_SHARED_LINKER_FLAGS_COVERAGE "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} --coverage" CACHE STRING
176-
"Flags used by the shared libraries linker during \"Coverage\" builds."
177-
FORCE
178-
)
179-
mark_as_advanced(
180-
CMAKE_C_FLAGS_COVERAGE
181-
CMAKE_EXE_LINKER_FLAGS_COVERAGE
182-
CMAKE_SHARED_LINKER_FLAGS_COVERAGE
183-
)
184-
185-
get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
186-
if(is_multi_config)
187-
list(APPEND CMAKE_CONFIGURATION_TYPES Coverage)
188-
else()
189-
set_property(CACHE CMAKE_BUILD_TYPE APPEND PROPERTY STRINGS Coverage)
173+
if(SECP256K1_COVERAGE)
174+
remove_flag_from_all_configs(-O[s0-9])
175+
else()
176+
# Prefer -O2 optimization level. (-O3 is CMake's default for Release for many compilers.)
177+
replace_flag_in_config(Release -O3 -O2)
178+
endif()
190179
endif()
191180

192-
include(TryAppendCFlags)
193181
if(MSVC)
194182
# Keep the following commands ordered lexicographically.
195183
try_append_c_flags(/W2) # Moderate warning level.
@@ -259,7 +247,7 @@ message("Optional binaries:")
259247
message(" benchmark ........................... ${SECP256K1_BUILD_BENCHMARK}")
260248
message(" noverify_tests ...................... ${SECP256K1_BUILD_TESTS}")
261249
set(tests_status "${SECP256K1_BUILD_TESTS}")
262-
if(CMAKE_BUILD_TYPE STREQUAL "Coverage")
250+
if(SECP256K1_COVERAGE)
263251
set(tests_status OFF)
264252
endif()
265253
message(" tests ............................... ${tests_status}")

src/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ if(SECP256K1_BUILD_TESTS)
5454
add_executable(noverify_tests tests.c)
5555
target_link_libraries(noverify_tests secp256k1_precomputed secp256k1_asm)
5656
add_test(NAME noverify_tests COMMAND noverify_tests)
57-
if(NOT CMAKE_BUILD_TYPE STREQUAL "Coverage")
57+
if(NOT SECP256K1_COVERAGE)
5858
add_executable(tests tests.c)
5959
target_compile_definitions(tests PRIVATE VERIFY)
6060
target_link_libraries(tests secp256k1_precomputed secp256k1_asm)
@@ -66,7 +66,7 @@ if(SECP256K1_BUILD_EXHAUSTIVE_TESTS)
6666
# Note: do not include secp256k1_precomputed in exhaustive_tests (it uses runtime-generated tables).
6767
add_executable(exhaustive_tests tests_exhaustive.c)
6868
target_link_libraries(exhaustive_tests secp256k1_asm)
69-
target_compile_definitions(exhaustive_tests PRIVATE $<$<NOT:$<CONFIG:Coverage>>:VERIFY>)
69+
target_compile_definitions(exhaustive_tests PRIVATE $<$<NOT:$<BOOL:${SECP256K1_COVERAGE}>>:VERIFY>)
7070
add_test(NAME exhaustive_tests COMMAND exhaustive_tests)
7171
endif()
7272

0 commit comments

Comments
 (0)