Skip to content

Commit 15c8c26

Browse files
authored
Merge pull request #32304 from nkcsgexi/extra-pcm-args
DependencyScanner: add a new extraPcmArgs field for each Swift module
2 parents 068bba3 + b6a8af5 commit 15c8c26

File tree

9 files changed

+106
-34
lines changed

9 files changed

+106
-34
lines changed

include/swift/AST/ModuleDependencies.h

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ class SwiftModuleDependenciesStorage : public ModuleDependenciesStorageBase {
7373
/// interface.
7474
const std::vector<std::string> buildCommandLine;
7575

76+
/// To build a PCM to be used by this Swift module, we need to append these
77+
/// arguments to the generic PCM build arguments reported from the dependency
78+
/// graph.
79+
const std::vector<std::string> extraPCMArgs;
80+
7681
/// The hash value that will be used for the generated module
7782
const std::string contextHash;
7883

@@ -92,10 +97,12 @@ class SwiftModuleDependenciesStorage : public ModuleDependenciesStorageBase {
9297
const std::string &compiledModulePath,
9398
const Optional<std::string> &swiftInterfaceFile,
9499
ArrayRef<StringRef> buildCommandLine,
100+
ArrayRef<StringRef> extraPCMArgs,
95101
StringRef contextHash
96102
) : ModuleDependenciesStorageBase(/*isSwiftModule=*/true, compiledModulePath),
97103
swiftInterfaceFile(swiftInterfaceFile),
98104
buildCommandLine(buildCommandLine.begin(), buildCommandLine.end()),
105+
extraPCMArgs(extraPCMArgs.begin(), extraPCMArgs.end()),
99106
contextHash(contextHash) { }
100107

101108
ModuleDependenciesStorageBase *clone() const override {
@@ -176,18 +183,31 @@ class ModuleDependencies {
176183
const std::string &compiledModulePath,
177184
const std::string &swiftInterfaceFile,
178185
ArrayRef<StringRef> buildCommands,
186+
ArrayRef<StringRef> extraPCMArgs,
179187
StringRef contextHash) {
180188
return ModuleDependencies(
181189
std::make_unique<SwiftModuleDependenciesStorage>(
182-
compiledModulePath, swiftInterfaceFile, buildCommands, contextHash));
190+
compiledModulePath, swiftInterfaceFile, buildCommands,
191+
extraPCMArgs, contextHash));
183192
}
184193

185194
/// Describe the module dependencies for a serialized or parsed Swift module.
186195
static ModuleDependencies forSwiftModule(
187196
const std::string &compiledModulePath) {
188197
return ModuleDependencies(
189198
std::make_unique<SwiftModuleDependenciesStorage>(
190-
compiledModulePath, None, ArrayRef<StringRef>(), StringRef()));
199+
compiledModulePath, None, ArrayRef<StringRef>(),
200+
ArrayRef<StringRef>(), StringRef()));
201+
}
202+
203+
/// Describe the main Swift module.
204+
static ModuleDependencies forMainSwiftModule(
205+
const std::string &compiledModulePath,
206+
ArrayRef<StringRef> extraPCMArgs) {
207+
return ModuleDependencies(
208+
std::make_unique<SwiftModuleDependenciesStorage>(
209+
compiledModulePath, None, ArrayRef<StringRef>(), extraPCMArgs,
210+
StringRef()));
191211
}
192212

193213
/// Describe the module dependencies for a Clang module that can be

include/swift/AST/ModuleLoader.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ struct SubCompilerInstanceInfo {
8989
CompilerInstance* Instance;
9090
StringRef Hash;
9191
ArrayRef<StringRef> BuildArguments;
92+
ArrayRef<StringRef> ExtraPCMArgs;
9293
};
9394

