Skip to content

Commit b817ead

Browse files
committed
[Explicit Module Builds] Refactor clang module dependency commandLine/input gathering to match swift
1 parent 58fc3dc commit b817ead

File tree

3 files changed

+65
-30
lines changed

3 files changed

+65
-30
lines changed

Sources/SwiftDriver/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ add_library(SwiftDriver
1111
"Explicit Module Builds/ExplicitModuleBuildHandler.swift"
1212
"Explicit Module Builds/InterModuleDependencyGraph.swift"
1313
"Explicit Module Builds/ModuleDependencyScanning.swift"
14-
"Explicit Module Builds/SwiftModuleArtifacts.swift"
14+
"Explicit Module Builds/ModuleArtifacts.swift"
1515

1616
Driver/CompilerMode.swift
1717
Driver/DebugInfo.swift

Sources/SwiftDriver/Explicit Module Builds/ExplicitModuleBuildHandler.swift

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,14 @@ public struct ExplicitModuleBuildHandler {
3232
private let toolchain: Toolchain
3333

3434
/// The file system which we should interact with.
35+
/// FIXME: Our end goal is to not have any direct filesystem manipulation in here, but that's dependent on getting the
36+
/// dependency scanner/dependency job generation moved into a Job.
3537
private let fileSystem: FileSystem
3638

3739
/// Path to the directory that will contain the temporary files.
3840
/// e.g. Explicit Swift module artifact files
41+
/// FIXME: Our end goal is to not have any direct filesystem manipulation in here, but that's dependent on getting the
42+
/// dependency scanner/dependency job generation moved into a Job.
3943
private let temporaryDirectory: AbsolutePath
4044

4145
public init(dependencyGraph: InterModuleDependencyGraph, toolchain: Toolchain,
@@ -236,43 +240,61 @@ public struct ExplicitModuleBuildHandler {
236240
commandLine.appendFlags("-disable-implicit-swift-modules", "-Xcc", "-Xclang", "-Xcc",
237241
"-fno-implicit-modules")
238242
var swiftDependencyArtifacts: [SwiftModuleArtifactInfo] = []
239-
try addModuleDependencies(moduleId: moduleId, pcmArgs: pcmArgs, inputs: &inputs,
240-
commandLine: &commandLine,
243+
var clangDependencyArtifacts: [ClangModuleArtifactInfo] = []
244+
try addModuleDependencies(moduleId: moduleId, pcmArgs: pcmArgs,
245+
clangDependencyArtifacts: &clangDependencyArtifacts,
241246
swiftDependencyArtifacts: &swiftDependencyArtifacts)
242247

248+
// Swift Module dependencies are passed encoded in a JSON file as described by
249+
// SwiftModuleArtifactInfo
243250
if !swiftDependencyArtifacts.isEmpty {
244-
let dependencyFile = try serializeModuleDependencies(for: moduleId,
245-
dependencyArtifacts: swiftDependencyArtifacts)
251+
let dependencyFile =
252+
try serializeModuleDependencies(for: moduleId, dependencyArtifacts: swiftDependencyArtifacts)
246253
commandLine.appendFlag("-explicit-swift-module-map-file")
247254
commandLine.appendPath(dependencyFile)
248255
inputs.append(TypedVirtualPath(file: try VirtualPath(path: dependencyFile.pathString),
249256
type: .jsonSwiftArtifacts))
250-
// Each individual moduel binary is still an "input" to ensure the build system gets the
257+
// Each individual module binary is still an "input" to ensure the build system gets the
251258
// order correctly.
252259
for dependencyModule in swiftDependencyArtifacts {
253260
inputs.append(TypedVirtualPath(file: try VirtualPath(path: dependencyModule.modulePath),
254261
type: .swiftModule))
255262
}
256263
}
264+
// Clang module depenencies are specified on the command line eplicitly
265+
for moduleArtifactInfo in clangDependencyArtifacts {
266+
let clangModulePath =
267+
TypedVirtualPath(file: try VirtualPath(path: moduleArtifactInfo.modulePath),
268+
type: .pcm)
269+
let clangModuleMapPath =
270+
TypedVirtualPath(file: try VirtualPath(path: moduleArtifactInfo.moduleMapPath),
271+
type: .clangModuleMap)
272+
commandLine.appendFlags("-Xcc", "-Xclang", "-Xcc",
273+
"-fmodule-file=\(clangModulePath.file.description)")
274+
commandLine.appendFlags("-Xcc", "-Xclang", "-Xcc",
275+
"-fmodule-map-file=\(clangModuleMapPath.file.description)")
276+
inputs.append(clangModulePath)
277+
inputs.append(clangModuleMapPath)
278+
}
257279
}
258280

259281
/// Add a specific module dependency as an input and a corresponding command
260282
/// line flag. Dispatches to clang and swift-specific variants.
261-
mutating private func addModuleDependencies(moduleId: ModuleDependencyId,
262-
pcmArgs: [String],
263-
inputs: inout [TypedVirtualPath],
264-
commandLine: inout [Job.ArgTemplate],
283+
mutating private func addModuleDependencies(moduleId: ModuleDependencyId, pcmArgs: [String],
284+
clangDependencyArtifacts: inout [ClangModuleArtifactInfo],
265285
swiftDependencyArtifacts: inout [SwiftModuleArtifactInfo]
266286
) throws {
267287
for dependencyId in try dependencyGraph.moduleInfo(of: moduleId).directDependencies {
268288
switch dependencyId {
269289
case .swift:
270290
try addSwiftModuleDependency(moduleId: moduleId, dependencyId: dependencyId,
271-
pcmArgs: pcmArgs, inputs: &inputs, commandLine: &commandLine,
291+
pcmArgs: pcmArgs,
292+
clangDependencyArtifacts: &clangDependencyArtifacts,
272293
swiftDependencyArtifacts: &swiftDependencyArtifacts)
273294
case .clang:
274295
try addClangModuleDependency(moduleId: moduleId, dependencyId: dependencyId,
275-
pcmArgs: pcmArgs, inputs: &inputs, commandLine: &commandLine,
296+
pcmArgs: pcmArgs,
297+
clangDependencyArtifacts: &clangDependencyArtifacts,
276298
swiftDependencyArtifacts: &swiftDependencyArtifacts)
277299
}
278300
}
@@ -285,8 +307,7 @@ public struct ExplicitModuleBuildHandler {
285307
mutating private func addSwiftModuleDependency(moduleId: ModuleDependencyId,
286308
dependencyId: ModuleDependencyId,
287309
pcmArgs: [String],
288-
inputs: inout [TypedVirtualPath],
289-
commandLine: inout [Job.ArgTemplate],
310+
clangDependencyArtifacts: inout [ClangModuleArtifactInfo],
290311
swiftDependencyArtifacts: inout [SwiftModuleArtifactInfo]
291312
) throws {
292313
// Generate a build job for the dependency module, if not already generated
@@ -300,15 +321,15 @@ public struct ExplicitModuleBuildHandler {
300321
let swiftModulePath = TypedVirtualPath(file: try VirtualPath(path: dependencyInfo.modulePath),
301322
type: .swiftModule)
302323

303-
// Collect the requried information about this module
324+
// Collect the required information about this module
304325
// TODO: add .swiftdoc and .swiftsourceinfo for this module.
305326
swiftDependencyArtifacts.append(
306327
SwiftModuleArtifactInfo(name: dependencyId.moduleName,
307328
modulePath: swiftModulePath.file.description))
308329

309330
// Process all transitive dependencies as direct
310331
try addModuleDependencies(moduleId: dependencyId, pcmArgs: pcmArgs,
311-
inputs: &inputs, commandLine: &commandLine,
332+
clangDependencyArtifacts: &clangDependencyArtifacts,
312333
swiftDependencyArtifacts: &swiftDependencyArtifacts)
313334
}
314335

@@ -319,8 +340,7 @@ public struct ExplicitModuleBuildHandler {
319340
mutating private func addClangModuleDependency(moduleId: ModuleDependencyId,
320341
dependencyId: ModuleDependencyId,
321342
pcmArgs: [String],
322-
inputs: inout [TypedVirtualPath],
323-
commandLine: inout [Job.ArgTemplate],
343+
clangDependencyArtifacts: inout [ClangModuleArtifactInfo],
324344
swiftDependencyArtifacts: inout [SwiftModuleArtifactInfo]
325345
) throws {
326346
// Generate a build job for the dependency module at the given target, if not already generated
@@ -333,21 +353,17 @@ public struct ExplicitModuleBuildHandler {
333353
let dependencyInfo = try dependencyGraph.moduleInfo(of: dependencyId)
334354
let dependencyClangModuleDetails = try dependencyGraph.clangModuleDetails(of: dependencyId)
335355
let clangModulePath =
336-
TypedVirtualPath(file: try ExplicitModuleBuildHandler.targetEncodedClangModuleFilePath(
337-
for: dependencyInfo, pcmArgs: pcmArgs), type: .pcm)
338-
let clangModuleMapPath =
339-
TypedVirtualPath(file: try VirtualPath(path: dependencyClangModuleDetails.moduleMapPath),
340-
type: .clangModuleMap)
341-
commandLine.appendFlags("-Xcc", "-Xclang", "-Xcc",
342-
"-fmodule-map-file=\(clangModuleMapPath.file.description)")
343-
commandLine.appendFlags("-Xcc", "-Xclang", "-Xcc",
344-
"-fmodule-file=\(clangModulePath.file.description)")
345-
inputs.append(clangModulePath)
346-
inputs.append(clangModuleMapPath)
356+
try ExplicitModuleBuildHandler.targetEncodedClangModuleFilePath(for: dependencyInfo,
357+
pcmArgs: pcmArgs)
358+
359+
// Collect the requried information about this module
360+
clangDependencyArtifacts.append(
361+
ClangModuleArtifactInfo(name: dependencyId.moduleName, modulePath: clangModulePath.description,
362+
moduleMapPath: dependencyClangModuleDetails.moduleMapPath))
347363

348364
// Process all transitive dependencies as direct
349365
try addModuleDependencies(moduleId: dependencyId, pcmArgs: pcmArgs,
350-
inputs: &inputs, commandLine: &commandLine,
366+
clangDependencyArtifacts: &clangDependencyArtifacts,
351367
swiftDependencyArtifacts: &swiftDependencyArtifacts)
352368
}
353369
}

Sources/SwiftDriver/Explicit Module Builds/SwiftModuleArtifacts.swift renamed to Sources/SwiftDriver/Explicit Module Builds/ModuleArtifacts.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,22 @@ public struct SwiftModuleArtifactInfo: Codable {
3333
self.sourceInfoPath = sourceInfoPath
3434
}
3535
}
36+
37+
/// Describes a given Clang module's pre-built module artifacts:
38+
/// - Clang Module (name)
39+
/// - Clang Module (PCM) Path
40+
/// - Clang Module Map Path
41+
public struct ClangModuleArtifactInfo {
42+
/// The module's name
43+
public let moduleName: String
44+
/// The path for the module's .pcm file
45+
public let modulePath: String
46+
/// The path for this module's .modulemap file
47+
public let moduleMapPath: String
48+
49+
init(name: String, modulePath: String, moduleMapPath: String) {
50+
self.moduleName = name
51+
self.modulePath = modulePath
52+
self.moduleMapPath = moduleMapPath
53+
}
54+
}

0 commit comments

Comments
 (0)