build: set build type and per-build-type flags as early as possible #31711

pull theuni wants to merge 2 commits into bitcoin:master from theuni:cmake-early-checks-flags changing 2 files +12 −12
  1. theuni commented at 7:05 pm on January 22, 2025: member

    This ensures that most compiler tests are not run with the wrong build type’s flags. The initial c++ checks are an exception to that because many internal CMake variables are unset until a language is selected, so it’s problematic to change our build type before that.

    The difference can be seen in build/CMakeFiles/CMakeConfigureLog.yaml. Before, Debug was used for many of the earlly checks. After this PR, it’s only the first 2 checks.

  2. DrahtBot commented at 7:05 pm on January 22, 2025: contributor

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    Code Coverage & Benchmarks

    For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/31711.

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    ACK hebasto
    Concept ACK BrandonOdiwuor

    If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #31723 (qa debug: Add –debugnode/-waitfordebugger [DRAFT] by hodlinator)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

  3. DrahtBot added the label Build system on Jan 22, 2025
  4. theuni commented at 7:06 pm on January 22, 2025: member
    Ping @hebasto. As discussed :)
  5. hebasto commented at 7:08 pm on January 22, 2025: member
    Concept ACK.
  6. in cmake/module/DefaultConfig.cmake:47 in 240ff782ad outdated
    42+  get_property(is_multi_config GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
    43+  if(is_multi_config)
    44+    get_property(help_string CACHE CMAKE_CONFIGURATION_TYPES PROPERTY HELPSTRING)
    45+    set(CMAKE_CONFIGURATION_TYPES "${all_configs}" CACHE STRING "${help_string}" FORCE)
    46+    # Also see https://gitlab.kitware.com/cmake/cmake/-/issues/19512.
    47+    set(CMAKE_TRY_COMPILE_CONFIGURATION "${config}" PARENT_SCOPE)
    


    hebasto commented at 7:17 pm on January 22, 2025:
    A note for reviewers: The CMAKE_TRY_COMPILE_CONFIGURATION variable is responsible for setting the build type in subsequent CMake tests for both single- and multi-config generators.
  7. BrandonOdiwuor commented at 1:18 pm on January 27, 2025: contributor
    Concept ACK
  8. fanquake added this to the milestone 29.0 on Jan 31, 2025
  9. in CMakeLists.txt:74 in 240ff782ad outdated
    69+
    70+# Set the default config before enabling c++ so the compiler is never run with
    71+# the wrong build type's flags.
    72+include(DefaultConfig)
    73+set_default_config(RelWithDebInfo)
    74+
    


    hebasto commented at 1:53 pm on February 3, 2025:

    Is the default build configuration really important for detecting the compiler and its capabilities?

    UPD. I agree with the changes. Just trying to clarify details.


    theuni commented at 3:40 pm on February 6, 2025:
    Not critically, no. I just figured it should for consistency. Is there a downside to this?
  10. hebasto commented at 1:53 pm on February 3, 2025: member

    Tested 240ff782ad5a8974aff35e78356520917926e525 on Ubuntu 24.10 with CMake 3.30.3.

    The CMAKE_BUILD_TYPE cache variable lost its help string:

    • on the master @ d7f56cc5d9e12ad31dd1ce8b34c3ff4ec5c1b70c:
    0$ cmake -B build -G "Ninja" -LH 2>&1 | grep -B 1 CMAKE_BUILD_TYPE:
    1// Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel ...
    2CMAKE_BUILD_TYPE:STRING=RelWithDebInfo
    
    • with this PR:
    0$ cmake -B build -G "Ninja" -LH 2>&1 | grep -B 1 CMAKE_BUILD_TYPE:
    1// 
    2CMAKE_BUILD_TYPE:STRING=RelWithDebInfo
    
  11. theuni commented at 3:45 pm on February 6, 2025: member

    The CMAKE_BUILD_TYPE cache variable lost its help string:

    Ugh, presumably because it’s enabling c++ that sets this?

    I guess that’s a downside, and there may be other side-effects as well. I suppose it’d be safer to do this after enabling c++, which means letting that check be done with the wrong build type. Which realistically shouldn’t ever be a problem. Agreed?

  12. hebasto commented at 3:49 pm on February 6, 2025: member

    The CMAKE_BUILD_TYPE cache variable lost its help string:

    Ugh, presumably because it’s enabling c++ that sets this?

    I guess that’s a downside, and there may be other side-effects as well. I suppose it’d be safer to do this after enabling c++, which means letting that check be done with the wrong build type. Which realistically shouldn’t ever be a problem. Agreed?

    Given #31711 (review), yes, I agree.

  13. build: refactor: set debug definitions in main CMakeLists
    No functional change. This is a simple move required the next commit.
    f605f7a9c2
  14. theuni force-pushed on Feb 6, 2025
  15. theuni commented at 8:29 pm on February 6, 2025: member
    Ok, updated description. It’s much simpler now and should be very straightforward to review.
  16. hebasto commented at 11:28 am on February 10, 2025: member

    I’d like to summarize here a few observations regarding CMake behaviour.

    1. Single-config generators, such as “Ninja”:
    • CMAKE_BUILD_TYPE is defined in enable_language().
    • Compiler checks in enable_language() ignore any configuration, unless CMAKE_TRY_COMPILE_CONFIGURATION is set beforehand.
    1. Single-config generators, such as “Ninja Multi-Config”:
    • CMAKE_CONFIGURATION_TYPES is defined in project().
    • Compiler checks in enable_language() use the “Debug” configuration (this choice may be generator-specific), unless CMAKE_TRY_COMPILE_CONFIGURATION is set before.

    Also see: https://gitlab.kitware.com/cmake/cmake/-/issues/26684.

  17. hebasto approved
  18. hebasto commented at 12:04 pm on February 10, 2025: member

    ACK 85c6bafe0b93ca180df3e0ec5c3f1c8d1fefbf9f, tested on Ubuntu 24.10 with the “Ninja” and “Ninja Multi-Config” generators.

    A single check still uses the default flags:https://github.com/bitcoin/bitcoin/blob/6a46be75c43e6489d7cb2aaea614ba552b31b35a/cmake/module/TryAppendCXXFlags.cmake#L125

    This issue can be addressed with the following patch:

     0--- a/cmake/module/ProcessConfigurations.cmake
     1+++ b/cmake/module/ProcessConfigurations.cmake
     2@@ -4,8 +4,6 @@
     3 
     4 include_guard(GLOBAL)
     5 
     6-include(TryAppendCXXFlags)
     7-
     8 macro(normalize_string string)
     9   string(REGEX REPLACE " +" " " ${string} "${${string}}")
    10   string(STRIP "${${string}}" ${string})
    11@@ -119,6 +117,8 @@ endfunction()
    12 
    13 set_default_config(RelWithDebInfo)
    14 
    15+include(TryAppendCXXFlags)
    16+
    17 # We leave assertions on.
    18 if(MSVC)
    19   remove_cxx_flag_from_all_configs(/DNDEBUG)
    
  19. DrahtBot requested review from BrandonOdiwuor on Feb 10, 2025
  20. build: set build type and per-build-type flags as early as possible
    With the exception of the first c++ checks, this ensures that compiler tests
    are never run with the wrong build type's flags.
    
    Co-Authored-By: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com>
    56a9b847bb
  21. theuni force-pushed on Feb 10, 2025
  22. theuni commented at 9:58 pm on February 10, 2025: member

    This issue can be addressed with the following patch:

    Yes, that’s great! Thanks. Applied and added you as co-author.

  23. hebasto approved
  24. hebasto commented at 10:41 am on February 11, 2025: member
    ACK 56a9b847bba2b8deb6a9c3f3a7eb95b4c71c2d14.
  25. fanquake merged this on Feb 12, 2025
  26. fanquake closed this on Feb 12, 2025


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2025-02-22 06:12 UTC

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