Skip to content

Commit 00872ba

Browse files
committed
DependencyScanner: add a new extraPcmArgs field for each Swift module
Building each Swift module explicitly requires dependency PCMs to be built with the exactly same deployment target version. This means we may need to build a Clang module multiple times with different target triples. This patch removes the -target arguments from the reported PCM build arguments and inserts extraPcmArgs fields to each Swift module. swift-driver can combine the generic PCM arguments with these extra arguments to get the command suitable for building a PCM specifically for that loading Swift module.
1 parent ecaf9af commit 00872ba

File tree

9 files changed

+77
-10
lines changed

9 files changed

+77
-10
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: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,18 @@ 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+
// Remove the -target arguments because we should use the target triple
235+
// from the depending Swift modules.
236+
if (*It == "-target") {
237+
It += 2;
238+
continue;
239+
}
240+
addClangArg(*It);
241+
++ It;
234242
}
243+
235244
// Swift frontend action: -emit-pcm
236245
swiftArgs.push_back("-emit-pcm");
237246
swiftArgs.push_back("-module-name");

lib/Frontend/ModuleInterfaceLoader.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1323,10 +1323,12 @@ bool InterfaceSubContextDelegateImpl::runInSubContext(StringRef moduleName,
13231323
StringRef interfacePath,
13241324
StringRef outputPath,
13251325
SourceLoc diagLoc,
1326-
llvm::function_ref<bool(ASTContext&, ArrayRef<StringRef>, StringRef)> action) {
1326+
llvm::function_ref<bool(ASTContext&, ArrayRef<StringRef>,
1327+
ArrayRef<StringRef>, StringRef)> action) {
13271328
return runInSubCompilerInstance(moduleName, interfacePath, outputPath, diagLoc,
13281329
[&](SubCompilerInstanceInfo &info){
13291330
return action(info.Instance->getASTContext(), info.BuildArguments,
1331+
info.ExtraPCMArgs,
13301332
info.Hash);
13311333
});
13321334
}
@@ -1398,6 +1400,10 @@ bool InterfaceSubContextDelegateImpl::runInSubCompilerInstance(StringRef moduleN
13981400
}
13991401
info.BuildArguments = BuildArgs;
14001402
info.Hash = CacheHash;
1403+
std::array<StringRef, 4> ExtraPCMArgs = {"-Xcc", "-target", "-Xcc",
1404+
// PCMs should use the target tripe the interface will be using to build
1405+
*(std::find(BuildArgs.rbegin(), BuildArgs.rend(), "-target") - 1)};
1406+
info.ExtraPCMArgs = ExtraPCMArgs;
14011407
// Run the action under the sub compiler instance.
14021408
return action(info);
14031409
}

lib/FrontendTool/ScanDependencies.cpp

Lines changed: 18 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.
@@ -414,7 +428,9 @@ bool swift::scanDependencies(CompilerInstance &instance) {
414428

415429
// Compute the dependencies of the main module.
416430
auto mainDependencies =
417-
ModuleDependencies::forSwiftModule(mainModulePath.str().str());
431+
ModuleDependencies::forMainSwiftModule(mainModulePath.str().str(), {
432+
"-Xcc", "-target", "-Xcc", instance.getASTContext().LangOpts.Target.str()
433+
});
418434
{
419435
llvm::StringSet<> alreadyAddedModules;
420436
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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ import SubE
8383
// CHECK-NEXT: {
8484
// CHECK-NEXT: "swift": "_cross_import_E"
8585
// CHECK-NEXT: }
86+
// CHECK-NEXT: ],
87+
88+
// CHECK: "extraPcmArgs": [
89+
// CHECK-NEXT: "-Xcc",
90+
// CHECK-NEXT: "-target",
91+
// CHECK-NEXT: "-Xcc",
8692

8793
// CHECK: "bridgingHeader":
8894
// CHECK-NEXT: "path":

0 commit comments

Comments
 (0)