-
Notifications
You must be signed in to change notification settings - Fork 187
Support swift build --experimental-xcframeworks-on-linux #1310
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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) | ||||||
| } | ||||||
|
|
@@ -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) | ||||||
| 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 | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can't use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -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 { | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed. |
||||||
| } | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -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) | ||||||
| } | ||||||
|
|
||||||
There was a problem hiding this comment.
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
usesPerArchSlicesproperty you added to LibraryKey and re-use that here.There was a problem hiding this comment.
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.