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 @@ -637,6 +637,10 @@ llvm::opt::ArgStringList &CC1Args, Action::OffloadKind DeviceOffloadKind) const; + /// Add options that need to be passed to cc1as for this target. + virtual void addClangCC1ASTargetOptions(const llvm::opt::ArgList &Args, + llvm::opt::ArgStringList &CC1ASArgs) const; + /// Add warning options that need to be passed to cc1 for this target. virtual void addClangWarningOptions(llvm::opt::ArgStringList &CC1Args) const; @@ -750,10 +754,6 @@ 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/ToolChain.cpp b/clang/lib/Driver/ToolChain.cpp --- a/clang/lib/Driver/ToolChain.cpp +++ b/clang/lib/Driver/ToolChain.cpp @@ -809,6 +809,9 @@ const ArgList &DriverArgs, ArgStringList &CC1Args, Action::OffloadKind DeviceOffloadKind) const {} +void ToolChain::addClangCC1ASTargetOptions( + const ArgList &Args, ArgStringList &CC1ASArgs) const {}; + void ToolChain::addClangWarningOptions(ArgStringList &CC1Args) const {} void ToolChain::addProfileRTLibs(const llvm::opt::ArgList &Args, 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 @@ -7901,8 +7901,6 @@ 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" @@ -7920,10 +7918,8 @@ // 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())); - } + + getToolChain().addClangCC1ASTargetOptions(Args, CmdArgs); // 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 @@ -496,12 +496,6 @@ : 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 @@ -512,6 +506,10 @@ llvm::opt::ArgStringList &CC1Args, Action::OffloadKind DeviceOffloadKind) const override; + virtual void addClangCC1ASTargetOptions(const llvm::opt::ArgList &Args, + llvm::opt::ArgStringList &CC1ASArgs) const override; + + StringRef getPlatformFamily() const; StringRef getOSLibraryNameSuffix(bool IgnoreSim = false) const override; diff --git a/clang/lib/Driver/ToolChains/Darwin.cpp b/clang/lib/Driver/ToolChains/Darwin.cpp --- a/clang/lib/Driver/ToolChains/Darwin.cpp +++ b/clang/lib/Driver/ToolChains/Darwin.cpp @@ -2788,10 +2788,26 @@ isAlignedAllocationUnavailable()) CC1Args.push_back("-faligned-alloc-unavailable"); + addClangCC1ASTargetOptions(DriverArgs, CC1Args); + + // Enable compatibility mode for NSItemProviderCompletionHandler in + // Foundation/NSItemProvider.h. + CC1Args.push_back("-fcompatibility-qualified-id-block-type-checking"); + + // Give static local variables in inline functions hidden visibility when + // -fvisibility-inlines-hidden is enabled. + if (!DriverArgs.getLastArgNoClaim( + options::OPT_fvisibility_inlines_hidden_static_local_var, + options::OPT_fno_visibility_inlines_hidden_static_local_var)) + CC1Args.push_back("-fvisibility-inlines-hidden-static-local-var"); +} + +void Darwin::addClangCC1ASTargetOptions(const llvm::opt::ArgList &Args, + llvm::opt::ArgStringList &CC1ASArgs) const { if (TargetVariantTriple) { - CC1Args.push_back("-darwin-target-variant-triple"); - CC1Args.push_back( - DriverArgs.MakeArgString(TargetVariantTriple->getTriple())); + CC1ASArgs.push_back("-darwin-target-variant-triple"); + CC1ASArgs.push_back( + Args.MakeArgString(TargetVariantTriple->getTriple())); } if (SDKInfo) { @@ -2801,7 +2817,7 @@ std::string Arg; llvm::raw_string_ostream OS(Arg); OS << "-target-sdk-version=" << V; - CC1Args.push_back(DriverArgs.MakeArgString(OS.str())); + CC1ASArgs.push_back(Args.MakeArgString(OS.str())); }; if (isTargetMacCatalyst()) { @@ -2823,7 +2839,7 @@ std::string Arg; llvm::raw_string_ostream OS(Arg); OS << "-darwin-target-variant-sdk-version=" << SDKInfo->getVersion(); - CC1Args.push_back(DriverArgs.MakeArgString(OS.str())); + CC1ASArgs.push_back(Args.MakeArgString(OS.str())); } else if (const auto *MacOStoMacCatalystMapping = SDKInfo->getVersionMapping( DarwinSDKInfo::OSEnvPair::macOStoMacCatalystPair())) { @@ -2833,22 +2849,11 @@ std::string Arg; llvm::raw_string_ostream OS(Arg); OS << "-darwin-target-variant-sdk-version=" << *SDKVersion; - CC1Args.push_back(DriverArgs.MakeArgString(OS.str())); + CC1ASArgs.push_back(Args.MakeArgString(OS.str())); } } } } - - // Enable compatibility mode for NSItemProviderCompletionHandler in - // Foundation/NSItemProvider.h. - CC1Args.push_back("-fcompatibility-qualified-id-block-type-checking"); - - // Give static local variables in inline functions hidden visibility when - // -fvisibility-inlines-hidden is enabled. - if (!DriverArgs.getLastArgNoClaim( - options::OPT_fvisibility_inlines_hidden_static_local_var, - options::OPT_fno_visibility_inlines_hidden_static_local_var)) - CC1Args.push_back("-fvisibility-inlines-hidden-static-local-var"); } DerivedArgList * diff --git a/clang/test/Driver/clang-cc1as-target-variant.s b/clang/test/Driver/clang-cc1as-target-variant.s new file mode 100644 --- /dev/null +++ b/clang/test/Driver/clang-cc1as-target-variant.s @@ -0,0 +1,33 @@ +// RUN: touch %t.S + +// RUN: %clang -target x86_64-apple-ios13.1-macabi -darwin-target-variant x86_64-apple-macos10.15 -isysroot %S/Inputs/MacOSX10.15.versioned.sdk -mlinker-version=520 -### %t.S 2>&1 \ +// RUN: | FileCheck %s + +// RUN: %clang -target x86_64-apple-ios-macabi -mmacos-version-min=10.15 -isysroot %S/Inputs/MacOSX10.15.versioned.sdk -mlinker-version=520 -### %t.S 2>&1 \ +// RUN: | FileCheck --check-prefix=CHECK-SDK-INFO %s + +// RUN: %clang -target x86_64-apple-ios-macabi -mmacos-version-min=10.15 -darwin-target-variant x86_64-apple-macos -isysroot %S/Inputs/MacOSX10.15.versioned.sdk -mlinker-version=520 -### %t.S 2>&1 \ +// RUN: | FileCheck --check-prefix=CHECK-VARIANT-SDK-INFO %s + +// RUN: %clang -target x86_64-apple-macos -mmacos-version-min=10.15 -darwin-target-variant x86_64-apple-ios-macabi -isysroot %S/Inputs/MacOSX10.15.versioned.sdk -mlinker-version=520 -### %t.S 2>&1 \ +// RUN: | FileCheck --check-prefix=CHECK-VARIANT-SDK-INFO-INV %s + +// CHECK: "-cc1as" +// CHECK-SAME: "-triple" "x86_64-apple-ios13.1.0-macabi" +// CHECK-SAME: "-darwin-target-variant-triple" "x86_64-apple-macos10.15" + +// CHECK-SDK-INFO: "-cc1as" +// CHECK-SDK-INFO-SAME: "-triple" "x86_64-apple-ios13.1.0-macabi" +// CHECK-SDK-INFO-SAME: "-target-sdk-version=13.1" + +// CHECK-VARIANT-SDK-INFO: "-cc1as" +// CHECK-VARIANT-SDK-INFO-SAME: "-triple" "x86_64-apple-ios13.1.0-macabi" +// CHECK-VARIANT-SDK-INFO-SAME: "-darwin-target-variant-triple" "x86_64-apple-macos" +// CHECK-VARIANT-SDK-INFO-SAME: "-target-sdk-version=13.1" +// CHECK-VARIANT-SDK-INFO-SAME: "-darwin-target-variant-sdk-version=10.15" + +// CHECK-VARIANT-SDK-INFO-INV: "-cc1as" +// CHECK-VARIANT-SDK-INFO-INV-SAME: "-triple" "x86_64-apple-macosx10.15.0" +// CHECK-VARIANT-SDK-INFO-INV-SAME: "-darwin-target-variant-triple" "x86_64-apple-ios-macabi" +// CHECK-VARIANT-SDK-INFO-INV-SAME: "-target-sdk-version=10.15" +// CHECK-VARIANT-SDK-INFO-INV-SAME: "-darwin-target-variant-sdk-version=13.1" diff --git a/clang/test/Misc/cc1as-darwin-target-variant-sdk-version.s b/clang/test/Misc/cc1as-darwin-target-variant-sdk-version.s new file mode 100644 --- /dev/null +++ b/clang/test/Misc/cc1as-darwin-target-variant-sdk-version.s @@ -0,0 +1,34 @@ +// Run cc1as using darwin-target-variant-sdk-version +// REQUIRES: x86-registered-target +// RUN: %clang -cc1as -triple x86_64-apple-macos10.9 -darwin-target-variant-triple x86_64-apple-ios13.1-macabi -target-sdk-version=11.1 -darwin-target-variant-sdk-version=14.1 -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: 14.1 +// 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 @@ -151,6 +151,10 @@ /// Darwin target variant triple, the variant of the deployment target /// for which the code is being compiled. llvm::Optional DarwinTargetVariantTriple; + + /// The version of the darwin target variant SDK which was used during the + /// compilation + llvm::VersionTuple DarwinTargetVariantSDKVersion; /// @} public: @@ -220,6 +224,14 @@ 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()); + if (Arg *A = Args.getLastArg(OPT_darwin_target_variant_sdk_version_EQ)) { + VersionTuple Version; + if (Version.tryParse(A->getValue())) + Diags.Report(diag::err_drv_invalid_value) + << A->getAsString(Args) << A->getValue(); + else + Opts.DarwinTargetVariantSDKVersion = Version; + } Opts.CPU = std::string(Args.getLastArgValue(OPT_target_cpu)); Opts.Features = Args.getAllArgValues(OPT_target_feature); @@ -433,6 +445,8 @@ TheTarget->createMCObjectFileInfo(Ctx, PIC)); if (Opts.DarwinTargetVariantTriple) MOFI->setDarwinTargetVariantTriple(*Opts.DarwinTargetVariantTriple); + if (!Opts.DarwinTargetVariantSDKVersion.empty()) + MOFI->setDarwinTargetVariantSDKVersion(Opts.DarwinTargetVariantSDKVersion); Ctx.setObjectFileInfo(MOFI.get()); if (Opts.SaveTemporaryLabels)