Skip to content

Commit a55d948

Browse files
committed
Build: allow static or shared but not both
1 parent df323b5 commit a55d948

File tree

4 files changed

+60
-82
lines changed

4 files changed

+60
-82
lines changed

CMakeLists.txt

+5-7
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@ set(CMAKE_C_EXTENSIONS OFF)
2626

2727
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
2828

29-
# We do not use CMake's BUILD_SHARED_LIBS option.
30-
option(SECP256K1_BUILD_SHARED "Build shared library." ON)
31-
option(SECP256K1_BUILD_STATIC "Build static library." ON)
32-
if(NOT SECP256K1_BUILD_SHARED AND NOT SECP256K1_BUILD_STATIC)
33-
message(FATAL_ERROR "At least one of SECP256K1_BUILD_SHARED and SECP256K1_BUILD_STATIC must be enabled.")
29+
option(BUILD_SHARED_LIBS "Build shared libraries" ON)
30+
option(SECP256K1_DISABLE_SHARED "Disable shared library. Overrides BUILD_SHARED_LIBS." OFF)
31+
if (SECP256K1_DISABLE_SHARED)
32+
set(BUILD_SHARED_LIBS FALSE)
3433
endif()
3534

3635
option(SECP256K1_ENABLE_MODULE_ECDH "Enable ECDH module." ON)
@@ -230,8 +229,7 @@ message("\n")
230229
message("secp256k1 configure summary")
231230
message("===========================")
232231
message("Build artifacts:")
233-
message(" shared library ...................... ${SECP256K1_BUILD_SHARED}")
234-
message(" static library ...................... ${SECP256K1_BUILD_STATIC}")
232+
message(" shared library ...................... ${BUILD_SHARED_LIBS}")
235233
message("Optional modules:")
236234
message(" ECDH ................................ ${SECP256K1_ENABLE_MODULE_ECDH}")
237235
message(" ECDSA pubkey recovery ............... ${SECP256K1_ENABLE_MODULE_RECOVERY}")

examples/CMakeLists.txt

