Skip to content

Conversation

hebasto
Copy link
Member

@hebasto hebasto commented Jun 19, 2025

The CMake cache is global in scope. Therefore, setting the standard cache variable BUILD_SHARED_LIBS can inadvertently affect the behavior of a parent project.

Consider configuring Bitcoin Core without explicit setting BUILD_SHARED_LIBS:

$ cmake -B build -DBUILD_KERNEL_LIB=ON

According to CMake’s documentation, this should configure libbitcoinkernel as STATIC.
However, that's not the case:

$ cmake --build build -t libbitcoinkernel
[143/143] Linking CXX shared library lib/libbitcoinkernel.so

This PR:

  1. Sets the BUILD_SHARED_LIBS cache variable only when libsecp256k1 is the top-level project.
  2. Removes the SECP256K1_DISABLE_SHARED cache variable. This enables parent projects that include libsecp256k1 as a subproject to rely solely on standard CMake variables for configuring the library type. During integration into a parent project, the static library can be forced as demonstrated here.

@hebasto
Copy link
Member Author

hebasto commented Jun 19, 2025

cc @theuni

@real-or-random
Copy link
Contributor

@theuni @purpleKarrot Can one of you review this?

@purpleKarrot
Copy link
Contributor

I'd rather drop the complete block of code. Every CMake project should behave well as a subproject. This becomes even more relevant with FetchContent getting more and more adoption. Not only should projects not interfere with superprojects (like setting BUILD_SHARED_LIBS or CMAKE_BUILD_TYPE in the cache), they should also not define their own settings that already have a default in CMake. Managing a superproject is much easier when you can rely on the assumption that projects can be controlled through standard CMake variables and you don't have to remember separate names like FOO_BUILD_SHARED, BAR_NO_SHARED, BAZ_DISABLE_STATIC, etc.

@real-or-random
Copy link
Contributor

The CMake cache is global in scope

Sigh, this is again confusing as hell (at least to me as someone who is not a CMake expert). Let me try to organize the discussion.

What is the cleanest thing to do in a library?

@hebasto:

According to CMake’s documentation, [...]

@purpleKarrot:

Not only should projects not interfere with superprojects (like setting BUILD_SHARED_LIBS or CMAKE_BUILD_TYPE in the cache) [...]

The docs say that top-level projects should call option(BUILD_SHARED_LIBS ...). Do we agree that, under the assumption that we are in control of both the top-level project and the subproject, this is only the second-best thing? And the best thing to do is to avoid setting this variable in the subproject?

Can we drop the entire block?

