[clang] [compiler-rt] [llvm] Get the linker version and pass the it to compiler-rt tests on Darwin. (PR #86220)

2024-03-22 Thread Usama Hameed via cfe-commits

https://github.com/usama54321 updated 
https://github.com/llvm/llvm-project/pull/86220

>From 0dab57b9f541a9fb6cbbe85f034117a19227c331 Mon Sep 17 00:00:00 2001
From: usama 
Date: Thu, 21 Mar 2024 16:56:32 -0700
Subject: [PATCH] Get the linker version and pass the it to compiler-rt tests
 on Darwin.

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
---
 clang/CMakeLists.txt   | 16 ++--
 cmake/Modules/GetDarwinLinkerVersion.cmake | 19 +++
 compiler-rt/CMakeLists.txt | 10 ++
 compiler-rt/test/lit.common.cfg.py |  4 
 compiler-rt/test/lit.common.configured.in  |  1 +
 5 files changed, 36 insertions(+), 14 deletions(-)
 create mode 100644 cmake/Modules/GetDarwinLinkerVersion.cmake

diff --git a/clang/CMakeLists.txt b/clang/CMakeLists.txt
index 761dab8c28c134..ee783d52e4a4e2 100644
--- a/clang/CMakeLists.txt
+++ b/clang/CMakeLists.txt
@@ -15,6 +15,7 @@ endif()
 
 # Must go below project(..)
 include(GNUInstallDirs)
+include(GetDarwinLinkerVersion)
 
 if(CLANG_BUILT_STANDALONE)
   set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to conform to")
@@ -346,20 +347,7 @@ endif ()
 # Determine HOST_LINK_VERSION on Darwin.
 set(HOST_LINK_VERSION)
 if (APPLE AND NOT CMAKE_LINKER MATCHES ".*lld.*")
-  set(LD_V_OUTPUT)
-  execute_process(
-COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
-RESULT_VARIABLE HAD_ERROR
-OUTPUT_VARIABLE LD_V_OUTPUT
-  )
-  if (HAD_ERROR)
-message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
-  endif()
-  if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
-string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" HOST_LINK_VERSION 
${LD_V_OUTPUT})
-  elseif ("${LD_V_OUTPUT}" MATCHES "[^0-9]*([0-9.]+).*")
-string(REGEX REPLACE "[^0-9]*([0-9.]+).*" "\\1" HOST_LINK_VERSION 
${LD_V_OUTPUT})
-  endif()
+  get_darwin_linker_version(HOST_LINK_VERSION)
   message(STATUS "Host linker version: ${HOST_LINK_VERSION}")
 endif()
 
diff --git a/cmake/Modules/GetDarwinLinkerVersion.cmake 
b/cmake/Modules/GetDarwinLinkerVersion.cmake
new file mode 100644
index 00..c27e50128586db
--- /dev/null
+++ b/cmake/Modules/GetDarwinLinkerVersion.cmake
@@ -0,0 +1,19 @@
+# Get the linker version on Darwin
+function(get_darwin_linker_version variable)
+  set(LINK_VERSION)
+  set(LD_V_OUTPUT)
+  execute_process(
+COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
+RESULT_VARIABLE HAD_ERROR
+OUTPUT_VARIABLE LD_V_OUTPUT
+)
+  if (HAD_ERROR)
+message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
+  endif()
+  if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
+string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" LINK_VERSION 
${LD_V_OUTPUT})
+  elseif ("${LD_V_OUTPUT}" MATCHES "[^0-9]*([0-9.]+).*")
+string(REGEX REPLACE "[^0-9]*([0-9.]+).*" "\\1" LINK_VERSION 
${LD_V_OUTPUT})
+  endif()
+  set(${variable} ${LINK_VERSION} PARENT_SCOPE)
+endfunction()
diff --git a/compiler-rt/CMakeLists.txt b/compiler-rt/CMakeLists.txt
index d562a6206c00e7..f4e92f14db8570 100644
--- a/compiler-rt/CMakeLists.txt
+++ b/compiler-rt/CMakeLists.txt
@@ -36,6 +36,7 @@ include(SetPlatformToolchainTools)
 include(base-config-ix)
 include(CompilerRTUtils)
 include(CMakeDependentOption)
