Skip to content

Commit 1db33c7

Browse files
committed
Get the linker version and pass the it to compiler-rt tests on Darwin. (llvm#86220)
The HOST_LINK_VERSION is a hardcoded string in Darwin clang that detects the linker version at configure time. The driver uses this information to build the correct set of arguments for the linker. This patch detects the linker version again during compiler-rt configuration and passes it to the tests. This allows a clang built on a machine with a new linker to run compiler-rt tests on a machine with an old linker. rdar://125198603
1 parent d1c4e1c commit 1db33c7

File tree

5 files changed

+36
-14
lines changed

5 files changed

+36
-14
lines changed

clang/CMakeLists.txt

+2-14
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ endif()
1515

1616
# Must go below project(..)
1717
include(GNUInstallDirs)
18+
include(GetDarwinLinkerVersion)
1819

1920
if(CLANG_BUILT_STANDALONE)
2021
set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to conform to")
@@ -320,20 +321,7 @@ endif ()
320321
# Determine HOST_LINK_VERSION on Darwin.
321322
set(HOST_LINK_VERSION)
322323
if (APPLE AND NOT CMAKE_LINKER MATCHES ".*lld.*")
323-
set(LD_V_OUTPUT)
324-
execute_process(
325-
COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
326-
RESULT_VARIABLE HAD_ERROR
327-
OUTPUT_VARIABLE LD_V_OUTPUT
328-
)
329-
if (HAD_ERROR)
330-
message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
331-
endif()
332-
if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
333-
string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
334-
elseif ("${LD_V_OUTPUT}" MATCHES "[^0-9]*([0-9.]+).*")
335-
string(REGEX REPLACE "[^0-9]*([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
336-
endif()
324+
get_darwin_linker_version(HOST_LINK_VERSION)
337325
message(STATUS "Host linker version: ${HOST_LINK_VERSION}")
338326
endif()
339327

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Get the linker version on Darwin
2+
function(get_darwin_linker_version variable)
3+
set(LINK_VERSION)
4+
set(LD_V_OUTPUT)
5+
execute_process(
6+
COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
7+
RESULT_VARIABLE HAD_ERROR
8+
OUTPUT_VARIABLE LD_V_OUTPUT
9+
)
10+
if (HAD_ERROR)
11+
message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
12+
endif()
13+
if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
14+
string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" LINK_VERSION ${LD_V_OUTPUT})
15+
elseif ("${LD_V_OUTPUT}" MATCHES "[^0-9]*([0-9.]+).*")
16+
string(REGEX REPLACE "[^0-9]*([0-9.]+).*" "\\1" LINK_VERSION ${LD_V_OUTPUT})
17+
endif()
18+
set(${variable} ${LINK_VERSION} PARENT_SCOPE)
19+
endfunction()

compiler-rt/CMakeLists.txt

+10
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ include(SetPlatformToolchainTools)
3434
include(base-config-ix)
3535
include(CompilerRTUtils)
3636
include(CMakeDependentOption)
37+
include(GetDarwinLinkerVersion)
3738

3839
option(COMPILER_RT_BUILD_BUILTINS "Build builtins" ON)
3940
mark_as_advanced(COMPILER_RT_BUILD_BUILTINS)
@@ -456,6 +457,15 @@ else()
456457
set(SANITIZER_USE_SYMBOLS FALSE)
457458
endif()
458459

460+
# Get the linker version while configuring compiler-rt and explicitly pass it
461+
# in cflags during testing. This fixes the compiler/linker version mismatch
462+
# issue when running a clang built with a newer Xcode in an older Xcode
463+
set(COMPILER_RT_DARWIN_LINKER_VERSION)
464+
if (APPLE AND NOT CMAKE_LINKER MATCHES ".*lld.*")
465+
get_darwin_linker_version(COMPILER_RT_DARWIN_LINKER_VERSION)
466+
message(STATUS "Host linker version: ${COMPILER_RT_DARWIN_LINKER_VERSION}")
467+
endif()
468+
459469
# Build sanitizer runtimes with debug info.
460470
if(MSVC)
461471
# Use /Z7 instead of /Zi for the asan runtime. This avoids the LNK4099

compiler-rt/test/lit.common.cfg.py

+4
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,10 @@ def is_windows_lto_supported():
895895
elif config.use_lld and (not config.has_lld):
896896
config.unsupported = True
897897

898+
if config.host_os == "Darwin":
899+
if getattr(config, "darwin_linker_version", None):
900+
extra_cflags += ["-mlinker-version=" + config.darwin_linker_version]
901+
898902
# Append any extra flags passed in lit_config
899903
append_target_cflags = lit_config.params.get("append_target_cflags", None)
900904
if append_target_cflags:

compiler-rt/test/lit.common.configured.in

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ set_default("have_rpc_xdr_h", @HAVE_RPC_XDR_H@)
4949
set_default("gwp_asan", @COMPILER_RT_HAS_GWP_ASAN_PYBOOL@)
5050
set_default("expensive_checks", @LLVM_ENABLE_EXPENSIVE_CHECKS_PYBOOL@)
5151
set_default("test_standalone_build_libs", @COMPILER_RT_TEST_STANDALONE_BUILD_LIBS_PYBOOL@)
52+
set_default("darwin_linker_version", "@COMPILER_RT_DARWIN_LINKER_VERSION@")
5253
# True iff the test suite supports ignoring the test compiler's runtime library path
5354
# and using `config.compiler_rt_libdir` instead. This only matters when the runtime
5455
# library paths differ.

0 commit comments

Comments
 (0)