Skip to content

Commit e96a690

Browse files
committed
[Explicit Module Builds] Add '-clang-target-variant' flag
swiftlang#37774 added '-clang-target' which allows us to specify a target triple that only differs from '-target' by the OS version, when we want to provide a different OS version for API availability and type-checking, in order to set a common/unified target triple for the entire Clang module dependency graph, for presenting a unified API surface to the Swift client, serving as a maximum type-checking epoch. This change adds an equivalent flag for the '-target-variant' configuration, as a mechanism to ensure that the entire module dependency graph presents a consistent os version.
1 parent 0997a7e commit e96a690

File tree

7 files changed

+54
-3
lines changed

7 files changed

+54
-3
lines changed

include/swift/Basic/LangOptions.h

+3
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ namespace swift {
149149
/// The lowering triple may result in multiple versions of the same Clang
150150
/// modules being built.
151151
std::optional<llvm::Triple> ClangTarget;
152+
std::optional<llvm::Triple> ClangTargetVariant;
152153

153154
/// The SDK version, if known.
154155
std::optional<llvm::VersionTuple> SDKVersion;
@@ -808,6 +809,8 @@ namespace swift {
808809
hashValue = llvm::hash_combine(hashValue, TargetVariant.value().str());
809810
if (ClangTarget.has_value())
810811
hashValue = llvm::hash_combine(hashValue, ClangTarget.value().str());
812+
if (ClangTargetVariant.has_value())
813+
hashValue = llvm::hash_combine(hashValue, ClangTargetVariant.value().str());
811814
if (SDKVersion.has_value())
812815
hashValue = llvm::hash_combine(hashValue, SDKVersion.value().getAsString());
813816
if (VariantSDKVersion.has_value())

include/swift/Option/Options.td

+5
Original file line numberDiff line numberDiff line change
@@ -1533,6 +1533,11 @@ def clang_target : Separate<["-"], "clang-target">,
15331533
Flags<[FrontendOption, SwiftSymbolGraphExtractOption,
15341534
SwiftAPIDigesterOption, SwiftSynthesizeInterfaceOption]>,
15351535
HelpText<"Separately set the target we should use for internal Clang instance">;
1536+
def clang_target_variant : Separate<["-"], "clang-target-variant">,
1537+
Flags<[FrontendOption, SwiftSymbolGraphExtractOption,
1538+
SwiftAPIDigesterOption, SwiftSynthesizeInterfaceOption]>,
1539+
HelpText<"Separately set the target we should use for internal Clang instance"
1540+
" for the 'zippered' code for macCatalyst">;
15361541

15371542
def disable_clang_target : Flag<["-"], "disable-clang-target">,
15381543
Flags<[NewDriverOnlyOption]>,

lib/ClangImporter/ClangImporter.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,8 @@ importer::addCommonInvocationArguments(
866866
// Passing the -target-variant along to clang causes clang's
867867
// CodeGenerator to emit zippered .o files.
868868
invocationArgStrs.push_back("-darwin-target-variant");
869+
if (ctx.LangOpts.ClangTargetVariant.has_value() && !ignoreClangTarget)
870+
variantTriple = ctx.LangOpts.ClangTargetVariant.value();
869871
invocationArgStrs.push_back(variantTriple->str());
870872
}
871873

@@ -1202,8 +1204,11 @@ std::optional<std::vector<std::string>> ClangImporter::getClangCC1Arguments(
12021204
}
12031205

12041206
// If clang target is ignored, using swift target.
1205-
if (ignoreClangTarget)
1207+
if (ignoreClangTarget) {
12061208
CI->getTargetOpts().Triple = ctx.LangOpts.Target.str();
1209+
if (ctx.LangOpts.TargetVariant.has_value())
1210+
CI->getTargetOpts().DarwinTargetVariantTriple = ctx.LangOpts.TargetVariant->str();
1211+
}
12071212

12081213
// Forward the index store path. That information is not passed to scanner
12091214
// and it is cached invariant so we don't want to re-scan if that changed.

lib/Frontend/CompilerInvocation.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -1433,9 +1433,10 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
14331433
// necessary because the textual interface hardcoded the proper target triple
14341434
// to use. Inferring -clang-target there will always give us the default
14351435
// target triple.
1436-
if (const Arg *A = Args.getLastArg(OPT_clang_target)) {
1436+
if (const Arg *A = Args.getLastArg(OPT_clang_target))
14371437
Opts.ClangTarget = llvm::Triple(A->getValue());
1438-
}
1438+
if (const Arg *A = Args.getLastArg(OPT_clang_target_variant))
1439+
Opts.ClangTargetVariant = llvm::Triple(A->getValue());
14391440

14401441
Opts.setCxxInteropFromArgs(Args, Diags);
14411442

lib/Frontend/Frontend.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,11 @@ SerializationOptions CompilerInvocation::computeSerializationOptions(
246246
serializationOpts.ExtraClangOptions.push_back("--target=" +
247247
LangOpts.ClangTarget->str());
248248
}
249+
if (LangOpts.ClangTargetVariant &&
250+
!getClangImporterOptions().DirectClangCC1ModuleBuild) {
251+
serializationOpts.ExtraClangOptions.push_back("-darwin-target-variant");
252+
serializationOpts.ExtraClangOptions.push_back(LangOpts.ClangTargetVariant->str());
253+
}
249254
if (LangOpts.EnableAppExtensionRestrictions) {
250255
serializationOpts.ExtraClangOptions.push_back("-fapplication-extension");
251256
}

lib/Frontend/ModuleInterfaceLoader.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -1668,6 +1668,15 @@ void InterfaceSubContextDelegateImpl::inheritOptionsForBuildingInterface(
16681668
GenericArgs.push_back(triple);
16691669
}
16701670

1671+
if (LangOpts.ClangTargetVariant.has_value()) {
1672+
genericSubInvocation.getLangOptions().ClangTargetVariant = LangOpts.ClangTargetVariant;
1673+
auto variantTriple = ArgSaver.save(genericSubInvocation.getLangOptions()
1674+
.ClangTargetVariant->getTriple());
1675+
assert(!variantTriple.empty());
1676+
GenericArgs.push_back("-clang-target-variant");
1677+
GenericArgs.push_back(variantTriple);
1678+
}
1679+
16711680
// Inherit the target SDK name and version
16721681
if (!LangOpts.SDKName.empty()) {
16731682
genericSubInvocation.getLangOptions().SDKName = LangOpts.SDKName;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// REQUIRES: objc_interop
2+
// REQUIRES: OS=macosx
3+
4+
// RUN: %empty-directory(%t)
5+
// RUN: %empty-directory(%t/module-cache)
6+
7+
// RUN: %target-swift-frontend -scan-dependencies -enable-objc-interop -module-load-mode prefer-interface -module-cache-path %t.module-cache %s -o %t.deps.json -I %S/Inputs/CHeaders -I %S/Inputs/Swift -target arm64e-apple-ios16.4-macabi -clang-target arm64e-apple-ios17.0-macabi -target-variant arm64e-apple-macosx14.4 -clang-target-variant arm64e-apple-macosx15.0
8+
9+
// RUN: %validate-json %t.deps.json | %FileCheck %s
10+
11+
// Ensure the flag affects Clang dependencies in the expected way
12+
// CHECK: "clang": "X"
13+
// CHECK: "modulePath": "{{.*}}X-{{.*}}.pcm"
14+
// CHECK: "-darwin-target-variant-triple"
15+
// CHECK-NEXT: "-Xcc"
16+
// CHECK-NEXT: "arm64e-apple-macosx15.0"
17+
18+
// Ensure the flag is propagated to Swift dependencies
19+
// CHECK: "-clang-target-variant"
20+
// CHECK-NEXT: "arm64e-apple-macosx15.0"
21+
22+
import X
23+

0 commit comments

Comments
 (0)