https://github.com/arsenm created 
https://github.com/llvm/llvm-project/pull/203504

Compute the cmake system name from the target triple, rather
than passing through the host's. This is primarily to stop
forwarding OSX specific cmake variables.

This fixes build failures when trying to build gpu libc on mac
hosts. Previously it would fail on several issues, starting with
an unused argument -mmacos-version-min error, followed by other
errors caused by passing -isysroot.

Secondarily, restrict the cmake imported targets when cross compiling.
Without this, the amdgpu build prints many cmake warnings about the
target not supporting shared libraries.

Claude did most of the actual work, though it required quite a few
rounds of prodding to get it into the right place. In particular it
took care of handling all of the cmake platform recognized names from
the triple.

Co-authored-by: Claude Opus 4.6 <[email protected]>

>From 33c56b4a353da854ce95508f91ab7a823b85d441 Mon Sep 17 00:00:00 2001
From: Matt Arsenault <[email protected]>
Date: Thu, 11 Jun 2026 20:34:18 +0200
Subject: [PATCH] runtimes: Pass CMAKE_SYSTEM_NAME based on target triple

Compute the cmake system name from the target triple, rather
than passing through the host's. This is primarily to stop
forwarding OSX specific cmake variables.

This fixes build failures when trying to build gpu libc on mac
hosts. Previously it would fail on several issues, starting with
an unused argument -mmacos-version-min error, followed by other
errors caused by passing -isysroot.

Secondarily, restrict the cmake imported targets when cross compiling.
Without this, the amdgpu build prints many cmake warnings about the
target not supporting shared libraries.

Claude did most of the actual work, though it required quite a few
rounds of prodding to get it into the right place. In particular it
took care of handling all of the cmake platform recognized names from
the triple.

Co-authored-by: Claude Opus 4.6 <[email protected]>
---
 clang/cmake/modules/ClangConfig.cmake.in      |  5 +-
 cmake/Modules/GetTripleCMakeSystemName.cmake  | 65 +++++++++++++++++
 llvm/cmake/modules/LLVMConfig.cmake.in        | 69 +++++++++----------
 .../modules/LLVMExternalProjectUtils.cmake    | 27 +++++++-
 llvm/runtimes/CMakeLists.txt                  |  4 --
 5 files changed, 126 insertions(+), 44 deletions(-)
 create mode 100644 cmake/Modules/GetTripleCMakeSystemName.cmake

diff --git a/clang/cmake/modules/ClangConfig.cmake.in 
b/clang/cmake/modules/ClangConfig.cmake.in
index 68f723d050117..e199c7e17b6b7 100644
--- a/clang/cmake/modules/ClangConfig.cmake.in
+++ b/clang/cmake/modules/ClangConfig.cmake.in
@@ -13,7 +13,10 @@ set(CLANG_LINK_CLANG_DYLIB "@CLANG_LINK_CLANG_DYLIB@")
 set(CLANG_DEFAULT_LINKER "@CLANG_DEFAULT_LINKER@")
 
 # Provide all our library targets to users.
-@CLANG_CONFIG_INCLUDE_EXPORTS@
+# Skip when cross-compiling, as host library targets are not usable.
+if(NOT CMAKE_CROSSCOMPILING)
+  @CLANG_CONFIG_INCLUDE_EXPORTS@
+endif()
 
 # By creating clang-tablegen-targets here, subprojects that depend on Clang's
 # tablegen-generated headers can always depend on this target whether building
