Skip to content

Commit ae196db

Browse files
committed
Add tests, refine Destination and DestinationInfoV2
1 parent 29f73b4 commit ae196db

File tree

7 files changed

+117
-43
lines changed

7 files changed

+117
-43
lines changed

Sources/Commands/SwiftTool.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -842,13 +842,13 @@ public class SwiftTool {
842842
}
843843
// Apply any manual overrides.
844844
if let triple = self.options.build.customCompileTriple {
845-
destination.target = triple
845+
destination.destinationTriple = triple
846846
}
847847
if let binDir = self.options.build.customCompileToolchain {
848848
destination.binDir = binDir.appending(components: "usr", "bin")
849849
}
850850
if let sdk = self.options.build.customCompileSDK {
851-
destination.sdk = sdk
851+
destination.sdkDir = sdk
852852
}
853853
destination.archs = options.build.archs
854854

Sources/PackageModel/Destination.swift

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,22 @@ public struct Destination: Encodable, Equatable {
4949
/// - abi = eabi, gnu, android, macho, elf, etc.
5050
///
5151
/// for more information see //https://clang.llvm.org/docs/CrossCompilation.html
52-
public var target: Triple?
52+
public var destinationTriple: Triple?
53+
54+
/// Triple describing hosts that can compile for this destination.
55+
let hostTriple: Triple?
5356

5457
/// The architectures to build for. We build for host architecture if this is empty.
5558
public var archs: [String] = []
5659

60+
/// The root SDK directory used to compile for the destination.
61+
@available(*, deprecated, message: "use `sdkDir` instead")
62+
public var sdk: AbsolutePath? {
63+
sdkDir
64+
}
65+
5766
/// The SDK used to compile for the destination.
58-
public var sdk: AbsolutePath?
67+
public var sdkDir: AbsolutePath?
5968

6069
/// The binDir in the containing the compilers/linker to be used for the compilation.
6170
public var binDir: AbsolutePath
@@ -82,7 +91,7 @@ public struct Destination: Encodable, Equatable {
8291
public let extraFlags: BuildFlags
8392

8493
/// Creates a compilation destination with the specified properties.
85-
@available(*, deprecated, message: "use `init(target:sdk:binDir:extraFlags)` instead")
94+
@available(*, deprecated, message: "use `init(destinationTriple:sdk:binDir:extraFlags)` instead")
8695
public init(
8796
target: Triple? = nil,
8897
sdk: AbsolutePath?,
@@ -91,8 +100,9 @@ public struct Destination: Encodable, Equatable {
91100
extraSwiftCFlags: [String] = [],
92101
extraCPPFlags: [String]
93102
) {
94-
self.target = target
95-
self.sdk = sdk
103+
self.hostTriple = nil
104+
self.destinationTriple = target
105+
self.sdkDir = sdk
96106
self.binDir = binDir
97107
self.extraFlags = BuildFlags(
98108
cCompilerFlags: extraCCFlags,
@@ -103,13 +113,15 @@ public struct Destination: Encodable, Equatable {
103113

104114
/// Creates a compilation destination with the specified properties.
105115
public init(
106-
target: Triple? = nil,
107-
sdk: AbsolutePath?,
116+
hostTriple: Triple? = nil,
117+
destinationTriple: Triple? = nil,
118+
sdkDir: AbsolutePath?,
108119
binDir: AbsolutePath,
109120
extraFlags: BuildFlags = BuildFlags()
110121
) {
111-
self.target = target
112-
self.sdk = sdk
122+
self.hostTriple = hostTriple
123+
self.destinationTriple = destinationTriple
124+
self.sdkDir = sdkDir
113125
self.binDir = binDir
114126
self.extraFlags = extraFlags
115127
}
@@ -181,8 +193,7 @@ public struct Destination: Encodable, Equatable {
181193
#endif
182194

183195
return Destination(
184-
target: nil,
185-
sdk: sdkPath,
196+
sdkDir: sdkPath,
186197
binDir: binDir,
187198
extraFlags: BuildFlags(cCompilerFlags: extraCCFlags, swiftCompilerFlags: extraSwiftCFlags)
188199
)
@@ -222,8 +233,8 @@ public struct Destination: Encodable, Equatable {
222233
.parentDirectory // usr
223234
.appending(components: "share", "wasi-sysroot")
224235
return Destination(
225-
target: triple,
226-
sdk: wasiSysroot,
236+
destinationTriple: triple,
237+
sdkDir: wasiSysroot,
227238
binDir: host.binDir
228239
)
229240
}
@@ -232,7 +243,7 @@ public struct Destination: Encodable, Equatable {
232243
}
233244

234245
extension Destination {
235-
/// Load a Destination description from a JSON representation from disk.
246+
/// Load a ``Destination`` description from a JSON representation from disk.
236247
public init(fromFile path: AbsolutePath, fileSystem: FileSystem) throws {
237248
let decoder = JSONDecoder.makeWithDefaults()
238249
let version = try decoder.decode(path: path, fileSystem: fileSystem, as: VersionInfo.self)
@@ -242,8 +253,8 @@ extension Destination {
242253
case 1:
243254
let destination = try decoder.decode(path: path, fileSystem: fileSystem, as: DestinationInfoV1.self)
244255
try self.init(
245-
target: destination.target.map{ try Triple($0) },
246-
sdk: destination.sdk,
256+
destinationTriple: destination.target.map{ try Triple($0) },
257+
sdkDir: destination.sdk,
247258
binDir: destination.binDir,
248259
extraFlags: .init(
249260
cCompilerFlags: destination.extraCCFlags,
@@ -256,8 +267,9 @@ extension Destination {
256267
let destinationDirectory = path.parentDirectory
257268

258269
try self.init(
259-
target: Triple(destination.destinationTriple),
260-
sdk: AbsolutePath(validating: destination.sdkDir, relativeTo: destinationDirectory),
270+
hostTriple: Triple(destination.hostTriple),
271+
destinationTriple: Triple(destination.destinationTriple),
272+
sdkDir: AbsolutePath(validating: destination.sdkDir, relativeTo: destinationDirectory),
261273
binDir: AbsolutePath(validating: destination.toolchainBinDir, relativeTo: destinationDirectory),
262274
extraFlags: .init(
263275
cCompilerFlags: destination.extraCCFlags,

Sources/PackageModel/Manifest.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,14 @@ public final class Manifest {
180180
}
181181

182182
var requiredDependencies: Set<PackageIdentity> = []
183-
for target in self.targetsRequired(for: products) {
184-
for targetDependency in target.dependencies {
183+
for destinationTriple in self.targetsRequired(for: products) {
184+
for targetDependency in destinationTriple.dependencies {
185185
if let dependency = self.packageDependency(referencedBy: targetDependency) {
186186
requiredDependencies.insert(dependency.identity)
187187
}
188188
}
189189

190-
target.pluginUsages?.forEach {
190+
destinationTriple.pluginUsages?.forEach {
191191
if let dependency = self.packageDependency(referencedBy: $0) {
192192
requiredDependencies.insert(dependency.identity)
193193
}

Sources/PackageModel/UserToolchain.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ public final class UserToolchain: Toolchain {
258258
}
259259

260260
internal static func deriveSwiftCFlags(triple: Triple, destination: Destination, environment: EnvironmentVariables) throws -> [String] {
261-
guard let sdk = destination.sdk else {
261+
guard let sdkDir = destination.sdkDir else {
262262
if triple.isWindows() {
263263
// Windows uses a variable named SDKROOT to determine the root of
264264
// the SDK. This is not the same value as the SDKROOT parameter
@@ -336,7 +336,7 @@ public final class UserToolchain: Toolchain {
336336
}
337337

338338
return (triple.isDarwin() || triple.isAndroid() || triple.isWASI() || triple.isWindows()
339-
? ["-sdk", sdk.pathString]
339+
? ["-sdk", sdkDir.pathString]
340340
: [])
341341
+ destination.extraFlags.swiftCompilerFlags
342342
}
@@ -373,7 +373,7 @@ public final class UserToolchain: Toolchain {
373373
self.archs = destination.archs
374374

375375
// Use the triple from destination or compute the host triple using swiftc.
376-
var triple = destination.target ?? Triple.getHostTriple(usingSwiftCompiler: swiftCompilers.compile)
376+
var triple = destination.destinationTriple ?? Triple.getHostTriple(usingSwiftCompiler: swiftCompilers.compile)
377377

378378
self.librarianPath = try UserToolchain.determineLibrarian(triple: triple, binDir: binDir, useXcrun: useXcrun, environment: environment, searchPaths: envSearchPaths)
379379

@@ -389,9 +389,9 @@ public final class UserToolchain: Toolchain {
389389

390390
self.extraFlags.swiftCompilerFlags = try Self.deriveSwiftCFlags(triple: triple, destination: destination, environment: environment)
391391

392-
if let sdk = destination.sdk {
392+
if let sdkDir = destination.sdkDir {
393393
self.extraFlags.cCompilerFlags = [
394-
triple.isDarwin() ? "-isysroot" : "--sysroot", sdk.pathString
394+
triple.isDarwin() ? "-isysroot" : "--sysroot", sdkDir.pathString
395395
] + destination.extraFlags.cCompilerFlags
396396

397397
self.extraFlags.cxxCompilerFlags = destination.extraFlags.cxxCompilerFlags
@@ -444,7 +444,7 @@ public final class UserToolchain: Toolchain {
444444
swiftCompilerFlags: self.extraFlags.swiftCompilerFlags,
445445
swiftCompilerEnvironment: environment,
446446
swiftPMLibrariesLocation: swiftPMLibrariesLocation,
447-
sdkRootPath: self.destination.sdk,
447+
sdkRootPath: self.destination.sdkDir,
448448
xctestPath: xctestPath
449449
)
450450
}
@@ -521,12 +521,12 @@ public final class UserToolchain: Toolchain {
521521
return try AbsolutePath(validating: path)
522522
}
523523
} else if triple.isWindows() {
524-
let sdkroot: AbsolutePath
524+
let sdkRoot: AbsolutePath
525525

526-
if let sdk = destination.sdk {
527-
sdkroot = sdk
528-
} else if let SDKROOT = environment["SDKROOT"], let sdk = try? AbsolutePath(validating: SDKROOT) {
529-
sdkroot = sdk
526+
if let sdkDir = destination.sdkDir {
527+
sdkRoot = sdkDir
528+
} else if let SDKROOT = environment["SDKROOT"], let sdkDir = try? AbsolutePath(validating: SDKROOT) {
529+
sdkRoot = sdkDir
530530
} else {
531531
return .none
532532
}
@@ -537,7 +537,7 @@ public final class UserToolchain: Toolchain {
537537
// Library/Developer/Platforms/[PLATFORM].platform/Developer/SDKs/[PLATFORM].sdk/...
538538
//
539539
// SDKROOT points to [PLATFORM].sdk
540-
let platform = sdkroot.parentDirectory.parentDirectory.parentDirectory
540+
let platform = sdkRoot.parentDirectory.parentDirectory.parentDirectory
541541

542542
if let info = WindowsPlatformInfo(reading: platform.appending(component: "Info.plist"),
543543
diagnostics: nil, filesystem: localFileSystem) {

Tests/BuildTests/BuildPlanTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3372,7 +3372,7 @@ final class BuildPlanTests: XCTestCase {
33723372
XCTAssertNoDiagnostics(observability.diagnostics)
33733373

33743374
let userDestination = Destination(
3375-
sdk: AbsolutePath(path: "/fake/sdk"),
3375+
sdkDir: AbsolutePath(path: "/fake/sdk"),
33763376
binDir: try UserToolchain.default.destination.binDir,
33773377
extraFlags: BuildFlags(
33783378
cCompilerFlags: ["-I/fake/sdk/sysroot", "-clang-flag-from-json"],
@@ -3400,7 +3400,7 @@ final class BuildPlanTests: XCTestCase {
34003400
#else
34013401
args += ["--sysroot"]
34023402
#endif
3403-
args += ["\(userDestination.sdk!)", "-I/fake/sdk/sysroot", "-clang-flag-from-json", .anySequence, "-clang-command-line-flag"]
3403+
args += ["\(userDestination.sdkDir!)", "-I/fake/sdk/sysroot", "-clang-flag-from-json", .anySequence, "-clang-command-line-flag"]
34043404
XCTAssertMatch(try lib.basicArguments(isCXX: false), args)
34053405

34063406
let exe = try result.target(for: "exe").swiftTarget().compileArguments()
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift open source project
4+
//
5+
// Copyright (c) 2014-2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import Basics
14+
@testable import PackageModel
15+
import TSCBasic
16+
import TSCUtility
17+
import XCTest
18+
19+
private let bundleRootPath = try! AbsolutePath(validating: "/tmp/cross-toolchain")
20+
private let toolchainBinDir = bundleRootPath.appending(.init("swift.xctoolchain/usr/bin"))
21+
private let sdkRootDir = bundleRootPath.appending(.init("ubuntu-jammy.sdk"))
22+
private let destinationTriple = "x86_64-unknown-linux-gnu"
23+
private let extraFlags = BuildFlags(
24+
cCompilerFlags: ["-fintegrated-as"],
25+
cxxCompilerFlags: ["-fno-exceptions"],
26+
swiftCompilerFlags: ["-enable-experimental-cxx-interop", "-use-ld=lld"],
27+
linkerFlags: ["-R/usr/lib/swift/linux/"]
28+
)
29+
30+
private let destinationV1JSON =
31+
#"""
32+
{
33+
"version": 1,
34+
"sdk": "\#(sdkRootDir)",
35+
"toolchain-bin-dir": "\#(toolchainBinDir)",
36+
"target": "\#(destinationTriple)",
37+
"extra-cc-flags": \#(extraFlags.cCompilerFlags),
38+
"extra-swiftc-flags": \#(extraFlags.swiftCompilerFlags),
39+
"extra-cpp-flags": \#(extraFlags.cxxCompilerFlags)
40+
}
41+
"""#
42+
43+
class DestinationTests: XCTestCase {
44+
func testDestinationCodable() throws {
45+
let fs = InMemoryFileSystem(files: ["/sdk/destination.json": ByteString(encodingAsUTF8: destinationV1JSON)])
46+
47+
let destinationV1 = try Destination(fromFile: AbsolutePath(validating: "/sdk/destination.json"), fileSystem: fs)
48+
49+
var flagsWithoutLinkerFlags = extraFlags
50+
flagsWithoutLinkerFlags.linkerFlags = []
51+
52+
XCTAssertEqual(
53+
destinationV1,
54+
Destination(
55+
destinationTriple: try Triple(destinationTriple),
56+
sdkDir: sdkRootDir,
57+
binDir: toolchainBinDir,
58+
extraFlags: flagsWithoutLinkerFlags
59+
)
60+
)
61+
}
62+
}

Tests/PackageModelTests/PackageModelTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,19 @@ class PackageModelTests: XCTestCase {
5656
}
5757

5858
func testAndroidCompilerFlags() throws {
59-
let target = try Triple("x86_64-unknown-linux-android")
60-
let sdk = AbsolutePath(path: "/some/path/to/an/SDK.sdk")
59+
let triple = try Triple("x86_64-unknown-linux-android")
60+
let sdkDir = AbsolutePath(path: "/some/path/to/an/SDK.sdk")
6161
let toolchainPath = AbsolutePath(path: "/some/path/to/a/toolchain.xctoolchain")
6262

6363
let destination = Destination(
64-
target: target,
65-
sdk: sdk,
64+
destinationTriple: triple,
65+
sdkDir: sdkDir,
6666
binDir: toolchainPath.appending(components: "usr", "bin")
6767
)
6868

69-
XCTAssertEqual(try UserToolchain.deriveSwiftCFlags(triple: target, destination: destination, environment: .process()), [
69+
XCTAssertEqual(try UserToolchain.deriveSwiftCFlags(triple: triple, destination: destination, environment: .process()), [
7070
// Needed when cross‐compiling for Android. 2020‐03‐01
71-
"-sdk", sdk.pathString,
71+
"-sdk", sdkDir.pathString,
7272
])
7373
}
7474
}

0 commit comments

Comments
 (0)