Skip to content

Commit eb59a19

Browse files
committed
cmake, refactor: Encapsulate adding secp256k1 subtree in function
1 parent 4f27e8c commit eb59a19

File tree

2 files changed

+52
-48
lines changed

2 files changed

+52
-48
lines changed

cmake/secp256k1.cmake

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright (c) 2023-present The Bitcoin Core developers
2+
# Distributed under the MIT software license, see the accompanying
3+
# file COPYING or https://opensource.org/license/mit/.
4+
5+
function(add_secp256k1 subdir)
6+
message("")
7+
message("Configuring secp256k1 subtree...")
8+
set(SECP256K1_DISABLE_SHARED ON CACHE BOOL "" FORCE)
9+
set(SECP256K1_ENABLE_MODULE_ECDH OFF CACHE BOOL "" FORCE)
10+
set(SECP256K1_ENABLE_MODULE_RECOVERY ON CACHE BOOL "" FORCE)
11+
set(SECP256K1_ENABLE_MODULE_MUSIG ON CACHE BOOL "" FORCE)
12+
set(SECP256K1_BUILD_BENCHMARK OFF CACHE BOOL "" FORCE)
13+
set(SECP256K1_BUILD_TESTS ${BUILD_TESTS} CACHE BOOL "" FORCE)
14+
set(SECP256K1_BUILD_EXHAUSTIVE_TESTS ${BUILD_TESTS} CACHE BOOL "" FORCE)
15+
if(NOT BUILD_TESTS)
16+
# Always skip the ctime tests, if we are building no other tests.
17+
# Otherwise, they are built if Valgrind is available. See SECP256K1_VALGRIND.
18+
set(SECP256K1_BUILD_CTIME_TESTS ${BUILD_TESTS} CACHE BOOL "" FORCE)
19+
endif()
20+
set(SECP256K1_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
21+
include(GetTargetInterface)
22+
# -fsanitize and related flags apply to both C++ and C,
23+
# so we can pass them down to libsecp256k1 as CFLAGS and LDFLAGS.
24+
get_target_interface(SECP256K1_APPEND_CFLAGS "" sanitize_interface COMPILE_OPTIONS)
25+
string(STRIP "${SECP256K1_APPEND_CFLAGS} ${APPEND_CPPFLAGS}" SECP256K1_APPEND_CFLAGS)
26+
string(STRIP "${SECP256K1_APPEND_CFLAGS} ${APPEND_CFLAGS}" SECP256K1_APPEND_CFLAGS)
27+
set(SECP256K1_APPEND_CFLAGS ${SECP256K1_APPEND_CFLAGS} CACHE STRING "" FORCE)
28+
get_target_interface(SECP256K1_APPEND_LDFLAGS "" sanitize_interface LINK_OPTIONS)
29+
string(STRIP "${SECP256K1_APPEND_LDFLAGS} ${APPEND_LDFLAGS}" SECP256K1_APPEND_LDFLAGS)
30+
set(SECP256K1_APPEND_LDFLAGS ${SECP256K1_APPEND_LDFLAGS} CACHE STRING "" FORCE)
31+
# We want to build libsecp256k1 with the most tested RelWithDebInfo configuration.
32+
enable_language(C)
33+
foreach(config IN LISTS CMAKE_BUILD_TYPE CMAKE_CONFIGURATION_TYPES)
34+
if(config STREQUAL "")
35+
continue()
36+
endif()
37+
string(TOUPPER "${config}" config)
38+
set(CMAKE_C_FLAGS_${config} "${CMAKE_C_FLAGS_RELWITHDEBINFO}")
39+
endforeach()
40+
# If the CFLAGS environment variable is defined during building depends
41+
# and configuring this build system, its content might be duplicated.
42+
if(DEFINED ENV{CFLAGS})
43+
deduplicate_flags(CMAKE_C_FLAGS)
44+
endif()
45+
set(CMAKE_EXPORT_COMPILE_COMMANDS OFF)
46+
add_subdirectory(${subdir})
47+
set_target_properties(secp256k1 PROPERTIES
48+
EXCLUDE_FROM_ALL TRUE
49+
)
50+
endfunction()

src/CMakeLists.txt

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -23,54 +23,8 @@ if (ENABLE_IPC AND NOT WITH_EXTERNAL_LIBMULTIPROCESS)
2323
include(../cmake/libmultiprocess.cmake)
2424
add_libmultiprocess(ipc/libmultiprocess)
2525
endif()
26-
#=============================
27-
# secp256k1 subtree
28-
#=============================
29-
message("")
30-
message("Configuring secp256k1 subtree...")
31-
set(SECP256K1_DISABLE_SHARED ON CACHE BOOL "" FORCE)
32-
set(SECP256K1_ENABLE_MODULE_ECDH OFF CACHE BOOL "" FORCE)
33-
set(SECP256K1_ENABLE_MODULE_RECOVERY ON CACHE BOOL "" FORCE)
34-
set(SECP256K1_ENABLE_MODULE_MUSIG ON CACHE BOOL "" FORCE)
35-
set(SECP256K1_BUILD_BENCHMARK OFF CACHE BOOL "" FORCE)
36-
set(SECP256K1_BUILD_TESTS ${BUILD_TESTS} CACHE BOOL "" FORCE)
37-
set(SECP256K1_BUILD_EXHAUSTIVE_TESTS ${BUILD_TESTS} CACHE BOOL "" FORCE)
38-
if(NOT BUILD_TESTS)
39-
# Always skip the ctime tests, if we are building no other tests.
40-
# Otherwise, they are built if Valgrind is available. See SECP256K1_VALGRIND.
41-
set(SECP256K1_BUILD_CTIME_TESTS ${BUILD_TESTS} CACHE BOOL "" FORCE)
42-
endif()
43-
set(SECP256K1_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
44-
include(GetTargetInterface)
45-
# -fsanitize and related flags apply to both C++ and C,
46-
# so we can pass them down to libsecp256k1 as CFLAGS and LDFLAGS.
47-
get_target_interface(SECP256K1_APPEND_CFLAGS "" sanitize_interface COMPILE_OPTIONS)
48-
string(STRIP "${SECP256K1_APPEND_CFLAGS} ${APPEND_CPPFLAGS}" SECP256K1_APPEND_CFLAGS)
49-
string(STRIP "${SECP256K1_APPEND_CFLAGS} ${APPEND_CFLAGS}" SECP256K1_APPEND_CFLAGS)
50-
set(SECP256K1_APPEND_CFLAGS ${SECP256K1_APPEND_CFLAGS} CACHE STRING "" FORCE)
51-
get_target_interface(SECP256K1_APPEND_LDFLAGS "" sanitize_interface LINK_OPTIONS)
52-
string(STRIP "${SECP256K1_APPEND_LDFLAGS} ${APPEND_LDFLAGS}" SECP256K1_APPEND_LDFLAGS)
53-
set(SECP256K1_APPEND_LDFLAGS ${SECP256K1_APPEND_LDFLAGS} CACHE STRING "" FORCE)
54-
# We want to build libsecp256k1 with the most tested RelWithDebInfo configuration.
55-
enable_language(C)
56-
foreach(config IN LISTS CMAKE_BUILD_TYPE CMAKE_CONFIGURATION_TYPES)
57-
if(config STREQUAL "")
58-
continue()
59-
endif()
60-
string(TOUPPER "${config}" config)
61-
set(CMAKE_C_FLAGS_${config} "${CMAKE_C_FLAGS_RELWITHDEBINFO}")
62-
endforeach()
63-
# If the CFLAGS environment variable is defined during building depends
64-
# and configuring this build system, its content might be duplicated.
65-
if(DEFINED ENV{CFLAGS})
66-
deduplicate_flags(CMAKE_C_FLAGS)
67-
endif()
68-
set(CMAKE_EXPORT_COMPILE_COMMANDS OFF)
69-
add_subdirectory(secp256k1)
70-
set_target_properties(secp256k1 PROPERTIES
71-
EXCLUDE_FROM_ALL TRUE
72-
)
73-
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
26+
include(../cmake/secp256k1.cmake)
27+
add_secp256k1(secp256k1)
7428

7529
# Set top-level target output locations.
7630
if(NOT CMAKE_RUNTIME_OUTPUT_DIRECTORY)

0 commit comments

Comments
 (0)