Skip to content

[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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions clang/lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
60 changes: 60 additions & 0 deletions clang/test/Driver/apple-specific-options.c
Original file line number Diff line number Diff line change
@@ -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"