Skip to content

Commit

Permalink
build: allow static or shared but not both
Browse files Browse the repository at this point in the history
  • Loading branch information
theuni committed Mar 9, 2023
1 parent 4f72904 commit f96bd51
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 80 deletions.
12 changes: 5 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ set(CMAKE_C_EXTENSIONS OFF)

list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)

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

option(SECP256K1_ENABLE_MODULE_ECDH "Enable ECDH module." ON)
Expand Down Expand Up @@ -230,8 +229,7 @@ message("\n")
message("secp256k1 configure summary")
message("===========================")
message("Build artifacts:")
message(" shared library ...................... ${SECP256K1_BUILD_SHARED}")
message(" static library ...................... ${SECP256K1_BUILD_STATIC}")
message(" shared library ...................... ${BUILD_SHARED_LIBS}")
message("Optional modules:")
message(" ECDH ................................ ${SECP256K1_ENABLE_MODULE_ECDH}")
message(" ECDSA pubkey recovery ............... ${SECP256K1_ENABLE_MODULE_RECOVERY}")
Expand Down
15 changes: 5 additions & 10 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,22 @@ target_compile_options(example INTERFACE
target_link_libraries(example INTERFACE
$<$<PLATFORM_ID:Windows>:bcrypt>
)
if(SECP256K1_BUILD_SHARED)
target_link_libraries(example INTERFACE secp256k1)
elseif(SECP256K1_BUILD_STATIC)
target_link_libraries(example INTERFACE secp256k1_static)
if(MSVC)
target_link_options(example INTERFACE /IGNORE:4217)
endif()
if(NOT BUILD_SHARED_LIBS AND MSVC)
target_link_options(example INTERFACE /IGNORE:4217)
endif()

add_executable(ecdsa_example ecdsa.c)
target_link_libraries(ecdsa_example example)
target_link_libraries(ecdsa_example example secp_common_objs secp_precomputed_objs)
add_test(ecdsa_example ecdsa_example)

if(SECP256K1_ENABLE_MODULE_ECDH)
add_executable(ecdh_example ecdh.c)
target_link_libraries(ecdh_example example)
target_link_libraries(ecdh_example example secp_common_objs secp_precomputed_objs)
add_test(ecdh_example ecdh_example)
endif()

if(SECP256K1_ENABLE_MODULE_SCHNORRSIG)
add_executable(schnorr_example schnorr.c)
target_link_libraries(schnorr_example example)
target_link_libraries(schnorr_example example secp_common_objs secp_precomputed_objs)
add_test(schnorr_example schnorr_example)
endif()
104 changes: 41 additions & 63 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,107 +1,85 @@
# Must be included before CMAKE_INSTALL_INCLUDEDIR is used.
include(GNUInstallDirs)
set(${PROJECT_NAME}_installables "")

if(SECP256K1_ASM STREQUAL "arm")
add_library(common OBJECT
target_sources(secp_common_objs PRIVATE
asm/field_10x26_arm.s
)
set(common_obj "$<TARGET_OBJECTS:common>")
else()
set(common_obj "")
endif()

add_library(precomputed OBJECT
add_library(secp_precomputed_objs OBJECT EXCLUDE_FROM_ALL
precomputed_ecmult.c
precomputed_ecmult_gen.c
)
set(internal_obj "$<TARGET_OBJECTS:precomputed>" "${common_obj}")

add_library(secp256k1 SHARED EXCLUDE_FROM_ALL
secp256k1.c
${internal_obj}
add_library(secp_common_objs OBJECT EXCLUDE_FROM_ALL secp256k1.c)

add_library(secp_interface INTERFACE)
target_link_libraries(secp_common_objs PRIVATE secp_interface)
target_link_libraries(secp_precomputed_objs PRIVATE secp_interface)

# Add objects explicitly rather than linking to the object libs to keep them
# from being exported.
add_library(secp256k1 $<TARGET_OBJECTS:secp_precomputed_objs> $<TARGET_OBJECTS:secp_common_objs>)

target_compile_definitions(secp_interface INTERFACE
$<$<C_COMPILER_ID:MSVC>:_CRT_SECURE_NO_WARNINGS>
)
target_include_directories(secp256k1 INTERFACE

if (BUILD_SHARED_LIBS)
target_compile_definitions(secp_interface INTERFACE
$<$<PLATFORM_ID:Windows>:DLL_EXPORT>
)
endif()

# The object libs don't know if they're being built for a shared or static lib.
# Grab the PIC property from secp256k1 which knows.
get_target_property(use_pic secp256k1 POSITION_INDEPENDENT_CODE)
set_target_properties(secp_precomputed_objs PROPERTIES POSITION_INDEPENDENT_CODE ${use_pic})
set_target_properties(secp_common_objs PROPERTIES POSITION_INDEPENDENT_CODE ${use_pic})

target_include_directories(secp256k1 PUBLIC
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
target_compile_definitions(secp256k1 PRIVATE
$<$<PLATFORM_ID:Windows>:DLL_EXPORT>
)
set_target_properties(secp256k1 PROPERTIES
VERSION "${${PROJECT_NAME}_LIB_VERSION_CURRENT}.${${PROJECT_NAME}_LIB_VERSION_AGE}.${${PROJECT_NAME}_LIB_VERSION_REVISION}"
SOVERSION "${${PROJECT_NAME}_LIB_VERSION_CURRENT}"
)
if(SECP256K1_BUILD_SHARED)
get_target_property(use_pic secp256k1 POSITION_INDEPENDENT_CODE)
set_target_properties(precomputed PROPERTIES POSITION_INDEPENDENT_CODE ${use_pic})
set_target_properties(secp256k1 PROPERTIES EXCLUDE_FROM_ALL FALSE)
list(APPEND ${PROJECT_NAME}_installables secp256k1)
endif()

add_library(secp256k1_static STATIC EXCLUDE_FROM_ALL
secp256k1.c
${internal_obj}
)
target_include_directories(secp256k1_static INTERFACE
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
if(NOT MSVC)
set_target_properties(secp256k1_static PROPERTIES
OUTPUT_NAME secp256k1
)
endif()
if(SECP256K1_BUILD_STATIC)
set_target_properties(secp256k1_static PROPERTIES EXCLUDE_FROM_ALL FALSE)
list(APPEND ${PROJECT_NAME}_installables secp256k1_static)
endif()

add_library(binary_interface INTERFACE)
target_compile_definitions(binary_interface INTERFACE
$<$<C_COMPILER_ID:MSVC>:_CRT_SECURE_NO_WARNINGS>
)

add_library(link_library INTERFACE)
if(SECP256K1_BUILD_SHARED)
target_link_libraries(link_library INTERFACE secp256k1)
elseif(SECP256K1_BUILD_STATIC)
target_link_libraries(link_library INTERFACE secp256k1_static)
endif()

if(SECP256K1_BUILD_BENCHMARK)
add_executable(bench bench.c)
target_link_libraries(bench binary_interface link_library)
add_executable(bench_internal bench_internal.c ${internal_obj})
target_link_libraries(bench_internal binary_interface)
add_executable(bench_ecmult bench_ecmult.c ${internal_obj})
target_link_libraries(bench_ecmult binary_interface)
target_link_libraries(bench secp_common_objs secp_precomputed_objs)
add_executable(bench_internal bench_internal.c)
target_link_libraries(bench_internal secp_precomputed_objs)
add_executable(bench_ecmult bench_ecmult.c)
target_link_libraries(bench_ecmult secp_precomputed_objs)
endif()

if(SECP256K1_BUILD_TESTS)
add_executable(noverify_tests tests.c ${internal_obj})
target_link_libraries(noverify_tests binary_interface)
add_executable(noverify_tests tests.c)
target_link_libraries(noverify_tests secp_precomputed_objs)
add_test(noverify_tests noverify_tests)
if(NOT CMAKE_BUILD_TYPE STREQUAL "Coverage")
add_executable(tests tests.c ${internal_obj})
add_executable(tests tests.c)
target_compile_definitions(tests PRIVATE VERIFY)
target_link_libraries(tests binary_interface)
target_link_libraries(tests secp_precomputed_objs)
add_test(tests tests)
endif()
endif()

if(SECP256K1_BUILD_EXHAUSTIVE_TESTS)
# Note: do not include $<TARGET_OBJECTS:precomputed> in exhaustive_tests (it uses runtime-generated tables).
add_executable(exhaustive_tests tests_exhaustive.c ${common_obj})
# Note: do not include secp_precomputed_objs in exhaustive_tests (it uses runtime-generated tables).
add_executable(exhaustive_tests tests_exhaustive.c)
target_compile_definitions(exhaustive_tests PRIVATE $<$<NOT:$<CONFIG:Coverage>>:VERIFY>)
target_link_libraries(exhaustive_tests binary_interface)
add_test(exhaustive_tests exhaustive_tests)
endif()

if(SECP256K1_BUILD_CTIME_TESTS)
add_executable(ctime_tests ctime_tests.c)
target_link_libraries(ctime_tests binary_interface link_library)
target_link_libraries(ctime_tests secp_common_objs secp_precomputed_objs)
endif()

install(TARGETS ${${PROJECT_NAME}_installables}
install(TARGETS secp256k1
EXPORT ${PROJECT_NAME}-targets
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
Expand Down

0 comments on commit f96bd51

Please sign in to comment.