+5-10
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,22 @@ target_compile_options(example INTERFACE
88
target_link_libraries(example INTERFACE
99
$<$<PLATFORM_ID:Windows>:bcrypt>
1010
)
11-
if(SECP256K1_BUILD_SHARED)
12-
target_link_libraries(example INTERFACE ${PROJECT_NAME})
13-
elseif(SECP256K1_BUILD_STATIC)
14-
target_link_libraries(example INTERFACE ${PROJECT_NAME}_static)
15-
if(MSVC)
16-
target_link_options(example INTERFACE /IGNORE:4217)
17-
endif()
11+
if(NOT BUILD_SHARED_LIBS AND MSVC)
12+
target_link_options(example INTERFACE /IGNORE:4217)
1813
endif()
1914

2015
add_executable(ecdsa_example ecdsa.c)
21-
target_link_libraries(ecdsa_example example)
16+
target_link_libraries(ecdsa_example example secp_common_objs secp_precomputed_objs)
2217
add_test(ecdsa_example ecdsa_example)
2318

2419
if(SECP256K1_ENABLE_MODULE_ECDH)
2520
add_executable(ecdh_example ecdh.c)
26-
target_link_libraries(ecdh_example example)
21+
target_link_libraries(ecdh_example example secp_common_objs secp_precomputed_objs)
2722
add_test(ecdh_example ecdh_example)
2823
endif()
2924

3025
if(SECP256K1_ENABLE_MODULE_SCHNORRSIG)
3126
add_executable(schnorr_example schnorr.c)
32-
target_link_libraries(schnorr_example example)
27+
target_link_libraries(schnorr_example example secp_common_objs secp_precomputed_objs)
3328
add_test(schnorr_example schnorr_example)
3429
endif()

src/CMakeLists.txt

+44-65
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,86 @@
11
# Must be included before CMAKE_INSTALL_INCLUDEDIR is used.
22
include(GNUInstallDirs)
3-
set(${PROJECT_NAME}_installables "")
3+
4+
add_library(secp_precomputed_objs OBJECT EXCLUDE_FROM_ALL
5+
precomputed_ecmult.c
6+
precomputed_ecmult_gen.c
7+
)
8+
add_library(secp_common_objs OBJECT EXCLUDE_FROM_ALL secp256k1.c)
49

510
if(SECP256K1_ASM STREQUAL "arm")
6-
add_library(common OBJECT
11+
target_sources(secp_common_objs PRIVATE
712
asm/field_10x26_arm.s
813
)
9-
set(common_obj "$<TARGET_OBJECTS:common>")
10-
else()
11-
set(common_obj "")
1214
endif()
1315

14-
add_library(precomputed OBJECT
15-
precomputed_ecmult.c
16-
precomputed_ecmult_gen.c
16+
add_library(secp_interface INTERFACE)
17+
target_link_libraries(secp_precomputed_objs PRIVATE secp_interface)
18+
target_link_libraries(secp_common_objs PRIVATE secp_interface)
19+
20+
# A dummy file is included to make CMake happy
21+
add_library(${PROJECT_NAME} dummy.c)
22+
23+
target_link_libraries(${PROJECT_NAME}
24+
$<TARGET_OBJECTS:secp_common_objs>
25+
$<TARGET_OBJECTS:secp_precomputed_objs>
1726
)
18-
set(internal_obj "$<TARGET_OBJECTS:precomputed>" "${common_obj}")
1927

20-
add_library(${PROJECT_NAME} SHARED EXCLUDE_FROM_ALL
21-
secp256k1.c
22-
${internal_obj}
28+
get_target_property(use_pic ${PROJECT_NAME} POSITION_INDEPENDENT_CODE)
29+
set_target_properties(secp_precomputed_objs PROPERTIES POSITION_INDEPENDENT_CODE ${use_pic})
30+
set_target_properties(secp_common_objs PROPERTIES POSITION_INDEPENDENT_CODE ${use_pic})
31+
32+
target_compile_definitions(secp_interface INTERFACE
33+
$<$<C_COMPILER_ID:MSVC>:_CRT_SECURE_NO_WARNINGS>
2334
)
24-
target_include_directories(${PROJECT_NAME} INTERFACE
35+
36+
if (BUILD_SHARED_LIBS)
37+
target_compile_definitions(secp_common_objs PRIVATE
38+
$<$<PLATFORM_ID:Windows>:DLL_EXPORT>
39+
)
40+
endif()
41+
42+
target_include_directories(${PROJECT_NAME} PUBLIC
2543
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
2644
)
27-
target_compile_definitions(${PROJECT_NAME} PRIVATE
28-
$<$<PLATFORM_ID:Windows>:DLL_EXPORT>
29-
)
3045
set_target_properties(${PROJECT_NAME} PROPERTIES
3146
VERSION "${${PROJECT_NAME}_LIB_VERSION_CURRENT}.${${PROJECT_NAME}_LIB_VERSION_AGE}.${${PROJECT_NAME}_LIB_VERSION_REVISION}"
3247
SOVERSION "${${PROJECT_NAME}_LIB_VERSION_CURRENT}"
3348
)
34-
if(SECP256K1_BUILD_SHARED)
35-
get_target_property(use_pic ${PROJECT_NAME} POSITION_INDEPENDENT_CODE)
36-
set_target_properties(precomputed PROPERTIES POSITION_INDEPENDENT_CODE ${use_pic})
37-
set_target_properties(${PROJECT_NAME} PROPERTIES EXCLUDE_FROM_ALL FALSE)
38-
list(APPEND ${PROJECT_NAME}_installables ${PROJECT_NAME})
39-
endif()
40-
41-
add_library(${PROJECT_NAME}_static STATIC EXCLUDE_FROM_ALL
42-
secp256k1.c
43-
${internal_obj}
44-
)
45-
target_include_directories(${PROJECT_NAME}_static INTERFACE
46-
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
47-
)
48-
if(NOT MSVC)
49-
set_target_properties(${PROJECT_NAME}_static PROPERTIES
50-
OUTPUT_NAME ${PROJECT_NAME}
51-
)
52-
endif()
53-
if(SECP256K1_BUILD_STATIC)
54-
set_target_properties(${PROJECT_NAME}_static PROPERTIES EXCLUDE_FROM_ALL FALSE)
55-
list(APPEND ${PROJECT_NAME}_installables ${PROJECT_NAME}_static)
56-
endif()
57-
58-
add_library(binary_interface INTERFACE)
59-
target_compile_definitions(binary_interface INTERFACE
60-
$<$<C_COMPILER_ID:MSVC>:_CRT_SECURE_NO_WARNINGS>
61-
)
62-
63-
add_library(link_library INTERFACE)
64-
if(SECP256K1_BUILD_SHARED)
65-
target_link_libraries(link_library INTERFACE ${PROJECT_NAME})
66-
elseif(SECP256K1_BUILD_STATIC)
67-
target_link_libraries(link_library INTERFACE ${PROJECT_NAME}_static)
68-
endif()
6949

7050
if(SECP256K1_BUILD_BENCHMARK)
7151
add_executable(bench bench.c)
72-
target_link_libraries(bench binary_interface link_library)
73-
add_executable(bench_internal bench_internal.c ${internal_obj})
74-
target_link_libraries(bench_internal binary_interface)
75-
add_executable(bench_ecmult bench_ecmult.c ${internal_obj})
76-
target_link_libraries(bench_ecmult binary_interface)
52+
target_link_libraries(bench secp_common_objs secp_precomputed_objs)
53+
add_executable(bench_internal bench_internal.c)
54+
target_link_libraries(bench_internal secp_precomputed_objs)
55+
add_executable(bench_ecmult bench_ecmult.c)
56+
target_link_libraries(bench_ecmult secp_precomputed_objs)
7757
endif()
7858

7959
if(SECP256K1_BUILD_TESTS)
80-
add_executable(noverify_tests tests.c ${internal_obj})
81-
target_link_libraries(noverify_tests binary_interface)
60+
add_executable(noverify_tests tests.c)
61+
target_link_libraries(noverify_tests secp_precomputed_objs)
8262
add_test(noverify_tests noverify_tests)
8363
if(NOT CMAKE_BUILD_TYPE STREQUAL "Coverage")
84-
add_executable(tests tests.c ${internal_obj})
64+
add_executable(tests tests.c)
8565
target_compile_definitions(tests PRIVATE VERIFY)
86-
target_link_libraries(tests binary_interface)
66+
target_link_libraries(tests secp_precomputed_objs)
8767
add_test(tests tests)
8868
endif()
8969
endif()
9070

9171
if(SECP256K1_BUILD_EXHAUSTIVE_TESTS)
92-
# Note: do not include $<TARGET_OBJECTS:precomputed> in exhaustive_tests (it uses runtime-generated tables).
93-
add_executable(exhaustive_tests tests_exhaustive.c ${common_obj})
72+
# Note: do not include secp_precomputed_objs in exhaustive_tests (it uses runtime-generated tables).
73+
add_executable(exhaustive_tests tests_exhaustive.c)
9474
target_compile_definitions(exhaustive_tests PRIVATE $<$<NOT:$<CONFIG:Coverage>>:VERIFY>)
95-
target_link_libraries(exhaustive_tests binary_interface)
9675
add_test(exhaustive_tests exhaustive_tests)
9776
endif()
9877

9978
if(SECP256K1_BUILD_CTIME_TESTS)
10079
add_executable(ctime_tests ctime_tests.c)
101-
target_link_libraries(ctime_tests binary_interface link_library)
80+
target_link_libraries(ctime_tests secp_common_objs secp_precomputed_objs)
10281
endif()
10382

104-
install(TARGETS ${${PROJECT_NAME}_installables}
83+
install(TARGETS ${PROJECT_NAME}
10584
EXPORT ${PROJECT_NAME}-targets
10685
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
10786
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}

src/dummy.c

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*
2+
This file only exists because CMake can't create a lib out of other object
3+
libs without at least one source file included.
4+
*/
5+
6+
static void secp256k1_dummy(void){}

0 commit comments

Comments
 (0)