9495
/// Abstract interface to run an action in a sub ASTContext.
@@ -97,7 +98,8 @@ struct InterfaceSubContextDelegate {
9798
StringRef interfacePath,
9899
StringRef outputPath,
99100
SourceLoc diagLoc,
100-
llvm::function_ref<bool(ASTContext&,ArrayRef<StringRef>, StringRef)> action) = 0;
101+
llvm::function_ref<bool(ASTContext&,ArrayRef<StringRef>,
102+
ArrayRef<StringRef>, StringRef)> action) = 0;
101103
virtual bool runInSubCompilerInstance(StringRef moduleName,
102104
StringRef interfacePath,
103105
StringRef outputPath,

include/swift/Frontend/ModuleInterfaceLoader.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,8 @@ struct InterfaceSubContextDelegateImpl: InterfaceSubContextDelegate {
280280
StringRef interfacePath,
281281
StringRef outputPath,
282282
SourceLoc diagLoc,
283-
llvm::function_ref<bool(ASTContext&, ArrayRef<StringRef>, StringRef)> action) override;
283+
llvm::function_ref<bool(ASTContext&, ArrayRef<StringRef>,
284+
ArrayRef<StringRef>, StringRef)> action) override;
284285
bool runInSubCompilerInstance(StringRef moduleName,
285286
StringRef interfacePath,
286287
StringRef outputPath,

lib/ClangImporter/ClangModuleDependencyScanner.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,23 @@ void ClangImporter::recordModuleDependencies(
229229
swiftArgs.push_back(arg.str());
230230
};
231231
// Add all args inheritted from creating the importer.
232-
for (auto arg: allArgs) {
233-
addClangArg(arg);
232+
auto It = allArgs.begin();
233+
while(It != allArgs.end()) {
234+
StringRef arg = *It;
235+
// Remove the -target arguments because we should use the target triple
236+
// from the depending Swift modules.
237+
if (arg == "-target") {
238+
It += 2;
239+
} else if (arg.startswith("-fapinotes-swift-version=")) {
240+
// Remove the apinotes version because we should use the language version
241+
// specified in the interface file.
242+
It += 1;
243+
} else {
244+
addClangArg(*It);
245+
++ It;
246+
}
234247
}
248+
235249
// Swift frontend action: -emit-pcm
236250
swiftArgs.push_back("-emit-pcm");
237251
swiftArgs.push_back("-module-name");

lib/Frontend/ModuleInterfaceLoader.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1050,6 +1050,13 @@ void InterfaceSubContextDelegateImpl::inheritOptionsForBuildingInterface(
10501050
GenericArgs.push_back(triple);
10511051
}
10521052

1053+
// Inherit the Swift language version
1054+
subInvocation.getLangOptions().EffectiveLanguageVersion =
1055+
LangOpts.EffectiveLanguageVersion;
1056+
GenericArgs.push_back("-swift-version");
1057+
GenericArgs.push_back(ArgSaver.save(subInvocation.getLangOptions()
1058+
.EffectiveLanguageVersion.asAPINotesVersionString()));
1059+
10531060
subInvocation.setImportSearchPaths(SearchPathOpts.ImportSearchPaths);
10541061
llvm::for_each(SearchPathOpts.ImportSearchPaths,
10551062
[&](const std::string &path) {
@@ -1323,10 +1330,12 @@ bool InterfaceSubContextDelegateImpl::runInSubContext(StringRef moduleName,
13231330
StringRef interfacePath,
13241331
StringRef outputPath,
13251332
SourceLoc diagLoc,
1326-
llvm::function_ref<bool(ASTContext&, ArrayRef<StringRef>, StringRef)> action) {
1333+
llvm::function_ref<bool(ASTContext&, ArrayRef<StringRef>,
1334+
ArrayRef<StringRef>, StringRef)> action) {
13271335
return runInSubCompilerInstance(moduleName, interfacePath, outputPath, diagLoc,
13281336
[&](SubCompilerInstanceInfo &info){
13291337
return action(info.Instance->getASTContext(), info.BuildArguments,
1338+
info.ExtraPCMArgs,
13301339
info.Hash);
13311340
});
13321341
}
@@ -1398,6 +1407,16 @@ bool InterfaceSubContextDelegateImpl::runInSubCompilerInstance(StringRef moduleN
13981407
}
13991408
info.BuildArguments = BuildArgs;
14001409
info.Hash = CacheHash;
1410+
auto target = *(std::find(BuildArgs.rbegin(), BuildArgs.rend(), "-target") - 1);
1411+
auto langVersion = *(std::find(BuildArgs.rbegin(), BuildArgs.rend(),
1412+
"-swift-version") - 1);
1413+
std::array<StringRef, 6> ExtraPCMArgs = {
1414+
// PCMs should use the target triple the interface will be using to build
1415+
"-Xcc", "-target", "-Xcc", target,
1416+
// PCMs should use the effective Swift language version for apinotes.
1417+
"-Xcc", ArgSaver.save((llvm::Twine("-fapinotes-swift-version=") + langVersion).str())
1418+
};
1419+
info.ExtraPCMArgs = ExtraPCMArgs;
14011420
// Run the action under the sub compiler instance.
14021421
return action(info);
14031422
}

lib/FrontendTool/ScanDependencies.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,21 @@ static void writeJSON(llvm::raw_ostream &out,
338338
out << "\n";
339339
}
340340
out.indent(5 * 2);
341-
out << "]\n";
341+
out << "],\n";
342+
}
343+
344+
if (!swiftDeps->extraPCMArgs.empty()) {
345+
out.indent(5 * 2);
346+
out << "\"extraPcmArgs\": [\n";
347+
for (auto &arg :swiftDeps->extraPCMArgs) {
348+
out.indent(6 * 2);
349+
out << "\"" << arg << "\"";
350+
if (&arg != &swiftDeps->extraPCMArgs.back())
351+
out << ",";
352+
out << "\n";
353+
}
354+
out.indent(5 * 2);
355+
out << (swiftDeps->bridgingHeaderFile.hasValue() ? "],\n" : "]\n");
342356
}
343357

344358
/// Bridging header and its source file dependencies, if any.
@@ -412,9 +426,16 @@ bool swift::scanDependencies(CompilerInstance &instance) {
412426
llvm::SmallString<32> mainModulePath = mainModule->getName().str();
413427
llvm::sys::path::replace_extension(mainModulePath, newExt);
414428

429+
std::string apinotesVer = (llvm::Twine("-fapinotes-swift-version=")
430+
+ instance.getASTContext().LangOpts.EffectiveLanguageVersion
431+
.asAPINotesVersionString()).str();
415432
// Compute the dependencies of the main module.
416433
auto mainDependencies =
417-
ModuleDependencies::forSwiftModule(mainModulePath.str().str());
434+
ModuleDependencies::forMainSwiftModule(mainModulePath.str().str(), {
435+
// ExtraPCMArgs
436+
"-Xcc", "-target", "-Xcc", instance.getASTContext().LangOpts.Target.str(),
437+
"-Xcc", apinotesVer
438+
});
418439
{
419440
llvm::StringSet<> alreadyAddedModules;
420441
for (auto fileUnit : mainModule->getFiles()) {

lib/Serialization/ModuleDependencyScanner.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,12 @@ ErrorOr<ModuleDependencies> ModuleDependencyScanner::scanInterfaceFile(
108108
moduleInterfacePath.str(),
109109
StringRef(),
110110
SourceLoc(),
111-
[&](ASTContext &Ctx, ArrayRef<StringRef> Args, StringRef Hash) {
111+
[&](ASTContext &Ctx, ArrayRef<StringRef> Args,
112+
ArrayRef<StringRef> PCMArgs, StringRef Hash) {
112113
Result = ModuleDependencies::forSwiftInterface(modulePath.str().str(),
113114
moduleInterfacePath.str(),
114115
Args,
116+
PCMArgs,
115117
Hash);
116118
// Open the interface file.
117119
auto &fs = *Ctx.SourceMgr.getFileSystem();

test/ScanDependencies/Inputs/ModuleDependencyGraph.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ struct SwiftModuleDetails: Codable {
6969
/// The Swift command line arguments that need to be passed through
7070
/// to the -compile-module-from-interface action to build this module.
7171
var commandLine: [String]?
72+
73+
/// To build a PCM to be used by this Swift module, we need to append these
74+
/// arguments to the generic PCM build arguments reported from the dependency
75+
/// graph.
76+
var extraPcmArgs: [String]?
7277
}
7378

7479
/// Details specific to Clang modules.

test/ScanDependencies/module_deps.swift

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,6 @@
1717
// RUN: %target-codesign %t/main
1818
// RUN: %target-run %t/main %t/deps.json
1919

20-
// RUN: mkdir -p %t/BuildModules
21-
// RUN: cp %S/Inputs/BuildModulesFromGraph.swift %t/BuildModules/main.swift
22-
// RUN: %target-build-swift %S/Inputs/ModuleDependencyGraph.swift %t/BuildModules/main.swift -o %t/ModuleBuilder
23-
// RUN: %target-codesign %t/ModuleBuilder
24-
25-
// RUN: %target-run %t/ModuleBuilder %t/deps.json %swift-path SwiftShims.pcm -o %t/clang-module-cache/SwiftShims.pcm | %S/Inputs/CommandRunner.py
26-
// RUN: ls %t/clang-module-cache/SwiftShims.pcm
27-
// RUN: %target-run %t/ModuleBuilder %t/deps.json %swift-path A.pcm -o %t/clang-module-cache/A.pcm | %S/Inputs/CommandRunner.py
28-
// RUN: ls %t/clang-module-cache/A.pcm
29-
// RUN: %target-run %t/ModuleBuilder %t/deps.json %swift-path B.pcm -o %t/clang-module-cache/B.pcm -Xcc -Xclang -Xcc -fmodule-map-file=%S/Inputs/CHeaders/module.modulemap -Xcc -Xclang -Xcc -fmodule-file=%t/clang-module-cache/A.pcm | %S/Inputs/CommandRunner.py
30-
// RUN: ls %t/clang-module-cache/B.pcm
31-
// RUN: %target-run %t/ModuleBuilder %t/deps.json %swift-path C.pcm -o %t/clang-module-cache/C.pcm -Xcc -Xclang -Xcc -fmodule-map-file=%S/Inputs/CHeaders/module.modulemap -Xcc -Xclang -Xcc -fmodule-file=%t/clang-module-cache/B.pcm | %S/Inputs/CommandRunner.py
32-
// RUN: ls %t/clang-module-cache/C.pcm
33-
34-
// RUN: %target-run %t/ModuleBuilder %t/deps.json %swift-path A.swiftmodule -o %t/clang-module-cache/A.swiftmodule -Xcc -Xclang -Xcc -fmodule-map-file=%S/Inputs/CHeaders/module.modulemap -Xcc -Xclang -Xcc -fmodule-file=%t/clang-module-cache/A.pcm -Xcc -Xclang -Xcc -fmodule-file=%t/clang-module-cache/SwiftShims.pcm | %S/Inputs/CommandRunner.py
35-
// RUN: ls %t/clang-module-cache/A.swiftmodule
36-
37-
// RUN: %target-run %t/ModuleBuilder %t/deps.json %swift-path E.swiftmodule -o %t/clang-module-cache/E.swiftmodule -Xcc -Xclang -Xcc -fmodule-map-file=%S/Inputs/CHeaders/module.modulemap -Xcc -Xclang -Xcc -fmodule-file=%t/clang-module-cache/SwiftShims.pcm | %S/Inputs/CommandRunner.py
38-
// RUN: ls %t/clang-module-cache/E.swiftmodule
39-
40-
// RUN: %target-run %t/ModuleBuilder %t/deps.json %swift-path SubE.swiftmodule -o %t/clang-module-cache/SubE.swiftmodule -Xcc -Xclang -Xcc -fmodule-map-file=%S/Inputs/CHeaders/module.modulemap -Xcc -Xclang -Xcc -fmodule-file=%t/clang-module-cache/SwiftShims.pcm -swift-module-file %t/clang-module-cache/E.swiftmodule | %S/Inputs/CommandRunner.py
41-
// RUN: ls %t/clang-module-cache/SubE.swiftmodule
42-
4320
// REQUIRES: executable_test
4421
// REQUIRES: objc_interop
4522

@@ -83,6 +60,13 @@ import SubE
8360
// CHECK-NEXT: {
8461
// CHECK-NEXT: "swift": "_cross_import_E"
8562
// CHECK-NEXT: }
63+
// CHECK-NEXT: ],
64+
65+
// CHECK: "extraPcmArgs": [
66+
// CHECK-NEXT: "-Xcc",
67+
// CHECK-NEXT: "-target",
68+
// CHECK-NEXT: "-Xcc",
69+
// CHECK: "-fapinotes-swift-version=4"
8670

8771
// CHECK: "bridgingHeader":
8872
// CHECK-NEXT: "path":
@@ -151,7 +135,11 @@ import SubE
151135
// CHECK: "G"
152136
// CHECK: "-swift-version"
153137
// CHECK: "5"
154-
// CHECK: ]
138+
// CHECK: ],
139+
// CHECK" "extraPcmArgs": [
140+
// CHECK" "-target",
141+
// CHECK" "-fapinotes-swift-version=5"
142+
// CHECK" ]
155143

156144
/// --------Swift module Swift
157145
// CHECK-LABEL: "modulePath": "Swift.swiftmodule",

0 commit comments

Comments
 (0)