@cryptotaxi247 / netdata-1 / commits / 10815cb24

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.

Austin S. Hemmelgarn committed Jan 31, 2025 at 08:22 UTC 10815cb24ac35a63461fdb3ec922b0446a043d75
5 files changed +40 -8
CMakeLists.txt
+7 -2
@@ -54,8 +54,6 @@ if(NOT CMAKE_BUILD_TYPE)
54 set(CMAKE_BUILD_TYPE "Release")
55 endif()
56
57 -include(NetdataCompilerFlags)
58 -
57 set(CMAKE_EXPORT_COMPILE_COMMANDS On)
58
59 # Check for the mold linker and try to use it if available
@@ -149,6 +147,13 @@ else()
147 message(FATAL_ERROR "Unknown/unsupported platform: ${CMAKE_SYSTEM_NAME} (Supported platforms: FreeBSD, Linux, macOS, Windows)")
148 endif()
149
150 +include(NetdataCompilerFlags)
151 +
152 +check_c_compiler_flag("-fexceptions" HAVE_FEXCEPTIONS)
153 +if (NOT HAVE_FEXCEPTIONS)
154 + message(FATAL_ERROR "Missing required compiler flag: -fexceptions.")
155 +endif()
156 +
157 # This is intended to make life easier for developers who are working on one
158 # specific feature.
159 #
netdata-installer.sh
+2 -6
@@ -312,12 +312,8 @@ while [ -n "${1}" ]; do
312 ;;
313 "--enable-ml") NETDATA_ENABLE_ML=1 ;;
314 "--disable-ml") NETDATA_ENABLE_ML=0 ;;
315 - "--enable-lto")
316 - # TODO: Needs CMake support
317 - ;;
318 - "--disable-lto")
319 - # TODO: Needs CMake support
320 - ;;
315 + "--enable-lto") NETDATA_ENABLE_LTO=1 ;;
316 + "--disable-lto") NETDATA_ENABLE_LTO=0 ;;
317 "--disable-x86-sse")
318 # XXX: No longer supported.
319 ;;
netdata.spec.in
+1
@@ -336,6 +336,7 @@ happened, on your systems and applications.
336 -DUSE_CXX_11=On \
337 %endif
338 %endif
339 + -DDISABLE_LTO=On \
340 %if %{_have_cups}
341 -DENABLE_PLUGIN_CUPS=On \
342 %else
packaging/cmake/Modules/NetdataCompilerFlags.cmake
+22
@@ -91,8 +91,10 @@ endfunction()
91
92 if(CMAKE_BUILD_TYPE STREQUAL "Debug")
93 option(DISABLE_HARDENING "Disable adding extra compiler flags for hardening" TRUE)
94 + option(USE_LTO "Attempt to use of LTO when building. Defaults to being enabled if supported for release builds." FALSE)
95 else()
96 option(DISABLE_HARDENING "Disable adding extra compiler flags for hardening" FALSE)
97 + option(USE_LTO "Attempt to use of LTO when building. Defaults to being enabled if supported for release builds." TRUE)
98 endif()
99
100 option(ENABLE_ADDRESS_SANITIZER "Build with address sanitizer enabled" False)
@@ -102,6 +104,26 @@ if(ENABLE_ADDRESS_SANITIZER)
104 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address")
105 endif()
106
107 +if(USE_LTO)
108 + if(OS_WINDOWS)
109 + message(WARNING "LTO not supported on Windows, not checking for it")
110 + else()
111 + include(CheckIPOSupported)
112 +
113 + message(CHECK_START "Checking for LTO support")
114 + check_ipo_supported(RESULT HAVE_LTO)
115 +
116 + if(HAVE_LTO)
117 + message(CHECK_PASS "supported")
118 + set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
119 + else()
120 + message(CHECK_FAIL "not supported")
121 + endif()
122 + endif()
123 +else()
124 + message(STATUS "Not checking for LTO support as it has been explicitly disabled")
125 +endif()
126 +
127 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_C_FLAGS}")
128
129 add_required_compiler_flag("-fexceptions")
packaging/installer/functions.sh
+8
@@ -297,6 +297,14 @@ prepare_cmake_options() {
297 NETDATA_CMAKE_OPTIONS="${NETDATA_CMAKE_OPTIONS} -DUSE_CXX_11=On"
298 fi
299
300 + if [ -n "${NETDATA_ENABLE_LTO}" ]; then
301 + if [ "${NETDATA_ENABLE_LTO}" -eq 1 ]; then
302 + NETDATA_CMAKE_OPTIONS="${NETDATA_CMAKE_OPTIONS} -DDISABLE_LTO=Off"
303 + else
304 + NETDATA_CMAKE_OPTIONS="${NETDATA_CMAKE_OPTIONS} -DDISABLE_LTO=On"
305 + fi
306 + fi
307 +
308 if [ "${ENABLE_GO:-1}" -eq 1 ]; then
309 enable_feature PLUGIN_GO 1
310 else