12
12
13
13
import Basics
14
14
import Foundation
15
+ import SystemPackage
15
16
import TSCBasic
16
17
17
18
import struct TSCUtility. Triple
@@ -232,28 +233,51 @@ extension Destination {
232
233
public init ( fromFile path: AbsolutePath , fileSystem: FileSystem ) throws {
233
234
let decoder = JSONDecoder . makeWithDefaults ( )
234
235
let version = try decoder. decode ( path: path, fileSystem: fileSystem, as: VersionInfo . self)
236
+
235
237
// Check schema version.
236
- guard version. version == 1 else {
238
+ if version. version == 1 {
239
+ let destination = try decoder. decode ( path: path, fileSystem: fileSystem, as: DestinationInfoV1 . self)
240
+ try self . init (
241
+ target: destination. target. map { try Triple ( $0) } ,
242
+ sdk: destination. sdk,
243
+ binDir: destination. binDir,
244
+ extraCCFlags: destination. extraCCFlags,
245
+ extraSwiftCFlags: destination. extraSwiftCFlags,
246
+ extraCPPFlags: destination. extraCPPFlags
247
+ )
248
+ } else if version. version == 2 {
249
+ let destination = try decoder. decode ( path: path, fileSystem: fileSystem, as: DestinationInfoV2 . self)
250
+ let destinationDirectory = path. parentDirectory
251
+
252
+ try self . init (
253
+ target: destination. target. map { try Triple ( $0) } ,
254
+ sdk: destination. sdk? . absolutePath ( relativeTo: destinationDirectory) ,
255
+ binDir: destination. binDir. absolutePath ( relativeTo: destinationDirectory) ,
256
+ extraCCFlags: destination. extraCCFlags,
257
+ extraSwiftCFlags: destination. extraSwiftCFlags,
258
+ extraCPPFlags: destination. extraCPPFlags
259
+ )
260
+ } else {
237
261
throw DestinationError . invalidSchemaVersion
238
262
}
239
- let destination = try decoder . decode ( path : path , fileSystem : fileSystem , as : DestinationInfo . self )
240
- try self . init (
241
- target : destination . target . map { try Triple ( $0 ) } ,
242
- sdk : destination . sdk ,
243
- binDir : destination . binDir ,
244
- extraCCFlags : destination . extraCCFlags ,
245
- extraSwiftCFlags : destination . extraSwiftCFlags ,
246
- // maintaining `destination.extraCPPFlags` naming inconsistency for compatibility.
247
- extraCXXFlags : destination . extraCPPFlags
248
- )
263
+ }
264
+ }
265
+
266
+ fileprivate extension FilePath {
267
+ func absolutePath ( relativeTo directory : AbsolutePath ) throws -> AbsolutePath {
268
+ if isRelative {
269
+ return directory . appending ( RelativePath ( string ) )
270
+ } else {
271
+ return try AbsolutePath ( validating : string )
272
+ }
249
273
}
250
274
}
251
275
252
276
fileprivate struct VersionInfo : Codable {
253
277
let version : Int
254
278
}
255
279
256
- fileprivate struct DestinationInfo : Codable {
280
+ fileprivate struct DestinationInfoV1 : Codable {
257
281
let target : String ?
258
282
let sdk : AbsolutePath ?
259
283
let binDir : AbsolutePath
@@ -270,3 +294,21 @@ fileprivate struct DestinationInfo: Codable {
270
294
case extraCPPFlags = " extra-cpp-flags "
271
295
}
272
296
}
297
+
298
+ fileprivate struct DestinationInfoV2 : Codable {
299
+ let target : String ?
300
+ let sdk : FilePath ?
301
+ let binDir : FilePath
302
+ let extraCCFlags : [ String ]
303
+ let extraSwiftCFlags : [ String ]
304
+ let extraCPPFlags : [ String ]
305
+
306
+ enum CodingKeys : String , CodingKey {
307
+ case target
308
+ case sdk
309
+ case binDir = " toolchain-bin-dir "
310
+ case extraCCFlags = " extra-cc-flags "
311
+ case extraSwiftCFlags = " extra-swiftc-flags "
312
+ case extraCPPFlags = " extra-cpp-flags "
313
+ }
314
+ }
0 commit comments