[clang] [compiler-rt] [llvm] [CMake] Use Clang to infer the target triple (PR #89425)
Petr Hosek via cfe-commits
cfe-commits at lists.llvm.org
Tue Jun 25 00:48:59 PDT 2024
https://github.com/petrhosek updated https://github.com/llvm/llvm-project/pull/89425
>From eb2459876526b78c97b04801dd07bd53540b2892 Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Tue, 15 Feb 2022 22:59:58 -0800
Subject: [PATCH 1/2] [CMake] Use Clang to infer the target triple
When using Clang as a compiler, use Clang to normalize the triple that's
used to construct path for runtime library build and install paths. This
ensures that paths are consistent and avoids the issue where the build
uses a different triple spelling.
Differential Revision: https://reviews.llvm.org/D140925
---
clang/cmake/caches/Fuchsia-stage2.cmake | 2 +-
compiler-rt/lib/builtins/CMakeLists.txt | 13 +++++++++++++
runtimes/CMakeLists.txt | 14 ++++++++++++++
3 files changed, 28 insertions(+), 1 deletion(-)
diff --git a/clang/cmake/caches/Fuchsia-stage2.cmake b/clang/cmake/caches/Fuchsia-stage2.cmake
index d5546e20873b3..029c069997396 100644
--- a/clang/cmake/caches/Fuchsia-stage2.cmake
+++ b/clang/cmake/caches/Fuchsia-stage2.cmake
@@ -142,7 +142,7 @@ if(WIN32 OR LLVM_WINSYSROOT)
set(RUNTIMES_${target}_CMAKE_MODULE_LINKER_FLAGS ${WINDOWS_LINK_FLAGS} CACHE STRING "")
endif()
-foreach(target aarch64-unknown-linux-gnu;armv7-unknown-linux-gnueabihf;i386-unknown-linux-gnu;riscv64-unknown-linux-gnu;x86_64-unknown-linux-gnu)
+foreach(target aarch64-linux-gnu;armv7-linux-gnueabihf;i386-linux-gnu;riscv64-linux-gnu;x86_64-linux-gnu)
if(LINUX_${target}_SYSROOT)
# Set the per-target builtins options.
list(APPEND BUILTIN_TARGETS "${target}")
diff --git a/compiler-rt/lib/builtins/CMakeLists.txt b/compiler-rt/lib/builtins/CMakeLists.txt
index f9611574a562b..4c6de992204c1 100644
--- a/compiler-rt/lib/builtins/CMakeLists.txt
+++ b/compiler-rt/lib/builtins/CMakeLists.txt
@@ -28,6 +28,19 @@ if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
if (NOT LLVM_RUNTIMES_BUILD)
load_llvm_config()
endif()
+ if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)
+ set(print_target_triple ${CMAKE_CXX_COMPILER} --target=${LLVM_RUNTIME_TRIPLE} -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_RUNTIME_TRIPLE ${output})
+ else()
+ string(REPLACE ";" " " print_target_triple "${print_target_triple}")
+ message(WARNING "Failed to execute `${print_target_triple}` to normalize target triple.")
+ endif()
+ endif()
construct_compiler_rt_default_triple()
include(SetPlatformToolchainTools)
diff --git a/runtimes/CMakeLists.txt b/runtimes/CMakeLists.txt
index 6f24fbcccec95..bb1f544706f2b 100644
--- a/runtimes/CMakeLists.txt
+++ b/runtimes/CMakeLists.txt
@@ -181,6 +181,20 @@ message(STATUS "LLVM default target triple: ${LLVM_DEFAULT_TARGET_TRIPLE}")
set(LLVM_TARGET_TRIPLE "${LLVM_DEFAULT_TARGET_TRIPLE}")
+if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)
+ set(print_target_triple ${CMAKE_CXX_COMPILER} --target=${LLVM_RUNTIME_TRIPLE} -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_RUNTIME_TRIPLE ${output})
+ else()
+ string(REPLACE ";" " " print_target_triple "${print_target_triple}")
+ message(WARNING "Failed to execute `${print_target_triple}` to normalize target triple.")
+ endif()
+endif()
+
option(LLVM_INCLUDE_TESTS "Generate build targets for the runtimes unit tests." ON)
option(LLVM_INCLUDE_DOCS "Generate build targets for the runtimes documentation." ON)
option(LLVM_ENABLE_SPHINX "Use Sphinx to generate the runtimes documentation." OFF)
>From 302fa322f445a40a299c2ff37bf215fb3d4754fd Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Tue, 25 Jun 2024 07:48:03 +0000
Subject: [PATCH 2/2] Update the patch to incorporate changes from #89234
---
compiler-rt/cmake/Modules/CompilerRTUtils.cmake | 16 ++++++++++++----
compiler-rt/lib/builtins/CMakeLists.txt | 13 -------------
runtimes/CMakeLists.txt | 10 +++++++---
3 files changed, 19 insertions(+), 20 deletions(-)
diff --git a/compiler-rt/cmake/Modules/CompilerRTUtils.cmake b/compiler-rt/cmake/Modules/CompilerRTUtils.cmake
index 9c7fe64d0bd35..b77b376b95f05 100644
--- a/compiler-rt/cmake/Modules/CompilerRTUtils.cmake
+++ b/compiler-rt/cmake/Modules/CompilerRTUtils.cmake
@@ -368,14 +368,22 @@ macro(construct_compiler_rt_default_triple)
"Default triple for which compiler-rt runtimes will be built.")
endif()
- if ("${CMAKE_C_COMPILER_ID}" MATCHES "Clang")
+ if(CMAKE_C_COMPILER_ID MATCHES "Clang")
set(option_prefix "")
if (CMAKE_C_SIMULATE_ID MATCHES "MSVC")
set(option_prefix "/clang:")
endif()
- execute_process(COMMAND ${CMAKE_C_COMPILER} ${option_prefix}--target=${COMPILER_RT_DEFAULT_TARGET_TRIPLE} ${option_prefix}-print-target-triple
- OUTPUT_VARIABLE COMPILER_RT_DEFAULT_TARGET_TRIPLE
- OUTPUT_STRIP_TRAILING_WHITESPACE)
+ set(print_target_triple ${CMAKE_C_COMPILER} ${option_prefix}--target=${COMPILER_RT_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(COMPILER_RT_DEFAULT_TARGET_TRIPLE ${output})
+ else()
+ string(REPLACE ";" " " print_target_triple "${print_target_triple}")
+ message(WARNING "Failed to execute `${print_target_triple}` to normalize target triple.")
+ endif()
endif()
string(REPLACE "-" ";" LLVM_TARGET_TRIPLE_LIST ${COMPILER_RT_DEFAULT_TARGET_TRIPLE})
diff --git a/compiler-rt/lib/builtins/CMakeLists.txt b/compiler-rt/lib/builtins/CMakeLists.txt
index 53c2a3bfd2eb1..ba81c78f16608 100644
--- a/compiler-rt/lib/builtins/CMakeLists.txt
+++ b/compiler-rt/lib/builtins/CMakeLists.txt
@@ -28,19 +28,6 @@ if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
if (NOT LLVM_RUNTIMES_BUILD)
load_llvm_config()
endif()
- if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)
- set(print_target_triple ${CMAKE_CXX_COMPILER} --target=${LLVM_RUNTIME_TRIPLE} -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_RUNTIME_TRIPLE ${output})
- else()
- string(REPLACE ";" " " print_target_triple "${print_target_triple}")
- message(WARNING "Failed to execute `${print_target_triple}` to normalize target triple.")
- endif()
- endif()
construct_compiler_rt_default_triple()
include(SetPlatformToolchainTools)
diff --git a/runtimes/CMakeLists.txt b/runtimes/CMakeLists.txt
index c3646a18b6689..ca176dc88a6c9 100644
--- a/runtimes/CMakeLists.txt
+++ b/runtimes/CMakeLists.txt
@@ -183,14 +183,18 @@ message(STATUS "LLVM default target triple: ${LLVM_DEFAULT_TARGET_TRIPLE}")
set(LLVM_TARGET_TRIPLE "${LLVM_DEFAULT_TARGET_TRIPLE}")
-if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)
- set(print_target_triple ${CMAKE_CXX_COMPILER} --target=${LLVM_RUNTIME_TRIPLE} -print-target-triple)
+if(CMAKE_C_COMPILER_ID MATCHES "Clang")
+ set(option_prefix "")
+ if (CMAKE_C_SIMULATE_ID 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_RUNTIME_TRIPLE ${output})
+ set(LLVM_DEFAULT_TARGET_TRIPLE ${output})
else()
string(REPLACE ";" " " print_target_triple "${print_target_triple}")
message(WARNING "Failed to execute `${print_target_triple}` to normalize target triple.")
More information about the cfe-commits
mailing list