Skip to content
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
50 changes: 40 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)

/// Whether the platform ships one slice per architecture (no fat binaries).
static func hasPerArchSlices(_ platform: String) -> Bool {
return BuildVersion.Platform(platform: platform, environment: nil) == nil
}

/// 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
}
// Non-Apple platforms ship per-arch XCFramework slices and typically omit SupportedPlatformVariant, so for
// those platforms, require exactly one target arch and allow a no-variant fallback.
guard let targetArch = architectures.only else { return nil }
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,19 @@ extension XCFramework {
let identifier: String
let platform: String
let platformVariant: String?
let architectures: [String]

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

static func == (lhs: LibraryKey, rhs: LibraryKey) -> Bool {
return lhs.platform == rhs.platform && lhs.platformVariant == rhs.platformVariant
return lhs.platform == rhs.platform
&& lhs.platformVariant == rhs.platformVariant
&& lhs.architectures == rhs.architectures
}
}

Expand Down Expand Up @@ -467,7 +492,12 @@ 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))
// Per-arch platforms discriminate slices on arch; fat-binary platforms don't.
var keyArchitectures: [String] = []
if Self.hasPerArchSlices(library.supportedPlatform) {
keyArchitectures = library.supportedArchitectures.sorted()
}
let (added, oldMember) = libraryKeys.insert(LibraryKey(identifier: library.libraryIdentifier, platform: library.supportedPlatform, platformVariant: library.platformVariant, architectures: keyArchitectures))
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
Loading