Skip to content

Commit 7227b44

Browse files
authored
[clang][driver] Fix -print-target-triple OS version for apple targets (#104037)
The target needs to be initialized in order to compute the correct target triple from the command line. Without initialized targets the OS component of the triple might not reflect what would be computed by the driver for an actual compiler invocation. Fixes #61762
1 parent 9a9ce91 commit 7227b44

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

clang/lib/Driver/Driver.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2271,8 +2271,7 @@ bool Driver::HandleImmediateArgs(Compilation &C) {
22712271
return false;
22722272
}
22732273

2274-
if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) {
2275-
ToolChain::RuntimeLibType RLT = TC.GetRuntimeLibType(C.getArgs());
2274+
auto initializeTargets = [&]() {
22762275
const llvm::Triple Triple(TC.ComputeEffectiveClangTriple(C.getArgs()));
22772276
// The 'Darwin' toolchain is initialized only when its arguments are
22782277
// computed. Get the default arguments for OFK_None to ensure that
@@ -2282,6 +2281,12 @@ bool Driver::HandleImmediateArgs(Compilation &C) {
22822281
// FIXME: For some more esoteric targets the default toolchain is not the
22832282
// correct one.
22842283
C.getArgsForToolChain(&TC, Triple.getArchName(), Action::OFK_None);
2284+
return Triple;
2285+
};
2286+
2287+
if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) {
2288+
ToolChain::RuntimeLibType RLT = TC.GetRuntimeLibType(C.getArgs());
2289+
const llvm::Triple Triple = initializeTargets();
22852290
RegisterEffectiveTriple TripleRAII(TC, Triple);
22862291
switch (RLT) {
22872292
case ToolChain::RLT_CompilerRT:
@@ -2325,7 +2330,9 @@ bool Driver::HandleImmediateArgs(Compilation &C) {
23252330
}
23262331

23272332
if (C.getArgs().hasArg(options::OPT_print_target_triple)) {
2328-
llvm::outs() << TC.getTripleString() << "\n";
2333+
initializeTargets();
2334+
llvm::Triple Triple(TC.ComputeEffectiveClangTriple(C.getArgs()));
2335+
llvm::outs() << Triple.getTriple() << "\n";
23292336
return false;
23302337
}
23312338

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Test the output of -print-target-triple on Darwin.
2+
// See https://github.com/llvm/llvm-project/issues/61762
3+
4+
//
5+
// All platforms
6+
//
7+
8+
// RUN: %clang -print-target-triple \
9+
// RUN: --target=x86_64-apple-macos -mmacos-version-min=15 \
10+
// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
11+
// RUN: | FileCheck --check-prefix=CHECK-CLANGRT-MACOS %s
12+
// CHECK-CLANGRT-MACOS: x86_64-apple-macosx15.0.0
13+
14+
// RUN: %clang -print-target-triple \
15+
// RUN: --target=arm64-apple-ios -mios-version-min=9 \
16+
// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
17+
// RUN: | FileCheck --check-prefix=CHECK-CLANGRT-IOS %s
18+
// CHECK-CLANGRT-IOS: arm64-apple-ios9.0.0
19+
20+
// RUN: %clang -print-target-triple \
21+
// RUN: --target=arm64-apple-watchos -mwatchos-version-min=3 \
22+
// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
23+
// RUN: | FileCheck --check-prefix=CHECK-CLANGRT-WATCHOS %s
24+
// CHECK-CLANGRT-WATCHOS: arm64-apple-watchos3.0.0
25+
26+
// RUN: %clang -print-target-triple \
27+
// RUN: --target=armv7k-apple-watchos -mwatchos-version-min=3 \
28+
// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
29+
// RUN: | FileCheck --check-prefix=CHECK-CLANGRT-WATCHOS-ARMV7K %s
30+
// CHECK-CLANGRT-WATCHOS-ARMV7K: thumbv7-apple-watchos3.0.0
31+
32+
// RUN: %clang -print-target-triple \
33+
// RUN: --target=arm64-apple-tvos -mtvos-version-min=1\
34+
// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
35+
// RUN: | FileCheck --check-prefix=CHECK-CLANGRT-TVOS %s
36+
// CHECK-CLANGRT-TVOS: arm64-apple-tvos1.0.0
37+
38+
// RUN: %clang -print-target-triple \
39+
// RUN: --target=arm64-apple-driverkit \
40+
// RUN: -resource-dir=%S/Inputs/resource_dir 2>&1 \
41+
// RUN: | FileCheck --check-prefix=CHECK-CLANGRT-DRIVERKIT %s
42+
// CHECK-CLANGRT-DRIVERKIT: arm64-apple-driverkit19.0.0

0 commit comments

Comments
 (0)