Skip to content

Commit 64bd510

Browse files
committed
Remove ifdef
1 parent 1802a7b commit 64bd510

7 files changed

Lines changed: 29 additions & 30 deletions

File tree

Sources/SWBCore/XCFramework.swift

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -363,41 +363,42 @@ public struct XCFramework: Hashable, Sendable {
363363
/// The minimum XCFramework version required to support mergeable metadata.
364364
@_spi(Testing) public static let mergeableMetadataVersion = Version(1, 1)
365365

366+
static func hasPerArchSlices(_ platform: String) -> Bool {
367+
return platform == "linux"
368+
}
369+
366370
/// Searches the `libraries` based on the current SDK being used.
367-
public func findLibrary(sdk: SDK?, sdkVariant: SDKVariant?) -> XCFramework.Library? {
371+
public func findLibrary(sdk: SDK?, sdkVariant: SDKVariant?, architectures: [String] = []) -> XCFramework.Library? {
368372
// Lookup the LC_BUILD_VERSION information as that it is how xcframeworks platform and variant values are defined.
369373
guard let platformName = sdkVariant?.llvmTargetTripleSys else {
370374
return nil
371375
}
372-
return findLibrary(platform: platformName, platformVariant: sdkVariant?.llvmTargetTripleEnvironment ?? "")
376+
return findLibrary(platform: platformName, platformVariant: sdkVariant?.llvmTargetTripleEnvironment ?? "", architectures: architectures)
373377
}
374378

375379
/// Given a platform and the variant, attempt to find an library within the XCFramework that can be used.
376380
public func findLibrary(platform: String, platformVariant: String = "", architectures: [String] = []) -> XCFramework.Library? {
377-
#if canImport(Darwin)
378-
return self.libraries.filter { lib in
379-
// Due to the fact that macro evaluation of empty settings returns empty strings, there is no meaningful distinction between nil and empty here.
380-
lib.supportedPlatform == platform && (lib.platformVariant ?? "") == platformVariant
381-
}.first
382-
#else
381+
guard Self.hasPerArchSlices(platform) else {
382+
return self.libraries.filter { lib in
383+
// Due to the fact that macro evaluation of empty settings returns empty strings, there is no meaningful distinction between nil and empty here.
384+
lib.supportedPlatform == platform && (lib.platformVariant ?? "") == platformVariant
385+
}.first
386+
}
383387
// Linux ships per-arch XCFramework slices and typically omits SupportedPlatformVariant, so for
384-
// that platform default archs to the host and allow a no-variant fallback.
385-
let platformMatches = libraries.filter { $0.supportedPlatform == platform }
386-
var effectiveArchs = architectures
387-
if platform == "linux", effectiveArchs.isEmpty, let hostArch = Architecture.hostStringValue {
388-
effectiveArchs = [hostArch]
389-
}
390-
let allowNoVariantFallback = platform == "linux" && !platformVariant.isEmpty
391-
let variants = allowNoVariantFallback ? [platformVariant, ""] : [platformVariant]
388+
// that platform require exactly one target arch and allow a no-variant fallback.
389+
guard architectures.count == 1 else { return nil }
390+
let targetArch = architectures[0]
391+
let variants: [String] = platformVariant.isEmpty ? [""] : [platformVariant, ""]
392392
for variant in variants {
393-
if let match = platformMatches.first(where: {
394-
($0.platformVariant ?? "") == variant && effectiveArchs.allSatisfy($0.supportedArchitectures.contains)
393+
if let match = libraries.first(where: {
394+
$0.supportedPlatform == platform
395+
&& ($0.platformVariant ?? "") == variant
396+
&& $0.supportedArchitectures.contains(targetArch)
395397
}) {
396398
return match
397399
}
398400
}
399401
return nil
400-
#endif
401402
}
402403
}
403404

@@ -409,7 +410,7 @@ extension XCFramework {
409410
let architectures: [String]
410411

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

414415
func hash(into hasher: inout Hasher) {
415416
// identifier is only used for tracking the offending library

Sources/SWBTaskConstruction/TaskProducers/BuildPhaseTaskProducers/CopyFilesTaskProducer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ class CopyFilesTaskProducer: FilesBasedBuildPhaseTaskProducerBase, FilesBasedBui
340340
} catch {
341341
context.error(error.localizedDescription)
342342
}
343-
if let xcFramework = xcFramework, let library = xcFramework.findLibrary(sdk: context.sdk, sdkVariant: context.sdkVariant) {
343+
if let xcFramework = xcFramework, let library = xcFramework.findLibrary(sdk: context.sdk, sdkVariant: context.sdkVariant, architectures: scope.evaluate(BuiltinMacros.ARCHS)) {
344344
// 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.
345345
// 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).
346346
var shouldSkipCopyingBinary = false

Sources/SWBTaskConstruction/TaskProducers/BuildPhaseTaskProducers/FilesBasedBuildPhaseTaskProducer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ package class FilesBasedBuildPhaseTaskProducerBase: PhasedTaskProducer {
584584
if let xcframework = try? context.globalProductPlan.planRequest.buildRequestContext.getCachedXCFramework(at: path) {
585585
// Find a library in the XCFramework which is compatible with the current platform.
586586
// 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.
587-
if let library = xcframework.findLibrary(sdk: context.sdk, sdkVariant: context.sdkVariant) {
587+
if let library = xcframework.findLibrary(sdk: context.sdk, sdkVariant: context.sdkVariant, architectures: scope.evaluate(BuiltinMacros.ARCHS)) {
588588
// 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.
589589
// rdar://59753495 - Copy step for XCFrameworks should pull from the target directory, not the actual framework
590590
let libraryTargetPath = path.join(library.libraryIdentifier).join(library.libraryPath)

Sources/SWBTaskConstruction/TaskProducers/BuildPhaseTaskProducers/SourcesTaskProducer.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ package final class SourcesTaskProducer: FilesBasedBuildPhaseTaskProducerBase, F
630630
continue
631631
}
632632

633-
guard let library = xcframework.findLibrary(sdk: context.sdk, sdkVariant: context.sdkVariant) else {
633+
guard let library = xcframework.findLibrary(sdk: context.sdk, sdkVariant: context.sdkVariant, architectures: scope.evaluate(BuiltinMacros.ARCHS)) else {
634634
// Let the XCFrameworkTaskProducer log an error here
635635
continue
636636
}
@@ -1586,7 +1586,7 @@ package final class SourcesTaskProducer: FilesBasedBuildPhaseTaskProducerBase, F
15861586
} catch {
15871587
context.error(error.localizedDescription)
15881588
}
1589-
if let xcFramework = xcFramework, let library = xcFramework.findLibrary(sdk: context.sdk, sdkVariant: context.sdkVariant) {
1589+
if let xcFramework = xcFramework, let library = xcFramework.findLibrary(sdk: context.sdk, sdkVariant: context.sdkVariant, architectures: scope.evaluate(BuiltinMacros.ARCHS)) {
15901590
var shouldCopyBinary = false
15911591
if library.mergeableMetadata {
15921592
// 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.

Sources/SWBTaskConstruction/TaskProducers/BuildPhaseTaskProducers/SwiftPackageCopyFilesTaskProducer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ final class SwiftPackageCopyFilesTaskProducer: CopyFilesTaskProducer {
8686

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

9191
switch library.libraryType {
9292
case .dynamicLibrary:

Sources/SWBTaskConstruction/TaskProducers/WorkspaceTaskProducers/XCFrameworkTaskProducer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ final class XCFrameworkTaskProducer: StandardTaskProducer, TaskProducer {
9494
let xcframeworkPath = buildFile.absolutePath
9595
let expectedSignature = (context.workspaceContext.workspace.lookupReference(for: buildFile.reference.guid) as? FileReference)?.expectedSignature
9696
try context.globalProductPlan.xcframeworkContext.add(xcframeworkPath, for: configuredTarget, expectedSignature: expectedSignature) { xcframework in
97-
guard let library = xcframework.findLibrary(sdk: context.sdk, sdkVariant: context.sdkVariant) else {
97+
guard let library = xcframework.findLibrary(sdk: context.sdk, sdkVariant: context.sdkVariant, architectures: context.settings.globalScope.evaluate(BuiltinMacros.ARCHS)) else {
9898
let platformDisplayName = context.sdk?.targetBuildVersionPlatform(sdkVariant: context.sdkVariant)?.displayName(infoLookup: context) ?? context.platform?.displayName ?? "No Platform"
9999
context.error("While building for \(platformDisplayName), no library for this platform was found in '\(xcframeworkPath.str)'.", location: .path(xcframeworkPath))
100100
return nil

Tests/SWBCoreTests/XCFrameworkTests.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,16 +260,15 @@ import SWBMacro
260260
#expect(xcframework.findLibrary(platform: "driverkit", platformVariant: "")?.libraryIdentifier == "lib5")
261261
}
262262

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

271-
#expect(xcframework.findLibrary(platform: "linux", platformVariant: "gnu")?.libraryIdentifier == "linux-x86_64")
272-
#expect(xcframework.findLibrary(platform: "linux")?.libraryIdentifier == "linux-x86_64")
270+
#expect(xcframework.findLibrary(platform: "linux", platformVariant: "gnu", architectures: ["x86_64"])?.libraryIdentifier == "linux-x86_64")
271+
#expect(xcframework.findLibrary(platform: "linux", architectures: ["x86_64"])?.libraryIdentifier == "linux-x86_64")
273272
}
274273

275274
@Test
@@ -294,7 +293,6 @@ import SWBMacro
294293
#expect(xcframework.findLibrary(platform: "linux", platformVariant: "gnu", architectures: ["aarch64"]) == nil)
295294
#expect(xcframework.findLibrary(platform: "linux", architectures: ["aarch64"]) == nil)
296295
}
297-
#endif
298296
}
299297

300298
@Suite fileprivate struct XCFrameworkInfoPlistv1ParsingTests {

0 commit comments

Comments
 (0)