Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
51 changes: 41 additions & 10 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 @@ -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"

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.

Please change this to:

Suggested change
return platform == "linux"
return !["macosx", "iphoneos", "iphonesimulator", "appletvos", "appletvsimulator", "watchos", "watchsimulator", "xros", "xrsimulator", "driverkit"].contains(platform)

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.

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, even more verbose as an existing test was failing.

}

/// 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

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 require exactly one target arch and allow a no-variant fallback.

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
// that platform require exactly one target arch and allow a no-variant fallback.
// those platforms, require exactly one target arch and allow a no-variant fallback.

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.

guard architectures.count == 1 else { return nil }
let targetArch = architectures[0]

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 combine the .count==1 check with this:

Suggested change
let targetArch = architectures[0]
guard let targetArch = architectures.only else { return nil }

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.

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
}
}

Expand All @@ -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 {

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 +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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ class CopyFilesTaskProducer: FilesBasedBuildPhaseTaskProducerBase, FilesBasedBui
} catch {
context.error(error.localizedDescription)
}
if let xcFramework = xcFramework, let library = xcFramework.findLibrary(sdk: context.sdk, sdkVariant: context.sdkVariant) {
if let xcFramework = xcFramework, let library = xcFramework.findLibrary(sdk: context.sdk, sdkVariant: context.sdkVariant, architectures: scope.evaluate(BuiltinMacros.ARCHS)) {
// At this point we know this is an XCFramework which we're copying, and we've successfully resolved information about the relevant library in it that we're using.
// Now, if this library contains mergeable metadata then we *either* want to skip copying its binary (if we're merging it), *or* we want to strip mergeable metadata from it (if we're not merging it).
var shouldSkipCopyingBinary = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ package class FilesBasedBuildPhaseTaskProducerBase: PhasedTaskProducer {
if let xcframework = try? context.globalProductPlan.planRequest.buildRequestContext.getCachedXCFramework(at: path) {
// Find a library in the XCFramework which is compatible with the current platform.
// Note that we don't validate supported architectures here because this code path only executes for non-frameworks build phases (like copy files), and it might actually be desirable in this case for an embedded framework to have a subset of the architectures of the current target.
if let library = xcframework.findLibrary(sdk: context.sdk, sdkVariant: context.sdkVariant) {
if let library = xcframework.findLibrary(sdk: context.sdk, sdkVariant: context.sdkVariant, architectures: scope.evaluate(BuiltinMacros.ARCHS)) {
// FIXME: This should really be pulling the framework out of the debug location. However, due to the complexities of dealing with multiple actions that can produce these across various targets in the workspace, and attempting to order those correctly, this pulls the data from the xcframework itself. This is will only be a problem when/if we move to having incomplete xcframeworks that we can build up.
// rdar://59753495 - Copy step for XCFrameworks should pull from the target directory, not the actual framework
let libraryTargetPath = path.join(library.libraryIdentifier).join(library.libraryPath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ package final class SourcesTaskProducer: FilesBasedBuildPhaseTaskProducerBase, F
continue
}

guard let library = xcframework.findLibrary(sdk: context.sdk, sdkVariant: context.sdkVariant) else {
guard let library = xcframework.findLibrary(sdk: context.sdk, sdkVariant: context.sdkVariant, architectures: scope.evaluate(BuiltinMacros.ARCHS)) else {
// Let the XCFrameworkTaskProducer log an error here
continue
}
Expand Down Expand Up @@ -1586,7 +1586,7 @@ package final class SourcesTaskProducer: FilesBasedBuildPhaseTaskProducerBase, F
} catch {
context.error(error.localizedDescription)
}
if let xcFramework = xcFramework, let library = xcFramework.findLibrary(sdk: context.sdk, sdkVariant: context.sdkVariant) {
if let xcFramework = xcFramework, let library = xcFramework.findLibrary(sdk: context.sdk, sdkVariant: context.sdkVariant, architectures: scope.evaluate(BuiltinMacros.ARCHS)) {
var shouldCopyBinary = false
if library.mergeableMetadata {
// We know we're copying a library which was built mergeable. Now what we want to know if whether we're merging it, or one of our dependencies is.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ final class SwiftPackageCopyFilesTaskProducer: CopyFilesTaskProducer {

func shouldEmbedXCFramework(path xcframeworkPath: Path) throws -> Bool {
let xcFramework = try XCFramework(path: xcframeworkPath, fs: context.fs)
guard let library = xcFramework.findLibrary(sdk: context.sdk, sdkVariant: context.sdkVariant) else { return false }
guard let library = xcFramework.findLibrary(sdk: context.sdk, sdkVariant: context.sdkVariant, architectures: scope.evaluate(BuiltinMacros.ARCHS)) else { return false }

switch library.libraryType {
case .dynamicLibrary:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ final class XCFrameworkTaskProducer: StandardTaskProducer, TaskProducer {
let xcframeworkPath = buildFile.absolutePath
let expectedSignature = (context.workspaceContext.workspace.lookupReference(for: buildFile.reference.guid) as? FileReference)?.expectedSignature
try context.globalProductPlan.xcframeworkContext.add(xcframeworkPath, for: configuredTarget, expectedSignature: expectedSignature) { xcframework in
guard let library = xcframework.findLibrary(sdk: context.sdk, sdkVariant: context.sdkVariant) else {
guard let library = xcframework.findLibrary(sdk: context.sdk, sdkVariant: context.sdkVariant, architectures: context.settings.globalScope.evaluate(BuiltinMacros.ARCHS)) else {
let platformDisplayName = context.sdk?.targetBuildVersionPlatform(sdkVariant: context.sdkVariant)?.displayName(infoLookup: context) ?? context.platform?.displayName ?? "No Platform"
context.error("While building for \(platformDisplayName), no library for this platform was found in '\(xcframeworkPath.str)'.", location: .path(xcframeworkPath))
return nil
Expand Down
34 changes: 34 additions & 0 deletions Tests/SWBCoreTests/XCFrameworkTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,40 @@ import SWBMacro
#expect(xcframework.findLibrary(platform: "macos", platformVariant: "")?.libraryIdentifier == "lib1")
#expect(xcframework.findLibrary(platform: "driverkit", platformVariant: "")?.libraryIdentifier == "lib5")
}

@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", architectures: ["x86_64"])?.libraryIdentifier == "linux-x86_64")
#expect(xcframework.findLibrary(platform: "linux", architectures: ["x86_64"])?.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)
}
}

@Suite fileprivate struct XCFrameworkInfoPlistv1ParsingTests {
Expand Down