10
10
//
11
11
//===----------------------------------------------------------------------===//
12
12
13
- import protocol Foundation. CustomNSError
14
- import var Foundation. NSLocalizedDescriptionKey
15
-
16
13
import enum TSCBasic. JSON
17
14
import class TSCBasic. Process
15
+ import struct SwiftDriver. Triple
18
16
19
- /// Triple - Helper class for working with Destination.target values
20
- ///
21
- /// Used for parsing values such as `x86_64-apple-macosx10.10` into
22
- /// set of enums. For os/arch/abi based conditions in build plan.
23
- ///
24
- /// @see Destination.target
25
- /// @see https://github.com/apple/swift-llvm/blob/stable/include/llvm/ADT/Triple.h
26
- ///
27
- public struct Triple : Encodable , Equatable , Sendable {
28
- public let tripleString : String
29
-
30
- public let arch : Arch
31
- public let vendor : Vendor
32
- public let os : OS
33
- public let abi : ABI
34
- public let osVersion : String ?
35
- public let abiVersion : String ?
36
-
37
- public enum Error : Swift . Error {
38
- case badFormat( triple: String )
39
- case unknownArch( arch: String )
40
- case unknownOS( os: String )
41
- }
42
-
43
- public enum Arch : String , Encodable , Sendable {
44
- case x86_64
45
- case x86_64h
46
- case i686
47
- case powerpc
48
- case powerpc64le
49
- case s390x
50
- case aarch64
51
- case amd64
52
- case armv7
53
- case armv7em
54
- case armv6
55
- case armv5
56
- case arm
57
- case arm64
58
- case arm64e
59
- case wasm32
60
- case riscv64
61
- case mips
62
- case mipsel
63
- case mips64
64
- case mips64el
65
- }
66
-
67
- public enum Vendor : String , Encodable , Sendable {
68
- case unknown
69
- case apple
70
- }
71
-
72
- public enum OS : String , Encodable , CaseIterable , Sendable {
73
- case darwin
74
- case macOS = " macosx "
75
- case linux
76
- case windows
77
- case wasi
78
- case openbsd
79
- // 'OS' suffix purely to avoid name clash with Optional.none
80
- case noneOS = " none "
81
- }
82
-
83
- public enum ABI : Encodable , Equatable , RawRepresentable , Sendable {
84
- case unknown
85
- case android
86
- case other( name: String )
87
-
88
- public init ? ( rawValue: String ) {
89
- if rawValue. hasPrefix ( ABI . android. rawValue) {
90
- self = . android
91
- } else if let version = rawValue. firstIndex ( where: { $0. isNumber } ) {
92
- self = . other( name: String ( rawValue [ ..< version] ) )
93
- } else {
94
- self = . other( name: rawValue)
95
- }
96
- }
97
-
98
- public var rawValue : String {
99
- switch self {
100
- case . android: return " android "
101
- case . other( let name) : return name
102
- case . unknown: return " unknown "
103
- }
104
- }
105
-
106
- public static func == ( lhs: ABI , rhs: ABI ) -> Bool {
107
- switch ( lhs, rhs) {
108
- case ( . unknown, . unknown) :
109
- return true
110
- case ( . android, . android) :
111
- return true
112
- case ( . other( let lhsName) , . other( let rhsName) ) :
113
- return lhsName == rhsName
114
- default :
115
- return false
116
- }
117
- }
118
- }
119
-
120
- public init ( _ string: String ) throws {
121
- let components = string. split ( separator: " - " ) . map ( String . init)
122
-
123
- guard components. count == 3 || components. count == 4 else {
124
- throw Error . badFormat ( triple: string)
125
- }
126
-
127
- guard let arch = Arch ( rawValue: components [ 0 ] ) else {
128
- throw Error . unknownArch ( arch: components [ 0 ] )
129
- }
130
-
131
- let vendor = Vendor ( rawValue: components [ 1 ] ) ?? . unknown
132
-
133
- guard let os = Triple . parseOS ( components [ 2 ] ) else {
134
- throw Error . unknownOS ( os: components [ 2 ] )
135
- }
136
-
137
- let osVersion = Triple . parseVersion ( components [ 2 ] )
138
-
139
- let abi = components. count > 3 ? Triple . ABI ( rawValue: components [ 3 ] ) : nil
140
- let abiVersion = components. count > 3 ? Triple . parseVersion ( components [ 3 ] ) : nil
17
+ public typealias Triple = SwiftDriver . Triple
141
18
142
- self . tripleString = string
143
- self . arch = arch
144
- self . vendor = vendor
145
- self . os = os
146
- self . osVersion = osVersion
147
- self . abi = abi ?? . unknown
148
- self . abiVersion = abiVersion
149
- }
150
-
151
- fileprivate static func parseOS( _ string: String ) -> OS ? {
152
- var candidates = OS . allCases. map { ( name: $0. rawValue, value: $0) }
153
- // LLVM target triples support this alternate spelling as well.
154
- candidates. append ( ( name: " macos " , value: . macOS) )
155
- return candidates. first ( where: { string. hasPrefix ( $0. name) } ) ? . value
19
+ extension Triple {
20
+ public init ( _ description: String ) throws {
21
+ self . init ( description, normalizing: false )
156
22
}
23
+ }
157
24
158
- fileprivate static func parseVersion( _ string: String ) -> String ? {
159
- let candidate = String ( string. drop ( while: { $0. isLetter } ) )
160
- if candidate != string && !candidate. isEmpty {
161
- return candidate
162
- }
163
-
164
- return nil
165
- }
25
+ extension Triple {
26
+ public static let macOS = try ! Self ( " x86_64-apple-macosx " )
27
+ }
166
28
29
+ extension Triple {
167
30
public func isApple( ) -> Bool {
168
31
vendor == . apple
169
32
}
170
33
171
34
public func isAndroid( ) -> Bool {
172
- os == . linux && abi == . android
35
+ os == . linux && environment == . android
173
36
}
174
37
175
38
public func isDarwin( ) -> Bool {
176
39
switch ( vendor, os) {
177
40
case ( . apple, . noneOS) :
178
41
return false
179
- case ( . apple, _) , ( _, . macOS ) , ( _, . darwin) :
42
+ case ( . apple, _) , ( _, . macosx ) , ( _, . darwin) :
180
43
return true
181
44
default :
182
45
return false
@@ -188,7 +51,7 @@ public struct Triple: Encodable, Equatable, Sendable {
188
51
}
189
52
190
53
public func isWindows( ) -> Bool {
191
- os == . windows
54
+ os == . win32
192
55
}
193
56
194
57
public func isWASI( ) -> Bool {
@@ -204,7 +67,18 @@ public struct Triple: Encodable, Equatable, Sendable {
204
67
/// This is currently meant for Apple platforms only.
205
68
public func tripleString( forPlatformVersion version: String ) -> String {
206
69
precondition ( isDarwin ( ) )
207
- return String ( self . tripleString. dropLast ( self . osVersion? . count ?? 0 ) ) + version
70
+ // This function did not handle triples with a specific environments and
71
+ // object formats previously to using SwiftDriver.Triple and still does
72
+ // not.
73
+ return """
74
+ \( self . archName) - \
75
+ \( self . vendorName) - \
76
+ \( self . osNameUnversioned) \( version)
77
+ """
78
+ }
79
+
80
+ public var tripleString : String {
81
+ self . triple
208
82
}
209
83
210
84
/// Determine the versioned host triple using the Swift compiler.
@@ -235,6 +109,7 @@ public struct Triple: Encodable, Equatable, Sendable {
235
109
" Target info does not contain a triple string ( \( error. interpolationDescription) ). \n Target info: \( parsedTargetInfo) "
236
110
)
237
111
}
112
+
238
113
// Parse the triple string.
239
114
do {
240
115
return try Triple ( tripleString)
@@ -244,18 +119,13 @@ public struct Triple: Encodable, Equatable, Sendable {
244
119
)
245
120
}
246
121
}
247
-
248
- public static func == ( lhs: Triple , rhs: Triple ) -> Bool {
249
- lhs. arch == rhs. arch && lhs. vendor == rhs. vendor && lhs. os == rhs. os && lhs. abi == rhs. abi && lhs
250
- . osVersion == rhs. osVersion && lhs. abiVersion == rhs. abiVersion
251
- }
252
122
}
253
123
254
124
extension Triple {
255
125
/// The file prefix for dynamic libraries
256
126
public var dynamicLibraryPrefix : String {
257
127
switch os {
258
- case . windows :
128
+ case . win32 :
259
129
return " "
260
130
default :
261
131
return " lib "
@@ -264,32 +134,42 @@ extension Triple {
264
134
265
135
/// The file extension for dynamic libraries (eg. `.dll`, `.so`, or `.dylib`)
266
136
public var dynamicLibraryExtension : String {
137
+ guard let os = self . os else {
138
+ fatalError ( " Cannot create dynamic libraries unknown os. " )
139
+ }
140
+
267
141
switch os {
268
- case . darwin, . macOS :
142
+ case . darwin, . macosx :
269
143
return " .dylib "
270
144
case . linux, . openbsd:
271
145
return " .so "
272
- case . windows :
146
+ case . win32 :
273
147
return " .dll "
274
148
case . wasi:
275
149
return " .wasm "
276
- case . noneOS :
277
- fatalError ( " Cannot create dynamic libraries for os \" none \" . " )
150
+ default :
151
+ fatalError ( " Cannot create dynamic libraries for os \" \( os ) \" . " )
278
152
}
279
153
}
280
154
281
155
public var executableExtension : String {
156
+ guard let os = self . os else {
157
+ return " "
158
+ }
159
+
282
160
switch os {
283
- case . darwin, . macOS :
161
+ case . darwin, . macosx :
284
162
return " "
285
163
case . linux, . openbsd:
286
164
return " "
287
165
case . wasi:
288
166
return " .wasm "
289
- case . windows :
167
+ case . win32 :
290
168
return " .exe "
291
169
case . noneOS:
292
170
return " "
171
+ default :
172
+ return " "
293
173
}
294
174
}
295
175
@@ -301,7 +181,7 @@ extension Triple {
301
181
/// The file extension for Foundation-style bundle.
302
182
public var nsbundleExtension : String {
303
183
switch os {
304
- case . darwin, . macOS :
184
+ case . darwin, . macosx :
305
185
return " .bundle "
306
186
default :
307
187
// See: https://github.com/apple/swift-corelibs-foundation/blob/master/Docs/FHS%20Bundles.md
@@ -314,21 +194,8 @@ extension Triple: CustomStringConvertible {
314
194
public var description : String { tripleString }
315
195
}
316
196
317
- extension Triple . Error : CustomNSError {
318
- public var errorUserInfo : [ String : Any ] {
319
- [ NSLocalizedDescriptionKey: " \( self ) " ]
320
- }
321
- }
322
-
323
- extension Triple . Error : CustomStringConvertible {
324
- public var description : String {
325
- switch self {
326
- case . badFormat( let triple) :
327
- return " couldn't parse triple string \( triple) "
328
- case . unknownArch( let arch) :
329
- return " unknown architecture \( arch) "
330
- case . unknownOS( let os) :
331
- return " unknown OS \( os) "
332
- }
197
+ extension Triple : Equatable {
198
+ public static func == ( lhs: Self , rhs: Self ) -> Bool {
199
+ lhs. triple == rhs. triple
333
200
}
334
201
}
0 commit comments