Skip to content

Commit 07efc00

Browse files
committed
Add tests, refine Destination and DestinationInfoV2
1 parent 1568555 commit 07efc00

File tree

5 files changed

+86
-23
lines changed

5 files changed

+86
-23
lines changed

Sources/PackageModel/Destination.swift

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ public struct Destination: Encodable, Equatable {
259259
}
260260

261261
extension Destination {
262-
/// Load a Destination description from a JSON representation from disk.
262+
/// Load a ``Destination`` description from a JSON representation from disk.
263263
public init(fromFile path: AbsolutePath, fileSystem: FileSystem) throws {
264264
let decoder = JSONDecoder.makeWithDefaults()
265265
let version = try decoder.decode(path: path, fileSystem: fileSystem, as: VersionInfo.self)
@@ -269,9 +269,9 @@ extension Destination {
269269
case 1:
270270
let destination = try decoder.decode(path: path, fileSystem: fileSystem, as: DestinationInfoV1.self)
271271
try self.init(
272-
target: destination.target.map{ try Triple($0) },
273-
sdk: destination.sdk,
274-
binDir: destination.binDir,
272+
destinationTriple: destination.target.map{ try Triple($0) },
273+
sdkRootDir: destination.sdk,
274+
toolchainBinDir: destination.binDir,
275275
extraFlags: .init(
276276
cCompilerFlags: destination.extraCCFlags,
277277
cxxCompilerFlags: destination.extraCPPFlags,
@@ -283,9 +283,10 @@ extension Destination {
283283
let destinationDirectory = path.parentDirectory
284284

285285
try self.init(
286-
target: Triple(destination.destinationTriple),
287-
sdk: AbsolutePath(validating: destination.sdkDir, relativeTo: destinationDirectory),
288-
binDir: AbsolutePath(validating: destination.toolchainBinDir, relativeTo: destinationDirectory),
286+
hostTriple: Triple(destination.hostTriple),
287+
destinationTriple: Triple(destination.destinationTriple),
288+
sdkRootDir: AbsolutePath(validating: destination.sdkDir, relativeTo: destinationDirectory),
289+
toolchainBinDir: AbsolutePath(validating: destination.toolchainBinDir, relativeTo: destinationDirectory),
289290
extraFlags: .init(
290291
cCompilerFlags: destination.extraCCFlags,
291292
cxxCompilerFlags: destination.extraCPPFlags,

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: 10 additions & 10 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.sdkRootDir else {
261+
guard let sdkDir = destination.sdkRootDir 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
}
@@ -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.sdkRootDir {
392+
if let sdkDir = destination.sdkRootDir {
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
@@ -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.sdkRootDir {
527-
sdkroot = sdk
528-
} else if let SDKROOT = environment["SDKROOT"], let sdk = try? AbsolutePath(validating: SDKROOT) {
529-
sdkroot = sdk
526+
if let sdkDir = destination.sdkRootDir {
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) {
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+
sdkRootDir: sdkRootDir,
57+
toolchainBinDir: toolchainBinDir,
58+
extraFlags: flagsWithoutLinkerFlags
59+
)
60+
)
61+
}
62+
}

Tests/PackageModelTests/PackageModelTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,18 @@ class PackageModelTests: XCTestCase {
5757

5858
func testAndroidCompilerFlags() throws {
5959
let triple = try Triple("x86_64-unknown-linux-android")
60-
let sdk = AbsolutePath(path: "/some/path/to/an/SDK.sdk")
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(
6464
destinationTriple: triple,
65-
sdkRootDir: sdk,
65+
sdkRootDir: sdkDir,
6666
toolchainBinDir: toolchainPath.appending(components: "usr", "bin")
6767
)
6868

6969
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)