Skip to content

Commit 03230fe

Browse files
committed
[Explicit Module Builds] Pass external module dependencies as placeholders to the scanning action
1 parent fe6d63d commit 03230fe

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public struct Driver {
194194
@_spi(Testing) public var explicitModuleBuildHandler: ExplicitModuleBuildHandler? = nil
195195

196196
/// A collection describing external dependencies for the current main module that may be invisible to
197-
/// the driver itself, but visible to its clients (e.g. build systems like SwiftPM). Along with the external dependenceis'
197+
/// the driver itself, but visible to its clients (e.g. build systems like SwiftPM). Along with the external dependencies'
198198
/// module dependency graphs.
199199
internal var externalDependencyArtifactMap: ExternalDependencyArtifactMap? = nil
200200

Sources/SwiftDriver/Explicit Module Builds/ExplicitModuleBuildHandler.swift

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ import TSCBasic
1313
import TSCUtility
1414
import Foundation
1515

16+
/// A map from a module identifier to a pair consisting of a path to its .swiftmodule file and its module dependency graph.
1617
public typealias ExternalDependencyArtifactMap =
1718
[ModuleDependencyId: (AbsolutePath, InterModuleDependencyGraph)]
1819

1920
/// In Explicit Module Build mode, this handler is responsible for generating and providing
2021
/// build jobs for all module dependencies and providing compile command options
2122
/// that specify said explicit module dependencies.
22-
2323
@_spi(Testing) public struct ExplicitModuleBuildHandler {
2424
/// The module dependency graph.
2525
public var dependencyGraph: InterModuleDependencyGraph
@@ -119,13 +119,27 @@ public typealias ExternalDependencyArtifactMap =
119119
/// - Generate Job: S1
120120
///
121121
mutating public func generateExplicitModuleDependenciesBuildJobs() throws -> [Job] {
122+
// Resolve placeholder dependencies in the dependency graph, if any.
123+
try resolvePlaceholderDependencies()
124+
125+
// Compute jobs for all main module dependencies
122126
var mainModuleInputs: [TypedVirtualPath] = []
123127
var mainModuleCommandLine: [Job.ArgTemplate] = []
124128
try resolveMainModuleDependencies(inputs: &mainModuleInputs,
125129
commandLine: &mainModuleCommandLine)
126130
return Array(swiftModuleBuildCache.values) + clangTargetModuleBuildCache.allJobs
127131
}
128132

133+
/// TODO: Explain
134+
mutating public func resolvePlaceholderDependencies() throws {
135+
print("Hello, World")
136+
for moduleId in dependencyGraph.modules.keys {
137+
if case .swiftPlaceholder(let moduleName) = moduleId {
138+
print(moduleName)
139+
}
140+
}
141+
}
142+
129143
/// Resolve all module dependencies of the main module and add them to the lists of
130144
/// inputs and command line flags.
131145
mutating public func resolveMainModuleDependencies(inputs: inout [TypedVirtualPath],
@@ -306,7 +320,7 @@ public typealias ExternalDependencyArtifactMap =
306320
clangDependencyArtifacts: inout [ClangModuleArtifactInfo],
307321
swiftDependencyArtifacts: inout [SwiftModuleArtifactInfo]
308322
) throws {
309-
for dependencyId in try dependencyGraph.moduleInfo(of: moduleId).directDependencies {
323+
for dependencyId in try dependencyGraph.moduleInfo(of: moduleId).directDependencies! {
310324
guard addedDependenciesSet.insert(dependencyId).inserted else {
311325
continue
312326
}
@@ -323,6 +337,8 @@ public typealias ExternalDependencyArtifactMap =
323337
addedDependenciesSet: &addedDependenciesSet,
324338
clangDependencyArtifacts: &clangDependencyArtifacts,
325339
swiftDependencyArtifacts: &swiftDependencyArtifacts)
340+
case .swiftPlaceholder:
341+
fatalError("Unresolved placeholder dependencies at planning stage.")
326342
}
327343
}
328344
}

Sources/SwiftDriver/Explicit Module Builds/ModuleDependencyScanning.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ extension Driver {
3131
moduleDependencyGraphUse: .dependencyScan)
3232
// FIXME: MSVC runtime flags
3333

34+
// Pass in external dependencies to be treated as placeholder dependencies by the scanner
35+
if let externalDependencyArtifactMap = externalDependencyArtifactMap {
36+
let dependencyPlaceholderMapFile =
37+
try serializeExternalDependencyArtifacts(externalDependencyArtifactMap:
38+
externalDependencyArtifactMap)
39+
commandLine.appendFlag("-placeholder-dependency-module-map-file")
40+
commandLine.appendPath(dependencyPlaceholderMapFile)
41+
}
42+
3443
// Pass on the input files
3544
commandLine.append(contentsOf: inputFiles.map { .path($0.file)})
3645

@@ -44,4 +53,24 @@ extension Driver {
4453
outputs: [TypedVirtualPath(file: .standardOutput, type: .jsonDependencies)],
4554
supportsResponseFiles: true)
4655
}
56+
57+
/// Serialize a map of placeholder
58+
func serializeExternalDependencyArtifacts(externalDependencyArtifactMap: ExternalDependencyArtifactMap)
59+
throws -> AbsolutePath {
60+
let temporaryDirectory = try determineTempDirectory()
61+
let placeholderMapFilePath =
62+
temporaryDirectory.appending(component: "\(moduleOutputInfo.name)-placeholder-modules.json")
63+
64+
var placeholderArtifacts: [SwiftModuleArtifactInfo] = []
65+
for (moduleId, dependencyInfo) in externalDependencyArtifactMap {
66+
placeholderArtifacts.append(
67+
SwiftModuleArtifactInfo(name: moduleId.moduleName,
68+
modulePath: dependencyInfo.0.description))
69+
}
70+
let encoder = JSONEncoder()
71+
encoder.outputFormatting = [.prettyPrinted]
72+
let contents = try encoder.encode(placeholderArtifacts)
73+
try fileSystem.writeFileContents(placeholderMapFilePath, bytes: ByteString(contents))
74+
return placeholderMapFilePath
75+
}
4776
}

0 commit comments

Comments
 (0)