Skip to content

Commit 29f73b4

Browse files
committed
PackageModel: relative paths for cross-compilation destinations
This introduces support for relative paths in v2 scheme of `destination.json` files. The path is then normalized relative to the location of `destination.json` files. As a result, it makes it easier for moving cross-compilation sysroots between different directories on the same filesystem, or even between different machines.
1 parent 29a16bc commit 29f73b4

File tree

1 file changed

+43
-14
lines changed

1 file changed

+43
-14
lines changed

Sources/PackageModel/Destination.swift

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import Basics
1414
import Foundation
15+
import SystemPackage
1516
import TSCBasic
1617

1718
import struct TSCUtility.Triple
@@ -235,30 +236,47 @@ extension Destination {
235236
public init(fromFile path: AbsolutePath, fileSystem: FileSystem) throws {
236237
let decoder = JSONDecoder.makeWithDefaults()
237238
let version = try decoder.decode(path: path, fileSystem: fileSystem, as: VersionInfo.self)
239+
238240
// Check schema version.
239-
guard version.version == 1 else {
241+
switch version.version {
242+
case 1:
243+
let destination = try decoder.decode(path: path, fileSystem: fileSystem, as: DestinationInfoV1.self)
244+
try self.init(
245+
target: destination.target.map{ try Triple($0) },
246+
sdk: destination.sdk,
247+
binDir: destination.binDir,
248+
extraFlags: .init(
249+
cCompilerFlags: destination.extraCCFlags,
250+
cxxCompilerFlags: destination.extraCPPFlags,
251+
swiftCompilerFlags: destination.extraSwiftCFlags
252+
)
253+
)
254+
case 2:
255+
let destination = try decoder.decode(path: path, fileSystem: fileSystem, as: DestinationInfoV2.self)
256+
let destinationDirectory = path.parentDirectory
257+
258+
try self.init(
259+
target: Triple(destination.destinationTriple),
260+
sdk: AbsolutePath(validating: destination.sdkDir, relativeTo: destinationDirectory),
261+
binDir: AbsolutePath(validating: destination.toolchainBinDir, relativeTo: destinationDirectory),
262+
extraFlags: .init(
263+
cCompilerFlags: destination.extraCCFlags,
264+
cxxCompilerFlags: destination.extraCPPFlags,
265+
swiftCompilerFlags: destination.extraSwiftCFlags,
266+
linkerFlags: destination.extraLinkerFlags
267+
)
268+
)
269+
default:
240270
throw DestinationError.invalidSchemaVersion
241271
}
242-
let destination = try decoder.decode(path: path, fileSystem: fileSystem, as: DestinationInfo.self)
243-
try self.init(
244-
target: destination.target.map{ try Triple($0) },
245-
sdk: destination.sdk,
246-
binDir: destination.binDir,
247-
extraFlags: BuildFlags(
248-
cCompilerFlags: destination.extraCCFlags,
249-
// maintaining `destination.extraCPPFlags` naming inconsistency for compatibility.
250-
cxxCompilerFlags: destination.extraCPPFlags,
251-
swiftCompilerFlags: destination.extraSwiftCFlags
252-
)
253-
)
254272
}
255273
}
256274

257275
fileprivate struct VersionInfo: Codable {
258276
let version: Int
259277
}
260278

261-
fileprivate struct DestinationInfo: Codable {
279+
fileprivate struct DestinationInfoV1: Codable {
262280
let target: String?
263281
let sdk: AbsolutePath?
264282
let binDir: AbsolutePath
@@ -275,3 +293,14 @@ fileprivate struct DestinationInfo: Codable {
275293
case extraCPPFlags = "extra-cpp-flags"
276294
}
277295
}
296+
297+
fileprivate struct DestinationInfoV2: Codable {
298+
let sdkDir: String
299+
let toolchainBinDir: String
300+
let hostTriple: String
301+
let destinationTriple: String
302+
let extraCCFlags: [String]
303+
let extraSwiftCFlags: [String]
304+
let extraCPPFlags: [String]
305+
let extraLinkerFlags: [String]
306+
}

0 commit comments

Comments
 (0)