Skip to content

Commit 9ef905a

Browse files
authored
Merge pull request #913 from apple/es-mod2
Module aliasing: use real module names to resolve dependency graph with both default and fallback deps scanner Make isFramework property optional in SwiftModuleDetails/SwiftPrebuiltExternalModuleDetails to fix JSON Decoding error Resolves rdar://85525296
2 parents a013934 + 6459882 commit 9ef905a

File tree

7 files changed

+311
-17
lines changed

7 files changed

+311
-17
lines changed

Sources/SwiftDriver/ExplicitModuleBuilds/ExplicitDependencyBuildPlanner.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ public typealias ExternalTargetModuleDetailsMap = [ModuleDependencyId: ExternalT
313313
let isFramework: Bool
314314
swiftModulePath = .init(file: dependencyInfo.modulePath.path,
315315
type: .swiftModule)
316-
isFramework = try dependencyGraph.swiftModuleDetails(of: dependencyId).isFramework
316+
isFramework = try dependencyGraph.swiftModuleDetails(of: dependencyId).isFramework ?? false
317317
// Accumulate the required information about this dependency
318318
// TODO: add .swiftdoc and .swiftsourceinfo for this module.
319319
swiftDependencyArtifacts.append(
@@ -336,7 +336,7 @@ public typealias ExternalTargetModuleDetailsMap = [ModuleDependencyId: ExternalT
336336
case .swiftPrebuiltExternal:
337337
let prebuiltModuleDetails = try dependencyGraph.swiftPrebuiltDetails(of: dependencyId)
338338
let compiledModulePath = prebuiltModuleDetails.compiledModulePath
339-
let isFramework = prebuiltModuleDetails.isFramework
339+
let isFramework = prebuiltModuleDetails.isFramework ?? false
340340
let swiftModulePath: TypedVirtualPath =
341341
.init(file: compiledModulePath.path, type: .swiftModule)
342342
// Accumulate the requried information about this dependency

Sources/SwiftDriver/ExplicitModuleBuilds/InterModuleDependencies/InterModuleDependencyGraph.swift

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public struct SwiftModuleDetails: Codable {
108108
public var extraPcmArgs: [String]
109109

110110
/// A flag to indicate whether or not this module is a framework.
111-
public var isFramework: Bool
111+
public var isFramework: Bool?
112112
}
113113

114114
/// Details specific to Swift placeholder dependencies.
@@ -133,7 +133,7 @@ public struct SwiftPrebuiltExternalModuleDetails: Codable {
133133
public var moduleSourceInfoPath: TextualVirtualPath?
134134

135135
/// A flag to indicate whether or not this module is a framework.
136-
public var isFramework: Bool
136+
public var isFramework: Bool?
137137

138138
public init(compiledModulePath: TextualVirtualPath,
139139
moduleDocPath: TextualVirtualPath? = nil,
@@ -274,4 +274,23 @@ public struct InterModuleDependencyGraph: Codable {
274274

275275
public struct InterModuleDependencyImports: Codable {
276276
public var imports: [String]
277+
278+
public init(imports: [String], moduleAliases: [String: String]? = nil) {
279+
var realImports = [String]()
280+
if let aliases = moduleAliases {
281+
for elem in imports {
282+
if let realName = aliases[elem] {
283+
realImports.append(realName)
284+
} else {
285+
realImports.append(elem)
286+
}
287+
}
288+
}
289+
290+
if !realImports.isEmpty {
291+
self.imports = realImports
292+
} else {
293+
self.imports = imports
294+
}
295+
}
277296
}

Sources/SwiftDriver/ExplicitModuleBuilds/InterModuleDependencies/InterModuleDependencyOracle.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,33 +31,39 @@ public class InterModuleDependencyOracle {
3131
public init() {}
3232

3333
@_spi(Testing) public func getDependencies(workingDirectory: AbsolutePath,
34+
moduleAliases: [String: String]? = nil,
3435
commandLine: [String])
3536
throws -> InterModuleDependencyGraph {
3637
precondition(hasScannerInstance)
3738
return try queue.sync {
3839
return try swiftScanLibInstance!.scanDependencies(workingDirectory: workingDirectory,
40+
moduleAliases: moduleAliases,
3941
invocationCommand: commandLine)
4042
}
4143
}
4244

4345
@_spi(Testing) public func getBatchDependencies(workingDirectory: AbsolutePath,
46+
moduleAliases: [String: String]? = nil,
4447
commandLine: [String],
4548
batchInfos: [BatchScanModuleInfo])
4649
throws -> [ModuleDependencyId: [InterModuleDependencyGraph]] {
4750
precondition(hasScannerInstance)
4851
return try queue.sync {
4952
return try swiftScanLibInstance!.batchScanDependencies(workingDirectory: workingDirectory,
53+
moduleAliases: moduleAliases,
5054
invocationCommand: commandLine,
5155
batchInfos: batchInfos)
5256
}
5357
}
5458

5559
@_spi(Testing) public func getImports(workingDirectory: AbsolutePath,
60+
moduleAliases: [String: String]? = nil,
5661
commandLine: [String])
5762
throws -> InterModuleDependencyImports {
5863
precondition(hasScannerInstance)
5964
return try queue.sync {
6065
return try swiftScanLibInstance!.preScanImports(workingDirectory: workingDirectory,
66+
moduleAliases: moduleAliases,
6167
invocationCommand: commandLine)
6268
}
6369
}

Sources/SwiftDriver/ExplicitModuleBuilds/ModuleDependencyScanning.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ public extension Driver {
131131
sanitizeCommandForLibScanInvocation(&command)
132132
imports =
133133
try interModuleDependencyOracle.getImports(workingDirectory: cwd,
134+
moduleAliases: moduleOutputInfo.aliases,
134135
commandLine: command)
135136

136137
} else {
@@ -159,6 +160,7 @@ public extension Driver {
159160
sanitizeCommandForLibScanInvocation(&command)
160161
dependencyGraph =
161162
try interModuleDependencyOracle.getDependencies(workingDirectory: cwd,
163+
moduleAliases: moduleOutputInfo.aliases,
162164
commandLine: command)
163165
} else {
164166
// Fallback to legacy invocation of the dependency scanner with
@@ -187,6 +189,7 @@ public extension Driver {
187189
sanitizeCommandForLibScanInvocation(&command)
188190
moduleVersionedGraphMap =
189191
try interModuleDependencyOracle.getBatchDependencies(workingDirectory: cwd,
192+
moduleAliases: moduleOutputInfo.aliases,
190193
commandLine: command,
191194
batchInfos: moduleInfos)
192195
} else {

Sources/SwiftDriver/SwiftScan/DependencyGraphBuilder.swift

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
internal extension SwiftScan {
1616
/// From a reference to a binary-format dependency graph returned by libSwiftScan,
1717
/// construct an instance of an `InterModuleDependencyGraph`.
18-
func constructGraph(from scannerGraphRef: swiftscan_dependency_graph_t) throws
18+
func constructGraph(from scannerGraphRef: swiftscan_dependency_graph_t,
19+
moduleAliases: [String: String]?) throws
1920
-> InterModuleDependencyGraph {
2021
let mainModuleNameRef =
2122
api.swiftscan_dependency_graph_get_main_module_name(scannerGraphRef)
@@ -35,7 +36,7 @@ internal extension SwiftScan {
3536
guard let moduleRef = moduleRefOrNull else {
3637
throw DependencyScanningError.missingField("dependency_set_t.modules[_]")
3738
}
38-
let (moduleId, moduleInfo) = try constructModuleInfo(from: moduleRef)
39+
let (moduleId, moduleInfo) = try constructModuleInfo(from: moduleRef, moduleAliases: moduleAliases)
3940
resultGraph.modules[moduleId] = moduleInfo
4041
}
4142

@@ -44,18 +45,20 @@ internal extension SwiftScan {
4445

4546
/// From a reference to a binary-format set of module imports return by libSwiftScan pre-scan query,
4647
/// construct an instance of an `InterModuleDependencyImports` set
47-
func constructImportSet(from importSetRef: swiftscan_import_set_t) throws
48+
func constructImportSet(from importSetRef: swiftscan_import_set_t,
49+
with moduleAliases: [String: String]?) throws
4850
-> InterModuleDependencyImports {
4951
guard let importsRef = api.swiftscan_import_set_get_imports(importSetRef) else {
5052
throw DependencyScanningError.missingField("import_set.imports")
5153
}
52-
return InterModuleDependencyImports(imports: try toSwiftStringArray(importsRef.pointee))
54+
return InterModuleDependencyImports(imports: try toSwiftStringArray(importsRef.pointee), moduleAliases: moduleAliases)
5355
}
5456

5557
/// From a reference to a binary-format dependency graph collection returned by libSwiftScan batch scan query,
5658
/// corresponding to the specified batch scan input (`BatchScanModuleInfo`), construct instances of
5759
/// `InterModuleDependencyGraph` for each result.
5860
func constructBatchResultGraphs(for batchInfos: [BatchScanModuleInfo],
61+
moduleAliases: [String: String]?,
5962
from batchResultRef: swiftscan_batch_scan_result_t) throws
6063
-> [ModuleDependencyId: [InterModuleDependencyGraph]] {
6164
var resultMap: [ModuleDependencyId: [InterModuleDependencyGraph]] = [:]
@@ -66,7 +69,7 @@ internal extension SwiftScan {
6669
guard let resultGraphRef = resultGraphRefOrNull else {
6770
throw DependencyScanningError.dependencyScanFailed
6871
}
69-
let decodedGraph = try constructGraph(from: resultGraphRef)
72+
let decodedGraph = try constructGraph(from: resultGraphRef, moduleAliases: moduleAliases)
7073

7174
let moduleId: ModuleDependencyId
7275
switch batchInfos[index] {
@@ -89,12 +92,13 @@ internal extension SwiftScan {
8992
private extension SwiftScan {
9093
/// From a reference to a binary-format module dependency module info returned by libSwiftScan,
9194
/// construct an instance of an `ModuleInfo` as used by the driver
92-
func constructModuleInfo(from moduleInfoRef: swiftscan_dependency_info_t)
95+
func constructModuleInfo(from moduleInfoRef: swiftscan_dependency_info_t,
96+
moduleAliases: [String: String]?)
9397
throws -> (ModuleDependencyId, ModuleInfo) {
9498
// Decode the module name and module kind
9599
let encodedModuleName =
96100
try toSwiftString(api.swiftscan_module_info_get_module_name(moduleInfoRef))
97-
let moduleId = try decodeModuleNameAndKind(from: encodedModuleName)
101+
let moduleId = try decodeModuleNameAndKind(from: encodedModuleName, moduleAliases: moduleAliases)
98102

99103
// Decode module path and source file locations
100104
let modulePathStr = try toSwiftString(api.swiftscan_module_info_get_module_path(moduleInfoRef))
@@ -111,7 +115,7 @@ private extension SwiftScan {
111115
if let encodedDirectDepsRef = api.swiftscan_module_info_get_direct_dependencies(moduleInfoRef) {
112116
let encodedDirectDependencies = try toSwiftStringArray(encodedDirectDepsRef.pointee)
113117
directDependencies =
114-
try encodedDirectDependencies.map { try decodeModuleNameAndKind(from: $0) }
118+
try encodedDirectDependencies.map { try decodeModuleNameAndKind(from: $0, moduleAliases: moduleAliases) }
115119
} else {
116120
directDependencies = nil
117121
}
@@ -373,12 +377,21 @@ private extension SwiftScan {
373377
/// "swiftBinary"
374378
/// "swiftPlaceholder"
375379
/// "clang""
376-
func decodeModuleNameAndKind(from encodedName: String) throws -> ModuleDependencyId {
380+
func decodeModuleNameAndKind(from encodedName: String,
381+
moduleAliases: [String: String]?) throws -> ModuleDependencyId {
377382
switch encodedName {
378383
case _ where encodedName.starts(with: "swiftTextual:"):
379-
return .swift(String(encodedName.suffix(encodedName.count - "swiftTextual:".count)))
384+
var namePart = String(encodedName.suffix(encodedName.count - "swiftTextual:".count))
385+
if let moduleAliases = moduleAliases, let realName = moduleAliases[namePart] {
386+
namePart = realName
387+
}
388+
return .swift(namePart)
380389
case _ where encodedName.starts(with: "swiftBinary:"):
381-
return .swiftPrebuiltExternal(String(encodedName.suffix(encodedName.count - "swiftBinary:".count)))
390+
var namePart = String(encodedName.suffix(encodedName.count - "swiftBinary:".count))
391+
if let moduleAliases = moduleAliases, let realName = moduleAliases[namePart] {
392+
namePart = realName
393+
}
394+
return .swiftPrebuiltExternal(namePart)
382395
case _ where encodedName.starts(with: "swiftPlaceholder:"):
383396
return .swiftPlaceholder(String(encodedName.suffix(encodedName.count - "swiftPlaceholder:".count)))
384397
case _ where encodedName.starts(with: "clang:"):

Sources/SwiftDriver/SwiftScan/SwiftScan.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ internal final class SwiftScan {
9393
}
9494

9595
func preScanImports(workingDirectory: AbsolutePath,
96+
moduleAliases: [String: String]?,
9697
invocationCommand: [String]) throws -> InterModuleDependencyImports {
9798
// Create and configure the scanner invocation
9899
let invocation = api.swiftscan_scan_invocation_create()
@@ -112,14 +113,15 @@ internal final class SwiftScan {
112113
throw DependencyScanningError.dependencyScanFailed
113114
}
114115

115-
let importSet = try constructImportSet(from: importSetRef)
116+
let importSet = try constructImportSet(from: importSetRef, with: moduleAliases)
116117
// Free the memory allocated for the in-memory representation of the import set
117118
// returned by the scanner, now that we have translated it.
118119
api.swiftscan_import_set_dispose(importSetRef)
119120
return importSet
120121
}
121122

122123
func scanDependencies(workingDirectory: AbsolutePath,
124+
moduleAliases: [String: String]?,
123125
invocationCommand: [String]) throws -> InterModuleDependencyGraph {
124126
// Create and configure the scanner invocation
125127
let invocation = api.swiftscan_scan_invocation_create()
@@ -139,7 +141,7 @@ internal final class SwiftScan {
139141
throw DependencyScanningError.dependencyScanFailed
140142
}
141143

142-
let dependencyGraph = try constructGraph(from: graphRef)
144+
let dependencyGraph = try constructGraph(from: graphRef, moduleAliases: moduleAliases)
143145
// Free the memory allocated for the in-memory representation of the dependency
144146
// graph returned by the scanner, now that we have translated it into an
145147
// `InterModuleDependencyGraph`.
@@ -148,6 +150,7 @@ internal final class SwiftScan {
148150
}
149151

150152
func batchScanDependencies(workingDirectory: AbsolutePath,
153+
moduleAliases: [String: String]?,
151154
invocationCommand: [String],
152155
batchInfos: [BatchScanModuleInfo])
153156
throws -> [ModuleDependencyId: [InterModuleDependencyGraph]] {
@@ -201,6 +204,7 @@ internal final class SwiftScan {
201204
// Translate `swiftscan_batch_scan_result_t`
202205
// into `[ModuleDependencyId: [InterModuleDependencyGraph]]`
203206
let resultGraphMap = try constructBatchResultGraphs(for: batchInfos,
207+
moduleAliases: moduleAliases,
204208
from: batchResultRef.pointee)
205209
// Free the memory allocated for the in-memory representation of the batch scan
206210
// result, now that we have translated it.

0 commit comments

Comments
 (0)