Skip to content

Commit ab73215

Browse files
Improve handling of non-macOS Darwin triples (#6732)
Co-authored-by: Aleksei Sapitskii <[email protected]>
1 parent dfd70cf commit ab73215

File tree

8 files changed

+132
-13
lines changed

8 files changed

+132
-13
lines changed

Sources/Basics/Triple+Basics.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ extension Triple {
136136
}
137137

138138
switch os {
139-
case .darwin, .macosx:
139+
case _ where isDarwin():
140140
return ".dylib"
141141
case .linux, .openbsd:
142142
return ".so"
@@ -155,7 +155,7 @@ extension Triple {
155155
}
156156

157157
switch os {
158-
case .darwin, .macosx:
158+
case _ where isDarwin():
159159
return ""
160160
case .linux, .openbsd:
161161
return ""
@@ -178,7 +178,7 @@ extension Triple {
178178
/// The file extension for Foundation-style bundle.
179179
public var nsbundleExtension: String {
180180
switch os {
181-
case .darwin, .macosx:
181+
case _ where isDarwin():
182182
return ".bundle"
183183
default:
184184
// See: https://github.com/apple/swift-corelibs-foundation/blob/master/Docs/FHS%20Bundles.md

Sources/Build/BuildDescription/ProductBuildDescription.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ public final class ProductBuildDescription: SPMBuildCore.ProductBuildDescription
267267

268268
// When deploying to macOS prior to macOS 12, add an rpath to the
269269
// back-deployed concurrency libraries.
270-
if useStdlibRpath, self.buildParameters.targetTriple.isDarwin(),
270+
if useStdlibRpath, self.buildParameters.targetTriple.isMacOSX,
271271
let macOSSupportedPlatform = self.package.platforms.getDerived(for: .macOS),
272272
macOSSupportedPlatform.version.major < 12
273273
{

Sources/Build/BuildPlan.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,11 @@ extension BuildParameters {
128128
var args = ["-target"]
129129
// Compute the triple string for Darwin platform using the platform version.
130130
if targetTriple.isDarwin() {
131-
guard let macOSSupportedPlatform = target.platforms.getDerived(for: .macOS) else {
132-
throw StringError("the target \(target) doesn't support building for macOS")
131+
let platform = buildEnvironment.platform
132+
guard let supportedPlatform = target.platforms.getDerived(for: platform) else {
133+
throw StringError("the target \(target) doesn't support building for the \(platform.name) platform")
133134
}
134-
args += [targetTriple.tripleString(forPlatformVersion: macOSSupportedPlatform.version.versionString)]
135+
args += [targetTriple.tripleString(forPlatformVersion: supportedPlatform.version.versionString)]
135136
} else {
136137
args += [targetTriple.tripleString]
137138
}

Sources/SPMBuildCore/BinaryTarget+Extensions.swift

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ extension BinaryTarget {
4141
// At the moment we return at most a single library.
4242
let metadata = try XCFrameworkMetadata.parse(fileSystem: fileSystem, rootPath: self.artifactPath)
4343
// Filter the libraries that are relevant to the triple.
44-
// FIXME: this filter needs to become more sophisticated
4544
guard let library = metadata.libraries.first(where: {
4645
$0.platform == triple.os?.asXCFrameworkPlatformString &&
47-
$0.architectures.contains(triple.archName)
46+
$0.variant == triple.environment?.asXCFrameworkPlatformVariantString &&
47+
$0.architectures.contains(triple.archName)
4848
}) else {
4949
return []
5050
}
@@ -100,8 +100,27 @@ extension Triple.OS {
100100
return nil // XCFrameworks do not support any of these platforms today.
101101
case .macosx:
102102
return "macos"
103+
case .ios:
104+
return "ios"
105+
case .tvos:
106+
return "tvos"
107+
case .watchos:
108+
return "watchos"
103109
default:
104110
return nil // XCFrameworks do not support any of these platforms today.
105111
}
106112
}
107113
}
114+
115+
extension Triple.Environment {
116+
fileprivate var asXCFrameworkPlatformVariantString: String? {
117+
switch self {
118+
case .simulator:
119+
return "simulator"
120+
case .macabi:
121+
return "maccatalyst"
122+
default:
123+
return nil // XCFrameworks do not support any of these platform variants today.
124+
}
125+
}
126+
}

Sources/SPMBuildCore/BuildParameters.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,18 @@ public struct BuildParameters: Encodable {
231231
/// The current platform we're building for.
232232
var currentPlatform: PackageModel.Platform {
233233
if self.targetTriple.isDarwin() {
234-
return .macOS
234+
switch self.targetTriple.darwinPlatform {
235+
case .iOS(.catalyst):
236+
return .macCatalyst
237+
case .iOS(.device), .iOS(.simulator):
238+
return .iOS
239+
case .tvOS:
240+
return .tvOS
241+
case .watchOS:
242+
return .watchOS
243+
case .macOS, nil:
244+
return .macOS
245+
}
235246
} else if self.targetTriple.isAndroid() {
236247
return .android
237248
} else if self.targetTriple.isWASI() {

Sources/SPMBuildCore/XCFrameworkMetadata.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,22 @@ public struct XCFrameworkMetadata: Equatable {
2525
public let headersPath: String?
2626
public let platform: String
2727
public let architectures: [String]
28+
public let variant: String?
2829

2930
public init(
3031
libraryIdentifier: String,
3132
libraryPath: String,
3233
headersPath: String?,
3334
platform: String,
34-
architectures: [String]
35+
architectures: [String],
36+
variant: String?
3537
) {
3638
self.libraryIdentifier = libraryIdentifier
3739
self.libraryPath = libraryPath
3840
self.headersPath = headersPath
3941
self.platform = platform
4042
self.architectures = architectures
43+
self.variant = variant
4144
}
4245
}
4346

@@ -78,6 +81,7 @@ extension XCFrameworkMetadata.Library: Decodable {
7881
case headersPath = "HeadersPath"
7982
case platform = "SupportedPlatform"
8083
case architectures = "SupportedArchitectures"
84+
case variant = "SupportedPlatformVariant"
8185
}
8286
}
8387

Tests/BuildTests/BuildPlanTests.swift

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3260,6 +3260,65 @@ final class BuildPlanTests: XCTestCase {
32603260
#endif
32613261
}
32623262

3263+
func testPlatformsCustomTriple() throws {
3264+
let fileSystem = InMemoryFileSystem(emptyFiles:
3265+
"/A/Sources/ATarget/foo.swift",
3266+
"/B/Sources/BTarget/foo.swift"
3267+
)
3268+
3269+
let observability = ObservabilitySystem.makeForTesting()
3270+
let graph = try loadPackageGraph(
3271+
fileSystem: fileSystem,
3272+
manifests: [
3273+
Manifest.createRootManifest(
3274+
displayName: "A",
3275+
path: "/A",
3276+
platforms: [
3277+
PlatformDescription(name: "ios", version: "11.0"),
3278+
PlatformDescription(name: "macos", version: "10.13"),
3279+
],
3280+
toolsVersion: .v5,
3281+
dependencies: [
3282+
.localSourceControl(path: "/B", requirement: .upToNextMajor(from: "1.0.0")),
3283+
],
3284+
targets: [
3285+
TargetDescription(name: "ATarget", dependencies: ["BLibrary"]),
3286+
]),
3287+
Manifest.createFileSystemManifest(
3288+
displayName: "B",
3289+
path: "/B",
3290+
platforms: [
3291+
PlatformDescription(name: "ios", version: "10.0"),
3292+
PlatformDescription(name: "macos", version: "10.12"),
3293+
],
3294+
toolsVersion: .v5,
3295+
products: [
3296+
ProductDescription(name: "BLibrary", type: .library(.automatic), targets: ["BTarget"]),
3297+
],
3298+
targets: [
3299+
TargetDescription(name: "BTarget", dependencies: []),
3300+
]),
3301+
],
3302+
observabilityScope: observability.topScope
3303+
)
3304+
XCTAssertNoDiagnostics(observability.diagnostics)
3305+
3306+
let result = try BuildPlanResult(plan: BuildPlan(
3307+
buildParameters: mockBuildParameters(targetTriple: .init("arm64-apple-ios")),
3308+
graph: graph,
3309+
fileSystem: fileSystem,
3310+
observabilityScope: observability.topScope
3311+
))
3312+
3313+
let targetTriple = try Triple("arm64-apple-ios")
3314+
3315+
let aTarget = try result.target(for: "ATarget").swiftTarget().compileArguments()
3316+
XCTAssertMatch(aTarget, [.equal("-target"), .equal(targetTriple.tripleString(forPlatformVersion: "11.0")), .anySequence])
3317+
3318+
let bTarget = try result.target(for: "BTarget").swiftTarget().compileArguments()
3319+
XCTAssertMatch(bTarget, [.equal("-target"), .equal(targetTriple.tripleString(forPlatformVersion: "11.0")), .anySequence])
3320+
}
3321+
32633322
func testPlatformsValidation() throws {
32643323
let fileSystem = InMemoryFileSystem(emptyFiles:
32653324
"/A/Sources/ATarget/foo.swift",

Tests/SPMBuildCoreTests/XCFrameworkMetadataTests.swift

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,21 @@ final class XCFrameworkMetadataTests: XCTestCase {
3737
<key>SupportedPlatform</key>
3838
<string>macos</string>
3939
</dict>
40+
<dict>
41+
<key>LibraryIdentifier</key>
42+
<string>ios-arm64_x86_64-simulator</string>
43+
<key>LibraryPath</key>
44+
<string>MyFramework.framework</string>
45+
<key>SupportedArchitectures</key>
46+
<array>
47+
<string>arm64</string>
48+
<string>x86_64</string>
49+
</array>
50+
<key>SupportedPlatform</key>
51+
<string>ios</string>
52+
<key>SupportedPlatformVariant</key>
53+
<string>simulator</string>
54+
</dict>
4055
</array>
4156
<key>CFBundlePackageType</key>
4257
<string>XFWK</string>
@@ -55,7 +70,16 @@ final class XCFrameworkMetadataTests: XCTestCase {
5570
libraryPath: "MyFramework.framework",
5671
headersPath: nil,
5772
platform: "macos",
58-
architectures: ["x86_64"]
73+
architectures: ["x86_64"],
74+
variant: nil
75+
),
76+
XCFrameworkMetadata.Library(
77+
libraryIdentifier: "ios-arm64_x86_64-simulator",
78+
libraryPath: "MyFramework.framework",
79+
headersPath: nil,
80+
platform: "ios",
81+
architectures: ["arm64", "x86_64"],
82+
variant: "simulator"
5983
),
6084
]))
6185
}
@@ -102,7 +126,8 @@ final class XCFrameworkMetadataTests: XCTestCase {
102126
libraryPath: "MyLibrary.a",
103127
headersPath: "Headers",
104128
platform: "macos",
105-
architectures: ["x86_64"]
129+
architectures: ["x86_64"],
130+
variant: nil
106131
),
107132
]))
108133
}

0 commit comments

Comments
 (0)