Skip to content

Commit

Permalink
cmake: Use SECP256K1_COVERAGE instead of CMAKE_BUILD_TYPE=Coverage
Browse files Browse the repository at this point in the history
This change fixes coverage-enabled builds for multi-configuration
generators, e.g., "Ninja Multi-Config".
  • Loading branch information
hebasto committed Jul 2, 2023
1 parent 014e063 commit 4b3bd66
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 32 deletions.
48 changes: 18 additions & 30 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,20 @@ if(SECP256K1_VALGRIND)
endif()

option(SECP256K1_BUILD_BENCHMARK "Build benchmarks." ON)

include(CMakeDependentOption)
cmake_dependent_option(SECP256K1_COVERAGE "Enable coverage analysis support." OFF "NOT MSVC" OFF)
include(TryAppendCFlags)
if(SECP256K1_COVERAGE)
add_compile_definitions(COVERAGE)
try_append_c_flags(-O0 --coverage)
add_link_options(--coverage)
endif()

option(SECP256K1_BUILD_TESTS "Build tests." ON)
option(SECP256K1_BUILD_EXHAUSTIVE_TESTS "Build exhaustive tests." ON)
option(SECP256K1_BUILD_CTIME_TESTS "Build constant-time tests." ${SECP256K1_VALGRIND})

option(SECP256K1_BUILD_EXAMPLES "Build examples." OFF)

include(ProcessConfigurations)
Expand All @@ -170,37 +181,14 @@ if(MSVC)
remove_flag_from_all_configs(/DNDEBUG)
else()
remove_flag_from_all_configs(-DNDEBUG)
# Prefer -O2 optimization level. (-O3 is CMake's default for Release for many compilers.)
replace_flag_in_config(Release -O3 -O2)
endif()

# Define custom "Coverage" build type.
set(CMAKE_C_FLAGS_COVERAGE "${CMAKE_C_FLAGS_RELWITHDEBINFO} -O0 -DCOVERAGE=1 --coverage" CACHE STRING
"Flags used by the C compiler during \"Coverage\" builds."
FORCE
)
set(CMAKE_EXE_LINKER_FLAGS_COVERAGE "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} --coverage" CACHE STRING
"Flags used for linking binaries during \"Coverage\" builds."
FORCE
)
set(CMAKE_SHARED_LINKER_FLAGS_COVERAGE "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} --coverage" CACHE STRING
"Flags used by the shared libraries linker during \"Coverage\" builds."
FORCE
)
mark_as_advanced(
CMAKE_C_FLAGS_COVERAGE
CMAKE_EXE_LINKER_FLAGS_COVERAGE
CMAKE_SHARED_LINKER_FLAGS_COVERAGE
)

get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
if(is_multi_config)
list(APPEND CMAKE_CONFIGURATION_TYPES Coverage)
else()
set_property(CACHE CMAKE_BUILD_TYPE APPEND PROPERTY STRINGS Coverage)
if(SECP256K1_COVERAGE)
remove_flag_from_all_configs(-O[s0-9])
else()
# Prefer -O2 optimization level. (-O3 is CMake's default for Release for many compilers.)
replace_flag_in_config(Release -O3 -O2)
endif()
endif()

include(TryAppendCFlags)
if(MSVC)
# Keep the following commands ordered lexicographically.
try_append_c_flags(/W3) # Production quality warning level.
Expand Down Expand Up @@ -275,7 +263,7 @@ message("Optional binaries:")
message(" benchmark ........................... ${SECP256K1_BUILD_BENCHMARK}")
message(" noverify_tests ...................... ${SECP256K1_BUILD_TESTS}")
set(tests_status "${SECP256K1_BUILD_TESTS}")
if(CMAKE_BUILD_TYPE STREQUAL "Coverage")
if(SECP256K1_COVERAGE)
set(tests_status OFF)
endif()
message(" tests ............................... ${tests_status}")
Expand Down
4 changes: 2 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ if(SECP256K1_BUILD_TESTS)
add_executable(noverify_tests tests.c)
target_link_libraries(noverify_tests secp256k1_precomputed secp256k1_asm)
add_test(NAME noverify_tests COMMAND noverify_tests)
if(NOT CMAKE_BUILD_TYPE STREQUAL "Coverage")
if(NOT SECP256K1_COVERAGE)
add_executable(tests tests.c)
target_compile_definitions(tests PRIVATE VERIFY)
target_link_libraries(tests secp256k1_precomputed secp256k1_asm)
Expand All @@ -100,7 +100,7 @@ if(SECP256K1_BUILD_EXHAUSTIVE_TESTS)
# Note: do not include secp256k1_precomputed in exhaustive_tests (it uses runtime-generated tables).
add_executable(exhaustive_tests tests_exhaustive.c)
target_link_libraries(exhaustive_tests secp256k1_asm)
target_compile_definitions(exhaustive_tests PRIVATE $<$<NOT:$<CONFIG:Coverage>>:VERIFY>)
target_compile_definitions(exhaustive_tests PRIVATE $<$<NOT:$<BOOL:${SECP256K1_COVERAGE}>>:VERIFY>)
add_test(NAME exhaustive_tests COMMAND exhaustive_tests)
endif()

Expand Down

0 comments on commit 4b3bd66

Please sign in to comment.