Detect and use ld.mold instead of the system linker. (#17534)
Not a mandatory dependency, but it significantly speeds up the final linking step for builds.
Austin S. Hemmelgarn committed
May 1, 2024 at 07:04 UTC
92c9fff172d5a4d78a9168a6792302b80dc7aaef
1 file changed
+26
CMakeLists.txt
+26
@@ -86,6 +86,32 @@ include(NetdataCompilerFlags)
86
87
set(CMAKE_EXPORT_COMPILE_COMMANDS On)
88
89
+# Check for the mold linker and try to use it if available
90
+option(USE_MOLD "If the MOLD linker is available on the system, use it instead of the default linker." TRUE)
91
+
92
+if(USE_MOLD)
93
+ message(CHECK_START "Searching for MOLD linker")
94
+ find_program(MOLD_LINKER NAMES ld.mold mold)
95
+
96
+ if(MOLD_LINKER)
97
+ execute_process(COMMAND ${MOLD_LINKER} --version
98
+ RESULT_VARIABLE MOLD_VERSION_RESULT
99
+ OUTPUT_VARIABLE MOLD_VERSION)
100
+
101
+ if(NOT MOLD_VERSION_RESULT)
102
+ string(REPLACE "\n" "" MOLD_VERSION "${MOLD_VERSION}")
103
+ message(CHECK_PASS "found (version: ${MOLD_VERSION})")
104
+ message(STATUS "Using mold instead of the system default for linking.")
105
+ add_compile_options("-fuse-ld=mold")
106
+ add_link_options("-fuse-ld=mold")
107
+ else()
108
+ message(CHECK_FAIL "failed")
109
+ endif()
110
+ else()
111
+ message(CHECK_FAIL "failed")
112
+ endif()
113
+endif()
114
+
115
set(CONFIG_H_DIR ${CMAKE_BINARY_DIR})
116
set(CONFIG_H ${CONFIG_H_DIR}/config.h)
117