build: Fix checking compiler flags like `-Wno-some-warning` #1332

pull hebasto wants to merge 1 commits into bitcoin-core:master from hebasto:230530-wno changing 2 files +13 −2
  1. hebasto commented at 9:47 AM on May 30, 2023: member

    Some compilers (GCC) produce no diagnostic for -Wno-some-warning unless other diagnostics are being produced:

    $ git diff
    diff --git a/CMakeLists.txt b/CMakeLists.txt
    index 3540784..90b1e12 100644
    --- a/CMakeLists.txt
    +++ b/CMakeLists.txt
    @@ -243,6 +243,7 @@ if(CMAKE_C_COMPILER_ID STREQUAL "MSVC")
       try_append_c_flags(/wd4267 TARGET secp256k1_common) # Disable warning C4267 "'var' : conversion from 'size_t' to 'type', possible loss of data".
     else()
       # Keep the following commands ordered lexicographically.
    +  try_append_c_flags(-Wno-some-warning TARGET secp256k1_common)
       try_append_c_flags(-pedantic TARGET secp256k1_common)
       try_append_c_flags(-Wcast-align TARGET secp256k1_common) # GCC >= 2.95.
       try_append_c_flags(-Wcast-align=strict TARGET secp256k1_common) # GCC >= 8.0.
    $ cmake -B build -DCMAKE_C_COMPILER=gcc 2>&1 | grep -i some
    -- Performing Test C_SUPPORTS__WNO_SOME_WARNING
    -- Performing Test C_SUPPORTS__WNO_SOME_WARNING - Success
    Compile options ....................... -Wall -Wno-some-warning -pedantic -Wcast-align -Wcast-align=strict -Wextra -Wleading-whitespace=spaces -Wnested-externs -Wno-long-long -Wno-overlength-strings -Wno-unused-function -Wshadow -Wstrict-prototypes -Wtrailing-whitespace=any -Wundef
    

    This PR fixes the compiler flag check in the CMake-based build system:

    $ cmake -B build -DCMAKE_C_COMPILER=gcc 2>&1 | grep -i some
    -- Performing Test C_SUPPORTS__WNO_SOME_WARNING
    -- Performing Test C_SUPPORTS__WNO_SOME_WARNING - Failed
    

    The Autotools-based build system has been fixed as well.

  2. real-or-random added the label build on May 30, 2023
  3. fanquake commented at 4:40 PM on July 1, 2024: member

    Is this currently an issue for anything? If the check is broken, then it should be fixed before we start using it.

  4. real-or-random commented at 4:48 PM on July 1, 2024: contributor

    Is this currently an issue for anything? If the check is broken, then it should be fixed before we start using it.

    I think noone has run into this so far, but yeah, we should fix it.

    Concept ACK

  5. in cmake/TryAppendCFlags.cmake:16 in 1e97094795 outdated
       9 | @@ -10,7 +10,13 @@ function(secp256k1_check_c_flags_internal flags output)
      10 |  
      11 |    # This avoids running a linker.
      12 |    set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
      13 | -  check_c_compiler_flag("${flags}" ${result})
      14 | +
      15 | +  # Some compilers (GCC) produce no diagnostic for -Wno-unknown-warning
      16 | +  # unless other diagnostics are being produced. Therefore, test the
      17 | +  # -Wsome-warning case instead of the -Wno-some-warning one.
    


    real-or-random commented at 4:52 PM on July 1, 2024:
      # Some compilers (GCC) produce no diagnostic for -Wno-some-warning,
      # if "some-warning" is unknown to the compiler and no other diagnostics are being produced.
      #  Therefore, test the
      # -Wsome-warning case instead of the -Wno-some-warning one.
    

    Modulo paragraph formatting.

    This confused me a lot because I thought -Wno-unknown-warning is a real flag that says "Don't warn on unknown warnings flags". (Clang has a real flag -Wno-unknown-warning-option for this!)


    hebasto commented at 10:45 PM on July 1, 2024:

    Thanks! Reworked.

  6. hebasto commented at 10:37 PM on July 1, 2024: member

    Is this currently an issue for anything?

    The newest current option---Wno-overlength-strings--is supported for GCC >=4.2. Only someone using an older compiler would be affected.

    If the check is broken, then it should be fixed before we start using it.

    The PR description mentions two checks, one per build system. The check in the Autotools-based build system: https://github.com/bitcoin-core/secp256k1/blob/a5269373fa13ff845f654d81b90629dd78495641/build-aux/m4/bitcoin_secp.m4#L64-L78 is broken.

    The check in the CMake-based build system mirrored the behaviour of its Autotools' counterpart.

    Therefore, both checks need to be fixed.

  7. hebasto force-pushed on Jul 1, 2024
  8. hebasto marked this as ready for review on Jul 1, 2024
  9. hebasto force-pushed on Nov 20, 2026
  10. hebasto commented at 12:53 PM on November 20, 2026: member

    Rebased to resolve a conflict with merged bitcoin-core/secp256k1#1996.

  11. real-or-random commented at 9:46 AM on November 23, 2026: contributor

    My Concept ACK stands but I think we should do this for both supported build systems in a single PR to avoid drift

  12. hebasto force-pushed on Nov 23, 2026
  13. hebasto commented at 11:48 AM on November 23, 2026: member

    ... I think we should do this for both supported build systems in a single PR to avoid drift

    Sure. Done.

  14. hebasto renamed this:
    cmake: Fix checking compiler flags like `-Wno-some-warning`
    build: Fix checking compiler flags like `-Wno-some-warning`
    on Nov 23, 2026
  15. real-or-random requested review from Copilot on Nov 23, 2026
  16. ?
    copilot_work_started real-or-random
  17. Copilot commented at 1:10 PM on November 23, 2026: none

    <!-- ccr-overview-v2 -->

    🟢 Approval recommended

    No active review issues are blocking the warning-flag detection changes.

    No open issues 🎉

    <details> <summary><strong>What changed in this PR</strong></summary>

    Fixes warning-flag detection in the CMake and Autotools build systems so unsupported -Wno-* options are not mistakenly accepted by compilers such as GCC.

    Changes:

    • Checks the corresponding -W* option, which produces a diagnostic when unsupported.
    • Keeps the original -Wno-* option for compilation when the check succeeds.
    File Description
    cmake/​TryAppendCFlags.cmake Converts negative warning options to positive forms for CMake support checks.
    autotools-aux/​m4/​bitcoin_secp.m4 Applies the same conversion to Autotools compiler-flag checks.

    </details>

    <sub>Review effort: Balanced</sub>

  18. in cmake/TryAppendCFlags.cmake:27 in 33b89c6d4a
      23 | +  # Some compilers (GCC) produce no diagnostic for -Wno-some-warning,
      24 | +  # if "some-warning" is unknown to the compiler and no other diagnostics
      25 | +  # are being produced. Therefore, test the -Wsome-warning case instead
      26 | +  # of the -Wno-some-warning one.
      27 | +  string(REPLACE "-Wno-" "-W" non_negated_flags "${flags}")
      28 | +  check_c_compiler_flag("${non_negated_flags}" ${result})
    


    real-or-random commented at 11:04 AM on November 24, 2026:

    Suggestion that implements the approach from the review comment:

      # Some compilers (GCC) produce no diagnostic for -Wno-some-warning,
      # if "some-warning" is unknown to the compiler and no other diagnostics
      # are being produced. Therefore, test -Wsome-warning as well. Pass
      # -Wno-some-warning after it, so that the warning is disabled again and
      # cannot fire on the test program.
      string(REPLACE "-Wno-" "-W" non_negated_flags "${flags}")
      check_c_compiler_flag("${non_negated_flags} ${flags}" ${result})
    
  19. in autotools-aux/m4/bitcoin_secp.m4:73 in 33b89c6d4a
      65 | @@ -66,7 +66,11 @@ dnl Append flags to VAR if CC accepts them.
      66 |  AC_DEFUN([SECP_TRY_APPEND_CFLAGS], [
      67 |    AC_MSG_CHECKING([if ${CC} supports $1])
      68 |    SECP_TRY_APPEND_CFLAGS_saved_CFLAGS="$CFLAGS"
      69 | -  CFLAGS="$1 $CFLAGS"
      70 | +  dnl Some compilers (GCC) produce no diagnostic for -Wno-some-warning,
      71 | +  dnl if "some-warning" is unknown to the compiler and no other diagnostics
      72 | +  dnl are being produced. Therefore, test the -Wsome-warning case instead
      73 | +  dnl of the -Wno-some-warning one.
      74 | +  CFLAGS="m4_bpatsubst([$1], [-Wno-], [-W]) $CFLAGS"
    


    real-or-random commented at 11:04 AM on November 24, 2026:

    Same for Autotools:

      dnl Some compilers (GCC) produce no diagnostic for -Wno-some-warning,
      dnl if "some-warning" is unknown to the compiler and no other diagnostics
      dnl are being produced. Therefore, test -Wsome-warning as well. Pass
      dnl -Wno-some-warning after it, so that the warning is disabled again and
      dnl cannot fire on the test program.
      CFLAGS="m4_bpatsubst([$1], [-Wno-], [-W]) $1 $CFLAGS"
    
  20. real-or-random commented at 11:06 AM on November 24, 2026: contributor

    33b89c6d4a76172acfeea649b0cad1f96cf60365

    I thought about this again, and I changed my opinion. The PR in its current state fixes one potential problem but introduces another one, so I'm not convinced it's an improvement as is:

    • Fixed: GCC silently accepts an unknown -Wno-foo, so the check wrongly accepts it. None of the flags we currently use is affected.
    • Introduced: the check now compiles the test program with -Wfoo -Werror, so if warning foo fires on the test program itself, a supported -Wno-foo is wrongly rejected. For example, the Autotools check now rejects -Wno-missing-variable-declarations with both GCC and Clang, because -Wmissing-variable-declarations warns on char foo;. The CMake check, whose test program is int main(void) { return 0; }, accepts it, so the two build systems would silently diverge. Again, none of the flags we currently use is affected.

    Idea: We can avoid both problems by passing the positive flags followed by the original ones. An unknown -Wfoo is still an error, and the later -Wno-foo disables the warning again, so it cannot fire on the test program. I tested this in both build systems with GCC 16 and Clang 23: a bogus -Wno-some-warning is rejected, -Wno-missing-variable-declarations is accepted, and the resulting flags are otherwise unchanged. See the suggestions below.

  21. hebasto force-pushed on Nov 25, 2026
  22. hebasto commented at 12:44 PM on November 25, 2026: member
    • Introduced: the check now compiles the test program with -Wfoo -Werror, so if warning foo fires on the test program itself, a supported -Wno-foo is wrongly rejected. For example, the Autotools check now rejects -Wno-missing-variable-declarations with both GCC and Clang, because -Wmissing-variable-declarations warns on char foo;.

    The same underlying issue exists on the master branch as well, because the check rejects -Wmissing-variable-declarations, which is actually supported by the compiler. Ideally, the test code should be warning-free.

    Idea: We can avoid both problems by passing the positive flags followed by the original ones.

    Thanks! Taken.

  23. in cmake/TryAppendCFlags.cmake:28 in 2d1218e305
      23 | +  # if "some-warning" is unknown to the compiler and no other diagnostics
      24 | +  # are being produced. Therefore, test -Wsome-warning as well. Pass
      25 | +  # -Wno-some-warning after it, so that the warning is disabled again and
      26 | +  # cannot fire on the test program.
      27 | +  list(TRANSFORM flags REPLACE "^-Wno-(.+)$" "-W\\1;-Wno-\\1")
      28 |    check_c_compiler_flag("${flags}" ${result})
    


    real-or-random commented at 3:12 PM on November 25, 2026:

    list(TRANSFORM flags ...) modifies flags itself, so target_compile_options below now also gets the positive flags. With GCC, the summary shows -Wlong-long -Wno-long-long -Woverlength-strings -Wno-overlength-strings -Wunused-function -Wno-unused-function, and these pairs end up on every compiler command line. The effect is the same because the later flag wins, but it's noisy and differs from Autotools, which appends only the original flags. Transforming into a separate variable avoids this (tested with GCC and Clang, the compile options are then the same as on master):

      list(TRANSFORM flags REPLACE "^-Wno-(.+)$" "-W\\1;-Wno-\\1" OUTPUT_VARIABLE checked_flags)
      check_c_compiler_flag("${checked_flags}" ${result})
    

    hebasto commented at 3:34 PM on November 25, 2026:

    Thanks! Fixed.

  24. real-or-random commented at 3:16 PM on November 25, 2026: contributor

    2d1218e305df557b80a8767ebbea1a881df9e81a

    Autotools side looks good, tested with GCC 16 and Clang 23. The CMake side has one issue, see below.

    Ideally, the test code should be warning-free.

    Agreed. These old Autotools test programs violate modern best practices heavily... This could be done in a separate PR.

    (I can't resist to ask. What's your opinion on dropping Autotools? I tend to say I'd like to get rid of it, but it's hard to know what ours users actually need.)

  25. hebasto commented at 3:26 PM on November 25, 2026: member

    (I can't resist to ask. What's your opinion on dropping Autotools? I tend to say I'd like to get rid of it, but it's hard to know what ours users actually need.)

    I have some thoughts on that topic. I'm going to raise them in a public discussion soon.

  26. build: Fix checking compiler flags like `-Wno-some-warning`
    Co-authored-by: Tim Ruffing <me@real-or-random.org>
    dcddb90b44
  27. hebasto force-pushed on Nov 25, 2026
  28. real-or-random approved
  29. real-or-random commented at 3:39 PM on November 25, 2026: contributor

    ACK dcddb90b441eeec59cfd888016878d87153bbf20

  30. hebasto commented at 4:16 PM on November 27, 2026: member

    (I can't resist to ask. What's your opinion on dropping Autotools? I tend to say I'd like to get rid of it, but it's hard to know what ours users actually need.)

    I have some thoughts on that topic. I'm going to raise them in a public discussion soon.

    Please see #2012.


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-02 10:15 UTC

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