-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathOSInfo.swift
69 lines (60 loc) · 1.48 KB
/
OSInfo.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
// Copyright 2022 Yandex LLC. All rights reserved.
internal import SwiftShims
public struct OSInfo: Sendable {
public enum Platform: Equatable, Sendable {
/// https://docs.swift.org/swift-book/ReferenceManual/Statements.html#ID539
case macOS, iOS, watchOS, tvOS, linux, windows
@inlinable
public static var current: Self {
#if os(macOS)
.macOS
#elseif os(iOS)
.iOS
#elseif os(watchOS)
.watchOS
#elseif os(tvOS)
.tvOS
#elseif os(Linux)
.linux
#elseif os(Windows)
.windows
#else
#error("Unknown platform")
#endif
}
}
public var version: OSVersion
public var platform: Platform
@inlinable
public init(
version: OSVersion,
platform: Platform
) {
self.version = version
self.platform = platform
}
static let _current = Self(
version: operatingSystemVersion(),
platform: Platform.current
)
static func operatingSystemVersion() -> OSVersion {
let v = _swift_stdlib_operatingSystemVersion()
return OSVersion(v.majorVersion, v.minorVersion, v.patchVersion)
}
#if INTERNAL_BUILD
private static let __current: AllocatedUnfairLock<Self> = .init(initialState: ._current)
public static var current: Self {
get {
__current.withLock { $0 }
}
set {
__current.withLock { $0 = newValue }
}
}
static func restoreSystemCurrent() {
current = _current
}
#else
public static let current: Self = ._current
#endif
}