Skip to content

Commit 803cdb1

Browse files
committed
Log information about discovered Swift modules
1 parent 38078ac commit 803cdb1

File tree

3 files changed

+53
-9
lines changed

3 files changed

+53
-9
lines changed

Sources/SwiftDriver/Jobs/PrebuiltModulesJob.swift

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212
import TSCBasic
1313
import SwiftOptions
14+
import Foundation
1415

1516
@_spi(Testing) public func isIosMacInterface(_ path: VirtualPath) throws -> Bool {
1617
let data = try localFileSystem.readFileContents(path).cString
@@ -241,6 +242,41 @@ public struct PrebuiltModuleInput {
241242
}
242243
}
243244

245+
public class SwiftAdopter: Codable {
246+
let name: String
247+
let moduleDir: String
248+
let hasInterface: Bool
249+
let hasModule: Bool
250+
let isFramework: Bool
251+
let isPrivateFramework: Bool
252+
init(_ name: String, _ moduleDir: AbsolutePath, _ hasInterface: AbsolutePath?, _ hasModule: AbsolutePath?) {
253+
self.name = name
254+
self.moduleDir = SwiftAdopter.relativeToSDK(moduleDir)
255+
self.hasInterface = hasInterface != nil
256+
self.hasModule = hasModule != nil
257+
self.isFramework = self.moduleDir.contains("\(name).framework")
258+
self.isPrivateFramework = self.moduleDir.contains("PrivateFrameworks")
259+
}
260+
static func relativeToSDK(_ fullPath: AbsolutePath) -> String {
261+
var SDKDir: AbsolutePath = fullPath
262+
while(SDKDir.extension != "sdk") {
263+
SDKDir = SDKDir.parentDirectory
264+
}
265+
assert(SDKDir.extension == "sdk")
266+
SDKDir = SDKDir.parentDirectory
267+
return fullPath.relative(to: SDKDir).pathString
268+
}
269+
270+
static public func emitSummary(_ adopters: [SwiftAdopter], to logDir: AbsolutePath?) throws {
271+
guard let logDir = logDir else { return }
272+
let data = try JSONEncoder().encode(adopters)
273+
if let json = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers),
274+
let jsonData = try? JSONSerialization.data(withJSONObject: json, options: .prettyPrinted) {
275+
try localFileSystem.writeFileContents(logDir.appending(component: "adopters.json"), bytes: ByteString(jsonData))
276+
}
277+
}
278+
}
279+
244280
typealias PrebuiltModuleOutput = PrebuiltModuleInput
245281

246282
public struct SDKPrebuiltModuleInputsCollector {
@@ -298,7 +334,8 @@ public struct SDKPrebuiltModuleInputsCollector {
298334
}
299335
}
300336

301-
public func collectSwiftInterfaceMap() throws -> [String: [PrebuiltModuleInput]] {
337+
public func collectSwiftInterfaceMap() throws -> (inputMap: [String: [PrebuiltModuleInput]], adopters: [SwiftAdopter]) {
338+
var allSwiftAdopters: [SwiftAdopter] = []
302339
var results: [String: [PrebuiltModuleInput]] = [:]
303340

304341
func updateResults(_ dir: AbsolutePath) throws {
@@ -309,7 +346,8 @@ public struct SDKPrebuiltModuleInputsCollector {
309346
if results[moduleName] == nil {
310347
results[moduleName] = []
311348
}
312-
349+
var hasInterface: AbsolutePath?
350+
var hasModule: AbsolutePath?
313351
// Search inside a .swiftmodule directory for any .swiftinterface file, and
314352
// add the files into the dictionary.
315353
// Duplicate entries are discarded, otherwise llbuild will complain.
@@ -322,11 +360,14 @@ public struct SDKPrebuiltModuleInputsCollector {
322360
if !results[moduleName]!.contains(where: { $0.path.file.basenameWithoutExt == currentBaseName }) {
323361
results[moduleName]!.append(PrebuiltModuleInput(interfacePath))
324362
}
363+
hasInterface = currentFile
325364
}
326365
if currentFile.extension == "swiftmodule" {
327366
diagEngine.emit(warning: "found \(currentFile)")
367+
hasModule = currentFile
328368
}
329369
}
370+
allSwiftAdopters.append(SwiftAdopter(moduleName, dir, hasInterface, hasModule))
330371
}
331372
// Search inside framework dirs in an SDK to find .swiftmodule directories.
332373
for dir in frameworkDirs {
@@ -358,7 +399,7 @@ public struct SDKPrebuiltModuleInputsCollector {
358399
}
359400
}
360401
}
361-
return sanitizeInterfaceMap(results)
402+
return (inputMap: sanitizeInterfaceMap(results), adopters: allSwiftAdopters)
362403
}
363404
}
364405

