Skip to content

Commit 2d39534

Browse files
committed
[cmake] Make LLVM_ENABLE_LLD=ON work better on macOS
LLVM_LINKER_IS_LLD is now set with LLVM_ENABLE_LLD=ON (or LLVM_USER_LINKER=lld) even on APPLE, and we pass -Wl,-order_file when LLVM_LINKER_IS_LLD on APPLE too. To make this straightforward, change the linker detection logic to go through the compiler driver on APPLE like on the other platforms. No intended behavior change if LLVM_ENABLE_LLD isn't set. Differential Revision: https://reviews.llvm.org/D113021
1 parent 98b761f commit 2d39534

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

clang/tools/driver/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ if(CLANG_ORDER_FILE AND
9595
(LLVM_LINKER_IS_LD64 OR LLVM_LINKER_IS_GOLD OR LLVM_LINKER_IS_LLD))
9696
include(LLVMCheckLinkerFlag)
9797

98-
if (LLVM_LINKER_IS_LD64)
98+
if (LLVM_LINKER_IS_LD64 OR (LLVM_LINKER_IS_LLD and APPLE))
9999
set(LINKER_ORDER_FILE_OPTION "-Wl,-order_file,${CLANG_ORDER_FILE}")
100100
elseif (LLVM_LINKER_IS_GOLD)
101101
set(LINKER_ORDER_FILE_OPTION "-Wl,--section-ordering-file,${CLANG_ORDER_FILE}")

llvm/cmake/modules/AddLLVM.cmake

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -166,33 +166,43 @@ function(add_llvm_symbol_exports target_name export_file)
166166
set(LLVM_COMMON_DEPENDS ${LLVM_COMMON_DEPENDS} PARENT_SCOPE)
167167
endfunction(add_llvm_symbol_exports)
168168

169-
if (NOT DEFINED LLVM_LINKER_DETECTED)
169+
if (NOT DEFINED LLVM_LINKER_DETECTED AND NOT WIN32)
170+
# Detect what linker we have here.
171+
if(APPLE)
172+
# Linkers with ld64-compatible flags.
173+
set(version_flag "-Wl,-v")
174+
else()
175+
# Linkers with BFD ld-compatible flags.
176+
set(version_flag "-Wl,--version")
177+
endif()
178+
179+
if(LLVM_USE_LINKER)
180+
set(command ${CMAKE_C_COMPILER} -fuse-ld=${LLVM_USE_LINKER} ${version_flag})
181+
else()
182+
separate_arguments(flags UNIX_COMMAND "${CMAKE_EXE_LINKER_FLAGS}")
183+
set(command ${CMAKE_C_COMPILER} ${flags} ${version_flag})
184+
endif()
185+
execute_process(
186+
COMMAND ${command}
187+
OUTPUT_VARIABLE stdout
188+
ERROR_VARIABLE stderr
189+
)
190+
170191
if(APPLE)
171-
execute_process(
172-
COMMAND "${CMAKE_LINKER}" -v
173-
ERROR_VARIABLE stderr
174-
)
175192
if("${stderr}" MATCHES "PROJECT:ld64")
176193
set(LLVM_LINKER_DETECTED YES CACHE INTERNAL "")
177194
set(LLVM_LINKER_IS_LD64 YES CACHE INTERNAL "")
178195
message(STATUS "Linker detection: ld64")
196+
elseif("${stderr}" MATCHES "^LLD" OR
197+
"${stdout}" MATCHES "^LLD")
198+
set(LLVM_LINKER_DETECTED YES CACHE INTERNAL "")
199+
set(LLVM_LINKER_IS_LLD YES CACHE INTERNAL "")
200+
message(STATUS "Linker detection: lld")
179201
else()
180202
set(LLVM_LINKER_DETECTED NO CACHE INTERNAL "")
181203
message(STATUS "Linker detection: unknown")
182204
endif()
183-
elseif(NOT WIN32)
184-
# Detect what linker we have here
185-
if( LLVM_USE_LINKER )
186-
set(command ${CMAKE_C_COMPILER} -fuse-ld=${LLVM_USE_LINKER} -Wl,--version)
187-
else()
188-
separate_arguments(flags UNIX_COMMAND "${CMAKE_EXE_LINKER_FLAGS}")
189-
set(command ${CMAKE_C_COMPILER} ${flags} -Wl,--version)
190-
endif()
191-
execute_process(
192-
COMMAND ${command}
193-
OUTPUT_VARIABLE stdout
194-
ERROR_VARIABLE stderr
195-
)
205+
else()
196206
if("${stdout}" MATCHES "^mold")
197207
set(LLVM_LINKER_DETECTED YES CACHE INTERNAL "")
198208
message(STATUS "Linker detection: mold")

0 commit comments

Comments
 (0)