diff --git a/cmake/Modules/GetTripleCMakeSystemName.cmake 
b/cmake/Modules/GetTripleCMakeSystemName.cmake
new file mode 100644
index 0000000000000..9bfb90d630387
--- /dev/null
+++ b/cmake/Modules/GetTripleCMakeSystemName.cmake
@@ -0,0 +1,65 @@
+#===--------------------------------------------------------------------===//
+#
+# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+# See https://llvm.org/LICENSE.txt for details.
+# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+#
+#===--------------------------------------------------------------------===//
+
+# Extract the OS component from a target triple and map it to the
+# corresponding CMake system name.
+#
+# Usage:
+#   get_triple_cmake_system_name(<triple> <out_var>)
+#
+# Parses the triple (arch-vendor-os[-env]) and sets <out_var> to the
+# CMake-style system name (e.g. "Darwin", "Linux", "Windows").
+# Unrecognized OS values are mapped to "Generic".
+
+function(get_triple_cmake_system_name triple out_var)
+  string(REPLACE "-" ";" _components "${triple}")
+  list(LENGTH _components _len)
+  if(_len LESS 3)
+    set(${out_var} "${CMAKE_HOST_SYSTEM_NAME}" PARENT_SCOPE)
+    return()
+  endif()
+
+  list(GET _components 1 _vendor)
+  list(GET _components 2 _os)
+
+  if("${_vendor}" STREQUAL "apple" OR "${_os}" MATCHES "^darwin|^macos")
+    set(${out_var} "Darwin" PARENT_SCOPE)
+  elseif("${_os}" MATCHES "^linux")
+    set(${out_var} "Linux" PARENT_SCOPE)
+  elseif("${_os}" MATCHES "^win32|^windows")
+    set(${out_var} "Windows" PARENT_SCOPE)
+  elseif("${_os}" MATCHES "^freebsd|^kfreebsd")
+    set(${out_var} "FreeBSD" PARENT_SCOPE)
+  elseif("${_os}" MATCHES "^fuchsia")
+    set(${out_var} "Fuchsia" PARENT_SCOPE)
+  elseif("${_os}" MATCHES "^netbsd")
+    set(${out_var} "NetBSD" PARENT_SCOPE)
+  elseif("${_os}" MATCHES "^openbsd")
+    set(${out_var} "OpenBSD" PARENT_SCOPE)
+  elseif("${_os}" MATCHES "^dragonfly")
+    set(${out_var} "DragonFly" PARENT_SCOPE)
+  elseif("${_os}" MATCHES "^solaris")
+    set(${out_var} "SunOS" PARENT_SCOPE)
+  elseif("${_os}" MATCHES "^aix")
+    set(${out_var} "AIX" PARENT_SCOPE)
+  elseif("${_os}" MATCHES "^haiku")
+    set(${out_var} "Haiku" PARENT_SCOPE)
+  elseif("${_os}" MATCHES "^wasi")
+    set(${out_var} "WASI" PARENT_SCOPE)
+  elseif("${_os}" MATCHES "^rtems")
+    set(${out_var} "RTEMS" PARENT_SCOPE)
+  elseif("${_os}" MATCHES "^zos")
+    set(${out_var} "OS390" PARENT_SCOPE)
+  elseif("${_os}" MATCHES "^hurd")
+    set(${out_var} "GNU" PARENT_SCOPE)
+  elseif("${_os}" MATCHES "^serenity")
+    set(${out_var} "SerenityOS" PARENT_SCOPE)
+  else()
+    set(${out_var} "Generic" PARENT_SCOPE)
+  endif()
+endfunction()
diff --git a/llvm/cmake/modules/LLVMConfig.cmake.in 
b/llvm/cmake/modules/LLVMConfig.cmake.in
index 300c25e7c6101..6ef0cef7d0296 100644
--- a/llvm/cmake/modules/LLVMConfig.cmake.in
+++ b/llvm/cmake/modules/LLVMConfig.cmake.in
@@ -56,52 +56,47 @@ set(LLVM_ENABLE_ASSERTIONS @LLVM_ENABLE_ASSERTIONS@)
 set(LLVM_ENABLE_EH @LLVM_ENABLE_EH@)
 
 set(LLVM_ENABLE_FFI @LLVM_ENABLE_FFI@)
-if(LLVM_ENABLE_FFI)
-  find_package(FFI)
-endif()
-
 set(LLVM_ENABLE_RTTI @LLVM_ENABLE_RTTI@)
