From 145ae3e28d587dd04866ec5893be9270bc99289a Mon Sep 17 00:00:00 2001 From: Cory Fields Date: Tue, 3 Jun 2025 18:17:33 +0000 Subject: [PATCH] cmake: add a helper for linking into static libs Parent projects (Bitcoin Core in this case) may wish to include secp256k1 in another static library (libbitcoinkernel) so that users are not forced to bring their own static libsecp256k1. Unfortunately, CMake lacks the machinery to link (combine) one static lib into another. To work around this, secp256k1_objs is exposed as an interface library which parent projects can "link" into static libs.. --- src/CMakeLists.txt | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index bed1b53303..291e0cbb8b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -54,6 +54,11 @@ add_library(secp256k1_precomputed OBJECT EXCLUDE_FROM_ALL # from being exported. target_sources(secp256k1 PRIVATE secp256k1.c $) +# Create a helper lib that parent projects can use to link secp256k1 into a +# static lib. +add_library(secp256k1_objs INTERFACE) +target_sources(secp256k1_objs INTERFACE $ $) + add_library(secp256k1_asm INTERFACE) if(SECP256K1_ASM STREQUAL "arm32") add_library(secp256k1_asm_arm OBJECT EXCLUDE_FROM_ALL) @@ -61,6 +66,7 @@ if(SECP256K1_ASM STREQUAL "arm32") asm/field_10x26_arm.s ) target_sources(secp256k1 PRIVATE $) + target_sources(secp256k1_objs INTERFACE $) target_link_libraries(secp256k1_asm INTERFACE secp256k1_asm_arm) endif() @@ -75,10 +81,13 @@ endif() get_target_property(use_pic secp256k1 POSITION_INDEPENDENT_CODE) set_target_properties(secp256k1_precomputed PROPERTIES POSITION_INDEPENDENT_CODE ${use_pic}) +# Add the include path for parent projects so that they don't have to manually add it. target_include_directories(secp256k1 INTERFACE - # Add the include path for parent projects so that they don't have to manually add it. $>:${PROJECT_SOURCE_DIR}/include>> ) +set_target_properties(secp256k1_objs PROPERTIES + INTERFACE_INCLUDE_DIRECTORIES "$" +) # This emulates Libtool to make sure Libtool and CMake agree on the ABI version, # see below "Calculate the version variables" in build-aux/ltmain.sh.