Skip to content

Commit e59abb0

Browse files
committed
[Explicit Module Builds] Add API for external dependencies for Explicit Package Builds
1 parent e2eca46 commit e59abb0

File tree

5 files changed

+42
-13
lines changed

5 files changed

+42
-13
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ public struct Driver {
193193
/// as explicit by the various compilation jobs.
194194
@_spi(Testing) public var explicitModuleBuildHandler: ExplicitModuleBuildHandler? = nil
195195

196+
/// 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'
198+
/// module dependency graphs.
199+
internal var externalDependencyArtifactMap: ExternalDependencyArtifactMap? = nil
200+
196201
/// Handler for emitting diagnostics to stderr.
197202
public static let stderrDiagnosticsHandler: DiagnosticsEngine.DiagnosticsHandler = { diagnostic in
198203
let stream = stderrStream
@@ -226,24 +231,30 @@ public struct Driver {
226231
/// - Parameter diagnosticsHandler: A callback executed when a diagnostic is
227232
/// emitted. The default argument prints diagnostics to stderr.
228233
/// - Parameter executor: Used by the driver to execute jobs. The default argument
229-
/// is present to streamline testing, it shouldn't be used in production.
234+
/// is present to streamline testing, it shouldn't be used in production.
235+
/// - Parameter externalModuleDependencies: A collection of external modules that the main module
236+
/// of the current compilation depends on. Explicit Module Build use only.
230237
public init(
231238
args: [String],
232239
env: [String: String] = ProcessEnv.vars,
233240
diagnosticsEngine: DiagnosticsEngine = DiagnosticsEngine(handlers: [Driver.stderrDiagnosticsHandler]),
234241
fileSystem: FileSystem = localFileSystem,
235-
executor: DriverExecutor
242+
executor: DriverExecutor,
243+
externalModuleDependencies: ExternalDependencyArtifactMap? = nil
236244
) throws {
237245
self.env = env
238246
self.fileSystem = fileSystem
239247

240248
self.diagnosticEngine = diagnosticsEngine
241249
self.executor = executor
242250

251+
self.externalDependencyArtifactMap = externalModuleDependencies
252+
243253
if case .subcommand = try Self.invocationRunMode(forArgs: args).mode {
244254
throw Error.subcommandPassedToDriver
245255
}
246256

257+
247258
var args = try Self.expandResponseFiles(args, fileSystem: fileSystem, diagnosticsEngine: self.diagnosticEngine)
248259

249260
self.driverKind = try Self.determineDriverKind(args: &args)

Sources/SwiftDriver/Explicit Module Builds/ExplicitModuleBuildHandler.swift

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

16+
public typealias ExternalDependencyArtifactMap =
17+
[ModuleDependencyId: (AbsolutePath, InterModuleDependencyGraph)]
18+
1619
/// In Explicit Module Build mode, this handler is responsible for generating and providing
1720
/// build jobs for all module dependencies and providing compile command options
1821
/// that specify said explicit module dependencies.
22+
1923
@_spi(Testing) public struct ExplicitModuleBuildHandler {
2024
/// The module dependency graph.
2125
public var dependencyGraph: InterModuleDependencyGraph
@@ -30,6 +34,9 @@ import Foundation
3034
/// The toolchain to be used for frontend job generation.
3135
private let toolchain: Toolchain
3236

37+
/// A collection of external dependency modules, and their binary module file paths and dependency graph.
38+
private let externalDependencyArtifactMap: ExternalDependencyArtifactMap
39+
3340
/// The file system which we should interact with.
3441
/// FIXME: Our end goal is to not have any direct filesystem manipulation in here, but that's dependent on getting the
3542
/// dependency scanner/dependency job generation moved into a Job.
@@ -42,9 +49,11 @@ import Foundation
4249
private let temporaryDirectory: AbsolutePath
4350

4451
public init(dependencyGraph: InterModuleDependencyGraph, toolchain: Toolchain,
45-
fileSystem: FileSystem) throws {
52+
fileSystem: FileSystem,
53+
externalDependencyArtifactMap: ExternalDependencyArtifactMap) throws {
4654
self.dependencyGraph = dependencyGraph
4755
self.toolchain = toolchain
56+
self.externalDependencyArtifactMap = externalDependencyArtifactMap
4857
self.fileSystem = fileSystem
4958
self.temporaryDirectory = try determineTempDirectory()
5059
}
@@ -457,4 +466,10 @@ private extension InterModuleDependencyGraph {
457466
}
458467
}
459468

460-
469+
// To keep the ExplicitModuleBuildHandler an implementation detail, provide an API
470+
// to access the dependency graph
471+
extension Driver {
472+
public var interModuleDependencyGraph: InterModuleDependencyGraph? {
473+
return explicitModuleBuildHandler?.dependencyGraph
474+
}
475+
}

Sources/SwiftDriver/Explicit Module Builds/InterModuleDependencyGraph.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import Foundation
1313

1414

15-
@_spi(Testing) public enum ModuleDependencyId: Hashable {
15+
public enum ModuleDependencyId: Hashable {
1616
case swift(String)
1717
case clang(String)
1818

@@ -53,7 +53,7 @@ extension ModuleDependencyId: Codable {
5353
}
5454

5555
/// Details specific to Swift modules.
56-
@_spi(Testing) public struct SwiftModuleDetails: Codable {
56+
public struct SwiftModuleDetails: Codable {
5757
/// The module interface from which this module was built, if any.
5858
public var moduleInterfacePath: String?
5959

@@ -79,7 +79,7 @@ extension ModuleDependencyId: Codable {
7979
}
8080

8181
/// Details specific to Clang modules.
82-
@_spi(Testing) public struct ClangModuleDetails: Codable {
82+
public struct ClangModuleDetails: Codable {
8383
/// The path to the module map used to build this module.
8484
public var moduleMapPath: String
8585

@@ -90,7 +90,7 @@ extension ModuleDependencyId: Codable {
9090
public var commandLine: [String]? = []
9191
}
9292

93-
@_spi(Testing) public struct ModuleInfo: Codable {
93+
public struct ModuleInfo: Codable {
9494
/// The path for the module.
9595
public var modulePath: String
9696

@@ -144,7 +144,7 @@ extension ModuleInfo.Details: Codable {
144144

145145
/// Describes the complete set of dependencies for a Swift module, including
146146
/// all of the Swift and C modules and source files it depends on.
147-
@_spi(Testing) public struct InterModuleDependencyGraph: Codable {
147+
public struct InterModuleDependencyGraph: Codable {
148148
/// The name of the main module.
149149
public var mainModuleName: String
150150

Sources/SwiftDriver/Jobs/Planning.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,11 @@ extension Driver {
230230
forceResponseFiles: forceResponseFiles,
231231
recordedInputModificationDates: recordedInputModificationDates)
232232

233-
explicitModuleBuildHandler = try ExplicitModuleBuildHandler(dependencyGraph: dependencyGraph,
234-
toolchain: toolchain,
235-
fileSystem: fileSystem)
233+
explicitModuleBuildHandler =
234+
try ExplicitModuleBuildHandler(dependencyGraph: dependencyGraph,
235+
toolchain: toolchain,
236+
fileSystem: fileSystem,
237+
externalDependencyArtifactMap: externalDependencyArtifactMap ?? [:])
236238
return try explicitModuleBuildHandler!.generateExplicitModuleDependenciesBuildJobs()
237239
}
238240

Tests/SwiftDriverTests/ExplicitModuleBuildTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ final class ExplicitModuleBuildTests: XCTestCase {
133133
from: ModuleDependenciesInputs.fastDependencyScannerOutput.data(using: .utf8)!)
134134
driver.explicitModuleBuildHandler = try ExplicitModuleBuildHandler(dependencyGraph: moduleDependencyGraph,
135135
toolchain: driver.toolchain,
136-
fileSystem: localFileSystem)
136+
fileSystem: localFileSystem,
137+
externalDependencyArtifactMap: [:])
137138
let modulePrebuildJobs =
138139
try driver.explicitModuleBuildHandler!.generateExplicitModuleDependenciesBuildJobs()
139140
XCTAssertEqual(modulePrebuildJobs.count, 4)

0 commit comments

Comments
 (0)