Skip to content

Commit 3bc71c2

Browse files
authored
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 51268a5 commit 3bc71c2

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")
@@ -346,20 +347,7 @@ endif ()
346347
# Determine HOST_LINK_VERSION on Darwin.
347348
set(HOST_LINK_VERSION)
348349
if (APPLE AND NOT CMAKE_LINKER MATCHES ".*lld.*")
349-
set(LD_V_OUTPUT)
350-
execute_process(
351-
COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
352-
RESULT_VARIABLE HAD_ERROR
353-
OUTPUT_VARIABLE LD_V_OUTPUT
354-
)
355-
if (HAD_ERROR)
356-
message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
357-
endif()
358-
if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
359-
string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
360-
elseif ("${LD_V_OUTPUT}" MATCHES "[^0-9]*([0-9.]+).*")
361-
string(REGEX REPLACE "[^0-9]*([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
362-
endif()
350+
get_darwin_linker_version(HOST_LINK_VERSION)
363351
message(STATUS "Host linker version: ${HOST_LINK_VERSION}")
364352
endif()
365353

+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
@@ -36,6 +36,7 @@ include(SetPlatformToolchainTools)
3636
include(base-config-ix)
3737
include(CompilerRTUtils)
3838
include(CMakeDependentOption)
39+
include(GetDarwinLinkerVersion)
3940

4041
option(COMPILER_RT_BUILD_BUILTINS "Build builtins" ON)
4142
mark_as_advanced(COMPILER_RT_BUILD_BUILTINS)
@@ -444,6 +445,15 @@ else()
444445
set(SANITIZER_USE_SYMBOLS FALSE)
445446
endif()
446447

448+
# Get the linker version while configuring compiler-rt and explicitly pass it
449+
# in cflags during testing. This fixes the compiler/linker version mismatch
450+
# issue when running a clang built with a newer Xcode in an older Xcode
451+
set(COMPILER_RT_DARWIN_LINKER_VERSION)
452+
if (APPLE AND NOT CMAKE_LINKER MATCHES ".*lld.*")
453+
get_darwin_linker_version(COMPILER_RT_DARWIN_LINKER_VERSION)
454+
message(STATUS "Host linker version: ${COMPILER_RT_DARWIN_LINKER_VERSION}")
455+
endif()
456+
447457
# Build sanitizer runtimes with debug info.
448458
if(MSVC)
449459
# 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
@@ -882,6 +882,10 @@ def is_windows_lto_supported():
882882
elif config.use_lld and (not config.has_lld):
883883
config.unsupported = True
884884

885+
if config.host_os == "Darwin":
886+
if getattr(config, "darwin_linker_version", None):
887+
extra_cflags += ["-mlinker-version=" + config.darwin_linker_version]
888+
885889
# Append any extra flags passed in lit_config
886890
append_target_cflags = lit_config.params.get("append_target_cflags", None)
887891
if append_target_cflags:

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

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ set_default("expensive_checks", @LLVM_ENABLE_EXPENSIVE_CHECKS_PYBOOL@)
5151
set_default("test_standalone_build_libs", @COMPILER_RT_TEST_STANDALONE_BUILD_LIBS_PYBOOL@)
5252
set_default("has_compiler_rt_libatomic", @COMPILER_RT_BUILD_STANDALONE_LIBATOMIC_PYBOOL@)
5353
set_default("aarch64_sme", @COMPILER_RT_HAS_AARCH64_SME_PYBOOL@)
54+
set_default("darwin_linker_version", "@COMPILER_RT_DARWIN_LINKER_VERSION@")
5455
# True iff the test suite supports ignoring the test compiler's runtime library path
5556
# and using `config.compiler_rt_libdir` instead. This only matters when the runtime
5657
# library paths differ.

0 commit comments

Comments
 (0)