-
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 2 commits
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) | ||||||
| } | ||||||
|
|
@@ -363,21 +363,42 @@ public struct XCFramework: Hashable, Sendable { | |||||
| /// The minimum XCFramework version required to support mergeable metadata. | ||||||
| @_spi(Testing) public static let mergeableMetadataVersion = Version(1, 1) | ||||||
|
|
||||||
| static func hasPerArchSlices(_ platform: String) -> Bool { | ||||||
| return platform == "linux" | ||||||
| } | ||||||
|
|
||||||
| /// Searches the `libraries` based on the current SDK being used. | ||||||
| public func findLibrary(sdk: SDK?, sdkVariant: SDKVariant?) -> XCFramework.Library? { | ||||||
| public func findLibrary(sdk: SDK?, sdkVariant: SDKVariant?, architectures: [String] = []) -> XCFramework.Library? { | ||||||
| // Lookup the LC_BUILD_VERSION information as that it is how xcframeworks platform and variant values are defined. | ||||||
| guard let platformName = sdkVariant?.llvmTargetTripleSys else { | ||||||
| return nil | ||||||
| } | ||||||
| return findLibrary(platform: platformName, platformVariant: sdkVariant?.llvmTargetTripleEnvironment ?? "") | ||||||
| return findLibrary(platform: platformName, platformVariant: sdkVariant?.llvmTargetTripleEnvironment ?? "", architectures: architectures) | ||||||
| } | ||||||
|
|
||||||
| /// 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? { | ||||||
| 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 | ||||||
| public func findLibrary(platform: String, platformVariant: String = "", architectures: [String] = []) -> XCFramework.Library? { | ||||||
| guard Self.hasPerArchSlices(platform) else { | ||||||
| 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 | ||||||
| } | ||||||
| // 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 require exactly one target arch and allow a no-variant fallback. | ||||||
|
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. |
||||||
| guard architectures.count == 1 else { return nil } | ||||||
| let targetArch = architectures[0] | ||||||
|
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 combine the .count==1 check with this:
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. Changed. |
||||||
| let variants: [String] = platformVariant.isEmpty ? [""] : [platformVariant, ""] | ||||||
| for variant in variants { | ||||||
| if let match = libraries.first(where: { | ||||||
| $0.supportedPlatform == platform | ||||||
| && ($0.platformVariant ?? "") == variant | ||||||
| && $0.supportedArchitectures.contains(targetArch) | ||||||
| }) { | ||||||
| return match | ||||||
| } | ||||||
| } | ||||||
| return nil | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -386,15 +407,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 { XCFramework.hasPerArchSlices(platform) } | ||||||
|
|
||||||
| 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 +498,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.
Please change this to:
It's a bit verbose, but this is more correct and protects against edge cases if we end up splitting linux into multiple platforms, which @owenv and I have talked about doing to solve some cross compilation issues.
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.
Adjusted, even more verbose as an existing test was failing.