Stop the build if the linker produces an error code. Otherwise, the numbers in
the linker's error message can be misinterpreted as a linker version number, the
build completes successfully, and the resulting compiler doesn't work because
the supposed linker version number is invalid.

https://github.com/llvm/llvm-project/issues/186194
--- a/clang/CMakeLists.txt
+++ b/clang/CMakeLists.txt
@@ -391,9 +391,9 @@
 if (APPLE)
   set(LD_V_OUTPUT)
   execute_process(
-    COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
+    COMMAND "${CMAKE_LINKER}" -v
     RESULT_VARIABLE HAD_ERROR
-    OUTPUT_VARIABLE LD_V_OUTPUT
+    ERROR_VARIABLE LD_V_OUTPUT
   )
   if (HAD_ERROR)
     message(FATAL_ERROR "${CMAKE_LINKER} failed with status ${HAD_ERROR}")
--- a/flang/CMakeLists.txt
+++ b/flang/CMakeLists.txt
@@ -373,9 +373,10 @@
 if (APPLE)
   set(LD_V_OUTPUT)
   execute_process(
-    COMMAND sh -c "${CMAKE_LINKER} -v 2>&1 | head -1"
+    COMMAND "${CMAKE_LINKER}" -v
     RESULT_VARIABLE HAD_ERROR
-    OUTPUT_VARIABLE LD_V_OUTPUT)
+    ERROR_VARIABLE LD_V_OUTPUT
+  )
   if (NOT HAD_ERROR)
     if ("${LD_V_OUTPUT}" MATCHES ".*ld64-([0-9.]+).*")
       string(REGEX REPLACE ".*ld64-([0-9.]+).*" "\\1" HOST_LINK_VERSION ${LD_V_OUTPUT})
