https://github.com/kovdan01 created https://github.com/llvm/llvm-project/pull/72821
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". 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. >From a1ad61f5194c1ba577222e9381d01f836432db32 Mon Sep 17 00:00:00 2001 From: Daniil Kovalev <dkova...@accesssoftek.com> Date: Fri, 17 Nov 2023 06:33:33 +0300 Subject: [PATCH] [clang] Enhance handling of Apple-specific '-arch'/'-target' option values 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. --- clang/lib/Driver/Driver.cpp | 28 ++++++++++ clang/test/Driver/apple-specific-options.c | 60 ++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 clang/test/Driver/apple-specific-options.c 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" _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits