-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[clang] Enhance handling of Apple-specific '-arch'/'-target' option values #72821
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…alues The '-arch' option itself is Apple-specific, so, if '-target' is not passed explicitely, we try to construct an Apple triple. If the default triple is Apple-specific, we just update it's arch so it matches the one passed via '-arch' option, otherwise, we construct a '<arch>-apple-darwin10' triple "from scratch". Passing just '-arch' without '-target' seems as a common practice in Apple's tests, and it previously led to undesireable triple values deduced if a person had, say, linux-specific LLVM_DEFAULT_TARGET_TRIPLE set. The arm64e arch value is also Apple-specific, so, if we have 'arm64e' or 'arm64e-apple' triple, append missing parts to it so it becomes 'arm64e-apple-darwin10'. See tests in Driver/apple-specific-options.c for detailed description of how different cases are handled.
@llvm/pr-subscribers-clang-driver @llvm/pr-subscribers-clang Author: Daniil Kovalev (kovdan01) ChangesThe '-arch' option itself is Apple-specific, so, if '-target' is not passed explicitely, we try to construct an Apple triple. If the default triple is Apple-specific, we just update it's arch so it matches the one passed via '-arch' option, otherwise, we construct a '<arch>-apple-darwin10' triple "from scratch". The arm64e arch value is also Apple-specific, so, if we have 'arm64e' or 'arm64e-apple' triple, append missing parts to it so it becomes 'arm64e-apple-darwin10'. See tests in Driver/apple-specific-options.c for detailed description of how different cases are handled. Full diff: https://github.com/llvm/llvm-project/pull/72821.diff 2 Files Affected:
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 6f5ff8141032677..4663189933cc1c2 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -521,6 +521,34 @@ static llvm::Triple computeTargetTriple(const Driver &D,
if (TargetTriple.contains("-unknown-gnu") || TargetTriple.contains("-pc-gnu"))
Target.setOSName("hurd");
+ auto SetDefaultAppleTarget = [&Target]() {
+ if (Target.getVendorName().empty())
+ Target.setVendor(llvm::Triple::Apple);
+ if (Target.getVendor() == llvm::Triple::Apple &&
+ Target.getOSAndEnvironmentName().empty())
+ Target.setOSName("darwin10");
+ };
+
+ // Since '-arch' is an Apple-specific option, construct a default Apple triple
+ // when '-target' is not explicitely passed.
+ if (Args.hasArg(options::OPT_arch) && !Args.hasArg(options::OPT_target)) {
+ StringRef ArchName = Args.getLastArg(options::OPT_arch)->getValue();
+ if (Target.isOSBinFormatMachO()) {
+ // The default triple is already Apple-specific - just update the arch.
+ tools::darwin::setTripleTypeForMachOArchName(Target, ArchName, Args);
+ } else {
+ // The default triple is not Apple-specific - construct a new one to avoid
+ // handling unrelated info from the default one (e.g. environment).
+ Target = llvm::Triple(ArchName);
+ SetDefaultAppleTarget();
+ }
+ }
+
+ // Since arm64e arch is Apple-specific, set VendorName and OS correspondingly
+ // if not set already.
+ if (Target.getArchName() == "arm64e")
+ SetDefaultAppleTarget();
+
// Handle Apple-specific options available here.
if (Target.isOSBinFormatMachO()) {
// If an explicit Darwin arch name is given, that trumps all.
diff --git a/clang/test/Driver/apple-specific-options.c b/clang/test/Driver/apple-specific-options.c
new file mode 100644
index 000000000000000..b683bf5a3de3a8f
--- /dev/null
+++ b/clang/test/Driver/apple-specific-options.c
@@ -0,0 +1,60 @@
+// Without '-target' explicitly passed, construct the default triple.
+// If the LLVM_DEFAULT_TARGET_TRIPLE is a Darwin triple, change it's architecture
+// to a one passed via '-arch'. Otherwise, use '<arch>-apple-darwin10'.
+
+// RUN: %clang -arch x86_64 -c %s -### 2>&1 | \
+// RUN: FileCheck %s --check-prefix ARCH
+
+// ARCH: "-triple" "x86_64-apple-
+
+// For non-Darwin explicitly passed '-target', ignore '-arch'.
+
+// RUN: %clang -arch arm64 -target x86_64-unknown-linux -c %s -### 2>&1 | \
+// RUN: FileCheck %s --check-prefix ARCH_NON_DARWIN1
+
+// ARCH_NON_DARWIN1: "-triple" "x86_64-unknown-linux"
+
+// RUN: %clang -arch arm64 -target x86_64-apple -c %s -### 2>&1 | \
+// RUN: FileCheck %s --check-prefix ARCH_NON_DARWIN2
+
+// ARCH_NON_DARWIN2: "-triple" "x86_64-apple"
+
+
+// For Darwin explicitly passed '-target', the '-arch' option overrides the architecture
+
+// RUN: %clang -arch arm64 -target x86_64-apple-ios7.0.0 -c %s -### 2>&1 | \
+// RUN: FileCheck %s --check-prefix ARCH_DARWIN
+
+// ARCH_DARWIN: "-triple" "arm64-apple-ios7.0.0"
+
+
+// For 'arm64e' and 'arm64e-apple' explicitly passed as '-target',
+// construct the default 'arm64e-apple-darwin10' triple.
+
+// RUN: %clang -target arm64e -c %s -### 2>&1 | \
+// RUN: FileCheck %s --check-prefix ARM64E
+// RUN: %clang -target arm64e-apple -c %s -### 2>&1 | \
+// RUN: FileCheck %s --check-prefix ARM64E
+// ARM64E: "-triple" "arm64e-apple-macosx10.6.0"
+
+
+// For non-Darwin explicitly passed '-target', keep it unchanged if not 'arm64e' and
+// 'arm64e-apple', which we implicitly narrow to the default 'arm64e-apple-darwin10'.
+
+// RUN: %clang -target arm64e-pc -c %s -### 2>&1 | \
+// RUN: FileCheck %s --check-prefix ARM64E_NON_DARWIN1
+
+// ARM64E_NON_DARWIN1: "-triple" "arm64e-pc"
+
+// RUN: %clang -target arm64e-unknown-linux -c %s -### 2>&1 | \
+// RUN: FileCheck %s --check-prefix ARM64E_NON_DARWIN2
+
+// ARM64E_NON_DARWIN2: "-triple" "arm64e-unknown-linux"
+
+
+// For Darwin explicitly passed '-target', keep it unchanged
+
+// RUN: %clang -target arm64e-apple-ios7.0.0 -c %s -### 2>&1 | \
+// RUN: FileCheck %s --check-prefix ARM64E_DARWIN
+
+// ARM64E_DARWIN: "-triple" "arm64e-apple-ios7.0.0"
|
@MaskRay @TNorthover @Artem-B Would be glad to see you review on the changes |
I think we should reject Created #74365 |
I don't think this is a good idea. Not only is that weird to magically switch to an apple build from a non-apple platform, this is also not even a particularly useful behavior for an Apple platform, where "darwin" is basically deprecated, and you're supposed to use a target of x86_64-apple-macos11 (or ios/watchos/etc). |
The '-arch' option itself is Apple-specific, so, if '-target' is not passed explicitely, we try to construct an Apple triple. If the default triple is Apple-specific, we just update it's arch so it matches the one passed via '-arch' option, otherwise, we construct a '-apple-darwin10' triple "from scratch". Passing just '-arch' without '-target' seems as a common practice in Apple's tests, and it previously led to undesireable triple values deduced if a person had, say, linux-specific LLVM_DEFAULT_TARGET_TRIPLE set.
The arm64e arch value is also Apple-specific, so, if we have 'arm64e' or 'arm64e-apple' triple, append missing parts to it so it becomes 'arm64e-apple-darwin10'.
See tests in Driver/apple-specific-options.c for detailed description of how different cases are handled.