+include(GetDarwinLinkerVersion)
 
 option(COMPILER_RT_BUILD_BUILTINS "Build builtins" ON)
 mark_as_advanced(COMPILER_RT_BUILD_BUILTINS)
@@ -444,6 +445,15 @@ else()
   set(SANITIZER_USE_SYMBOLS FALSE)
 endif()
 
+# Get the linker version while configuring compiler-rt and explicitly pass it
+# in cflags during testing. This fixes the compiler/linker version mismatch
+# issue when running a clang built with a newer Xcode in an older Xcode
+set(COMPILER_RT_DARWIN_LINKER_VERSION)
+if (APPLE AND NOT CMAKE_LINKER MATCHES ".*lld.*")
+  get_darwin_linker_version(COMPILER_RT_DARWIN_LINKER_VERSION)
+  message(STATUS "Host linker version: ${COMPILER_RT_DARWIN_LINKER_VERSION}")
+endif()
+
 # Build sanitizer runtimes with debug info.
 if(MSVC)
   # Use /Z7 instead of /Zi for the asan runtime. This avoids the LNK4099
diff --git a/compiler-rt/test/lit.common.cfg.py 
b/compiler-rt/test/lit.common.cfg.py
index bd9b926c1505b1..0ac20a9831d950 100644
--- a/compiler-rt/test/lit.common.cfg.py
+++ b/compiler-rt/test/lit.common.cfg.py
@@ -882,6 +882,10 @@ def is_windows_lto_supported():
 elif config.use_lld and (not config.has_lld):
 config.unsupported = True
 
+if config.host_os == "Darwin":
+if getattr(config, "darwin_linker_version", None):
+extra_cflags += ["-mlinker-version=" + config.darwin_linker_version]
+
 # Append any extra 

