-
Notifications
You must be signed in to change notification settings - Fork 182
/
Triple+Platforms.swift
438 lines (404 loc) · 13.6 KB
/
Triple+Platforms.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//===--------------- Triple+Platforms.swift - Swift Platform Triples ------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
/// Represents any of the "Apple" platforms handled by `DarwinToolchain`.
/// This boils down a lot of complicated logic about different variants and
/// environments into a straightforward, tightly-modeled type that can be
/// switched over.
///
/// `DarwinPlatform` does not contain version information, but
/// `Triple.version(for:)` retrieves a version based on the
/// corresponding `DarwinPlatform`.
public enum DarwinPlatform: Hashable {
/// macOS, corresponding to the `macosx`, `macos`, and `darwin` OS names.
case macOS
/// iOS, corresponding to the `ios` and `iphoneos` OS names. This does not
/// match tvOS.
case iOS(Environment)
/// tvOS, corresponding to the `tvos` OS name.
case tvOS(EnvironmentWithoutCatalyst)
/// watchOS, corresponding to the `watchos` OS name.
case watchOS(EnvironmentWithoutCatalyst)
/// The most general form of environment information attached to a
/// `DarwinPlatform`.
///
/// The environment is a variant of the platform like `device` or `simulator`.
/// Not all platforms support all values of environment. This type is a superset of
/// all the environments available on any case.
public enum Environment: Hashable {
case device
case simulator
case catalyst
var withoutCatalyst: EnvironmentWithoutCatalyst? {
switch self {
case .device:
return .device
case .simulator:
return .simulator
case .catalyst:
return nil
}
}
}
public enum EnvironmentWithoutCatalyst: Hashable {
case device
case simulator
}
/// Returns the same platform, but with the environment replaced by
/// `environment`. Returns `nil` if `environment` is not valid
/// for `self`.
func with(_ environment: Environment) -> DarwinPlatform? {
switch self {
case .macOS:
guard environment == .device else { return nil }
return .macOS
case .iOS:
return .iOS(environment)
case .tvOS:
guard let withoutCatalyst = environment.withoutCatalyst else { return nil }
return .tvOS(withoutCatalyst)
case .watchOS:
guard let withoutCatalyst = environment.withoutCatalyst else { return nil }
return .watchOS(withoutCatalyst)
}
}
public var platformDisplayName: String {
switch self {
case .macOS:
return "macOS"
case .iOS(.device):
return "iOS"
case .iOS(.simulator):
return "iOS Simulator"
case .iOS(.catalyst):
return "Mac Catalyst"
case .tvOS(.device):
return "tvOS"
case .tvOS(.simulator):
return "tvOS Simulator"
case .watchOS(.device):
return "watchOS"
case .watchOS(.simulator):
return "watchOS Simulator"
}
}
/// The platform name, i.e. the name clang uses to identify this platform in its
/// resource directory.
public var platformName: String {
switch self {
case .macOS:
return "macosx"
case .iOS(.device):
return "iphoneos"
case .iOS(.simulator):
return "iphonesimulator"
case .iOS(.catalyst):
return "maccatalyst"
case .tvOS(.device):
return "appletvos"
case .tvOS(.simulator):
return "appletvsimulator"
case .watchOS(.device):
return "watchos"
case .watchOS(.simulator):
return "watchsimulator"
}
}
/// The name the linker uses to identify this platform.
public var linkerPlatformName: String {
switch self {
case .macOS:
return "macos"
case .iOS(.device):
return "ios"
case .iOS(.simulator):
return "ios-simulator"
case .iOS(.catalyst):
return "mac-catalyst"
case .tvOS(.device):
return "tvos"
case .tvOS(.simulator):
return "tvos-simulator"
case .watchOS(.device):
return "watchos"
case .watchOS(.simulator):
return "watchos-simulator"
}
}
/// The name used to identify this platform in compiler_rt file names.
public var libraryNameSuffix: String {
switch self {
case .macOS:
return "osx"
case .iOS(.device):
return "ios"
case .iOS(.simulator):
return "iossim"
case .iOS(.catalyst):
return "osx"
case .tvOS(.device):
return "tvos"
case .tvOS(.simulator):
return "tvossim"
case .watchOS(.device):
return "watchos"
case .watchOS(.simulator):
return "watchossim"
}
}
}
extension Triple {
/// If this is a Darwin device platform, should it be inferred to be a device simulator?
public var _isSimulatorEnvironment: Bool {
return environment == .simulator
}
/// Returns the OS version equivalent for the given platform, converting and
/// defaulting various representations.
///
/// - Parameter compatibilityPlatform: Overrides the platform to be fetched.
/// For compatibility reasons, you sometimes have to e.g. pass an argument with a macOS
/// version number even when you're building watchOS code. This parameter specifies the
/// platform you need a version number for; the method will then return an arbitrary but
/// suitable version number for `compatibilityPlatform`.
///
/// - Precondition: `self` must be able to provide a version for `compatibilityPlatform`.
/// Not all combinations are valid; in particular, you cannot fetch a watchOS version
/// from an iOS/tvOS triple or vice versa.
public func version(for compatibilityPlatform: DarwinPlatform? = nil)
-> Triple.Version
{
switch compatibilityPlatform ?? darwinPlatform! {
case .macOS:
return _macOSVersion ?? osVersion
case .iOS, .tvOS:
return _iOSVersion
case .watchOS:
return _watchOSVersion
}
}
/// Returns the `DarwinPlatform` for this triple, or `nil` if it is a non-Darwin
/// platform.
///
/// - SeeAlso: DarwinPlatform
public var darwinPlatform: DarwinPlatform? {
func makeEnvironment() -> DarwinPlatform.EnvironmentWithoutCatalyst {
_isSimulatorEnvironment ? .simulator : .device
}
switch os {
case .darwin, .macosx:
return .macOS
case .ios:
if isMacCatalyst {
return .iOS(.catalyst)
} else if _isSimulatorEnvironment {
return .iOS(.simulator)
} else {
return .iOS(.device)
}
case .watchos:
return .watchOS(makeEnvironment())
case .tvos:
return .tvOS(makeEnvironment())
default:
return nil
}
}
// The Darwin platform version used for linking.
public var darwinLinkerPlatformVersion: Version {
precondition(self.isDarwin)
switch darwinPlatform! {
case .macOS:
// The integrated driver falls back to `osVersion` for invalid macOS
// versions, this decision might be worth revisiting.
let macVersion = _macOSVersion ?? osVersion
// The first deployment of arm64 for macOS is version 11
if macVersion.major < 11 && arch == .aarch64 {
return Version(11, 0, 0)
}
return macVersion
case .iOS(.catalyst):
// Mac Catalyst on arm was introduced with an iOS deployment target of
// 14.0; the linker doesn't want to see a deployment target before that.
if _iOSVersion.major < 14 && arch == .aarch64 {
return Version(14, 0, 0)
}
// Mac Catalyst was introduced with an iOS deployment target of 13.1;
// the linker doesn't want to see a deployment target before that.
if _iOSVersion.major < 13 {
return Version(13, 1, 0)
}
return _iOSVersion
case .iOS(.device), .iOS(.simulator), .tvOS(_):
// The first deployment of arm64 simulators is iOS/tvOS 14.0;
// the linker doesn't want to see a deployment target before that.
if _isSimulatorEnvironment && _iOSVersion.major < 14 && arch == .aarch64 {
return Version(14, 0, 0)
}
return _iOSVersion
case .watchOS(_):
// The first deployment of arm64 simulators is watchOS 7;
// the linker doesn't want to see a deployment target before that.
if _isSimulatorEnvironment && osVersion.major < 7 && arch == .aarch64 {
return Version(7, 0, 0)
}
return osVersion
}
}
/// The platform name, i.e. the name clang uses to identify this target in its
/// resource directory.
///
/// - Parameter conflatingDarwin: If true, all Darwin platforms will be
/// identified as just `darwin` instead of by individual platform names.
/// Defaults to `false`.
public func platformName(conflatingDarwin: Bool = false) -> String? {
switch os {
case nil:
fatalError("unknown OS")
case .darwin, .macosx, .ios, .tvos, .watchos:
guard let darwinPlatform = darwinPlatform else {
fatalError("unsupported darwin platform kind?")
}
return conflatingDarwin ? "darwin" : darwinPlatform.platformName
case .linux:
switch environment {
case .musl:
// The triple for linux-static is <arch>-swift-linux-musl, to distinguish
// it from a "normal" musl set-up (ala Alpine).
if vendor == .swift {
return "linux-static"
}
fallthrough
case .musl, .musleabihf, .musleabi:
return "musl"
case .android:
return "android"
default:
return "linux"
}
case .freeBSD:
return "freebsd"
case .openbsd:
return "openbsd"
case .win32:
switch environment {
case .cygnus:
return "cygwin"
case .gnu:
return "mingw"
case .msvc, .itanium:
return "windows"
default:
fatalError("unsupported Windows environment: \(environment, or: "nil")")
}
case .ps4:
return "ps4"
case .haiku:
return "haiku"
case .wasi:
return "wasi"
case .noneOS:
return nil
// Explicitly spell out the remaining cases to force a compile error when
// Triple updates
case .ananas, .cloudABI, .dragonFly, .fuchsia, .kfreebsd, .lv2, .netbsd,
.solaris, .minix, .rtems, .nacl, .cnk, .aix, .cuda, .nvcl, .amdhsa,
.elfiamcu, .mesa3d, .contiki, .amdpal, .hermitcore, .hurd, .emscripten:
return nil
}
}
}
extension Triple {
/// Represents the availability of a feature that is supported on some platforms
/// and versions, but not all. For Darwin versions, the version numbers provided
/// should be the version where the feature was added or the change was
/// introduced, because all version checks are in the form of
/// `tripleVersion >= featureVersion`.
///
/// - SeeAlso: `Triple.supports(_:)`
public struct FeatureAvailability {
public enum Availability {
case unavailable
case available(since: Version)
case availableInAllVersions
}
public let macOS: Availability
public let iOS: Availability
public let tvOS: Availability
public let watchOS: Availability
// TODO: We should have linux, windows, etc.
public let nonDarwin: Bool
/// Describes the availability of a feature that is supported on multiple platforms,
/// but is tied to a particular version.
public init(
macOS: Availability,
iOS: Availability,
tvOS: Availability,
watchOS: Availability,
nonDarwin: Bool = false
) {
self.macOS = macOS
self.iOS = iOS
self.tvOS = tvOS
self.watchOS = watchOS
self.nonDarwin = nonDarwin
}
/// Returns the version when the feature was introduced on the specified Darwin
/// platform, or `.unavailable` if the feature has not been introduced there.
public subscript(darwinPlatform: DarwinPlatform) -> Availability {
switch darwinPlatform {
case .macOS:
return macOS
case .iOS:
return iOS
case .tvOS:
return tvOS
case .watchOS:
return watchOS
}
}
}
/// Checks whether the triple supports the specified feature, i.e., the feature
/// has been introduced by the OS and version indicated by the triple.
public func supports(_ feature: FeatureAvailability) -> Bool {
guard let darwinPlatform = darwinPlatform else {
return feature.nonDarwin
}
switch feature[darwinPlatform] {
case .unavailable:
return false
case .available(let introducedVersion):
return version(for: darwinPlatform) >= introducedVersion
case .availableInAllVersions:
return true
}
}
}
extension Triple.FeatureAvailability {
/// Linking `libarclite` is unnecessary for triples supporting this feature.
///
/// This impacts the `-link-objc-runtime` flag in Swift, which is akin to the
/// `-fobjc-link-runtime` build setting in clang. When set, these flags
/// automatically link libobjc, and any compatibility libraries that don't
/// ship with the OS. The versions here are the first OSes that support
/// ARC natively in their respective copies of the Objective-C runtime,
/// and therefore do not require additional support libraries.
static let nativeARC = Self(
macOS: .available(since: Triple.Version(10, 11, 0)),
iOS: .available(since: Triple.Version(9, 0, 0)),
tvOS: .available(since: Triple.Version(9, 0, 0)),
watchOS: .availableInAllVersions
)
// When updating the versions listed here, please record the most recent
// feature being depended on and when it was introduced:
//
// - Make assigning 'nil' to an NSMutableDictionary subscript delete the
// entry, like it does for Swift.Dictionary, rather than trap.
}