diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -4849,16 +4849,21 @@ MarshallingInfoString>; def target_sdk_version_EQ : Joined<["-"], "target-sdk-version=">, HelpText<"The version of target SDK used for compilation">; -def darwin_target_variant_triple : Separate<["-"], "darwin-target-variant-triple">, - HelpText<"Specify the darwin target variant triple">, - MarshallingInfoString>, - Normalizer<"normalizeTriple">; def darwin_target_variant_sdk_version_EQ : Joined<["-"], "darwin-target-variant-sdk-version=">, HelpText<"The version of darwin target variant SDK used for compilation">; } // let Flags = [CC1Option, CC1AsOption, NoDriverOption] +let Flags = [CC1Option, CC1AsOption] in { + +def darwin_target_variant_triple : Separate<["-"], "darwin-target-variant-triple">, + HelpText<"Specify the darwin target variant triple">, + MarshallingInfoString>, + Normalizer<"normalizeTriple">; + +} // let Flags = [CC1Option, CC1AsOption] + //===----------------------------------------------------------------------===// // Target Options (cc1 + cc1as + fc1) //===----------------------------------------------------------------------===// diff --git a/clang/include/clang/Driver/ToolChain.h b/clang/include/clang/Driver/ToolChain.h --- a/clang/include/clang/Driver/ToolChain.h +++ b/clang/include/clang/Driver/ToolChain.h @@ -717,6 +717,10 @@ return llvm::DenormalMode::getIEEE(); } + virtual Optional getTargetVariantTriple() const { + return llvm::None; + } + // We want to expand the shortened versions of the triples passed in to // the values used for the bitcode libraries. static llvm::Triple getOpenMPTriple(StringRef TripleStr) { diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -7719,6 +7719,8 @@ const llvm::Triple &Triple = getToolChain().getEffectiveTriple(); const std::string &TripleStr = Triple.getTriple(); + const Optional TargetVariantTriple = + getToolChain().getTargetVariantTriple(); const auto &D = getToolChain().getDriver(); // Don't warn about "clang -w -c foo.s" @@ -7736,6 +7738,10 @@ // Add the "effective" target triple. CmdArgs.push_back("-triple"); CmdArgs.push_back(Args.MakeArgString(TripleStr)); + if (TargetVariantTriple) { + CmdArgs.push_back("-darwin-target-variant-triple"); + CmdArgs.push_back(Args.MakeArgString(TargetVariantTriple->getTriple())); + } // Set the output mode, we currently only expect to be used as a real // assembler. diff --git a/clang/lib/Driver/ToolChains/Darwin.h b/clang/lib/Driver/ToolChains/Darwin.h --- a/clang/lib/Driver/ToolChains/Darwin.h +++ b/clang/lib/Driver/ToolChains/Darwin.h @@ -489,6 +489,12 @@ : TargetVersion) < VersionTuple(V0, V1, V2); } + /// Returns the darwin target variant triple, the variant of the deployment + /// target for which the code is being compiled. + Optional getTargetVariantTriple() const override { + return TargetVariantTriple; + } + protected: /// Return true if c++17 aligned allocation/deallocation functions are not /// implemented in the c++ standard library of the deployment target we are diff --git a/clang/test/Misc/cc1as-darwin-target-variant-triple.s b/clang/test/Misc/cc1as-darwin-target-variant-triple.s new file mode 100644 --- /dev/null +++ b/clang/test/Misc/cc1as-darwin-target-variant-triple.s @@ -0,0 +1,34 @@ +// Run cc1as using darwin-target-variant-triple +// REQUIRES: x86-registered-target +// RUN: %clang -cc1as -triple x86_64-apple-macos10.9 -darwin-target-variant-triple x86_64-apple-ios13.1-macabi -filetype obj %s -o - \ +// RUN: | llvm-readobj --file-headers --macho-version-min - \ +// RUN: | FileCheck --check-prefix=CHECK %s + +// CHECK: File: +// CHECK-NEXT: Format: Mach-O 64-bit x86-64 +// CHECK-NEXT: Arch: x86_64 +// CHECK-NEXT: AddressSize: 64bit +// CHECK-NEXT: MachHeader { +// CHECK-NEXT: Magic: Magic64 (0xFEEDFACF) +// CHECK-NEXT: CpuType: X86-64 (0x1000007) +// CHECK-NEXT: CpuSubType: CPU_SUBTYPE_X86_64_ALL (0x3) +// CHECK-NEXT: FileType: Relocatable (0x1) +// CHECK-NEXT: NumOfLoadCommands: 3 +// CHECK-NEXT: SizeOfLoadCommands: 192 +// CHECK-NEXT: Flags [ (0x0) +// CHECK-NEXT: ] +// CHECK-NEXT: Reserved: 0x0 +// CHECK-NEXT: } +// CHECK-NEXT: MinVersion { +// CHECK-NEXT: Cmd: LC_VERSION_MIN_MACOSX +// CHECK-NEXT: Size: 16 +// CHECK-NEXT: Version: 10.9 +// CHECK-NEXT: SDK: n/a +// CHECK-NEXT: } +// CHECK-NEXT: MinVersion { +// CHECK-NEXT: Cmd: LC_BUILD_VERSION +// CHECK-NEXT: Size: 24 +// CHECK-NEXT: Platform: macCatalyst +// CHECK-NEXT: Version: 13.1 +// CHECK-NEXT: SDK: n/a +// CHECK-NEXT: } diff --git a/clang/tools/driver/cc1as_main.cpp b/clang/tools/driver/cc1as_main.cpp --- a/clang/tools/driver/cc1as_main.cpp +++ b/clang/tools/driver/cc1as_main.cpp @@ -144,6 +144,9 @@ /// otherwise. std::string TargetABI; + /// Darwin target variant triple, the variant of the deployment target + /// for which the code is being compiled. + llvm::Optional DarwinTargetVariantTriple; /// @} public: @@ -209,6 +212,9 @@ // Target Options Opts.Triple = llvm::Triple::normalize(Args.getLastArgValue(OPT_triple)); + if (Arg *A = Args.getLastArg(options::OPT_darwin_target_variant_triple)) + Opts.DarwinTargetVariantTriple = llvm::Triple(A->getValue()); + Opts.CPU = std::string(Args.getLastArgValue(OPT_target_cpu)); Opts.Features = Args.getAllArgValues(OPT_target_feature); @@ -407,6 +413,8 @@ // MCObjectFileInfo needs a MCContext reference in order to initialize itself. std::unique_ptr MOFI( TheTarget->createMCObjectFileInfo(Ctx, PIC)); + if (Opts.DarwinTargetVariantTriple) + MOFI->setDarwinTargetVariantTriple(*Opts.DarwinTargetVariantTriple); Ctx.setObjectFileInfo(MOFI.get()); if (Opts.SaveTemporaryLabels)