[clang] [compiler-rt] [llvm] Get the linker version and pass the it to compiler-rt tests on Darwin. (PR #86220)

2024-03-22 Thread Usama Hameed via cfe-commits

https://github.com/usama54321 closed 
https://github.com/llvm/llvm-project/pull/86220
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [compiler-rt] [llvm] Get the linker version and pass the it to compiler-rt tests on Darwin. (PR #86220)

2024-03-22 Thread via cfe-commits

github-actions[bot] wrote:



:white_check_mark: With the latest revision this PR passed the Python code 
formatter.

https://github.com/llvm/llvm-project/pull/86220
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [compiler-rt] [llvm] Get the linker version and pass the it to compiler-rt tests on Darwin. (PR #86220)

2024-03-22 Thread via cfe-commits

github-actions[bot] wrote:



:white_check_mark: With the latest revision this PR passed the C/C++ code 
formatter.

https://github.com/llvm/llvm-project/pull/86220
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [compiler-rt] [llvm] Get the linker version and pass the it to compiler-rt tests on Darwin. (PR #86220)

2024-03-22 Thread Usama Hameed via cfe-commits

https://github.com/usama54321 edited 
https://github.com/llvm/llvm-project/pull/86220
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [compiler-rt] [llvm] Get the linker version and pass the it to compiler-rt tests on Darwin. (PR #86220)

2024-03-22 Thread Usama Hameed via cfe-commits

https://github.com/usama54321 edited 
https://github.com/llvm/llvm-project/pull/86220
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [compiler-rt] [llvm] Get the linker version and pass the it to compiler-rt tests on Darwin. (PR #86220)

2024-03-22 Thread Usama Hameed via cfe-commits

https://github.com/usama54321 edited 
https://github.com/llvm/llvm-project/pull/86220
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [compiler-rt] [llvm] Get the linker version and pass the it to compiler-rt tests on Darwin. (PR #86220)

2024-03-22 Thread Usama Hameed via cfe-commits

https://github.com/usama54321 edited 
https://github.com/llvm/llvm-project/pull/86220
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [compiler-rt] [llvm] Get the linker version and pass the it to compiler-rt tests on Darwin. (PR #86220)

2024-03-22 Thread Julian Lettner via cfe-commits

https://github.com/yln approved this pull request.

Thanks, approved with a few nits!

https://github.com/llvm/llvm-project/pull/86220
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [compiler-rt] [llvm] Get the linker version and pass the it to compiler-rt tests on Darwin. (PR #86220)

2024-03-22 Thread Julian Lettner via cfe-commits


@@ -15,6 +15,7 @@ endif()
 
 # Must go below project(..)
 include(GNUInstallDirs)
+include(GetDarwinLinkerVersion)

yln wrote:

Thanks for extracting this into a separate file!  

https://github.com/llvm/llvm-project/pull/86220
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [compiler-rt] [llvm] Get the linker version and pass the it to compiler-rt tests on Darwin. (PR #86220)

2024-03-22 Thread Julian Lettner via cfe-commits


@@ -51,6 +51,7 @@ set_default("expensive_checks", 
@LLVM_ENABLE_EXPENSIVE_CHECKS_PYBOOL@)
 set_default("test_standalone_build_libs", 
@COMPILER_RT_TEST_STANDALONE_BUILD_LIBS_PYBOOL@)
 set_default("has_compiler_rt_libatomic", 
@COMPILER_RT_BUILD_STANDALONE_LIBATOMIC_PYBOOL@)
 set_default("aarch64_sme", @COMPILER_RT_HAS_AARCH64_SME_PYBOOL@)
+set_default("host_link_version", "@HOST_LINK_VERSION@")

yln wrote:

- [ ]  Many of the other vars here have a `COMPILER_RT` prefix.  Should we do 
the same or does it imply some additional semantics ("user option" in CMAKE 
script)?
- [ ] `set_default("darwin_linker_version", 
"@COMPILER_RT_DARWIN_LINKER_VERSION@")`

https://github.com/llvm/llvm-project/pull/86220
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [compiler-rt] [llvm] Get the linker version and pass the it to compiler-rt tests on Darwin. (PR #86220)

2024-03-22 Thread Julian Lettner via cfe-commits


@@ -444,6 +445,15 @@ else()
   set(SANITIZER_USE_SYMBOLS FALSE)
 endif()
 
+# Get the linker version while configuring compiler-rt and explicitly pass it
+# in cflags during testing. This fixes the compiler/linker version mismatch
+# issue when running a clang built with a newer Xcode in an older Xcode
+set(HOST_LINK_VERSION)

yln wrote:

- [ ] How about `DARWIN_LINKER_VERSION`?

https://github.com/llvm/llvm-project/pull/86220
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [compiler-rt] [llvm] Get the linker version and pass the it to compiler-rt tests on Darwin. (PR #86220)

2024-03-22 Thread Julian Lettner via cfe-commits


@@ -346,20 +347,7 @@ endif ()
 # Determine HOST_LINK_VERSION on Darwin.
 set(HOST_LINK_VERSION)
 if (APPLE AND NOT CMAKE_LINKER MATCHES ".*lld.*")
-  set(LD_V_OUTPUT)
-  execute_process(
-COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
-RESULT_VARIABLE HAD_ERROR
-OUTPUT_VARIABLE LD_V_OUTPUT
-  )
-  if (HAD_ERROR)
-message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
-  endif()
-  if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
-string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" HOST_LINK_VERSION 
${LD_V_OUTPUT})
-  elseif ("${LD_V_OUTPUT}" MATCHES "[^0-9]*([0-9.]+).*")
-string(REGEX REPLACE "[^0-9]*([0-9.]+).*" "\\1" HOST_LINK_VERSION 
${LD_V_OUTPUT})
-  endif()
+  get_darwin_linker_version()

yln wrote:

- [ ]  Would it be possible to "declare" where the value is stored, i.e., a 
CMAKE output parameter?  So the relationship between `HOST_LINK_VERSION` and 
`get_darwin_linker_version()` is explicit (and other variable names can be 
used)?
```suggestion
  get_darwin_linker_version(HOST_LINK_VERSION)
```



https://github.com/llvm/llvm-project/pull/86220
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [compiler-rt] [llvm] Get the linker version and pass the it to compiler-rt tests on Darwin. (PR #86220)

2024-03-22 Thread Julian Lettner via cfe-commits

https://github.com/yln edited https://github.com/llvm/llvm-project/pull/86220
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [compiler-rt] [llvm] Get the linker version and pass the it to compiler-rt tests on Darwin. (PR #86220)

2024-03-22 Thread Mariusz Borsa via cfe-commits

https://github.com/wrotki approved this pull request.


https://github.com/llvm/llvm-project/pull/86220
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [compiler-rt] [llvm] Get the linker version and pass the it to compiler-rt tests on Darwin. (PR #86220)

2024-03-22 Thread Mariusz Borsa via cfe-commits

wrotki wrote:

Yes, it makes sense

https://github.com/llvm/llvm-project/pull/86220
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [compiler-rt] [llvm] Get the linker version and pass the it to compiler-rt tests on Darwin. (PR #86220)

2024-03-21 Thread via cfe-commits

https://github.com/thetruestblue approved this pull request.

Thanks for looking into this.

https://github.com/llvm/llvm-project/pull/86220
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [compiler-rt] [llvm] Get the linker version and pass the it to compiler-rt tests on Darwin. (PR #86220)

2024-03-21 Thread Usama Hameed via cfe-commits

https://github.com/usama54321 created 
https://github.com/llvm/llvm-project/pull/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

>From 3148a5e1030493d37da0b24079b1280d9f893331 Mon Sep 17 00:00:00 2001
From: usama 
Date: Thu, 21 Mar 2024 16:56:32 -0700
Subject: [PATCH] Get the linker version and pass the it to compiler-rt tests
 on Darwin.

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
---
 clang/CMakeLists.txt   | 16 ++--
 cmake/Modules/GetDarwinLinkerVersion.cmake | 19 +++
 compiler-rt/CMakeLists.txt | 10 ++
 compiler-rt/test/lit.common.cfg.py |  4 
 compiler-rt/test/lit.common.configured.in  |  1 +
 5 files changed, 36 insertions(+), 14 deletions(-)
 create mode 100644 cmake/Modules/GetDarwinLinkerVersion.cmake

diff --git a/clang/CMakeLists.txt b/clang/CMakeLists.txt
index 761dab8c28c134..bcafbc37fbe1fb 100644
--- a/clang/CMakeLists.txt
+++ b/clang/CMakeLists.txt
@@ -15,6 +15,7 @@ endif()
 
 # Must go below project(..)
 include(GNUInstallDirs)
+include(GetDarwinLinkerVersion)
 
 if(CLANG_BUILT_STANDALONE)
   set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to conform to")
@@ -346,20 +347,7 @@ endif ()
 # Determine HOST_LINK_VERSION on Darwin.
 set(HOST_LINK_VERSION)
 if (APPLE AND NOT CMAKE_LINKER MATCHES ".*lld.*")
-  set(LD_V_OUTPUT)
-  execute_process(
-COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
-RESULT_VARIABLE HAD_ERROR
-OUTPUT_VARIABLE LD_V_OUTPUT
-  )
-  if (HAD_ERROR)
-message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
-  endif()
-  if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
-string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" HOST_LINK_VERSION 
${LD_V_OUTPUT})
-  elseif ("${LD_V_OUTPUT}" MATCHES "[^0-9]*([0-9.]+).*")
-string(REGEX REPLACE "[^0-9]*([0-9.]+).*" "\\1" HOST_LINK_VERSION 
${LD_V_OUTPUT})
-  endif()
+  get_darwin_linker_version()
   message(STATUS "Host linker version: ${HOST_LINK_VERSION}")
 endif()
 
diff --git a/cmake/Modules/GetDarwinLinkerVersion.cmake 
b/cmake/Modules/GetDarwinLinkerVersion.cmake
new file mode 100644
index 00..b6a10fdc173677
--- /dev/null
+++ b/cmake/Modules/GetDarwinLinkerVersion.cmake
@@ -0,0 +1,19 @@
+# Get the linker version on Darwin
+function(get_darwin_linker_version)
+  set(LINK_VERSION)
+  set(LD_V_OUTPUT)
+  execute_process(
+COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
+RESULT_VARIABLE HAD_ERROR
+OUTPUT_VARIABLE LD_V_OUTPUT
+)
+  if (HAD_ERROR)
+message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
+  endif()
+  if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
+string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" LINK_VERSION 
${LD_V_OUTPUT})
+  elseif ("${LD_V_OUTPUT}" MATCHES "[^0-9]*([0-9.]+).*")
+string(REGEX REPLACE "[^0-9]*([0-9.]+).*" "\\1" LINK_VERSION 
${LD_V_OUTPUT})
+  endif()
+  set(HOST_LINK_VERSION ${LINK_VERSION} PARENT_SCOPE)
+endfunction()
diff --git a/compiler-rt/CMakeLists.txt b/compiler-rt/CMakeLists.txt
index d562a6206c00e7..9fb15403b933dc 100644
--- a/compiler-rt/CMakeLists.txt
+++ b/compiler-rt/CMakeLists.txt
@@ -36,6 +36,7 @@ include(SetPlatformToolchainTools)
 include(base-config-ix)
 include(CompilerRTUtils)
 include(CMakeDependentOption)
+include(GetDarwinLinkerVersion)
 
 option(COMPILER_RT_BUILD_BUILTINS "Build builtins" ON)
 mark_as_advanced(COMPILER_RT_BUILD_BUILTINS)
@@ -444,6 +445,15 @@ else()
   set(SANITIZER_USE_SYMBOLS FALSE)
 endif()
 
+# Get the linker version while configuring compiler-rt and explicitly pass it
+# in cflags during testing. This fixes the compiler/linker version mismatch
+# issue when running a clang built with a newer Xcode in an older Xcode
+set(HOST_LINK_VERSION)
+if (APPLE AND NOT CMAKE_LINKER MATCHES ".*lld.*")
+  get_darwin_linker_version()
+  message(STATUS "Host linker version: ${HOST_LINK_VERSION}")
+endif()
+
 # Build sanitizer runtimes with debug info.
 if(MSVC)
   # Use /Z7 instead of /Zi for the asan runtime. This avoids the LNK4099
diff --git a/compiler-rt/test/lit.common.cfg.py 
b/compiler-rt/test/lit.common.cfg.py
index bd9b926c1505b1..cc59a794e11352 100644
--- a/compiler-rt/test/lit.common.cfg.py
+++ 

[clang] [compiler-rt] [llvm] Get the linker version and pass the it to compiler-rt tests on Darwin. (PR #86220)

2024-03-21 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang

Author: Usama Hameed (usama54321)


Changes

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

---
Full diff: https://github.com/llvm/llvm-project/pull/86220.diff


5 Files Affected:

- (modified) clang/CMakeLists.txt (+2-14) 
- (added) cmake/Modules/GetDarwinLinkerVersion.cmake (+19) 
- (modified) compiler-rt/CMakeLists.txt (+10) 
- (modified) compiler-rt/test/lit.common.cfg.py (+4) 
- (modified) compiler-rt/test/lit.common.configured.in (+1) 


``diff
diff --git a/clang/CMakeLists.txt b/clang/CMakeLists.txt
index 761dab8c28c134..bcafbc37fbe1fb 100644
--- a/clang/CMakeLists.txt
+++ b/clang/CMakeLists.txt
@@ -15,6 +15,7 @@ endif()
 
 # Must go below project(..)
 include(GNUInstallDirs)
+include(GetDarwinLinkerVersion)
 
 if(CLANG_BUILT_STANDALONE)
   set(CMAKE_CXX_STANDARD 17 CACHE STRING "C++ standard to conform to")
@@ -346,20 +347,7 @@ endif ()
 # Determine HOST_LINK_VERSION on Darwin.
 set(HOST_LINK_VERSION)
 if (APPLE AND NOT CMAKE_LINKER MATCHES ".*lld.*")
-  set(LD_V_OUTPUT)
-  execute_process(
-COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
-RESULT_VARIABLE HAD_ERROR
-OUTPUT_VARIABLE LD_V_OUTPUT
-  )
-  if (HAD_ERROR)
-message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
-  endif()
-  if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
-string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" HOST_LINK_VERSION 
${LD_V_OUTPUT})
-  elseif ("${LD_V_OUTPUT}" MATCHES "[^0-9]*([0-9.]+).*")
-string(REGEX REPLACE "[^0-9]*([0-9.]+).*" "\\1" HOST_LINK_VERSION 
${LD_V_OUTPUT})
-  endif()
+  get_darwin_linker_version()
   message(STATUS "Host linker version: ${HOST_LINK_VERSION}")
 endif()
 
diff --git a/cmake/Modules/GetDarwinLinkerVersion.cmake 
b/cmake/Modules/GetDarwinLinkerVersion.cmake
new file mode 100644
index 00..b6a10fdc173677
--- /dev/null
+++ b/cmake/Modules/GetDarwinLinkerVersion.cmake
@@ -0,0 +1,19 @@
+# Get the linker version on Darwin
+function(get_darwin_linker_version)
+  set(LINK_VERSION)
+  set(LD_V_OUTPUT)
+  execute_process(
+COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
+RESULT_VARIABLE HAD_ERROR
+OUTPUT_VARIABLE LD_V_OUTPUT
+)
+  if (HAD_ERROR)
+message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
+  endif()
+  if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
+string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" LINK_VERSION 
${LD_V_OUTPUT})
+  elseif ("${LD_V_OUTPUT}" MATCHES "[^0-9]*([0-9.]+).*")
+string(REGEX REPLACE "[^0-9]*([0-9.]+).*" "\\1" LINK_VERSION 
${LD_V_OUTPUT})
+  endif()
+  set(HOST_LINK_VERSION ${LINK_VERSION} PARENT_SCOPE)
+endfunction()
diff --git a/compiler-rt/CMakeLists.txt b/compiler-rt/CMakeLists.txt
index d562a6206c00e7..9fb15403b933dc 100644
--- a/compiler-rt/CMakeLists.txt
+++ b/compiler-rt/CMakeLists.txt
@@ -36,6 +36,7 @@ include(SetPlatformToolchainTools)
 include(base-config-ix)
 include(CompilerRTUtils)
 include(CMakeDependentOption)
+include(GetDarwinLinkerVersion)
 
 option(COMPILER_RT_BUILD_BUILTINS "Build builtins" ON)
 mark_as_advanced(COMPILER_RT_BUILD_BUILTINS)
@@ -444,6 +445,15 @@ else()
   set(SANITIZER_USE_SYMBOLS FALSE)
 endif()
 
+# Get the linker version while configuring compiler-rt and explicitly pass it
+# in cflags during testing. This fixes the compiler/linker version mismatch
+# issue when running a clang built with a newer Xcode in an older Xcode
+set(HOST_LINK_VERSION)
+if (APPLE AND NOT CMAKE_LINKER MATCHES ".*lld.*")
+  get_darwin_linker_version()
+  message(STATUS "Host linker version: ${HOST_LINK_VERSION}")
+endif()
+
 # Build sanitizer runtimes with debug info.
 if(MSVC)
   # Use /Z7 instead of /Zi for the asan runtime. This avoids the LNK4099
diff --git a/compiler-rt/test/lit.common.cfg.py 
b/compiler-rt/test/lit.common.cfg.py
index bd9b926c1505b1..cc59a794e11352 100644
--- a/compiler-rt/test/lit.common.cfg.py
+++ b/compiler-rt/test/lit.common.cfg.py
@@ -882,6 +882,10 @@ def is_windows_lto_supported():
 elif config.use_lld and (not config.has_lld):
 config.unsupported = True
 
+if config.host_os == "Darwin":
+if getattr(config, "host_link_version", None):
+extra_cflags += ["-mlinker-version=" + config.host_link_version]
+
 # Append any extra flags passed in lit_config
 append_target_cflags = lit_config.params.get("append_target_cflags", None)
 if append_target_cflags:
diff --git a/compiler-rt/test/lit.common.configured.in 
b/compiler-rt/test/lit.common.configured.in
index db5d7c598b7310..28c0f02a03976c 100644
--- a/compiler-rt/test/lit.common.configured.in
+++