runtimes: Pass CMAKE_SYSTEM_NAME based on target triple (#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 <noreply@anthropic.com>
This commit is contained in:
Matt Arsenault 2026-06-22 16:32:46 +02:00 committed by GitHub
parent c79333e16d
commit 6e4e181c83
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 212 additions and 73 deletions

View file

@ -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

View file

@ -0,0 +1,89 @@
#===--------------------------------------------------------------------===//
#
# 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". This expects a
# normalized triple.
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)
set(_env "")
if(_len GREATER_EQUAL 4)
list(GET _components 3 _env)
endif()
# Check the special environment components first, since it can
# override the usual OS mapping.
if("${_env}" MATCHES "^android")
set(${out_var} "Android" PARENT_SCOPE)
elseif("${_env}" MATCHES "^cygnus")
set(${out_var} "CYGWIN" PARENT_SCOPE)
elseif("${_os}" MATCHES "^darwin|^macos")
set(${out_var} "Darwin" PARENT_SCOPE)
elseif("${_os}" MATCHES "^ios")
set(${out_var} "iOS" PARENT_SCOPE)
elseif("${_os}" MATCHES "^tvos")
set(${out_var} "tvOS" PARENT_SCOPE)
elseif("${_os}" MATCHES "^watchos")
set(${out_var} "watchOS" PARENT_SCOPE)
elseif("${_os}" MATCHES "^xros|^visionos")
set(${out_var} "visionOS" PARENT_SCOPE)
elseif("${_vendor}" STREQUAL "apple")
# Catch-all for other Apple triples (e.g. driverkit, bridgeos).
set(${out_var} "Darwin" PARENT_SCOPE)
elseif("${_os}" MATCHES "^linux")
set(${out_var} "Linux" PARENT_SCOPE)
elseif("${_os}" MATCHES "^windows")
set(${out_var} "Windows" PARENT_SCOPE)
elseif("${_os}" MATCHES "^freebsd|^kfreebsd")
set(${out_var} "FreeBSD" 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 "^fuchsia")
set(${out_var} "Fuchsia" PARENT_SCOPE)
elseif("${_os}" MATCHES "^haiku")
set(${out_var} "Haiku" PARENT_SCOPE)
elseif("${_os}" MATCHES "^emscripten")
set(${out_var} "Emscripten" 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()

View file

@ -0,0 +1,36 @@
#===--------------------------------------------------------------------===//
#
# 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
#
#===--------------------------------------------------------------------===//
# Normalize a target triple using clang's -print-target-triple.
#
# Usage:
# normalize_triple(<compiler> <triple> <out_var>)
#
# Runs <compiler> --target=<triple> -print-target-triple to produce a
# canonical triple. If the compiler invocation fails (e.g. the compiler
# is not clang), <triple> is returned unchanged.
function(normalize_triple compiler triple out_var)
set(_prefix "")
if(CMAKE_C_COMPILER_FRONTEND_VARIANT MATCHES "MSVC")
set(_prefix "/clang:")
endif()
execute_process(
COMMAND "${compiler}" "${_prefix}--target=${triple}" "${_prefix}-print-target-triple"
RESULT_VARIABLE _result
OUTPUT_VARIABLE _output
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET)
if(_result EQUAL 0 AND _output)
set(${out_var} "${_output}" PARENT_SCOPE)
else()
# TODO(#97876): Report an error.
message(WARNING "Failed to execute `${compiler} ${_prefix}--target=${triple} ${_prefix}-print-target-triple` to normalize target triple.")
set(${out_var} "${triple}" PARENT_SCOPE)
endif()
endfunction()

View file

@ -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()

View file

@ -84,12 +84,6 @@ function(llvm_ExternalProject_Add name source_dir)
endif()
endforeach()
# If CMAKE_SYSTEM_NAME is not set explicitly in the arguments passed to us,
# reflect CMake's own default.
if (NOT _cmake_system_name)
set(_cmake_system_name "${CMAKE_HOST_SYSTEM_NAME}")
endif()
if(NOT ARG_TARGET_TRIPLE)
set(target_triple ${LLVM_DEFAULT_TARGET_TRIPLE})
else()
@ -98,6 +92,36 @@ function(llvm_ExternalProject_Add name source_dir)
is_msvc_triple(is_msvc_target "${target_triple}")
if(ARG_USE_TOOLCHAIN AND NOT CMAKE_CROSSCOMPILING)
set(_cmake_c_compiler "${LLVM_RUNTIME_OUTPUT_INTDIR}/clang${CMAKE_EXECUTABLE_SUFFIX}")
set(_cmake_cxx_compiler "${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++${CMAKE_EXECUTABLE_SUFFIX}")
set(_cmake_asm_compiler "${_cmake_c_compiler}")
if(is_msvc_target)
set(_cmake_c_compiler "${LLVM_RUNTIME_OUTPUT_INTDIR}/clang-cl${CMAKE_EXECUTABLE_SUFFIX}")
set(_cmake_cxx_compiler "${_cmake_c_compiler}")
set(_cmake_asm_compiler "${_cmake_c_compiler}")
endif()
else()
set(_cmake_c_compiler "${CMAKE_C_COMPILER}")
set(_cmake_cxx_compiler "${CMAKE_CXX_COMPILER}")
set(_cmake_asm_compiler "${CMAKE_C_COMPILER}")
endif()
# If CMAKE_SYSTEM_NAME is not set explicitly in the arguments passed to us,
# 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)
if(ARG_TARGET_TRIPLE)
include(NormalizeTriple)
normalize_triple("${_cmake_c_compiler}" "${ARG_TARGET_TRIPLE}" _normalized_triple)
include(GetTripleCMakeSystemName)
get_triple_cmake_system_name("${_normalized_triple}" _cmake_system_name)
else()
set(_cmake_system_name "${CMAKE_HOST_SYSTEM_NAME}")
endif()
endif()
if(NOT ARG_TOOLCHAIN_TOOLS)
set(ARG_TOOLCHAIN_TOOLS clang)
if (ARG_ENABLE_FORTRAN)
@ -231,15 +255,9 @@ function(llvm_ExternalProject_Add name source_dir)
if(ARG_USE_TOOLCHAIN AND NOT CMAKE_CROSSCOMPILING)
if(CLANG_IN_TOOLCHAIN)
if(is_msvc_target)
set(compiler_args -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang-cl${CMAKE_EXECUTABLE_SUFFIX}
-DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang-cl${CMAKE_EXECUTABLE_SUFFIX}
-DCMAKE_ASM_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang-cl${CMAKE_EXECUTABLE_SUFFIX})
else()
set(compiler_args -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang${CMAKE_EXECUTABLE_SUFFIX}
-DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++${CMAKE_EXECUTABLE_SUFFIX}
-DCMAKE_ASM_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang${CMAKE_EXECUTABLE_SUFFIX})
endif()
set(compiler_args -DCMAKE_C_COMPILER=${_cmake_c_compiler}
-DCMAKE_CXX_COMPILER=${_cmake_cxx_compiler}
-DCMAKE_ASM_COMPILER=${_cmake_asm_compiler})
endif()
if(FLANG_IN_TOOLCHAIN)
list(APPEND compiler_args -DCMAKE_Fortran_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/flang${CMAKE_EXECUTABLE_SUFFIX})
@ -379,6 +397,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)

