Skip to content

Commit fa22e32

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 8bb08b2 commit fa22e32

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
@@ -262,30 +263,47 @@ extension Destination {
262263
public init(fromFile path: AbsolutePath, fileSystem: FileSystem) throws {
263264
let decoder = JSONDecoder.makeWithDefaults()
264265
let version = try decoder.decode(path: path, fileSystem: fileSystem, as: VersionInfo.self)
266+
265267
// Check schema version.
266-
guard version.version == 1 else {
268+
switch version.version {
269+
case 1:
270+
let destination = try decoder.decode(path: path, fileSystem: fileSystem, as: DestinationInfoV1.self)
271+
try self.init(
272+
target: destination.target.map{ try Triple($0) },
273+
sdk: destination.sdk,
274+
binDir: destination.binDir,
275+
extraFlags: .init(
276+
cCompilerFlags: destination.extraCCFlags,
277+
cxxCompilerFlags: destination.extraCPPFlags,
278+
swiftCompilerFlags: destination.extraSwiftCFlags
279+
)
280+
)
281+
case 2:
282+
let destination = try decoder.decode(path: path, fileSystem: fileSystem, as: DestinationInfoV2.self)
283+
let destinationDirectory = path.parentDirectory
284+
285+
try self.init(
286+
target: Triple(destination.destinationTriple),
287+
sdk: AbsolutePath(validating: destination.sdkDir, relativeTo: destinationDirectory),
288+
binDir: AbsolutePath(validating: destination.toolchainBinDir, relativeTo: destinationDirectory),
289+
extraFlags: .init(
290+
cCompilerFlags: destination.extraCCFlags,
291+
cxxCompilerFlags: destination.extraCPPFlags,
292+
swiftCompilerFlags: destination.extraSwiftCFlags,
293+
linkerFlags: destination.extraLinkerFlags
294+
)
295+
)
296+
default:
267297
throw DestinationError.invalidSchemaVersion
268298
}
269-
let destination = try decoder.decode(path: path, fileSystem: fileSystem, as: DestinationInfo.self)
270-
try self.init(
271-
destinationTriple: destination.target.map{ try Triple($0) },
272-
sdkRootDir: destination.sdk,
273-
toolchainBinDir: destination.binDir,
274-
extraFlags: BuildFlags(
275-
cCompilerFlags: destination.extraCCFlags,
276-
// maintaining `destination.extraCPPFlags` naming inconsistency for compatibility.
277-
cxxCompilerFlags: destination.extraCPPFlags,
278-
swiftCompilerFlags: destination.extraSwiftCFlags
279-
)
280-
)
281299
}
282300
}
283301

284302
fileprivate struct VersionInfo: Codable {
285303
let version: Int
286304
}
287305

288-
fileprivate struct DestinationInfo: Codable {
306+
fileprivate struct DestinationInfoV1: Codable {
289307
let target: String?
290308
let sdk: AbsolutePath?
291309
let binDir: AbsolutePath
@@ -302,3 +320,14 @@ fileprivate struct DestinationInfo: Codable {
302320
case extraCPPFlags = "extra-cpp-flags"
303321
}
304322
}
323+
324+
fileprivate struct DestinationInfoV2: Codable {
325+
let sdkDir: String
326+
let toolchainBinDir: String
327+
let hostTriple: String
328+
let destinationTriple: String
329+
let extraCCFlags: [String]
330+
let extraSwiftCFlags: [String]
331+
let extraCPPFlags: [String]
332+
let extraLinkerFlags: [String]
333+
}

0 commit comments

Comments
 (0)