Skip to content

Commit 4e2fdbc

Browse files
authored
Merge pull request #38420 from nkcsgexi/clang-target-5.5
[5.5] cherry-pick -clang-target related changes.
2 parents 7a7c764 + db668aa commit 4e2fdbc

File tree

10 files changed

+117
-16
lines changed

10 files changed

+117
-16
lines changed

include/swift/Basic/LangOptions.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ namespace swift {
9090
/// performed.
9191
llvm::Optional<llvm::Triple> TargetVariant;
9292

93+
/// The target triple to instantiate the internal clang instance.
94+
/// When not specified, the compiler will use the value of -target to
95+
/// instantiate the clang instance.
96+
/// This is mainly used to avoid lowering the target triple to use for clang when
97+
/// importing a .swiftinterface whose -target value may be different from
98+
/// the loading module.
99+
/// The lowering triple may result in multiple versions of the same Clang
100+
/// modules being built.
101+
llvm::Optional<llvm::Triple> ClangTarget;
102+
93103
/// The SDK version, if known.
94104
Optional<llvm::VersionTuple> SDKVersion;
95105

include/swift/Option/Options.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,13 @@ def target_variant : Separate<["-"], "target-variant">,
10661066
HelpText<"Generate 'zippered' code for macCatalyst that can run on the specified"
10671067
" variant target triple in addition to the main -target triple">;
10681068

1069+
def clang_target : Separate<["-"], "clang-target">,
1070+
Flags<[FrontendOption, SwiftAPIExtractOption, SwiftSymbolGraphExtractOption]>,
1071+
HelpText<"Separately set the target we should use for internal Clang instance">;
1072+
1073+
def disable_clang_target : Flag<["-"], "disable-clang-target">,
1074+
HelpText<"Disable a separately specified target triple for Clang instance to use">;
1075+
10691076
def profile_generate : Flag<["-"], "profile-generate">,
10701077
Flags<[FrontendOption, NoInteractiveOption]>,
10711078
HelpText<"Generate instrumented code to collect execution counts">;

lib/ClangImporter/ClangImporter.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,11 @@ importer::addCommonInvocationArguments(
729729
std::vector<std::string> &invocationArgStrs,
730730
ASTContext &ctx) {
731731
using ImporterImpl = ClangImporter::Implementation;
732-
const llvm::Triple &triple = ctx.LangOpts.Target;
732+
llvm::Triple triple = ctx.LangOpts.Target;
733+
// Use clang specific target triple if given.
734+
if (ctx.LangOpts.ClangTarget.hasValue()) {
735+
triple = ctx.LangOpts.ClangTarget.getValue();
736+
}
733737
SearchPathOptions &searchPathOpts = ctx.SearchPathOpts;
734738
const ClangImporterOptions &importerOpts = ctx.ClangImporterOpts;
735739

lib/ClangImporter/ClangModuleDependencyScanner.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ void ClangImporter::recordModuleDependencies(
232232
while(It != allArgs.end()) {
233233
StringRef arg = *It;
234234
// Remove the -target arguments because we should use the target triple
235+
// specified with `-clang-target` on the scanner invocation, or
235236
// from the depending Swift modules.
236237
if (arg == "-target") {
237238
It += 2;
@@ -254,6 +255,16 @@ void ClangImporter::recordModuleDependencies(
254255
swiftArgs.push_back(clangArg);
255256
}
256257

258+
// If the scanner is invoked with '-clang-target', ensure this is the target
259+
// used to build this PCM.
260+
if (Impl.SwiftContext.LangOpts.ClangTarget.hasValue()) {
261+
llvm::Triple triple = Impl.SwiftContext.LangOpts.ClangTarget.getValue();
262+
swiftArgs.push_back("-Xcc");
263+
swiftArgs.push_back("-target");
264+
swiftArgs.push_back("-Xcc");
265+
swiftArgs.push_back(triple.str());
266+
}
267+
257268
// Swift frontend action: -emit-pcm
258269
swiftArgs.push_back("-emit-pcm");
259270
swiftArgs.push_back("-module-name");

lib/DependencyScan/ScanDependencies.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,11 +1033,16 @@ identifyMainModuleDependencies(CompilerInstance &instance) {
10331033
instance.getASTContext()
10341034
.LangOpts.EffectiveLanguageVersion.asAPINotesVersionString())
10351035
.str();
1036+
10361037
// Compute the dependencies of the main module.
1037-
auto mainDependencies = ModuleDependencies::forMainSwiftModule(
1038-
{// ExtraPCMArgs
1039-
"-Xcc", "-target", "-Xcc",
1040-
instance.getASTContext().LangOpts.Target.str(), "-Xcc", apinotesVer});
1038+
std::vector<StringRef> ExtraPCMArgs = {
1039+
"-Xcc", apinotesVer
1040+
};
1041+
if (!instance.getASTContext().LangOpts.ClangTarget.hasValue())
1042+
ExtraPCMArgs.insert(ExtraPCMArgs.begin(),
1043+
{"-Xcc", "-target", "-Xcc",
1044+
instance.getASTContext().LangOpts.Target.str()});
1045+
auto mainDependencies = ModuleDependencies::forMainSwiftModule(ExtraPCMArgs);
10411046

10421047
// Compute Implicit dependencies of the main module
10431048
{

lib/Frontend/CompilerInvocation.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,20 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
640640
Opts.TargetVariant = llvm::Triple(A->getValue());
641641
}
642642

643+
// Collect -clang-target value if specified in the front-end invocation.
644+
// Usually, the driver will pass down a clang target with the
645+
// exactly same value as the main target, so we could dignose the usage of
646+
// unavailable APIs.
647+
// The reason we cannot infer clang target from -target is that not all
648+
// front-end invocation will include a -target to start with. For instance,
649+
// when compiling a Swift module from a textual interface, -target isn't
650+
// necessary because the textual interface hardcoded the proper target triple
651+
// to use. Inferring -clang-target there will always give us the default
652+
// target triple.
653+
if (const Arg *A = Args.getLastArg(OPT_clang_target)) {
654+
Opts.ClangTarget = llvm::Triple(A->getValue());
655+
}
656+
643657
Opts.EnableCXXInterop |= Args.hasArg(OPT_enable_cxx_interop);
644658
Opts.EnableObjCInterop =
645659
Args.hasFlag(OPT_enable_objc_interop, OPT_disable_objc_interop,

lib/Frontend/ModuleInterfaceLoader.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,6 +1132,17 @@ void InterfaceSubContextDelegateImpl::inheritOptionsForBuildingInterface(
11321132
GenericArgs.push_back(triple);
11331133
}
11341134

1135+
if (LangOpts.ClangTarget.hasValue()) {
1136+
genericSubInvocation.getLangOptions().ClangTarget = LangOpts.ClangTarget;
1137+
auto triple = ArgSaver.save(genericSubInvocation.getLangOptions()
1138+
.ClangTarget->getTriple());
1139+
assert(!triple.empty());
1140+
// In explicit module build, all PCMs will be built using the given clang target.
1141+
// So the Swift interface should know that as well to load these PCMs properly.
1142+
GenericArgs.push_back("-clang-target");
1143+
GenericArgs.push_back(triple);
1144+
}
1145+
11351146
// Inherit the Swift language version
11361147
genericSubInvocation.getLangOptions().EffectiveLanguageVersion =
11371148
LangOpts.EffectiveLanguageVersion;
@@ -1495,15 +1506,20 @@ InterfaceSubContextDelegateImpl::runInSubCompilerInstance(StringRef moduleName,
14951506
}
14961507
info.BuildArguments = BuildArgs;
14971508
info.Hash = CacheHash;
1498-
auto target = *(std::find(BuildArgs.rbegin(), BuildArgs.rend(), "-target") - 1);
1509+
auto target = *(std::find(BuildArgs.rbegin(), BuildArgs.rend(), "-target") - 1);
14991510
auto langVersion = *(std::find(BuildArgs.rbegin(), BuildArgs.rend(),
15001511
"-swift-version") - 1);
1501-
std::array<StringRef, 6> ExtraPCMArgs = {
1502-
// PCMs should use the target triple the interface will be using to build
1503-
"-Xcc", "-target", "-Xcc", target,
1512+
1513+
std::vector<StringRef> ExtraPCMArgs = {
15041514
// PCMs should use the effective Swift language version for apinotes.
1505-
"-Xcc", ArgSaver.save((llvm::Twine("-fapinotes-swift-version=") + langVersion).str())
1515+
"-Xcc",
1516+
ArgSaver.save((llvm::Twine("-fapinotes-swift-version=") + langVersion).str())
15061517
};
1518+
if (!subInvocation.getLangOptions().ClangTarget.hasValue()) {
1519+
ExtraPCMArgs.insert(ExtraPCMArgs.begin(), {"-Xcc", "-target",
1520+
"-Xcc", target});
1521+
}
1522+
15071523
info.ExtraPCMArgs = ExtraPCMArgs;
15081524
// Run the action under the sub compiler instance.
15091525
return action(info);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// swift-interface-format-version: 1.0
2+
// swift-module-flags: -module-name XWithTarget -target x86_64-apple-macosx10.9
3+
import Swift
4+
@_exported import X
5+
public func overlayFuncX() { }
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// REQUIRES: VENDOR=apple
2+
// RUN: %empty-directory(%t.module-cache)
3+
// RUN: %target-swift-frontend -emit-module -o %t.foo.swiftmodule -module-cache-path %t.module-cache -I %S/Inputs/CHeaders -I %S/Inputs/Swift %s -target x86_64-apple-macosx10.14
4+
5+
// Without -clang-target, we build two X.pcm
6+
// RUN: find %t.module-cache -name "X-*.pcm" | count 2
7+
8+
// RUN: %empty-directory(%t.module-cache)
9+
// RUN: %target-swift-frontend -emit-module -o %t.foo.swiftmodule -module-cache-path %t.module-cache -I %S/Inputs/CHeaders -I %S/Inputs/Swift %s -target x86_64-apple-macosx10.14 -clang-target x86_64-apple-macosx10.14
10+
11+
// With -clang-target, we build one X.pcm
12+
// RUN: find %t.module-cache -name "X-*.pcm" | count 1
13+
14+
// RUN: %target-swift-frontend -scan-dependencies -module-cache-path %t.module-cache %s -o %t.deps.json -I %S/Inputs/CHeaders -I %S/Inputs/Swift -target x86_64-apple-macosx10.14 -clang-target x86_64-apple-macosx10.14
15+
// RUN: %FileCheck %s < %t.deps.json
16+
17+
// CHECK: "-clang-target"
18+
// CHECK-NEXT: "x86_64-apple-macosx10.14"
19+
20+
import X
21+
import XWithTarget

test/ScanDependencies/module_deps.swift

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// RUN: %empty-directory(%t)
22
// RUN: mkdir -p %t/clang-module-cache
3-
// RUN: %target-swift-frontend -scan-dependencies -module-cache-path %t/clang-module-cache %s -o %t/deps.json -I %S/Inputs/CHeaders -I %S/Inputs/Swift -emit-dependencies -emit-dependencies-path %t/deps.d -import-objc-header %S/Inputs/CHeaders/Bridging.h -swift-version 4
43

4+
// RUN: %target-swift-frontend -scan-dependencies -module-cache-path %t/clang-module-cache %s -o %t/deps.json -I %S/Inputs/CHeaders -I %S/Inputs/Swift -emit-dependencies -emit-dependencies-path %t/deps.d -import-objc-header %S/Inputs/CHeaders/Bridging.h -swift-version 4
55
// Check the contents of the JSON output
6-
// RUN: %FileCheck %s < %t/deps.json
6+
// RUN: %FileCheck -check-prefix CHECK_NO_CLANG_TARGET %s < %t/deps.json
77

88
// Check the contents of the JSON output
99
// RUN: %FileCheck %s -check-prefix CHECK-NO-SEARCH-PATHS < %t/deps.json
@@ -20,6 +20,11 @@
2020
// RUN: %target-codesign %t/main
2121
// RUN: %target-run %t/main %t/deps.json
2222

23+
// Ensure that scanning with `-clang-target` makes sure that Swift modules' respecitve PCM-dependency-build-argument sets do not contain target triples.
24+
// RUN: %target-swift-frontend -scan-dependencies -module-cache-path %t/clang-module-cache %s -o %t/deps_clang_target.json -I %S/Inputs/CHeaders -I %S/Inputs/Swift -import-objc-header %S/Inputs/CHeaders/Bridging.h -swift-version 4 -clang-target %target-cpu-apple-macosx10.14
25+
// Check the contents of the JSON output
26+
// RUN: %FileCheck -check-prefix CHECK_CLANG_TARGET %s < %t/deps_clang_target.json
27+
2328
// REQUIRES: executable_test
2429
// REQUIRES: objc_interop
2530

@@ -174,10 +179,13 @@ import SubE
174179
// CHECK: "-swift-version"
175180
// CHECK: "5"
176181
// CHECK: ],
177-
// CHECK" "extraPcmArgs": [
178-
// CHECK" "-target",
179-
// CHECK" "-fapinotes-swift-version=5"
180-
// CHECK" ]
182+
// CHECK_NO_CLANG_TARGET: "extraPcmArgs": [
183+
// CHECK_NO_CLANG_TARGET-NEXT: "-Xcc",
184+
// CHECK_NO_CLANG_TARGET-NEXT: "-target",
185+
// CHECK_CLANG_TARGET: "extraPcmArgs": [
186+
// CHECK_CLANG_TARGET-NEXT: "-Xcc",
187+
// CHECK_CLANG_TARGET-NEXT: "-fapinotes-swift-version={{.*}}"
188+
// CHECK_CLANG_TARGET-NEXT: ]
181189

182190
/// --------Swift module Swift
183191
// CHECK-LABEL: "modulePath": "Swift.swiftmodule",

0 commit comments

Comments
 (0)