0
0
Fork 0
mirror of https://github.com/netdata/netdata.git synced 2025-04-14 09:38:34 +00:00
netdata_netdata/packaging/cmake/Modules/NetdataCompilerFlags.cmake
Austin S. Hemmelgarn 10815cb24a
Add LTO support in CMake build system. ()
* Add LTO support in CMake build system.

Internally, CMake calls LTO ‘Interprocedural Optimization’, and it
provides functionality for checking for support for this as well as
enabling it by default for targets. This leverages that support to
auto-detect LTO and enable it if it’s supported.

* Default to disabling LTO for Debug builds.

* Add handling for LTO to netdata installer code.

* Switch back to DISABLE_LTO as option name.

Using `ENABLE_LTO` leads to a possibility for confusion among users, as it
does behave in the most intuitive manner. Instead of ensuring that LTO
is used (and thus behaving like every other `ENABLE_*` option we have),
it allows the usage LTO if it’s supported. Thus a build with
`-DENABLE_LTO=True` may not actually be built with LTO.

By instead using `DISABLE_LTO`, the behavior matches up directly with
how most people are likely to interpret the meaning, because a build
with `-DDISABLE_LTO=True` will _never_ have LTO flags added to the
compiler/linker flags.

* Fix condition for determining default for DISABLE_LTO.

* Turn off LTO auto-detection in RPM package builds.

On pretty much all RPM platforms, the RPM build process itself will
correctly add the required compiler flags when building, so we don’t
actually need to auto-detect LTO support in CMake here.

Additionally, on at least some RPM platforms, CMake’s auto-detection
for LTO support actually breaks the build when used.

* Disable function and data sections when using LTO.

On at least some systems, `-fdata-sections` combined with LTO reliably
causes failures at link time with our code.

The final binary size on systems where the combination _works_ differs by
no more than a few KiB on average (tested on 64-bit x86 on Ubuntu 22.04,
Debian 12, Fedora 39, and Rocky Linux 9), so we’re not actually getting
almost any benefit out of using both with things as they are now, but
LTO gives us a meaasurable performance improvement that per-function and
per-data sections do not.

* Restructure in-line with current repo state.

* Disable LTO on Windows builds since it doesn’t work there.

* Fix compiler flag handling order.

* Switch LTO option name to USE_LTO for consistency with USE_MOLD.
2025-01-31 08:22:23 -05:00

144 lines
5.2 KiB
CMake

