Skip to content

Commit be3812d

Browse files
committed
[Dependency Scanning] Add swift dependency compile arguments required for self-contained commands
- '-o <output_path>' - '-disable-implicit-swift-modules' - '-Xcc -fno-implicit-modules' and '-Xcc -fno-implicit-module-maps' - '-candidate-module-file' These were previously supplied by the driver. Instead, they will now be ready to be run directly from the dependency scanner's output.
1 parent c989823 commit be3812d

File tree

4 files changed

+36
-23
lines changed

4 files changed

+36
-23
lines changed

lib/ClangImporter/ClangModuleDependencyScanner.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,15 @@ void ClangImporter::recordModuleDependencies(
153153
// We are using Swift frontend mode.
154154
swiftArgs.push_back("-frontend");
155155

156-
// We pass the entire argument list via -Xcc, so the invocation should
157-
// use extra clang options alone.
158-
swiftArgs.push_back("-only-use-extra-clang-opts");
159-
160156
// Swift frontend action: -emit-pcm
161157
swiftArgs.push_back("-emit-pcm");
162158
swiftArgs.push_back("-module-name");
163159
swiftArgs.push_back(clangModuleDep.ID.ModuleName);
164160

161+
// We pass the entire argument list via -Xcc, so the invocation should
162+
// use extra clang options alone.
163+
swiftArgs.push_back("-only-use-extra-clang-opts");
164+
165165
auto pcmPath = moduleCacheRelativeLookupModuleOutput(
166166
clangModuleDep.ID, ModuleOutputKind::ModuleFile,
167167
getModuleCachePathFromClang(getClangInstance()));

lib/DependencyScan/ScanDependencies.cpp

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -176,16 +176,14 @@ static std::vector<ModuleDependencyID>
176176
computeTopologicalSortOfExplicitDependencies(
177177
const ModuleDependencyIDSetVector &allModules,
178178
const ModuleDependenciesCache &cache) {
179+
std::unordered_set<ModuleDependencyID> visited;
180+
std::vector<ModuleDependencyID> result;
181+
std::stack<ModuleDependencyID> stack;
182+
179183
// Must be explicitly-typed to allow recursion
180-
std::function<void(const ModuleDependencyID &,
181-
std::stack<ModuleDependencyID> &,
182-
std::unordered_set<ModuleDependencyID> &,
183-
std::vector<ModuleDependencyID> &)>
184-
visit;
185-
visit = [&visit, &cache](const ModuleDependencyID &moduleID,
186-
std::stack<ModuleDependencyID> &stack,
187-
std::unordered_set<ModuleDependencyID> &visited,
188-
std::vector<ModuleDependencyID> &result) {
184+
std::function<void(const ModuleDependencyID &)> visit;
185+
visit = [&visit, &cache, &visited, &result,
186+
&stack](const ModuleDependencyID &moduleID) {
189187
// Mark this node as visited -- we are done if it already was.
190188
if (!visited.insert(moduleID).second)
191189
return;
@@ -196,7 +194,7 @@ computeTopologicalSortOfExplicitDependencies(
196194
// since that would mean we have found a cycle, which should not
197195
// be possible because we checked for cycles earlier.
198196
stack.push(succID);
199-
visit(succID, stack, visited, result);
197+
visit(succID);
200198
auto top = stack.top();
201199
stack.pop();
202200
assert(top == succID);
@@ -206,13 +204,10 @@ computeTopologicalSortOfExplicitDependencies(
206204
result.push_back(moduleID);
207205
};
208206

209-
std::unordered_set<ModuleDependencyID> visited;
210-
std::vector<ModuleDependencyID> result;
211-
std::stack<ModuleDependencyID> stack;
212207
for (const auto &modID : allModules) {
213208
assert(stack.empty());
214209
stack.push(modID);
215-
visit(modID, stack, visited, result);
210+
visit(modID);
216211
auto top = stack.top();
217212
stack.pop();
218213
assert(top == modID);
@@ -222,13 +217,15 @@ computeTopologicalSortOfExplicitDependencies(
222217
return result;
223218
}
224219

225-
/// For each module in the graph, compute a set of all its dependencies
220+
/// For each module in the graph, compute a set of all its dependencies,
226221
/// direct *and* transitive.
227222
static std::unordered_map<ModuleDependencyID,
228223
std::set<ModuleDependencyID>>
229224
computeTransitiveClosureOfExplicitDependencies(
230225
const std::vector<ModuleDependencyID> &topologicallySortedModuleList,
231226
const ModuleDependenciesCache &cache) {
227+
// The usage of an ordered ::set is important to ensure the
228+
// dependencies are listed in a deterministic order.
232229
std::unordered_map<ModuleDependencyID, std::set<ModuleDependencyID>>
233230
result;
234231
for (const auto &modID : topologicallySortedModuleList)
@@ -286,7 +283,7 @@ resolveExplicitModuleInputs(ModuleDependencyID moduleID,
286283
} break;
287284
case swift::ModuleDependencyKind::Clang: {
288285
auto clangDepDetails = depInfo->getAsClangModule();
289-
assert(binaryDepDetails && "Expected Clang Module dependency.");
286+
assert(clangDepDetails && "Expected Clang Module dependency.");
290287
commandLine.push_back("-Xcc");
291288
commandLine.push_back("-fmodule-file=" + depModuleID.first + "=" +
292289
clangDepDetails->pcmOutputPath);

lib/Serialization/ModuleDependencyScanner.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,21 +112,38 @@ ErrorOr<ModuleDependencyInfo> ModuleDependencyScanner::scanInterfaceFile(
112112
StringRef(),
113113
SourceLoc(),
114114
[&](ASTContext &Ctx, ModuleDecl *mainMod,
115-
ArrayRef<StringRef> Args,
115+
ArrayRef<StringRef> BaseArgs,
116116
ArrayRef<StringRef> PCMArgs, StringRef Hash) {
117117
assert(mainMod);
118118
std::string InPath = moduleInterfacePath.str();
119119
auto compiledCandidates = getCompiledCandidates(Ctx, realModuleName.str(),
120120
InPath);
121+
std::vector<std::string> Args(BaseArgs.begin(), BaseArgs.end());
122+
123+
// Add explicit Swift dependency compilation flags
124+
Args.push_back("-explicit-interface-module-build");
125+
Args.push_back("-disable-implicit-swift-modules");
126+
Args.push_back("-Xcc"); Args.push_back("-fno-implicit-modules");
127+
Args.push_back("-Xcc"); Args.push_back("-fno-implicit-module-maps");
128+
for (const auto &candidate : compiledCandidates) {
129+
Args.push_back("-candidate-module-file");
130+
Args.push_back(candidate);
131+
}
121132

133+
// Compute the output path and add it to the command line
122134
SmallString<128> outputPathBase(moduleCachePath);
123135
llvm::sys::path::append(
124136
outputPathBase,
125137
moduleName.str() + "-" + Hash + "." +
126138
file_types::getExtension(file_types::TY_SwiftModuleFile));
139+
Args.push_back("-o");
140+
Args.push_back(outputPathBase.str().str());
141+
142+
std::vector<StringRef> ArgsRefs(Args.begin(), Args.end());
127143
Result = ModuleDependencyInfo::forSwiftInterfaceModule(
128-
outputPathBase.str().str(), InPath, compiledCandidates, Args, PCMArgs,
144+
outputPathBase.str().str(), InPath, compiledCandidates, ArgsRefs, PCMArgs,
129145
Hash, isFramework);
146+
130147
// Open the interface file.
131148
auto &fs = *Ctx.SourceMgr.getFileSystem();
132149
auto interfaceBuf = fs.getBufferForFile(moduleInterfacePath);

test/ScanDependencies/module_deps_cache_reuse.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ import SubE
8181
// CHECK-DAG: "swift": "SwiftOnoneSupport"
8282
// CHECK: ],
8383

84-
8584
/// --------Swift module G
8685
// CHECK-LABEL: "modulePath": "{{.*}}{{/|\\}}G-{{.*}}.swiftmodule"
8786
// CHECK: "directDependencies"

0 commit comments

Comments
 (0)