View file

@ -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})

View file

@ -228,22 +228,8 @@ message(STATUS "LLVM default target triple: ${LLVM_DEFAULT_TARGET_TRIPLE}")
set(LLVM_TARGET_TRIPLE "${LLVM_DEFAULT_TARGET_TRIPLE}")
if(CMAKE_C_COMPILER_ID MATCHES "Clang")
set(option_prefix "")
if (CMAKE_C_COMPILER_FRONTEND_VARIANT MATCHES "MSVC")
set(option_prefix "/clang:")
endif()
set(print_target_triple ${CMAKE_C_COMPILER} ${option_prefix}--target=${LLVM_DEFAULT_TARGET_TRIPLE} ${option_prefix}-print-target-triple)
execute_process(COMMAND ${print_target_triple}
RESULT_VARIABLE result
OUTPUT_VARIABLE output
OUTPUT_STRIP_TRAILING_WHITESPACE)
if(result EQUAL 0)
set(LLVM_DEFAULT_TARGET_TRIPLE ${output})
else()
string(REPLACE ";" " " print_target_triple "${print_target_triple}")
# TODO(#97876): Report an error.
message(WARNING "Failed to execute `${print_target_triple}` to normalize target triple.")
endif()
include(NormalizeTriple)
normalize_triple("${CMAKE_C_COMPILER}" "${LLVM_DEFAULT_TARGET_TRIPLE}" LLVM_DEFAULT_TARGET_TRIPLE)
endif()
# Determine output and install paths based on LLVM_TARGET_TRIPLE