Skip to content

Commit a4e21bd

Browse files
committed
Guard Explicit Package Build test from running with older, unsupported toolchains.
Catch SwiftDriver's .unableToDecodeFrontendTargetInfo to detect a compiler that is definitely too old for the integreated driver to be used.
1 parent 6669703 commit a4e21bd

File tree

1 file changed

+44
-33
lines changed

1 file changed

+44
-33
lines changed

Tests/BuildTests/BuildPlanTests.swift

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import PackageModel
1717
@testable import PackageLoading
1818
import SPMBuildCore
1919
import Build
20+
import SwiftDriver
2021

2122
let hostTriple = Resources.default.toolchain.triple
2223
#if os(macOS)
@@ -25,7 +26,7 @@ let hostTriple = Resources.default.toolchain.triple
2526
let defaultTargetTriple: String = hostTriple.tripleString
2627
#endif
2728

28-
private struct MockToolchain: Toolchain {
29+
private struct MockToolchain: SPMBuildCore.Toolchain {
2930
let swiftCompiler = AbsolutePath("/fake/path/to/swiftc")
3031
let extraCCFlags: [String] = []
3132
let extraSwiftCFlags: [String] = []
@@ -58,10 +59,10 @@ final class BuildPlanTests: XCTestCase {
5859
func mockBuildParameters(
5960
buildPath: AbsolutePath = AbsolutePath("/path/to/build"),
6061
config: BuildConfiguration = .debug,
61-
toolchain: Toolchain = MockToolchain(),
62+
toolchain: SPMBuildCore.Toolchain = MockToolchain(),
6263
flags: BuildFlags = BuildFlags(),
6364
shouldLinkStaticSwiftStdlib: Bool = false,
64-
destinationTriple: Triple = hostTriple,
65+
destinationTriple: SPMBuildCore.Triple = hostTriple,
6566
indexStoreMode: BuildParameters.IndexStoreMode = .off,
6667
useExplicitModuleBuild: Bool = false
6768
) -> BuildParameters {
@@ -80,7 +81,7 @@ final class BuildPlanTests: XCTestCase {
8081
}
8182

8283
func mockBuildParameters(environment: BuildEnvironment) -> BuildParameters {
83-
let triple: Triple
84+
let triple: SPMBuildCore.Triple
8485
switch environment.platform {
8586
case .macOS:
8687
triple = Triple.macOS
@@ -221,31 +222,40 @@ final class BuildPlanTests: XCTestCase {
221222
]
222223
)
223224
XCTAssertNoDiagnostics(diagnostics)
224-
let plan = try BuildPlan(
225-
buildParameters: mockBuildParameters(
226-
buildPath: buildDirPath,
227-
config: .release,
228-
toolchain: Resources.default.toolchain,
229-
destinationTriple: Resources.default.toolchain.triple,
230-
useExplicitModuleBuild: true
231-
),
232-
graph: graph,
233-
diagnostics: diagnostics,
234-
fileSystem: fs
235-
)
225+
do {
226+
let plan = try BuildPlan(
227+
buildParameters: mockBuildParameters(
228+
buildPath: buildDirPath,
229+
config: .release,
230+
toolchain: Resources.default.toolchain,
231+
destinationTriple: Resources.default.toolchain.triple,
232+
useExplicitModuleBuild: true
233+
),
234+
graph: graph,
235+
diagnostics: diagnostics,
236+
fileSystem: fs
237+
)
236238

237-
let yaml = buildDirPath.appending(component: "release.yaml")
238-
let llbuild = LLBuildManifestBuilder(plan)
239-
try llbuild.generateManifest(at: yaml)
240-
let contents = try localFileSystem.readFileContents(yaml).description
241239

242-
// A few basic checks
243-
XCTAssertMatch(contents, .contains("-disable-implicit-swift-modules"))
244-
XCTAssertMatch(contents, .contains("-fno-implicit-modules"))
245-
XCTAssertMatch(contents, .contains("-explicit-swift-module-map-file"))
246-
XCTAssertMatch(contents, .contains("A-dependencies.json"))
247-
XCTAssertMatch(contents, .contains("B-dependencies.json"))
248-
XCTAssertMatch(contents, .contains("C-dependencies.json"))
240+
let yaml = buildDirPath.appending(component: "release.yaml")
241+
let llbuild = LLBuildManifestBuilder(plan)
242+
try llbuild.generateManifest(at: yaml)
243+
let contents = try localFileSystem.readFileContents(yaml).description
244+
245+
// A few basic checks
246+
XCTAssertMatch(contents, .contains("-disable-implicit-swift-modules"))
247+
XCTAssertMatch(contents, .contains("-fno-implicit-modules"))
248+
XCTAssertMatch(contents, .contains("-explicit-swift-module-map-file"))
249+
XCTAssertMatch(contents, .contains("A-dependencies.json"))
250+
XCTAssertMatch(contents, .contains("B-dependencies.json"))
251+
XCTAssertMatch(contents, .contains("C-dependencies.json"))
252+
} catch Driver.Error.unableToDecodeFrontendTargetInfo {
253+
// If the toolchain being used is sufficiently old, the integrated driver
254+
// will not be able to parse the `-print-target-info` output. In which case,
255+
// we cannot yet rely on the integrated swift driver.
256+
// This effectively guards the test from running on unupported, older toolchains.
257+
return
258+
}
249259
}
250260
}
251261

@@ -1858,7 +1868,7 @@ final class BuildPlanTests: XCTestCase {
18581868
)
18591869
XCTAssertNoDiagnostics(diagnostics)
18601870

1861-
func createResult(for dest: Triple) throws -> BuildPlanResult {
1871+
func createResult(for dest: SPMBuildCore.Triple) throws -> BuildPlanResult {
18621872
return BuildPlanResult(plan: try BuildPlan(
18631873
buildParameters: mockBuildParameters(destinationTriple: dest),
18641874
graph: graph, diagnostics: diagnostics,
@@ -2302,7 +2312,8 @@ final class BuildPlanTests: XCTestCase {
23022312
])
23032313
}
23042314

2305-
func testBinaryTargets(platform: String, arch: String, destinationTriple: Triple) throws {
2315+
func testBinaryTargets(platform: String, arch: String, destinationTriple: SPMBuildCore.Triple)
2316+
throws {
23062317
let fs = InMemoryFileSystem(emptyFiles:
23072318
"/Pkg/Sources/exe/main.swift",
23082319
"/Pkg/Sources/Library/Library.swift",
@@ -2446,9 +2457,9 @@ final class BuildPlanTests: XCTestCase {
24462457

24472458
func testBinaryTargets() throws {
24482459
try testBinaryTargets(platform: "macos", arch: "x86_64", destinationTriple: .macOS)
2449-
let arm64Triple = try Triple("arm64-apple-macosx")
2460+
let arm64Triple = try SPMBuildCore.Triple("arm64-apple-macosx")
24502461
try testBinaryTargets(platform: "macos", arch: "arm64", destinationTriple: arm64Triple)
2451-
let arm64eTriple = try Triple("arm64e-apple-macosx")
2462+
let arm64eTriple = try SPMBuildCore.Triple("arm64e-apple-macosx")
24522463
try testBinaryTargets(platform: "macos", arch: "arm64e", destinationTriple: arm64eTriple)
24532464
}
24542465

@@ -2468,7 +2479,7 @@ final class BuildPlanTests: XCTestCase {
24682479
try sanitizerTest(.scudo, expectedName: "scudo")
24692480
}
24702481

2471-
private func sanitizerTest(_ sanitizer: Sanitizer, expectedName: String) throws {
2482+
private func sanitizerTest(_ sanitizer: SPMBuildCore.Sanitizer, expectedName: String) throws {
24722483
let fs = InMemoryFileSystem(emptyFiles:
24732484
"/Pkg/Sources/exe/main.swift",
24742485
"/Pkg/Sources/lib/lib.swift",
@@ -2582,7 +2593,7 @@ fileprivate extension TargetBuildDescription {
25822593
}
25832594
}
25842595

2585-
fileprivate extension Triple {
2596+
fileprivate extension SPMBuildCore.Triple {
25862597
static let x86_64Linux = try! Triple("x86_64-unknown-linux-gnu")
25872598
static let arm64Linux = try! Triple("aarch64-unknown-linux-gnu")
25882599
static let arm64Android = try! Triple("aarch64-unknown-linux-android")

0 commit comments

Comments
 (0)