Skip to content

AdopterAnalysis: record whether a framework has compatibility header and whether it's a mixed-source project #1042

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions Sources/SwiftDriver/Jobs/PrebuiltModulesJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -250,15 +250,32 @@ public class SwiftAdopter: Codable {
public let hasModule: Bool
public let isFramework: Bool
public let isPrivate: Bool
init(_ name: String, _ moduleDir: AbsolutePath, _ hasInterface: [AbsolutePath], _ hasModule: [AbsolutePath]) {
public let hasCompatibilityHeader: Bool
public let isMixed: Bool
init(_ name: String, _ moduleDir: AbsolutePath, _ hasInterface: [AbsolutePath], _ hasModule: [AbsolutePath]) throws {
self.name = name
self.moduleDir = SwiftAdopter.relativeToSDK(moduleDir)
self.hasInterface = !hasInterface.isEmpty
self.hasPrivateInterface = hasInterface.contains { $0.basename.hasSuffix(".private.swiftinterface") }
self.hasModule = !hasModule.isEmpty
self.isFramework = self.moduleDir.contains("\(name).framework")
self.isPrivate = self.moduleDir.contains("PrivateFrameworks")
let headers = try SwiftAdopter.collectHeaderNames(moduleDir.parentDirectory.parentDirectory)
self.hasCompatibilityHeader = headers.contains { $0 == "\(name)-Swift.h" }
self.isMixed = headers.contains { $0 != "\(name)-Swift.h" }
}

static func collectHeaderNames(_ headersIn: AbsolutePath) throws -> [String] {
var results: [String] = []
let collector = { (dir: AbsolutePath) in
guard localFileSystem.exists(dir) else { return }
try localFileSystem.getDirectoryContents(dir).forEach { results.append($0) }
}
try collector(headersIn.appending(component: "Headers"))
try collector(headersIn.appending(component: "PrivateHeaders"))
return results
}

static func relativeToSDK(_ fullPath: AbsolutePath) -> String {
var SDKDir: AbsolutePath = fullPath
while(SDKDir.extension != "sdk") {
Expand Down Expand Up @@ -372,7 +389,7 @@ public struct SDKPrebuiltModuleInputsCollector {
hasModule.append(currentFile)
}
}
allSwiftAdopters.append(SwiftAdopter(moduleName, dir, hasInterface, hasModule))
allSwiftAdopters.append(try! SwiftAdopter(moduleName, dir, hasInterface, hasModule))
}
// Search inside framework dirs in an SDK to find .swiftmodule directories.
for dir in frameworkDirs {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
framework module B [extern_c] {
umbrella header "B.h"
export *
module * { export * }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// Nothing interesting
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// nothing interesting
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// swift-interface-format-version: 1.0
// swift-module-flags: -module-name C
import Swift
public func FuncE() { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// swift-interface-format-version: 1.0
// swift-module-flags: -module-name C
import Swift
public func FuncE() { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// swift-interface-format-version: 1.0
// swift-module-flags: -module-name C
import Swift
public func FuncE() { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// swift-interface-format-version: 1.0
// swift-module-flags: -module-name C
import Swift
public func FuncE() { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
framework module C [extern_c] {
umbrella header "C.h"
export *
module * { export * }
}
17 changes: 17 additions & 0 deletions Tests/SwiftDriverTests/ExplicitModuleBuildTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1555,5 +1555,22 @@ final class ExplicitModuleBuildTests: XCTestCase {
XCTAssertFalse(B.hasModule)
XCTAssertTrue(B.hasPrivateInterface)
}

func testCollectSwiftAdoptersWhetherMixed() throws {
let mockSDKPath = testInputsPath.appending(component: "mock-sdk.Internal.sdk")
let mockSDKPathStr: String = mockSDKPath.pathString
let collector = try SDKPrebuiltModuleInputsCollector(VirtualPath(path: mockSDKPathStr).absolutePath!, DiagnosticsEngine())
let adopters = try collector.collectSwiftInterfaceMap().adopters
XCTAssertTrue(!adopters.isEmpty)
let B = adopters.first {$0.name == "B"}!
XCTAssertTrue(B.isFramework)
XCTAssertTrue(B.hasCompatibilityHeader)
XCTAssertFalse(B.isMixed)

let C = adopters.first {$0.name == "C"}!
XCTAssertTrue(C.isFramework)
XCTAssertFalse(C.hasCompatibilityHeader)
XCTAssertTrue(C.isMixed)
}
#endif
}