Sources/swift-build-sdk-interfaces/main.swift

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ do {
6767
diagnosticsEngine.emit(error: "cannot find sdk: \(sdkPath.pathString)")
6868
exit(1)
6969
}
70+
let logDir = try getArgumentAsPath("-log-path")
7071
let collector = SDKPrebuiltModuleInputsCollector(sdkPath, diagnosticsEngine)
7172
var outputDir = try VirtualPath(path: rawOutputDir).absolutePath!
7273
// if the given output dir ends with 'prebuilt-modules', we should
@@ -108,7 +109,11 @@ do {
108109
.appending(component: "SystemVersion.plist"),
109110
to: sysVersionFile)
110111
let processSet = ProcessSet()
111-
let inputMap = try collector.collectSwiftInterfaceMap()
112+
let inputTuple = try collector.collectSwiftInterfaceMap()
113+
let allAdopters = inputTuple.adopters
114+
let currentABIDir = try getArgumentAsPath("-current-abi-dir")
115+
try SwiftAdopter.emitSummary(allAdopters, to: currentABIDir)
116+
let inputMap = inputTuple.inputMap
112117
let allModules = coreMode ? ["Foundation"] : Array(inputMap.keys)
113118
try withTemporaryFile(suffix: ".swift") {
114119
let tempPath = $0.path
@@ -131,8 +136,6 @@ do {
131136
args.append(mcpFlag)
132137
args.append(mcp)
133138
}
134-
let logDir = try getArgumentAsPath("-log-path")
135-
let currentABIDir = try getArgumentAsPath("-current-abi-dir")
136139
let baselineABIDir = try getArgumentAsPath("-baseline-abi-dir")
137140
var driver = try Driver(args: args,
138141
diagnosticsEngine: diagnosticsEngine,

Tests/SwiftDriverTests/ExplicitModuleBuildTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,7 +1310,7 @@ final class ExplicitModuleBuildTests: XCTestCase {
13101310
testInputsPath.appending(component: "mock-sdk.sdk").pathString
13111311
let diagnosticEnging = DiagnosticsEngine()
13121312
let collector = try SDKPrebuiltModuleInputsCollector(VirtualPath(path: mockSDKPath).absolutePath!, diagnosticEnging)
1313-
let interfaceMap = try collector.collectSwiftInterfaceMap()
1313+
let interfaceMap = try collector.collectSwiftInterfaceMap().inputMap
13141314

13151315
// Check interface map always contain everything
13161316
XCTAssertTrue(interfaceMap["Swift"]!.count == 3)
@@ -1486,7 +1486,7 @@ final class ExplicitModuleBuildTests: XCTestCase {
14861486
let baselineABIPath: String =
14871487
testInputsPath.appending(component: "ABIBaselines").pathString
14881488
let collector = try SDKPrebuiltModuleInputsCollector(VirtualPath(path: mockSDKPath).absolutePath!, DiagnosticsEngine())
1489-
let interfaceMap = try collector.collectSwiftInterfaceMap()
1489+
let interfaceMap = try collector.collectSwiftInterfaceMap().inputMap
14901490
try withTemporaryDirectory { path in
14911491
let main = path.appending(component: "testPrebuiltModuleGenerationJobs.swift")
14921492
try localFileSystem.writeFileContents(main) {
@@ -1514,7 +1514,7 @@ final class ExplicitModuleBuildTests: XCTestCase {
15141514
let mockSDKPath = testInputsPath.appending(component: "mock-sdk.Internal.sdk")
15151515
let mockSDKPathStr: String = mockSDKPath.pathString
15161516
let collector = try SDKPrebuiltModuleInputsCollector(VirtualPath(path: mockSDKPathStr).absolutePath!, DiagnosticsEngine())
1517-
let interfaceMap = try collector.collectSwiftInterfaceMap()
1517+
let interfaceMap = try collector.collectSwiftInterfaceMap().inputMap
15181518
try withTemporaryDirectory { path in
15191519
let main = path.appending(component: "testPrebuiltModuleGenerationJobs.swift")
15201520
try localFileSystem.writeFileContents(main) {

0 commit comments

Comments
 (0)