Skip to content

Commit 3cd486a

Browse files
authored
Update XCFramework logic (#2828)
* Update XCFramework logic This hardcoded platform and architectures before, make it mor generic to handle macOS on arm64. (cherry picked from commit f204c90) * Extend binary target tests to multiple arches (#2831) In #2812, I updated the logic to support other architectures than just x86_64, but I did not update the tests. This extends the existing `testBinaryTargets` with the missing coverage. (cherry picked from commit c6ec2b4)
1 parent d1aec6c commit 3cd486a

File tree

3 files changed

+42
-20
lines changed

3 files changed

+42
-20
lines changed

Sources/Build/BuildPlan.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,9 +1711,9 @@ public class BuildPlan {
17111711
return nil
17121712
}
17131713

1714-
// Check that it supports macOS.
1714+
// Check that it supports the target platform and architecture.
17151715
guard let library = info.libraries.first(where: {
1716-
$0.platform == "macos" && $0.architectures.contains(Triple.Arch.x86_64.rawValue)
1716+
return $0.platform == buildParameters.triple.os.asXCFrameworkPlatformString && $0.architectures.contains(buildParameters.triple.arch.rawValue)
17171717
}) else {
17181718
diagnostics.emit(error: """
17191719
artifact '\(target.name)' does not support the target platform and architecture \
@@ -1833,3 +1833,15 @@ private func generateResourceInfoPlist(
18331833
try fileSystem.writeIfChanged(path: path, bytes: stream.bytes)
18341834
return true
18351835
}
1836+
1837+
fileprivate extension Triple.OS {
1838+
/// Returns a representation of the receiver that can be compared with platform strings declared in an XCFramework.
1839+
var asXCFrameworkPlatformString: String? {
1840+
switch self {
1841+
case .darwin, .linux, .wasi, .windows:
1842+
return nil // XCFrameworks do not support any of these platforms today.
1843+
case .macOS:
1844+
return "macos"
1845+
}
1846+
}
1847+
}

Sources/Workspace/InitPackage.swift

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ public final class InitPackage {
153153

154154
var param = ".\(platform.manifestName)("
155155
if supportedPlatform.isManifestAPIAvailable {
156-
if platform == .macOS {
157-
param += ".v10_\(version.minor)"
156+
if version.minor > 0 {
157+
param += ".v\(version.major)_\(version.minor)"
158158
} else {
159159
param += ".v\(version.major)"
160160
}
@@ -516,11 +516,11 @@ extension PackageModel.Platform {
516516

517517
extension SupportedPlatform {
518518
var isManifestAPIAvailable: Bool {
519-
if platform == .macOS {
520-
guard self.version.major == 10, self.version.patch == 0 else {
519+
if platform == .macOS && self.version.major == 10 {
520+
guard self.version.patch == 0 else {
521521
return false
522522
}
523-
} else if [Platform.iOS, .watchOS, .tvOS].contains(platform) {
523+
} else if [Platform.macOS, .iOS, .watchOS, .tvOS].contains(platform) {
524524
guard self.version.minor == 0, self.version.patch == 0 else {
525525
return false
526526
}
@@ -529,14 +529,16 @@ extension SupportedPlatform {
529529
}
530530

531531
switch platform {
532-
case .macOS:
532+
case .macOS where version.major == 10:
533533
return (10...15).contains(version.minor)
534+
case .macOS:
535+
return (11...11).contains(version.major)
534536
case .iOS:
535-
return (8...13).contains(version.major)
537+
return (8...14).contains(version.major)
536538
case .tvOS:
537-
return (9...13).contains(version.major)
539+
return (9...14).contains(version.major)
538540
case .watchOS:
539-
return (2...6).contains(version.major)
541+
return (2...7).contains(version.major)
540542

541543
default:
542544
return false

Tests/BuildTests/BuildPlanTests.swift

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2207,7 +2207,7 @@ final class BuildPlanTests: XCTestCase {
22072207
])
22082208
}
22092209

2210-
func testBinaryTargets() throws {
2210+
func testBinaryTargets(platform: String, arch: String, destinationTriple: Triple) throws {
22112211
let fs = InMemoryFileSystem(emptyFiles:
22122212
"/Pkg/Sources/exe/main.swift",
22132213
"/Pkg/Sources/Library/Library.swift",
@@ -2227,15 +2227,15 @@ final class BuildPlanTests: XCTestCase {
22272227
<array>
22282228
<dict>
22292229
<key>LibraryIdentifier</key>
2230-
<string>macos-x86_64</string>
2230+
<string>\(platform)-\(arch)</string>
22312231
<key>LibraryPath</key>
22322232
<string>Framework.framework</string>
22332233
<key>SupportedArchitectures</key>
22342234
<array>
2235-
<string>x86_64</string>
2235+
<string>\(arch)</string>
22362236
</array>
22372237
<key>SupportedPlatform</key>
2238-
<string>macos</string>
2238+
<string>\(platform)</string>
22392239
</dict>
22402240
</array>
22412241
<key>CFBundlePackageType</key>
@@ -2258,17 +2258,17 @@ final class BuildPlanTests: XCTestCase {
22582258
<array>
22592259
<dict>
22602260
<key>LibraryIdentifier</key>
2261-
<string>macos-x86_64</string>
2261+
<string>\(platform)-\(arch)</string>
22622262
<key>HeadersPath</key>
22632263
<string>Headers</string>
22642264
<key>LibraryPath</key>
22652265
<string>libStaticLibrary.a</string>
22662266
<key>SupportedArchitectures</key>
22672267
<array>
2268-
<string>x86_64</string>
2268+
<string>\(arch)</string>
22692269
</array>
22702270
<key>SupportedPlatform</key>
2271-
<string>macos</string>
2271+
<string>\(platform)</string>
22722272
</dict>
22732273
</array>
22742274
<key>CFBundlePackageType</key>
@@ -2307,7 +2307,7 @@ final class BuildPlanTests: XCTestCase {
23072307
XCTAssertNoDiagnostics(diagnostics)
23082308

23092309
let result = BuildPlanResult(plan: try BuildPlan(
2310-
buildParameters: mockBuildParameters(destinationTriple: .macOS),
2310+
buildParameters: mockBuildParameters(destinationTriple: destinationTriple),
23112311
graph: graph,
23122312
diagnostics: diagnostics,
23132313
fileSystem: fs
@@ -2335,14 +2335,22 @@ final class BuildPlanTests: XCTestCase {
23352335

23362336
let clibraryBasicArguments = try result.target(for: "CLibrary").clangTarget().basicArguments()
23372337
XCTAssertMatch(clibraryBasicArguments, [.anySequence, "-F", "/path/to/build/debug", .anySequence])
2338-
XCTAssertMatch(clibraryBasicArguments, [.anySequence, "-I", "/Pkg/StaticLibrary.xcframework/macos-x86_64/Headers", .anySequence])
2338+
XCTAssertMatch(clibraryBasicArguments, [.anySequence, "-I", "/Pkg/StaticLibrary.xcframework/\(platform)-\(arch)/Headers", .anySequence])
23392339

23402340
let clibraryLinkArguments = try result.buildProduct(for: "CLibrary").linkArguments()
23412341
XCTAssertMatch(clibraryLinkArguments, [.anySequence, "-F", "/path/to/build/debug", .anySequence])
23422342
XCTAssertMatch(clibraryLinkArguments, [.anySequence, "-L", "/path/to/build/debug", .anySequence])
23432343
XCTAssertMatch(clibraryLinkArguments, ["-lStaticLibrary"])
23442344
}
23452345

2346+
func testBinaryTargets() throws {
2347+
try testBinaryTargets(platform: "macos", arch: "x86_64", destinationTriple: .macOS)
2348+
let arm64Triple = try Triple("arm64-apple-macosx")
2349+
try testBinaryTargets(platform: "macos", arch: "arm64", destinationTriple: arm64Triple)
2350+
let arm64eTriple = try Triple("arm64e-apple-macosx")
2351+
try testBinaryTargets(platform: "macos", arch: "arm64e", destinationTriple: arm64eTriple)
2352+
}
2353+
23462354
func testAddressSanitizer() throws {
23472355
try sanitizerTest(.address, expectedName: "address")
23482356
}

0 commit comments

Comments
 (0)