-
-set(LLVM_ENABLE_LIBEDIT @HAVE_LIBEDIT@)
-if(LLVM_ENABLE_LIBEDIT)
-  find_package(LibEdit)
-endif()
-
 set(LLVM_ENABLE_THREADS @LLVM_ENABLE_THREADS@)
-
 set(LLVM_ENABLE_UNWIND_TABLES @LLVM_ENABLE_UNWIND_TABLES@)
-
 set(LLVM_ENABLE_ZLIB @LLVM_ENABLE_ZLIB@)
-if(LLVM_ENABLE_ZLIB)
-  set(ZLIB_ROOT @ZLIB_ROOT@)
-  find_package(ZLIB)
-endif()
-
 set(LLVM_ENABLE_ZSTD @LLVM_ENABLE_ZSTD@)
-if(LLVM_ENABLE_ZSTD)
-  find_package(zstd)
-endif()
-
 set(LLVM_ENABLE_LIBXML2 @LLVM_ENABLE_LIBXML2@)
-if(LLVM_ENABLE_LIBXML2)
-  find_package(LibXml2)
-endif()
-
 set(LLVM_ENABLE_CURL @LLVM_ENABLE_CURL@)
-if(LLVM_ENABLE_CURL)
-  find_package(CURL)
-endif()
-
 set(LLVM_ENABLE_HTTPLIB @LLVM_ENABLE_HTTPLIB@)
-if(LLVM_ENABLE_HTTPLIB)
-  find_package(httplib)
-endif()
-
 set(LLVM_WITH_Z3 @LLVM_WITH_Z3@)
-
 set(LLVM_ENABLE_DIA_SDK @LLVM_ENABLE_DIA_SDK@)
-if(LLVM_ENABLE_DIA_SDK)
-  find_package(DIASDK)
+set(LLVM_ENABLE_LIBEDIT @HAVE_LIBEDIT@)
+
+# These are host libraries that LLVM was built with. Only find them when the
+# consumer can actually use them (i.e. not when cross-compiling for an
+# incompatible target).
+if(NOT CMAKE_CROSSCOMPILING)
+  if(LLVM_ENABLE_FFI)
+    find_package(FFI)
+  endif()
+  if(LLVM_ENABLE_LIBEDIT)
+    find_package(LibEdit)
+  endif()
+  if(LLVM_ENABLE_ZLIB)
+    set(ZLIB_ROOT @ZLIB_ROOT@)
+    find_package(ZLIB)
+  endif()
+  if(LLVM_ENABLE_ZSTD)
+    find_package(zstd)
+  endif()
+  if(LLVM_ENABLE_LIBXML2)
+    find_package(LibXml2)
+  endif()
+  if(LLVM_ENABLE_CURL)
+    find_package(CURL)
+  endif()
+  if(LLVM_ENABLE_HTTPLIB)
+    find_package(httplib)
+  endif()
+  if(LLVM_ENABLE_DIA_SDK)
+    find_package(DIASDK)
+  endif()
 endif()
 
 set(LLVM_NATIVE_ARCH @LLVM_NATIVE_ARCH@)
@@ -152,7 +147,7 @@ set(LLVM_ENABLE_SHARED_LIBS @BUILD_SHARED_LIBS@)
 set(LLVM_DEFAULT_EXTERNAL_LIT "@LLVM_CONFIG_DEFAULT_EXTERNAL_LIT@")
 set(LLVM_LIT_ARGS "@LLVM_LIT_ARGS@")
 
-if(NOT TARGET LLVMSupport)
+if(NOT TARGET LLVMSupport AND NOT CMAKE_CROSSCOMPILING)
   @LLVM_CONFIG_INCLUDE_EXPORTS@
   @llvm_config_include_buildtree_only_exports@
 endif()
