Skip to content

cmake: incorrect assumption that Debug build type will not use optimisations #30800

@fanquake

Description

@fanquake

CMakeLists.txt currently states:

bitcoin/CMakeLists.txt

Lines 487 to 491 in 9cb9651

# When the build configuration is Debug, all optimizations are disabled.
# However, _FORTIFY_SOURCE requires that there is some level of optimization,
# otherwise it does nothing and just creates a compiler warning.
# Since _FORTIFY_SOURCE is a no-op without optimizations, do not enable it
# when the build configuration is Debug.

However that isn't always true. If, for example, depends is built with DEBUG=1, and CMake is configured with -DCMAKE_BUILD_TYPE=Debug, -O1 will actually be used. This is confusing, not just because the documentation is incorrect, but when making a change to switch the build type to Debug in oss-fuzz, it's caused unexpected results: google/oss-fuzz#12443 (comment).

Rather than assuming that builds with the Debug build type don't use optimisations, the check should be fixed to actually check what optimisation level is being used.

i.e:

make -C depends/ NO_QT=1 NO_WALLET=1 NO_ZMQ=1 NO_UPNP=1 -j17 DEBUG=1
cmake -B build --toolchain=/root/ci_scratch/depends/x86_64-pc-linux-gnu/toolchain.cmake -DCMAKE_BUILD_TYPE=Debug
<snip>
C++ compiler flags .................... -pipe -std=c++20 -O0 -ftrapv -O1 -g3

(-O1 is last, so is what will be used).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions