Skip to content

BuildPlan: infer -lc++ based on run-time triple #6581

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Sources/Build/BuildPlan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -632,9 +632,9 @@ public class BuildPlan: SPMBuildCore.BuildPlan {
// Note: This will come from build settings in future.
for target in dependencies.staticTargets {
if case let target as ClangTarget = target.underlyingTarget, target.isCXX {
if buildParameters.hostTriple.isDarwin() {
if buildParameters.triple.isDarwin() {
buildProduct.additionalFlags += ["-lc++"]
} else if buildParameters.hostTriple.isWindows() {
} else if buildParameters.triple.isWindows() {
// Don't link any C++ library.
} else {
buildProduct.additionalFlags += ["-lstdc++"]
Expand Down
17 changes: 15 additions & 2 deletions Tests/BuildTests/BuildPlanTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2174,21 +2174,34 @@ final class BuildPlanTests: XCTestCase {
)
XCTAssertNoDiagnostics(observability.diagnostics)

let result = try BuildPlanResult(plan: BuildPlan(
var result = try BuildPlanResult(plan: BuildPlan(
buildParameters: mockBuildParameters(),
graph: graph,
fileSystem: fs,
observabilityScope: observability.topScope
))
result.checkProductsCount(1)
result.checkTargetsCount(2)
let linkArgs = try result.buildProduct(for: "exe").linkArguments()
var linkArgs = try result.buildProduct(for: "exe").linkArguments()

#if os(macOS)
XCTAssertMatch(linkArgs, ["-lc++"])
#elseif !os(Windows)
XCTAssertMatch(linkArgs, ["-lstdc++"])
#endif

// Verify that `-lstdc++` is passed instead of `-lc++` when cross-compiling to Linux.
result = try BuildPlanResult(plan: BuildPlan(
buildParameters: mockBuildParameters(destinationTriple: .arm64Linux),
graph: graph,
fileSystem: fs,
observabilityScope: observability.topScope
))
result.checkProductsCount(1)
result.checkTargetsCount(2)
linkArgs = try result.buildProduct(for: "exe").linkArguments()

XCTAssertMatch(linkArgs, ["-lstdc++"])
}

func testDynamicProducts() throws {
Expand Down
1 change: 1 addition & 0 deletions Tests/BuildTests/MockBuildTestHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ struct MockToolchain: PackageModel.Toolchain {


extension Basics.Triple {
static let x86_64MacOS = try! Self("x86_64-apple-macosx")
static let x86_64Linux = try! Self("x86_64-unknown-linux-gnu")
static let arm64Linux = try! Self("aarch64-unknown-linux-gnu")
static let arm64Android = try! Self("aarch64-unknown-linux-android")
Expand Down