[clang] [clang][driver] Fix -print-target-triple OS version for apple targets (PR #104037)

Gábor Horváth via cfe-commits cfe-commits at lists.llvm.org
Wed Aug 14 08:22:04 PDT 2024


https://github.com/Xazax-hun created https://github.com/llvm/llvm-project/pull/104037

The target needs to be initialized in order to computer the correct target triple from the command line. Without initialized targets the OS component of the tripple might not reflect what would be computer by the driver for an actual compiler invocation.

>From 02ee150e3fb3017ee55d73f4254b8a498cc85ada Mon Sep 17 00:00:00 2001
From: Gabor Horvath <gaborh at apple.com>
Date: Wed, 14 Aug 2024 16:17:40 +0100
Subject: [PATCH] Fix -print-target-triple OS version for apple targets

The target needs to be initialized in order to computer the correct
target triple from the command line. Without initialized targets the OS
component of the tripple might not reflect what would be computer by the
driver for an actual compiler invocation.
---
 clang/lib/Driver/Driver.cpp                   | 13 +++++--
 .../test/Driver/darwin-print-target-triple.c  | 35 +++++++++++++++++++
 2 files changed, 45 insertions(+), 3 deletions(-)
 create mode 100644 clang/test/Driver/darwin-print-target-triple.c

diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index e12416e51f8d24..5b95019c25cab6 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -2271,8 +2271,7 @@ bool Driver::HandleImmediateArgs(Compilation &C) {
     return false;
   }
 
-  if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) {
-    ToolChain::RuntimeLibType RLT = TC.GetRuntimeLibType(C.getArgs());
+  auto initializeTargets = [&]() {
     const llvm::Triple Triple(TC.ComputeEffectiveClangTriple(C.getArgs()));
     // The 'Darwin' toolchain is initialized only when its arguments are
     // computed. Get the default arguments for OFK_None to ensure that
@@ -2282,6 +2281,12 @@ bool Driver::HandleImmediateArgs(Compilation &C) {
     // FIXME: For some more esoteric targets the default toolchain is not the
     //        correct one.
     C.getArgsForToolChain(&TC, Triple.getArchName(), Action::OFK_None);
+    return Triple;
+  };
+
+  if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) {
+    ToolChain::RuntimeLibType RLT = TC.GetRuntimeLibType(C.getArgs());
+    const llvm::Triple Triple = initializeTargets();
     RegisterEffectiveTriple TripleRAII(TC, Triple);
     switch (RLT) {
     case ToolChain::RLT_CompilerRT:
@@ -2325,7 +2330,9 @@ bool Driver::HandleImmediateArgs(Compilation &C) {
   }
 
   if (C.getArgs().hasArg(options::OPT_print_target_triple)) {
-    llvm::outs() << TC.getTripleString() << "\n";
+    initializeTargets();
+    llvm::Triple Triple(TC.ComputeEffectiveClangTriple(C.getArgs()));
+    llvm::outs() << Triple.getTriple() << "\n";
     return false;
   }
 
diff --git a/clang/test/Driver/darwin-print-target-triple.c b/clang/test/Driver/darwin-print-target-triple.c
new file mode 100644
index 00000000000000..84f7832b83f478
--- /dev/null
+++ b/clang/test/Driver/darwin-print-target-triple.c
@@ -0,0 +1,35 @@
+// Test the output of -print-target-triple on Darwin.
+
+//
+// All platforms
+//
+
+// RUN: %clang -print-target-triple \
+// RUN:     --target=x86_64-apple-macos -mmacos-version-min=15 \
+// RUN:     -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-MACOS %s
+// CHECK-CLANGRT-MACOS: x86_64-apple-macosx15.0.0
+
+// RUN: %clang -print-target-triple \
+// RUN:     --target=arm64-apple-ios -mios-version-min=9 \
+// RUN:     -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-IOS %s
+// CHECK-CLANGRT-IOS: arm64-apple-ios9.0.0
+
+// RUN: %clang -print-target-triple \
+// RUN:     --target=arm64-apple-watchos -mwatchos-version-min=3 \
+// RUN:     -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-WATCHOS %s
+// CHECK-CLANGRT-WATCHOS: arm64-apple-watchos3.0.0
+
+// RUN: %clang -print-target-triple \
+// RUN:     --target=arm64-apple-tvos -mtvos-version-min=1\
+// RUN:     -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-TVOS %s
+// CHECK-CLANGRT-TVOS: arm64-apple-tvos1.0.0
+
+// RUN: %clang -print-target-triple \
+// RUN:     --target=arm64-apple-driverkit \
+// RUN:     -resource-dir=%S/Inputs/resource_dir 2>&1 \
+// RUN:   | FileCheck --check-prefix=CHECK-CLANGRT-DRIVERKIT %s
+// CHECK-CLANGRT-DRIVERKIT: arm64-apple-driverkit19.0.0



More information about the cfe-commits mailing list