mirror of
https://github.com/netdata/netdata.git
synced 2025-04-07 06:45:39 +00:00
Add LTO support in CMake build system. (#17027)
* 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.
This commit is contained in:
parent
34236bffa4
commit
10815cb24a
5 changed files with 40 additions and 8 deletions
|
@ -54,8 +54,6 @@ if(NOT CMAKE_BUILD_TYPE)
|
|||
set(CMAKE_BUILD_TYPE "Release")
|
||||
endif()
|
||||
|
||||
include(NetdataCompilerFlags)
|
||||
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS On)
|
||||
|
||||
# Check for the mold linker and try to use it if available
|
||||
|
@ -149,6 +147,13 @@ else()
|
|||
message(FATAL_ERROR "Unknown/unsupported platform: ${CMAKE_SYSTEM_NAME} (Supported platforms: FreeBSD, Linux, macOS, Windows)")
|
||||
endif()
|
||||
|
||||
include(NetdataCompilerFlags)
|
||||
|
||||
check_c_compiler_flag("-fexceptions" HAVE_FEXCEPTIONS)
|
||||
if (NOT HAVE_FEXCEPTIONS)
|
||||
message(FATAL_ERROR "Missing required compiler flag: -fexceptions.")
|
||||
endif()
|
||||
|
||||
# This is intended to make life easier for developers who are working on one
|
||||
# specific feature.
|
||||
#
|
||||
|
|
|
@ -312,12 +312,8 @@ while [ -n "${1}" ]; do
|
|||
;;
|
||||
"--enable-ml") NETDATA_ENABLE_ML=1 ;;
|
||||
"--disable-ml") NETDATA_ENABLE_ML=0 ;;
|
||||
"--enable-lto")
|
||||
# TODO: Needs CMake support
|
||||
;;
|
||||
"--disable-lto")
|
||||
# TODO: Needs CMake support
|
||||
;;
|
||||
"--enable-lto") NETDATA_ENABLE_LTO=1 ;;
|
||||
"--disable-lto") NETDATA_ENABLE_LTO=0 ;;
|
||||
"--disable-x86-sse")
|
||||
# XXX: No longer supported.
|
||||
;;
|
||||
|
|
|
@ -336,6 +336,7 @@ happened, on your systems and applications.
|
|||
-DUSE_CXX_11=On \
|
||||
%endif
|
||||
%endif
|
||||
-DDISABLE_LTO=On \
|
||||
%if %{_have_cups}
|
||||
-DENABLE_PLUGIN_CUPS=On \
|
||||
%else
|
||||
|
|
|
@ -91,8 +91,10 @@ 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)
|
||||
|
@ -102,6 +104,26 @@ 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")
|
||||
|
|
|
@ -297,6 +297,14 @@ prepare_cmake_options() {
|
|||
NETDATA_CMAKE_OPTIONS="${NETDATA_CMAKE_OPTIONS} -DUSE_CXX_11=On"
|
||||
fi
|
||||
|
||||
if [ -n "${NETDATA_ENABLE_LTO}" ]; then
|
||||
if [ "${NETDATA_ENABLE_LTO}" -eq 1 ]; then
|
||||
NETDATA_CMAKE_OPTIONS="${NETDATA_CMAKE_OPTIONS} -DDISABLE_LTO=Off"
|
||||
else
|
||||
NETDATA_CMAKE_OPTIONS="${NETDATA_CMAKE_OPTIONS} -DDISABLE_LTO=On"
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ "${ENABLE_GO:-1}" -eq 1 ]; then
|
||||
enable_feature PLUGIN_GO 1
|
||||
else
|
||||
|
|
Loading…
Add table
Reference in a new issue