diff --git a/llvm/cmake/modules/LLVMExternalProjectUtils.cmake 
b/llvm/cmake/modules/LLVMExternalProjectUtils.cmake
index 47b12dc9a3d8c..fcb7e3c32cc2e 100644
--- a/llvm/cmake/modules/LLVMExternalProjectUtils.cmake
+++ b/llvm/cmake/modules/LLVMExternalProjectUtils.cmake
@@ -85,9 +85,16 @@ function(llvm_ExternalProject_Add name source_dir)
   endforeach()
 
   # If CMAKE_SYSTEM_NAME is not set explicitly in the arguments passed to us,
-  # reflect CMake's own default.
+  # derive it from the target triple if available, otherwise reflect CMake's
+  # own default. This ensures that cross-compilation targets get the correct
+  # platform files (e.g. AMDGPU targets on a Darwin host won't get macOS 
flags).
   if (NOT _cmake_system_name)
-    set(_cmake_system_name "${CMAKE_HOST_SYSTEM_NAME}")
+    if(ARG_TARGET_TRIPLE)
+      include(GetTripleCMakeSystemName)
+      get_triple_cmake_system_name("${ARG_TARGET_TRIPLE}" _cmake_system_name)
+    else()
+      set(_cmake_system_name "${CMAKE_HOST_SYSTEM_NAME}")
+    endif()
   endif()
 
   if(NOT ARG_TARGET_TRIPLE)
@@ -376,6 +383,22 @@ function(llvm_ExternalProject_Add name source_dir)
     list(APPEND compiler_args -DCMAKE_CXX_COMPILER_TARGET=${ARG_TARGET_TRIPLE})
     list(APPEND compiler_args 
-DCMAKE_Fortran_COMPILER_TARGET=${ARG_TARGET_TRIPLE})
     list(APPEND compiler_args -DCMAKE_ASM_COMPILER_TARGET=${ARG_TARGET_TRIPLE})
+
+    # Pass CMAKE_SYSTEM_NAME derived from the target triple so the sub-build
+    # loads the correct platform files instead of the host's.
+    if(NOT "${_cmake_system_name}" STREQUAL "${CMAKE_HOST_SYSTEM_NAME}")
+      list(APPEND compiler_args -DCMAKE_SYSTEM_NAME=${_cmake_system_name})
+    endif()
+
+    # Forward Darwin-specific variables only when targeting Darwin.
+    if("${_cmake_system_name}" STREQUAL "Darwin")
+      if(CMAKE_OSX_SYSROOT)
+        list(APPEND compiler_args -DCMAKE_OSX_SYSROOT=${CMAKE_OSX_SYSROOT})
+      endif()
+      if(CMAKE_OSX_DEPLOYMENT_TARGET)
+        list(APPEND compiler_args 
-DCMAKE_OSX_DEPLOYMENT_TARGET=${CMAKE_OSX_DEPLOYMENT_TARGET})
+      endif()
+    endif()
   endif()
 
   if(CMAKE_VERBOSE_MAKEFILE)
diff --git a/llvm/runtimes/CMakeLists.txt b/llvm/runtimes/CMakeLists.txt
index bbc91a709c553..ae08b5993412d 100644
--- a/llvm/runtimes/CMakeLists.txt
+++ b/llvm/runtimes/CMakeLists.txt
@@ -4,10 +4,6 @@
 # the two files.
 
 set(COMMON_CMAKE_ARGS 
"-DHAVE_LLVM_LIT=ON;-DCLANG_RESOURCE_DIR=${CLANG_RESOURCE_DIR}")
-if(APPLE AND CMAKE_OSX_SYSROOT AND (LLVM_TARGET_TRIPLE STREQUAL 
LLVM_HOST_TRIPLE))
-  # Only propagate the host sysroot for native runtimes builds.
-  list(APPEND RUNTIMES_CMAKE_ARGS "-DCMAKE_OSX_SYSROOT=${CMAKE_OSX_SYSROOT}")
-endif()
 foreach(proj ${LLVM_ENABLE_RUNTIMES})
   string(TOUPPER "${proj}" canon_name)
   STRING(REGEX REPLACE "-" "_" canon_name ${canon_name})

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to