Skip to content

Commit db668aa

Browse files
artemcmnkcsgexi
authored andcommitted
[Dependency Scanning] Do not include -target in extraPCMArgs when scanning an interface
#37774 (related to rdar://72480261) has made it so that the target of built clang modules (even downstream from Swift interface dependencies) can be consistent with that of the main module. This means that when building transitive Clang dependencies of the main module, they will always have a matching triple to the main module itself (ensured with `-clang-target`). This means we no longer have to report the target triple in the set of `extraPCMArgs` which encode an interface-specific requirement for building its dependencies.
1 parent 6f82cea commit db668aa

File tree

4 files changed

+44
-15
lines changed

4 files changed

+44
-15
lines changed

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/ModuleInterfaceLoader.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,15 +1506,20 @@ InterfaceSubContextDelegateImpl::runInSubCompilerInstance(StringRef moduleName,
15061506
}
15071507
info.BuildArguments = BuildArgs;
15081508
info.Hash = CacheHash;
1509-
auto target = *(std::find(BuildArgs.rbegin(), BuildArgs.rend(), "-target") - 1);
1509+
auto target = *(std::find(BuildArgs.rbegin(), BuildArgs.rend(), "-target") - 1);
15101510
auto langVersion = *(std::find(BuildArgs.rbegin(), BuildArgs.rend(),
15111511
"-swift-version") - 1);
1512-
std::array<StringRef, 6> ExtraPCMArgs = {
1513-
// PCMs should use the target triple the interface will be using to build
1514-
"-Xcc", "-target", "-Xcc", target,
1512+
1513+
std::vector<StringRef> ExtraPCMArgs = {
15151514
// PCMs should use the effective Swift language version for apinotes.
1516-
"-Xcc", ArgSaver.save((llvm::Twine("-fapinotes-swift-version=") + langVersion).str())
1515+
"-Xcc",
1516+
ArgSaver.save((llvm::Twine("-fapinotes-swift-version=") + langVersion).str())
15171517
};
1518+
if (!subInvocation.getLangOptions().ClangTarget.hasValue()) {
1519+
ExtraPCMArgs.insert(ExtraPCMArgs.begin(), {"-Xcc", "-target",
1520+
"-Xcc", target});
1521+
}
1522+
15181523
info.ExtraPCMArgs = ExtraPCMArgs;
15191524
// Run the action under the sub compiler instance.
15201525
return action(info);

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)