mirror-llvm-project/cmake/Modules/NormalizeTriple.cmake
Matt Arsenault 6e4e181c83
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>
2026-06-22 16:32:46 +02:00

36 lines
1.4 KiB
CMake

#===--------------------------------------------------------------------===//
#
# 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()