I might be wrong, but wouldn't dropping the option(BUILD_SHARED_LIBS ...) here mean that this variable can't be set on the command line using -D (because it's not even a cache variable then)? At least, this is how I read the docs.

@purpleKarrot:

they should also not define their own settings that already have a default in CMake.

The thing is that, according to the docs, BUILD_SHARED_LIBS does not have a default.

If we can drop the entire block, should we do it?

I'm not sure, but I tend to say no. I'd rather keep the default of building a shared library and accept a few lines of CMake code. If we decide to drop it, we should at least document this in the README.

Do we need SECP256K1_DISABLE_SHARED?

Managing a superproject is much easier when you can rely on the assumption that projects can be controlled through standard CMake variables and you don't have to remember separate names like FOO_BUILD_SHARED, BAR_NO_SHARED, BAZ_DISABLE_STATIC, etc.

I had to look this up. The reason why we have SECP256K1_DISABLE_SHARED is this:

@theuni in #1230 (comment):

A new BUILD_SHARED_LIBS option is added to match CMake convention, as well as a SECP256K1_DISABLE_SHARED option which overrides it. That way even projects which have BUILD_SHARED_LIBS=1 can opt-into a static libsecp in particular.

@purpleKarrot
Are you saying this is not necessary? (Real question, I really don't understand all of this stuff.)

@hebasto
Copy link
Member Author

hebasto commented Jul 18, 2025

The CMake cache is global in scope

Sigh, this is again confusing as hell (at least to me as someone who is not a CMake expert). Let me try to organize the discussion.

What is the cleanest thing to do in a library?

@hebasto:

According to CMake’s documentation, [...]

@purpleKarrot:

Not only should projects not interfere with superprojects (like setting BUILD_SHARED_LIBS or CMAKE_BUILD_TYPE in the cache) [...]

The docs say that top-level projects should call option(BUILD_SHARED_LIBS ...). Do we agree that, under the assumption that we are in control of both the top-level project and the subproject, this is only the second-best thing? And the best thing to do is to avoid setting this variable in the subproject?

This excerpt from the docs literally describes how fragile it is to set BUILD_SHARED_LIBS as a cache variable:

... if bringing external dependencies directly into the build ... and one of those dependencies has such a call to option(BUILD_SHARED_LIBS ...), the top level project must also call option(BUILD_SHARED_LIBS ...) before bringing in its dependencies.

Additionally, from Professional CMake: A Practical Guide 21st Edition, Section 40.2:

Another common problem is modifying variables that have the potential to affect the whole build, not just the project... Quite often, these variables [BUILD_SHARED_LIBS, ...] shouldn’t be defined by the project as cache variables at all...


Can we drop the entire block?

I might be wrong, but wouldn't dropping the option(BUILD_SHARED_LIBS ...) here mean that this variable can't be set on the command line using -D (because it's not even a cache variable then)? At least, this is how I read the docs.

When set using -D, the variable becomes a cache variable.

@purpleKarrot:

they should also not define their own settings that already have a default in CMake.

The thing is that, according to the docs, BUILD_SHARED_LIBS does not have a default.

The default state of BUILD_SHARED_LIBS is "undefined". And the behaviour of the add_library command is well-defined in this case.


If we can drop the entire block, should we do it?

I'm not sure, but I tend to say no. I'd rather keep the default of building a shared library and accept a few lines of CMake code. If we decide to drop it, we should at least document this in the README.

I agree with that. That's why I chose the suggested approach.

A shared library is a real library and should be considered as the default by-product of the build process.


Do we need SECP256K1_DISABLE_SHARED?

Managing a superproject is much easier when you can rely on the assumption that projects can be controlled through standard CMake variables and you don't have to remember separate names like FOO_BUILD_SHARED, BAR_NO_SHARED, BAZ_DISABLE_STATIC, etc.

I had to look this up. The reason why we have SECP256K1_DISABLE_SHARED is this:

@theuni in #1230 (comment):

A new BUILD_SHARED_LIBS option is added to match CMake convention, as well as a SECP256K1_DISABLE_SHARED option which overrides it. That way even projects which have BUILD_SHARED_LIBS=1 can opt-into a static libsecp in particular.

@purpleKarrot Are you saying this is not necessary? (Real question, I really don't understand all of this stuff.)

Using project-specific versions of well-known CMake variables is hard to maintain consistent. This project defines its own variable to override CMake's BUILD_SHARED_LIBS, but omits others such as CMAKE_EXPORT_COMPILE_COMMANDS. Specifying the library type is not the only concern when integrating into a parent project. In Bitcoin Core, we currently use an approach that encapsulates the entire integration logic within a function.

@purpleKarrot
Copy link
Contributor

Real question, I really don't understand all of this stuff.

I hear you, @real-or-random. That deserves a longer explanation. I will provide extensive information with examples on the weekend.

@purpleKarrot
Copy link
Contributor

@real-or-random
Copy link
Contributor

That's a great post!

What's your conclusion for this PR? From the post, it appears that you support the if(PROJECT_IS_TOP_LEVEL) approach as currently implemented in this PR. But you said above that you'd rather drop the complete block of code. Did you change your mind when writing the post?


A minor comment, not related to the discussion here:

It does not base the condition on GNUC, because it regards the necessary declaration as a property of the platform rather than a capability of the compiler.

The point of the __GNUC__ is to check that the compiler supports __attribute__((visibility("default"))) at all. You wouldn't want to set it on some non-GCC and non-Clang compiler.

@theuni
Copy link
Contributor

theuni commented Jul 22, 2025

The point of the __GNUC__ is to check that the compiler supports __attribute__((visibility("default"))) at all. You wouldn't want to set it on some non-GCC and non-Clang compiler.

Note that modern gcc/clang support has_attribute. It can be used like this:

#if defined(__has_attribute)
#  if __has_attribute(visibility)
#    define HAS_VISIBILITY_ATTRIBUTE
#  endif
#endif
#ifdef HAS_VISIBILITY_ATTRIBUTE
#  define DEFAULT_VISIBILITY_ATTRIBUTE __attribute__((visibility("default")))
#else
#  define DEFAULT_VISIBILITY_ATTRIBUTE
#endif

As illustrated here.

Of course to be useful for libsecp it'd need a fallback for older versions.

@purpleKarrot
Copy link
Contributor

That's a great post!

Thanks! It was a huge effort.

What's your conclusion for this PR? From the post, it appears that you support the if(PROJECT_IS_TOP_LEVEL) approach as currently implemented in this PR. But you said above that you'd rather drop the complete block of code. Did you change your mind when writing the post?

In my post, I show several approaches and possibilities.

Project Qux sets BUILD_SHARED_LIBS as an option based on Qux_IS_TOP_LEVEL, similar to this PR. I prefer to use project specific variables in CMakeLists.txt files and use generic variables like PROJECT_IS_TOP_LEVEL in generic code.

In project Bar, I show that no such option is actually needed for building with both BUILD_SHARED_LIBS=ON and BUILD_SHARED_LIBS=OFF.

In project Baz, I show that even with BUILD_SHARED_LIBS globally set to ON, it is possible to set it to a different value for a subproject, without there being a QUX_DISABLE_SHARED option.

For this PR, it means I would definitely drop the else part, and preferably the complete block of code.

@purpleKarrot
Copy link
Contributor

Note that modern gcc/clang support has_attribute.

Clang supports visibility since version 2.0 and __has_attribute since version 2.9. How should __has_attribute(visibility) ever be false? Maybe for targeting embedded platforms, but you would not want to build shared libraries for those.

I am aware of 1309c03, but I cannot tell whether there is a actual use case to support compilers older than bitcoin, or whether that change was motivated by because-we-can.

But yeah, that is off-topic for this PR. Please use nostr to comment on the blog post.

@real-or-random
Copy link
Contributor

real-or-random commented Jul 23, 2025

In project Baz, I show that even with BUILD_SHARED_LIBS globally set to ON, it is possible to set it to a different value for a subproject, without there being a QUX_DISABLE_SHARED option.

For this PR, it means I would definitely drop the else part, and preferably the complete block of code.

Ok, I get that we don't need SECP256K1_DISABLE_SHARED because the parent project can put BUILD_SHARED_LIBS in a block() (IIUC), so we don't need the "else" branch.

Maybe it's just me, but I still can't follow why you prefer dropping the "then" branch? I still think that defaulting to a shared library is a good idea.

Project Qux sets BUILD_SHARED_LIBS as an option based on Qux_IS_TOP_LEVEL, similar to this PR. I prefer to use project specific variables in CMakeLists.txt files and use generic variables like PROJECT_IS_TOP_LEVEL in generic code.

Okay, I wasn't aware of <PROJECT-NAME>_IS_TOP_LEVEL, and your convention makes sense to me.

CMakeLists.txt Outdated
Comment on lines 36 to 39
if(PROJECT_IS_TOP_LEVEL)
option(BUILD_SHARED_LIBS "Build shared libraries." ON)
else()
option(SECP256K1_DISABLE_SHARED "Disable shared library. Overrides BUILD_SHARED_LIBS." OFF)
if(SECP256K1_DISABLE_SHARED)
set(BUILD_SHARED_LIBS OFF)
endif()
endif()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So my current thinking is that we do this:

Suggested change
if(PROJECT_IS_TOP_LEVEL)
option(BUILD_SHARED_LIBS "Build shared libraries." ON)
else()
option(SECP256K1_DISABLE_SHARED "Disable shared library. Overrides BUILD_SHARED_LIBS." OFF)
if(SECP256K1_DISABLE_SHARED)
set(BUILD_SHARED_LIBS OFF)
endif()
endif()
if(libsecp256k1_IS_TOP_LEVEL)
option(BUILD_SHARED_LIBS "Build shared libraries." ON)
endif()

The CMake cache is global in scope. Therefore, setting the standard
cache variable `BUILD_SHARED_LIBS` can inadvertently affect the behavior
of a parent project.

This change:
1. Sets the `BUILD_SHARED_LIBS` cache variable only when libsecp256k1 is
   the top-level project.
2. Removes the `SECP256K1_DISABLE_SHARED` cache variable. This enables
   parent projects that include libsecp256k1 as a subproject to rely
   solely on standard CMake variables for configuring the library type.
@hebasto hebasto force-pushed the 250619-cmake-shared branch from 533aea1 to 7b07b22 Compare July 27, 2025 14:36
@hebasto
Copy link
Member Author

hebasto commented Jul 27, 2025

Reworked per discussion.

Copy link
Contributor

@theuni theuni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK 7b07b22

@real-or-random
Copy link
Contributor

@purpleKarrot Want to take a look at the updated version?

@purpleKarrot
Copy link
Contributor

ACK 7b07b22

@real-or-random real-or-random merged commit 20e3b44 into bitcoin-core:master Jul 30, 2025
116 checks passed
@hebasto hebasto deleted the 250619-cmake-shared branch July 30, 2025 12:07
fanquake added a commit to bitcoin/bitcoin that referenced this pull request Aug 6, 2025
b093a19 cmake: Proactively avoid use of `SECP256K1_DISABLE_SHARED` (Hennadii Stepanov)
eb59a19 cmake, refactor: Encapsulate adding secp256k1 subtree in function (Hennadii Stepanov)

Pull request description:

  The `SECP256K1_DISABLE_SHARED` CMake variable has been [removed](bitcoin-core/secp256k1#1688) upstream.

  This PR removes its usage ahead of the next `secp256k1` subtree update to prevent breakage and simplify integration.

ACKs for top commit:
  fanquake:
    ACK b093a19

Tree-SHA512: a87cee71cf356f458f68d3163253ca5c4f86e56d268006b6b8e1d4b2c009ba436148a07a6b67b89ddbb2d0e3c1113ab4b4906c5fc5624cb3082b20e916e0e82b
josibake added a commit to josibake/bitcoin that referenced this pull request Sep 5, 2025
aa85bfb530 docs: update README
9f42a30b82 ci: enable silentpayments module
d504e48145 tests: add sha256 tag test
124750d580 tests: add constant time tests
b35ffa2e30 tests: add BIP-352 test vectors
038c5b9c9d silentpayments: add benchmarks for scanning
88eb3d4545 silentpayments: add examples/silentpayments.c
22b20fd617 silentpayments: receiving
df1de93765 silentpayments: recipient label support
76a0451c76 silentpayments: sending
3cd3a93bff build: add skeleton for new silentpayments (BIP352) module
f36afb8b3d Merge bitcoin-core/secp256k1#1725: tests: refactor tagged hash verification
5153cf1c91 tests: refactor tagged hash tests
d2dcf52091 Merge bitcoin-core/secp256k1#1726: docs: fix broken link to Tromer's cache.pdf paper
489a43d1bf docs: fix broken link to eprint cache.pdf paper
d599714147 Merge bitcoin-core/secp256k1#1722: docs: Exclude modules' `bench_impl.h` headers from coverage report
0458def51e doc: Add `--gcov-ignore-parse-errors=all` option to `gcovr` invocations
1aecce5936 doc: Add `--merge-mode-functions=separate` option to `gcovr` invocations
106a7cbf41 doc: Exclude modules' `bench_impl.h` headers from coverage report
a9e955d3ea autotools, docs: Adjust help string for `--enable-coverage` option
e523e4f90e Merge bitcoin-core/secp256k1#1720: chore(ci): Fix typo in Dockerfile comment
24ba8ff168 chore(ci): Fix typo in Dockerfile comment
74b8068c5d Merge bitcoin-core/secp256k1#1717: test: update wycheproof test vectors
c25c3c8a88 test: update wycheproof test vectors
20e3b44746 Merge bitcoin-core/secp256k1#1688: cmake: Avoid contaminating parent project's cache with `BUILD_SHARED_LIBS`
2c076d907a Merge bitcoin-core/secp256k1#1711: tests: update Wycheproof
7b07b22957 cmake: Avoid contaminating parent project's cache with BUILD_SHARED_LIBS
5433648ca0 Fix typos and spellings
9ea54c69b7 tests: update Wycheproof files

git-subtree-dir: src/secp256k1
git-subtree-split: aa85bfb530b9ffc3dde6eaa7a976e129b8bd2f58
vmta added a commit to umkoin/umkoin that referenced this pull request Sep 21, 2025
36e76952c Merge bitcoin-core/secp256k1#1738: check-abi: remove support for obsolete CMake library output location (src/libsecp256k1.so)
4985ac0f8 Merge bitcoin-core/secp256k1#1737: doc: mention ctx requirement for `_ellswift_create` (not secp256k1_context_static)
7ebaa134a check-abi: remove support for obsolete CMake library output location (src/libsecp256k1.so)
806de38bf doc: mention ctx requirement for `_ellswift_create` (not secp256k1_context_static)
03fb60ad2 Merge bitcoin-core/secp256k1#1681: doc: Recommend clang-cl when building on Windows
d93380fb3 Merge bitcoin-core/secp256k1#1731: schnorrsig: Securely clear buf containing k or its negation
8113671f8 Merge bitcoin-core/secp256k1#1729: hash: Use size_t instead of int for RFC6979 outlen copy
325d65a8c Rename and clear var containing k or -k
960ba5f9c Use size_t instead of int for RFC6979 outlen copy
737912430 ci: Add more tests for clang-cl
7379a5bed doc: Recommend clang-cl when building on Windows
f36afb8b3 Merge bitcoin-core/secp256k1#1725: tests: refactor tagged hash verification
5153cf1c9 tests: refactor tagged hash tests
d2dcf5209 Merge bitcoin-core/secp256k1#1726: docs: fix broken link to Tromer's cache.pdf paper
489a43d1b docs: fix broken link to eprint cache.pdf paper
d59971414 Merge bitcoin-core/secp256k1#1722: docs: Exclude modules' `bench_impl.h` headers from coverage report
0458def51 doc: Add `--gcov-ignore-parse-errors=all` option to `gcovr` invocations
1aecce593 doc: Add `--merge-mode-functions=separate` option to `gcovr` invocations
106a7cbf4 doc: Exclude modules' `bench_impl.h` headers from coverage report
a9e955d3e autotools, docs: Adjust help string for `--enable-coverage` option
e523e4f90 Merge bitcoin-core/secp256k1#1720: chore(ci): Fix typo in Dockerfile comment
24ba8ff16 chore(ci): Fix typo in Dockerfile comment
74b8068c5 Merge bitcoin-core/secp256k1#1717: test: update wycheproof test vectors
c25c3c8a8 test: update wycheproof test vectors
20e3b4474 Merge bitcoin-core/secp256k1#1688: cmake: Avoid contaminating parent project's cache with `BUILD_SHARED_LIBS`
2c076d907 Merge bitcoin-core/secp256k1#1711: tests: update Wycheproof
7b07b2295 cmake: Avoid contaminating parent project's cache with BUILD_SHARED_LIBS
5433648ca Fix typos and spellings
9ea54c69b tests: update Wycheproof files
b9313c6e1 Merge bitcoin-core/secp256k1#1708: release cleanup: bump version after 0.7.0
a660a4976 Merge bitcoin-core/secp256k1#1707: release: Prepare for 0.7.0
7ab8b0cc0 release cleanup: bump version after 0.7.0
a3e742d94 release: Prepare for 0.7.0
f67b0ac1a ci: Don't hardcode ABI version
020ee6049 Merge bitcoin-core/secp256k1#1706: musig/tests: initialize keypair
cde413089 musig/tests: initialize keypair
6037833c9 Merge bitcoin-core/secp256k1#1702: changelog: update
40b4a0652 changelog: update
5e74086dc Merge bitcoin-core/secp256k1#1705: musig/test: Remove dead code
7c3380423 Merge bitcoin-core/secp256k1#1696: build: Refactor visibility logic and add override
8d967a602 musig/test: Remove dead code
983711cd6 musig/tests: Refactor vectors_signverify
73a695958 Merge bitcoin-core/secp256k1#1704: cmake: Make `secp256k1_objs` inherit interface defines from `secp256k1`
bf082221f cmake: Make `secp256k1_objs` inherit interface defines from `secp256k1`
c82d84bb8 build: add CMake option for disabling symbol visibility attributes
ce7923874 build: Add SECP256K1_NO_API_VISIBILITY_ATTRIBUTES
e5297f6d7 build: Refactor visibility logic
cbbbf3bd6 Merge bitcoin-core/secp256k1#1699: ci: enable musig module for native macOS arm64 job
943479a7a Merge bitcoin-core/secp256k1#1694: Revert "cmake: configure libsecp256k1.pc during install"
3352f9d66 ci: enable musig module for native macOS arm64 job
ad60ef7ea Merge bitcoin-core/secp256k1#1689: ci: Convert `arm64` Cirrus tasks to GHA jobs
c49877909 Merge bitcoin-core/secp256k1#1687: cmake: support the use of launchers in ctest -S scripts
44b205e9e Revert "cmake: configure libsecp256k1.pc during install"
0dfe387db cmake: support the use of launchers in ctest -S scripts
89096c234 Merge bitcoin-core/secp256k1#1692: cmake: configure libsecp256k1.pc during install
7106dce6f cmake: configure libsecp256k1.pc during install
29e73f4ba Merge bitcoin-core/secp256k1#1685: cmake: Emulate Libtool's behavior on FreeBSD
746e36b14 Merge bitcoin-core/secp256k1#1678: cmake: add a helper for linking into static libs
a28c2ffa5 Merge bitcoin-core/secp256k1#1683: README: add link to musig example
2a9d37473 Merge bitcoin-core/secp256k1#1690: ci: Bump GCC snapshot major version to 16
add146e10 ci: Bump GCC snapshot major version to 16
004f57fcd ci: Move Valgrind build for `arm64` from Cirrus to GHA
5fafdfc30 ci: Move `gcc-snapshot` build for `arm64` from Cirrus to GHA
e814b79a8 ci: Switch `arm64_debian` from QEMU to native `arm64` Docker image
bcf77346b ci: Add `arm64` architecture to `docker_cache` job
b77aae922 ci: Rename Docker image tag to reflect architecture
145ae3e28 cmake: add a helper for linking into static libs
819210974 README: add link to musig example, generalize module enabling hint
95db29b14 Merge bitcoin-core/secp256k1#1679: cmake: Use `PUBLIC_HEADER` target property in installation logic
37dd422b5 cmake: Emulate Libtool's behavior on FreeBSD
f24b838be Merge bitcoin-core/secp256k1#1680: doc: Promote "Building with CMake" to standard procedure
3f31ac43e doc: Promote "Building with CMake" to standard procedure
6f67151ee cmake: Use `PUBLIC_HEADER` target property
c32715b2a cmake, move-only: Move module option processing to `src/CMakeLists.txt`
201b2b8f0 Merge bitcoin-core/secp256k1#1675: cmake: Bump minimum required CMake version to 3.22
3af71987a cmake: Bump minimum required CMake version to 3.22
92394476e Merge bitcoin-core/secp256k1#1673: Assert field magnitude at control-flow join
3a4f448cb Assert field magnitude at control-flow join

git-subtree-dir: src/secp256k1
git-subtree-split: 36e76952cbf1cf54ddd2d8756cc31a486e2ba1d9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants