cmake: Avoid use of non-target directives #1996

pull hebasto wants to merge 4 commits into bitcoin-core:master from hebasto:261113-non-target-directive changing 4 files +120 −66
  1. hebasto commented at 6:07 PM on November 13, 2026: member

    Use of non-target directives, such as add_compile_definitions(), include_directories() or add_compile_options(), is not recommended. As of CMake 4.5.0, it triggers -Wnon-target-directive warnings.

    For example:

    $ cmake -B build -Wnon-target-directive
    -- The C compiler identification is GNU 16.2.1
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Check for working C compiler: /usr/lib64/ccache/cc - skipped
    -- Detecting C compile features
    -- Detecting C compile features - done
    CMake Warning (non-target-directive) at CMakeLists.txt:66 (add_compile_definitions):
      Use of non-target directives is not recommended.  Consider
      target_compile_definitions instead.
    This warning is for project developers.  Use -Wno-author or
    -Wno-non-target-directive to suppress it.
    
    CMake Warning (non-target-directive) at CMakeLists.txt:78 (add_compile_definitions):
      Use of non-target directives is not recommended.  Consider
      target_compile_definitions instead.
    This warning is for project developers.  Use -Wno-author or
    -Wno-non-target-directive to suppress it.
    
    CMake Warning (non-target-directive) at CMakeLists.txt:79 (add_compile_definitions):
      Use of non-target directives is not recommended.  Consider
      target_compile_definitions instead.
    This warning is for project developers.  Use -Wno-author or
    -Wno-non-target-directive to suppress it.
    ...
    

    This issue affects downstream projects. For example, Bitcoin Core sets "errors": {"dev": true} in its "dev-mode" preset, which makes the configuration step fail.

    This PR introduces a project-wide interface library, following the approach used in Bitcoin Core.

  2. hebasto renamed this:
    261113 non target directive
    cmake: Avoid Use of non-target directives
    on Nov 13, 2026
  3. hebasto renamed this:
    cmake: Avoid Use of non-target directives
    cmake: Avoid use of non-target directives
    on Nov 13, 2026
  4. theStack added the label build on Nov 15, 2026
  5. fanquake referenced this in commit d55f55eda7 on Nov 18, 2026
  6. purpleKarrot commented at 1:39 PM on November 19, 2026: contributor

    Coming from https://github.com/bitcoin/bitcoin/pull/36890#issuecomment-7005606172

    Two observations about the proposed changes themselves:

    • $<BUILD_INTERFACE:secp256k1_common> keeps the target out of installation exports, but not out of build-tree exports created with export(). $<BUILD_LOCAL_INTERFACE:...> would be more appropriate here, but requires CMake 3.26. Given the current minimum version requirement, this is not necessarily a problem, but the comment is misleading.
    • The migration is not entirely behavior-preserving. Previously, the ENABLE_MODULE_* compile definitions were not visible to targets in the examples directory. With this change, they are. Examples should demonstrate how to consume the public interface of the library, not depend on its internal configuration.

    Beyond that, I have a more fundamental concern about the direction of the CMake code.

    I have been advocating against the now discouraged commands for almost ten years. For just as long, I have advocated for using namespaces in target names and against configuring compiler warnings in project definitions. I have also repeatedly pointed out that printing configuration summaries is confusing when the project is included in a superproject and particularly annoying when using ccmake, where it results in a dialog that has to be dismissed by pressing E after every configure run.

    The secp256k1 project has one artifact to produce: the secp256k1 library. Its build system should reflect that.

    • There is no need for a secp256k1_common interface library. Compile definitions such as ENABLE_MODULE_MUSIG=1 should be PRIVATE compile definitions of secp256k1.
    • secp256k1_precomputed, secp256k1_asm, and secp256k1_asm_arm should not be separate internal libraries. Their source files should simply be added to secp256k1 using target_sources().
    • Compiler warning policies belong in the build configuration, not in the project definition.

    I see no reason why the entire project should require a single generator expression.

    The fact that the maintainers of the source code are not particularly deep into CMake should be an additional motivation to keep the build system simple. Modern CMake is not about replacing old commands with more sophisticated constructs. It is about describing the artifacts to be built and their requirements as directly as possible.

    I appreciate being asked for a review, but it is somewhat frustrating to see the same recommendations being ignored for years, only to be consulted when the consequences of not following them become a problem.

  7. hebasto commented at 2:05 PM on November 19, 2026: member

    @purpleKarrot

    Coming from bitcoin/bitcoin#36890 (comment)

    Two observations about the proposed changes themselves: ... I appreciate being asked for a review, but it is somewhat frustrating to see the same recommendations being ignored for years, only to be consulted when the consequences of not following them become a problem.

    Thank you for the review.

    Some aspects of the current CMake scripts stem from the intention to make the new CMake-based build system comparable to the legacy Autotools-based one. In the short term, that decision was a win, but it comes with long-term drawbacks. These include the configuration summary, internal libraries and compiler warning policies. Each of these topics can be discussed in a separate issue or a dedicated pull request.

    The migration is not entirely behavior-preserving. Previously, the ENABLE_MODULE_* compile definitions were not visible to targets in the examples directory. With this change, they are. Examples should demonstrate how to consume the public interface of the library, not depend on its internal configuration.

    I'll look into that.

  8. hebasto commented at 2:19 PM on November 19, 2026: member

    Compile definitions such as ENABLE_MODULE_MUSIG=1 should be PRIVATE compile definitions of secp256k1.

    I don’t think that would work. For example, the exhaustive_tests target also needs those definitions.

  9. cmake: Introduce `secp256k1_common` interface library
    The new `secp256k1_common` interface library encapsulates usage
    requirements shared by all buildable targets. It is empty for
    now, so this change has no effect on the build. The following commits
    populate it.
    664cd159e6
  10. hebasto force-pushed on Nov 19, 2026
  11. hebasto commented at 2:50 PM on November 19, 2026: member
    • The migration is not entirely behavior-preserving. Previously, the ENABLE_MODULE_* compile definitions were not visible to targets in the examples directory. With this change, they are. Examples should demonstrate how to consume the public interface of the library, not depend on its internal configuration.

    Should be fixed now.

  12. real-or-random commented at 4:42 PM on November 19, 2026: contributor

    https://github.com/bitcoin-core/secp256k1/pull/1996/commits/1346a275530f63f5c4681509c681b5d0489195da

    I think now the benchmarks don't get the ENABLE_MODULE_* definitions. I guess secp256k1_modules should also be used for the benchmarks.

  13. hebasto commented at 4:58 PM on November 19, 2026: member

    1346a27

    I think now the benchmarks don't get the ENABLE_MODULE_* definitions. I guess secp256k1_modules should also be used for the benchmarks.

    The bench target uses it.

    bench_internal and bench_ecmult do not, because:

    $ git grep "ENABLE_MODULE_" -- src/bench_internal.c src/bench_ecmult.c | wc -l
    0
    
  14. real-or-random commented at 10:03 PM on November 19, 2026: contributor

    Sure, bench_internal.c and bench_ecmult.c don't directly use these macros, but they depend on these macros indirectly: both #include "secp256k1.c", which #includes the modules' main_impl.h depending on ENABLE_MODULE_*.

    And sure, this doesn't change what the benchmarks compute, since they don't call any module functions. But surprisingly (to me, at least) the compiler output changes. I had an LLM build bench_internal with GCC at -O2, both with and without the module definitions, and compare the binaries. The result:

    • Without the module definitions, the binary is ~120 KB smaller.
    • About 20 functions compile differently, including the benchmarked code. For example, without the modules, secp256k1_gej_add_var is inlined into bench_group_add_var; with them, it's an ordinary call.

    The reason is that the module code adds callers to static core functions, which changes GCC's inlining decisions. So, with this PR, the bench_internal and bench_ecmult numbers from a CMake build would no longer be comparable to the numbers from master or from Autotools.

    (Also, these inlining artifacts are bad for testing: the tests may exercise different machine code than what ends up in the library, depending on which modules happen to be compiled into the same unit. But that's out of scope for this PR.)

    More generally, and I think this is the more important point, I don't think whether a target currently checks a macro should decide whether it gets it. Every target that compiles library sources should see the same configuration, so that a later change to those sources can't silently get a different config. That also applies to secp256k1_precomputed. It doesn't check the module macros today, but it got them on master, and it gets them with Autotools.

    So I suggest linking secp256k1_modules in bench_internal, bench_ecmult and secp256k1_precomputed, and leaving only the examples without it, because they're supposed to use only the public API.

  15. real-or-random commented at 10:18 PM on November 19, 2026: contributor

    @purpleKarrot

    Well, some of the targets here are indeed a bit unusual. As @hebasto mentioned, the test targets also need the library's configuration: the tests, exhaustive_tests and the internal benchmarks don't link against the library. They #include "secp256k1.c" and compile the sources themselves to access internal functions. The tests also link the precomputed tables and the assembly code separately, which is why those are separate targets. So I may very well be the case that the structure can be simplified but probably not the extend you suggest (namely just one artifact).

    And enabling warnings and choosing optimization levels by default (e.g., -O2 instead of -O3 for Release, and keeping assertions on) is a deliberate choice here. The goal is that anyone who builds the library gets a reasonable and safe configuration (e.g., -Os and -O0 are more likely be unsafe due to issues with constant-time compilation). I agree that this is not common, and it has drawbacks, especially when the project is built inside a larger one. But I think in the end, it's a tradeoff, not a matter of right or wrong. And in a crypto library, I prefer erring on the side of caution.

    I have also repeatedly pointed out that printing configuration summaries is confusing when the project is included in a superproject and particularly annoying when using ccmake, where it results in a dialog that has to be dismissed by pressing E after every configure run.

    It's a bit off-topic but have we ever considered printing the summary only if PROJECT_IS_TOP_LEVEL? This could be a cheap improvement for superprojects.

  16. cmake: Switch `add_compile_definitions` -> `target_compile_definitions`
    Use of non-target directives is not recommended. It triggers
    `-Wnon-target-directive` warnings since CMake 4.5.0.
    827661a2ab
  17. cmake: Switch `include_directories` -> `target_include_directories`
    Use of non-target directives is not recommended. It triggers
    `-Wnon-target-directive` warnings since CMake 4.5.0.
    1b6ab424d4
  18. cmake: Switch `add_compile_options` -> `target_compile_options`
    Use of non-target directives is not recommended. It triggers
    `-Wnon-target-directive` warnings since CMake 4.5.0.
    f6394a0738
  19. hebasto force-pushed on Nov 19, 2026
  20. hebasto commented at 10:39 PM on November 19, 2026: member

    So I suggest linking secp256k1_modules in bench_internal, bench_ecmult and secp256k1_precomputed, and leaving only the examples without it, because they're supposed to use only the public API.

    Thanks! Done.

  21. real-or-random approved
  22. real-or-random commented at 10:56 PM on November 19, 2026: contributor

    ACK f6394a0738a9cd908af646b00ccac9a11ca53a1f

  23. real-or-random merged this on Nov 20, 2026
  24. real-or-random closed this on Nov 20, 2026

  25. real-or-random commented at 7:38 AM on November 20, 2026: contributor

    Merging this because it's low-risk and delaying causes issues in Bitcoin Core right now. @purpleKarrot Thanks for the quick review. Further tweaks (on this PR or the more general structure of the build system) can be handled in separate PRs.

  26. hebasto deleted the branch on Nov 20, 2026
  27. hebasto referenced this in commit b3dcf1931b on Nov 20, 2026
  28. fanquake referenced this in commit c3d1b4c0b3 on Nov 20, 2026
  29. hebasto referenced this in commit 50b4bf0a6a on Nov 20, 2026
  30. hebasto referenced this in commit a1f7b1040a on Nov 20, 2026
  31. hebasto referenced this in commit 624ebbf606 on Nov 20, 2026
  32. github-actions[bot] referenced this in commit 6fb808f76e on Nov 20, 2026

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: 2026-12-04 01:15 UTC

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