Skip to content

Commit 9cb9a97

Browse files
authored
[CMake] Use Clang to infer the target triple (#89425)
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
1 parent ccf357f commit 9cb9a97

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

clang/cmake/caches/Fuchsia-stage2.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ if(WIN32 OR LLVM_WINSYSROOT)
141141
set(RUNTIMES_${target}_CMAKE_MODULE_LINKER_FLAGS ${WINDOWS_LINK_FLAGS} CACHE STRING "")
142142
endif()
143143

144-
foreach(target aarch64-unknown-linux-gnu;armv7-unknown-linux-gnueabihf;i386-unknown-linux-gnu;riscv64-unknown-linux-gnu;x86_64-unknown-linux-gnu)
144+
foreach(target aarch64-linux-gnu;armv7-linux-gnueabihf;i386-linux-gnu;riscv64-linux-gnu;x86_64-linux-gnu)
145145
if(LINUX_${target}_SYSROOT)
146146
# Set the per-target builtins options.
147147
list(APPEND BUILTIN_TARGETS "${target}")

compiler-rt/cmake/Modules/CompilerRTUtils.cmake

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -368,14 +368,23 @@ macro(construct_compiler_rt_default_triple)
368368
"Default triple for which compiler-rt runtimes will be built.")
369369
endif()
370370

371-
if ("${CMAKE_C_COMPILER_ID}" MATCHES "Clang")
371+
if(CMAKE_C_COMPILER_ID MATCHES "Clang")
372372
set(option_prefix "")
373373
if (CMAKE_C_SIMULATE_ID MATCHES "MSVC")
374374
set(option_prefix "/clang:")
375375
endif()
376-
execute_process(COMMAND ${CMAKE_C_COMPILER} ${option_prefix}--target=${COMPILER_RT_DEFAULT_TARGET_TRIPLE} ${option_prefix}-print-target-triple
377-
OUTPUT_VARIABLE COMPILER_RT_DEFAULT_TARGET_TRIPLE
378-
OUTPUT_STRIP_TRAILING_WHITESPACE)
376+
set(print_target_triple ${CMAKE_C_COMPILER} ${option_prefix}--target=${COMPILER_RT_DEFAULT_TARGET_TRIPLE} ${option_prefix}-print-target-triple)
377+
execute_process(COMMAND ${print_target_triple}
378+
RESULT_VARIABLE result
379+
OUTPUT_VARIABLE output
380+
OUTPUT_STRIP_TRAILING_WHITESPACE)
381+
if(result EQUAL 0)
382+
set(COMPILER_RT_DEFAULT_TARGET_TRIPLE ${output})
383+
else()
384+
string(REPLACE ";" " " print_target_triple "${print_target_triple}")
385+
# TODO(#97876): Report an error.
386+
message(WARNING "Failed to execute `${print_target_triple}` to normalize target triple.")
387+
endif()
379388
endif()
380389

381390
string(REPLACE "-" ";" LLVM_TARGET_TRIPLE_LIST ${COMPILER_RT_DEFAULT_TARGET_TRIPLE})

runtimes/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,25 @@ message(STATUS "LLVM default target triple: ${LLVM_DEFAULT_TARGET_TRIPLE}")
183183

184184
set(LLVM_TARGET_TRIPLE "${LLVM_DEFAULT_TARGET_TRIPLE}")
185185

186+
if(CMAKE_C_COMPILER_ID MATCHES "Clang")
187+
set(option_prefix "")
188+
if (CMAKE_C_SIMULATE_ID MATCHES "MSVC")
189+
set(option_prefix "/clang:")
190+
endif()
191+
set(print_target_triple ${CMAKE_C_COMPILER} ${option_prefix}--target=${LLVM_DEFAULT_TARGET_TRIPLE} ${option_prefix}-print-target-triple)
192+
execute_process(COMMAND ${print_target_triple}
193+
RESULT_VARIABLE result
194+
OUTPUT_VARIABLE output
195+
OUTPUT_STRIP_TRAILING_WHITESPACE)
196+
if(result EQUAL 0)
197+
set(LLVM_DEFAULT_TARGET_TRIPLE ${output})
198+
else()
199+
string(REPLACE ";" " " print_target_triple "${print_target_triple}")
200+
# TODO(#97876): Report an error.
201+
message(WARNING "Failed to execute `${print_target_triple}` to normalize target triple.")
202+
endif()
203+
endif()
204+
186205
option(LLVM_INCLUDE_TESTS "Generate build targets for the runtimes unit tests." ON)
187206
option(LLVM_INCLUDE_DOCS "Generate build targets for the runtimes documentation." ON)
188207
option(LLVM_ENABLE_SPHINX "Use Sphinx to generate the runtimes documentation." OFF)

0 commit comments

Comments
 (0)