# SPDX-License-Identifier: GPL-3.0-or-later
# Functions to simplify handling of extra compiler flags.
include(CheckCCompilerFlag)
include(CheckCXXCompilerFlag)
# Construct a pre-processor safe name
#
# This takes a specified value, and assigns the generated name to the
# specified target.
function(make_cpp_safe_name value target)
string(REPLACE "-" "_" tmp "${value}")
string(REPLACE "=" "_" tmp "${tmp}")
set(${target} "${tmp}" PARENT_SCOPE)
endfunction()
# Conditionally add an extra compiler flag to C and C++ flags.
#
# If the language flags already match the `match` argument, skip this flag.
# Otherwise, check for support for `flag` and if support is found, add it to
# the compiler flags for the run.
function(add_simple_extra_compiler_flag match flag)
set(CMAKE_REQUIRED_FLAGS "-Werror")
make_cpp_safe_name("${flag}" flag_name)
if(NOT ${CMAKE_C_FLAGS} MATCHES ${match})
check_c_compiler_flag("${flag}" HAVE_C_${flag_name})
endif()
if(NOT ${CMAKE_CXX_FLAGS} MATCHES ${match})
check_cxx_compiler_flag("${flag}" HAVE_CXX_${flag_name})
endif()
if(HAVE_C_${flag_name} AND HAVE_CXX_${flag_name})
add_compile_options("${flag}")
add_link_options("${flag}")
endif()
endfunction()
# Same as add_simple_extra_compiler_flag, but check for a second flag if the
# first one is unsupported.
function(add_double_extra_compiler_flag match flag1 flag2)
set(CMAKE_REQUIRED_FLAGS "-Werror")
make_cpp_safe_name("${flag1}" flag1_name)
make_cpp_safe_name("${flag2}" flag2_name)
if(NOT ${CMAKE_C_FLAGS} MATCHES ${match})
check_c_compiler_flag("${flag1}" HAVE_C_${flag1_name})
if(NOT HAVE_C_${flag1_name})
check_c_compiler_flag("${flag2}" HAVE_C_${flag2_name})
endif()
endif()
if(NOT ${CMAKE_CXX_FLAGS} MATCHES ${match})
check_cxx_compiler_flag("${flag1}" HAVE_CXX_${flag1_name})
if(NOT HAVE_CXX_${flag1_name})
check_cxx_compiler_flag("${flag2}" HAVE_CXX_${flag2_name})
endif()
endif()
if(HAVE_C_${flag1_name} AND HAVE_CXX_${flag1_name})
add_compile_options("${flag1}")
add_link_options("${flag1}")
elseif(HAVE_C_${flag2_name} AND HAVE_CXX${flag2_name})
add_compile_options("${flag2}")
add_link_options("${flag2}")
endif()
endfunction()
# Add a required extra compiler flag to C and C++ flags.
#
# Similar logic as add_simple_extra_compiler_flag, but ignores existing
# instances and throws an error if the flag is not supported.
function(add_required_compiler_flag flag)
set(CMAKE_REQUIRED_FLAGS "-Werror")
make_cpp_safe_name("${flag}" flag_name)
check_c_compiler_flag("${flag}" HAVE_C_${flag_name})
check_cxx_compiler_flag("${flag}" HAVE_CXX_${flag_name})
if(HAVE_C_${flag_name} AND HAVE_CXX_${flag_name})
add_compile_options("${flag}")
add_link_options("${flag}")
else()
message(FATAL_ERROR "${flag} support is required to build Netdata")
endif()
endfunction()
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
option(DISABLE_HARDENING "Disable adding extra compiler flags for hardening" TRUE)
option(USE_LTO "Attempt to use of LTO when building. Defaults to being enabled if supported for release builds." FALSE)
else()
option(DISABLE_HARDENING "Disable adding extra compiler flags for hardening" FALSE)
option(USE_LTO "Attempt to use of LTO when building. Defaults to being enabled if supported for release builds." TRUE)
endif()
option(ENABLE_ADDRESS_SANITIZER "Build with address sanitizer enabled" False)
mark_as_advanced(ENABLE_ADDRESS_SANITIZER)
if(ENABLE_ADDRESS_SANITIZER)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address")
endif()
if(USE_LTO)
if(OS_WINDOWS)
message(WARNING "LTO not supported on Windows, not checking for it")
else()
include(CheckIPOSupported)
message(CHECK_START "Checking for LTO support")
check_ipo_supported(RESULT HAVE_LTO)
if(HAVE_LTO)
message(CHECK_PASS "supported")
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
else()
message(CHECK_FAIL "not supported")
endif()
endif()
else()
message(STATUS "Not checking for LTO support as it has been explicitly disabled")
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_C_FLAGS}")
add_required_compiler_flag("-fexceptions")
if(NOT ${DISABLE_HARDENING})
add_double_extra_compiler_flag("stack-protector" "-fstack-protector-strong" "-fstack-protector")
add_double_extra_compiler_flag("_FORTIFY_SOURCE" "-D_FORTIFY_SOURCE=3" "-D_FORTIFY_SOURCE=2")
add_simple_extra_compiler_flag("stack-clash-protection" "-fstack-clash-protection")
add_simple_extra_compiler_flag("-fcf-protection" "-fcf-protection=full")
add_simple_extra_compiler_flag("branch-protection" "-mbranch-protection=standard")
endif()
foreach(FLAG function-sections data-sections)
add_simple_extra_compiler_flag("${FLAG}" "-f${FLAG}")
endforeach()
add_simple_extra_compiler_flag("-Wbuiltin-macro-redefined" "-Wno-builtin-macro-redefined")
add_simple_extra_compiler_flag("-fexecptions" "-fexceptions")