Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
38 changes: 34 additions & 4 deletions Sources/SWBCore/XCFramework.swift
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public struct XCFramework: Hashable, Sendable {

switch ext {
case "framework": self = .framework; break
case "dylib": self = .dynamicLibrary; break
case "dylib", "so": self = .dynamicLibrary; break
case "a": self = .staticLibrary; break
default: self = .unknown(fileExtension: ext)
}
Expand Down Expand Up @@ -373,11 +373,31 @@ public struct XCFramework: Hashable, Sendable {
}

/// Given a platform and the variant, attempt to find an library within the XCFramework that can be used.
public func findLibrary(platform: String, platformVariant: String = "") -> XCFramework.Library? {
public func findLibrary(platform: String, platformVariant: String = "", architectures: [String] = []) -> XCFramework.Library? {
#if canImport(Darwin)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't use ifdefs here because this will not work for cross-compilation.

I think the actual condition here is whether the platform uses per-arch slices? You can extract the logic in the usesPerArchSlices property you added to LibraryKey and re-use that here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK! ifdef removed, originally I was trying to minimise the changes required and not thread through this argument.

return self.libraries.filter { lib in
// Due to the fact that macro evaluation of empty settings returns empty strings, there is no meaningful distinction between nil and empty here.
lib.supportedPlatform == platform && (lib.platformVariant ?? "") == platformVariant
}.first
#else
// Linux ships per-arch XCFramework slices and typically omits SupportedPlatformVariant, so for

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Linux ships per-arch XCFramework slices and typically omits SupportedPlatformVariant, so for
// Non-Apple platforms ship per-arch XCFramework slices and typically omit SupportedPlatformVariant, so for

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adjusted.

// that platform default archs to the host and allow a no-variant fallback.
let platformMatches = libraries.filter { $0.supportedPlatform == platform }
var effectiveArchs = architectures
if platform == "linux", effectiveArchs.isEmpty, let hostArch = Architecture.hostStringValue {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't use Architecture.hostStringValue here because this will not work for cross-compilation. architectures should never be an empty list - for the Linux case you can check that it contains exactly one value, and look up that value.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adjusted.

effectiveArchs = [hostArch]
}
let allowNoVariantFallback = platform == "linux" && !platformVariant.isEmpty
let variants = allowNoVariantFallback ? [platformVariant, ""] : [platformVariant]
for variant in variants {
if let match = platformMatches.first(where: {
($0.platformVariant ?? "") == variant && effectiveArchs.allSatisfy($0.supportedArchitectures.contains)
}) {
return match
}
}
return nil
#endif
}
}

Expand All @@ -386,15 +406,25 @@ extension XCFramework {
let identifier: String
let platform: String
let platformVariant: String?
let architectures: [String]

// Per-arch slice platforms — sibling slices must not collide on `(platform, variant)` alone.
private var usesPerArchSlices: Bool { platform == "linux" }

func hash(into hasher: inout Hasher) {
// identifier is only used for tracking the offending library
hasher.combine(platform)
hasher.combine(platformVariant)
if usesPerArchSlices {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be unconditionally added to the hash; Hashable and Equatable conformances are not the right place for this sort of business logic.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Business logic moved to validate().

hasher.combine(architectures)
}
}

static func == (lhs: LibraryKey, rhs: LibraryKey) -> Bool {
return lhs.platform == rhs.platform && lhs.platformVariant == rhs.platformVariant
guard lhs.platform == rhs.platform, lhs.platformVariant == rhs.platformVariant else {
return false
}
return lhs.usesPerArchSlices ? (lhs.architectures == rhs.architectures) : true

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be unconditionally compared; Hashable and Equatable conformances are not the right place for this sort of business logic.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed.

}
}

Expand Down Expand Up @@ -467,7 +497,7 @@ extension XCFramework {
throw XCFrameworkValidationError.headerPathNotSupported(libraryType: library.libraryType, libraryIdentifier: library.libraryIdentifier)
}

let (added, oldMember) = libraryKeys.insert(LibraryKey(identifier: library.libraryIdentifier, platform: library.supportedPlatform, platformVariant: library.platformVariant))
let (added, oldMember) = libraryKeys.insert(LibraryKey(identifier: library.libraryIdentifier, platform: library.supportedPlatform, platformVariant: library.platformVariant, architectures: library.supportedArchitectures.sorted()))
guard added == true else {
throw XCFrameworkValidationError.conflictingLibraryDefinitions(libraryIdentifier: oldMember.identifier, otherLibraryIdentifier: library.libraryIdentifier)
}
Expand Down
36 changes: 36 additions & 0 deletions Tests/SWBCoreTests/XCFrameworkTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,42 @@ import SWBMacro
#expect(xcframework.findLibrary(platform: "macos", platformVariant: "")?.libraryIdentifier == "lib1")
#expect(xcframework.findLibrary(platform: "driverkit", platformVariant: "")?.libraryIdentifier == "lib5")
}

#if !canImport(Darwin)
@Test
func findingLinuxLibraryFallsBackFromGNUVariantToNoVariant() throws {
let libraries: OrderedSet<XCFramework.Library> = [
XCFramework.Library(libraryIdentifier: "linux-x86_64", supportedPlatform: "linux", supportedArchitectures: ["x86_64"], platformVariant: nil, libraryPath: Path("libtest.so"), binaryPath: Path("libtest.so"), headersPath: nil),
]
let xcframework = try XCFramework(version: Version(1, 0), libraries: libraries)

#expect(xcframework.findLibrary(platform: "linux", platformVariant: "gnu")?.libraryIdentifier == "linux-x86_64")
#expect(xcframework.findLibrary(platform: "linux")?.libraryIdentifier == "linux-x86_64")
}

@Test
func findingLinuxLibraryPrefersMatchingArchitecture() throws {
let libraries: OrderedSet<XCFramework.Library> = [
XCFramework.Library(libraryIdentifier: "linux-aarch64", supportedPlatform: "linux", supportedArchitectures: ["aarch64"], platformVariant: nil, libraryPath: Path("libtest.so"), binaryPath: Path("libtest.so"), headersPath: nil),
XCFramework.Library(libraryIdentifier: "linux-x86_64", supportedPlatform: "linux", supportedArchitectures: ["x86_64"], platformVariant: nil, libraryPath: Path("libtest.so"), binaryPath: Path("libtest.so"), headersPath: nil),
]
let xcframework = try XCFramework(version: Version(1, 0), libraries: libraries)

#expect(xcframework.findLibrary(platform: "linux", platformVariant: "gnu", architectures: ["aarch64"])?.libraryIdentifier == "linux-aarch64")
#expect(xcframework.findLibrary(platform: "linux", platformVariant: "gnu", architectures: ["x86_64"])?.libraryIdentifier == "linux-x86_64")
}

@Test
func findingLibraryReturnsNilForUnsupportedArchitecture() throws {
let libraries: OrderedSet<XCFramework.Library> = [
XCFramework.Library(libraryIdentifier: "linux-x86_64", supportedPlatform: "linux", supportedArchitectures: ["x86_64"], platformVariant: nil, libraryPath: Path("libtest.so"), binaryPath: Path("libtest.so"), headersPath: nil),
]
let xcframework = try XCFramework(version: Version(1, 0), libraries: libraries)

#expect(xcframework.findLibrary(platform: "linux", platformVariant: "gnu", architectures: ["aarch64"]) == nil)
#expect(xcframework.findLibrary(platform: "linux", architectures: ["aarch64"]) == nil)
}
#endif
}

@Suite fileprivate struct XCFrameworkInfoPlistv1ParsingTests {
Expand Down