cmake: Avoid contaminating parent project’s cache with BUILD_SHARED_LIBS #1688

pull hebasto wants to merge 1 commits into bitcoin-core:master from hebasto:250619-cmake-shared changing 1 files +7 −4
  1. hebasto commented at 11:25 am on June 19, 2025: member

    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:

    0$ cmake -B build -DBUILD_KERNEL_LIB=ON
    

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

    0$ cmake --build build -t libbitcoinkernel
    1[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. Uses the SECP256K1_DISABLE_SHARED cache variable only when libsecp256k1 is included as a subproject.

    Alternatively, the entire code block can be dropped. In that case:

    1. The CMake-base build system will build a static library by default.
    2. During integration into a parent project, the static library can be forced as demonstrated here.
  2. cmake: Avoid contaminating parent project's cache with BUILD_SHARED_LIBS
    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. Uses the `SECP256K1_DISABLE_SHARED` cache variable only when
       libsecp256k1 is included as a subproject.
    533aea12f1
  3. hebasto commented at 11:25 am on June 19, 2025: member
    cc @theuni
  4. real-or-random added the label build on Jun 19, 2025
  5. real-or-random added the label refactor/smell on Jun 19, 2025

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin-core/secp256k1. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2025-06-28 19:15 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me