Skip to content

Commit 6c03b32

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 75877ea commit 6c03b32

File tree

1 file changed

+54
-12
lines changed

1 file changed

+54
-12
lines changed

Sources/PackageModel/Destination.swift

Lines changed: 54 additions & 12 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
@@ -232,28 +233,51 @@ extension Destination {
232233
public init(fromFile path: AbsolutePath, fileSystem: FileSystem) throws {
233234
let decoder = JSONDecoder.makeWithDefaults()
234235
let version = try decoder.decode(path: path, fileSystem: fileSystem, as: VersionInfo.self)
236+
235237
// 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 {
237261
throw DestinationError.invalidSchemaVersion
238262
}
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+
}
249273
}
250274
}
251275

252276
fileprivate struct VersionInfo: Codable {
253277
let version: Int
254278
}
255279

256-
fileprivate struct DestinationInfo: Codable {
280+
fileprivate struct DestinationInfoV1: Codable {
257281
let target: String?
258282
let sdk: AbsolutePath?
259283
let binDir: AbsolutePath
@@ -270,3 +294,21 @@ fileprivate struct DestinationInfo: Codable {
270294
case extraCPPFlags = "extra-cpp-flags"
271295
}
272296
}
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

Comments
 (0)