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
@@ -212,27 +213,51 @@ extension Destination {
212
213
public init ( fromFile path: AbsolutePath , fileSystem: FileSystem ) throws {
213
214
let decoder = JSONDecoder . makeWithDefaults ( )
214
215
let version = try decoder. decode ( path: path, fileSystem: fileSystem, as: VersionInfo . self)
216
+
215
217
// Check schema version.
216
- guard version. version == 1 else {
218
+ if version. version == 1 {
219
+ let destination = try decoder. decode ( path: path, fileSystem: fileSystem, as: DestinationInfoV1 . self)
220
+ try self . init (
221
+ target: destination. target. map { try Triple ( $0) } ,
222
+ sdk: destination. sdk,
223
+ binDir: destination. binDir,
224
+ extraCCFlags: destination. extraCCFlags,
225
+ extraSwiftCFlags: destination. extraSwiftCFlags,
226
+ extraCPPFlags: destination. extraCPPFlags
227
+ )
228
+ } else if version. version == 2 {
229
+ let destination = try decoder. decode ( path: path, fileSystem: fileSystem, as: DestinationInfoV2 . self)
230
+ let destinationDirectory = path. parentDirectory
231
+
232
+ try self . init (
233
+ target: destination. target. map { try Triple ( $0) } ,
234
+ sdk: destination. sdk? . absolutePath ( relativeTo: destinationDirectory) ,
235
+ binDir: destination. binDir. absolutePath ( relativeTo: destinationDirectory) ,
236
+ extraCCFlags: destination. extraCCFlags,
237
+ extraSwiftCFlags: destination. extraSwiftCFlags,
238
+ extraCPPFlags: destination. extraCPPFlags
239
+ )
240
+ } else {
217
241
throw DestinationError . invalidSchemaVersion
218
242
}
219
- let destination = try decoder. decode ( path: path, fileSystem: fileSystem, as: DestinationInfo . self)
220
- try self . init (
221
- target: destination. target. map { try Triple ( $0) } ,
222
- sdk: destination. sdk,
223
- binDir: destination. binDir,
224
- extraCCFlags: destination. extraCCFlags,
225
- extraSwiftCFlags: destination. extraSwiftCFlags,
226
- extraCPPFlags: destination. extraCPPFlags
227
- )
243
+ }
244
+ }
245
+
246
+ fileprivate extension FilePath {
247
+ func absolutePath( relativeTo directory: AbsolutePath ) -> AbsolutePath {
248
+ if isRelative {
249
+ return directory. appending ( RelativePath ( string) )
250
+ } else {
251
+ return AbsolutePath ( string)
252
+ }
228
253
}
229
254
}
230
255
231
256
fileprivate struct VersionInfo : Codable {
232
257
let version : Int
233
258
}
234
259
235
- fileprivate struct DestinationInfo : Codable {
260
+ fileprivate struct DestinationInfoV1 : Codable {
236
261
let target : String ?
237
262
let sdk : AbsolutePath ?
238
263
let binDir : AbsolutePath
@@ -249,3 +274,21 @@ fileprivate struct DestinationInfo: Codable {
249
274
case extraCPPFlags = " extra-cpp-flags "
250
275
}
251
276
}
277
+
278
+ fileprivate struct DestinationInfoV2 : Codable {
279
+ let target : String ?
280
+ let sdk : FilePath ?
281
+ let binDir : FilePath
282
+ let extraCCFlags : [ String ]
283
+ let extraSwiftCFlags : [ String ]
284
+ let extraCPPFlags : [ String ]
285
+
286
+ enum CodingKeys : String , CodingKey {
287
+ case target
288
+ case sdk
289
+ case binDir = " toolchain-bin-dir "
290
+ case extraCCFlags = " extra-cc-flags "
291
+ case extraSwiftCFlags = " extra-swiftc-flags "
292
+ case extraCPPFlags = " extra-cpp-flags "
293
+ }
294
+ }
0 commit comments