Skip to content
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

[cxx-interop] Do not pass -lc++/-lstdc++ flags to the compiler #8160

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
21 changes: 0 additions & 21 deletions Sources/Build/BuildPlan/BuildPlan+Product.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,6 @@ extension BuildPlan {
}
}

// Don't link libc++ or libstd++ when building for Embedded Swift.
// Users can still link it manually for embedded platforms when needed,
// by providing `-Xlinker -lc++` options via CLI or `Package.swift`.
if !buildProduct.product.modules.contains(where: \.underlying.isEmbeddedSwiftTarget) {
// Link C++ if needed.
// Note: This will come from build settings in future.
for description in dependencies.staticTargets {
if case let target as ClangModule = description.module.underlying, target.isCXX {
let triple = buildProduct.buildParameters.triple
if triple.isDarwin() {
buildProduct.additionalFlags += ["-lc++"]
} else if triple.isWindows() {
// Don't link any C++ library.
} else {
buildProduct.additionalFlags += ["-lstdc++"]
}
break
}
}
}

for description in dependencies.staticTargets {
switch description.module.underlying {
case is SwiftModule:
Expand Down
15 changes: 0 additions & 15 deletions Tests/BuildTests/BuildPlanTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1657,7 +1657,6 @@ final class BuildPlanTests: XCTestCase {
#if os(macOS)
XCTAssertEqual(try result.buildProduct(for: "exe").linkArguments(), [
result.plan.destinationBuildParameters.toolchain.swiftCompilerPath.pathString,
"-lc++",
"-L", buildPath.pathString,
"-o", buildPath.appending(components: "exe").pathString,
"-module-name", "exe",
Expand All @@ -1684,7 +1683,6 @@ final class BuildPlanTests: XCTestCase {
#else
XCTAssertEqual(try result.buildProduct(for: "exe").linkArguments(), [
result.plan.destinationBuildParameters.toolchain.swiftCompilerPath.pathString,
"-lstdc++",
"-L", buildPath.pathString,
"-o", buildPath.appending(components: "exe").pathString,
"-module-name", "exe",
Expand Down Expand Up @@ -2900,15 +2898,7 @@ final class BuildPlanTests: XCTestCase {
))
result.checkProductsCount(1)
result.checkTargetsCount(2)
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 await BuildPlanResult(plan: mockBuildPlan(
triple: .arm64Linux,
graph: graph,
Expand All @@ -2917,9 +2907,6 @@ final class BuildPlanTests: XCTestCase {
))
result.checkProductsCount(1)
result.checkTargetsCount(2)
linkArgs = try result.buildProduct(for: "exe").linkArguments()

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

func testDynamicProducts() async throws {
Expand Down Expand Up @@ -3282,7 +3269,6 @@ final class BuildPlanTests: XCTestCase {
#if os(macOS)
XCTAssertEqual(try result.buildProduct(for: "lib").linkArguments(), [
result.plan.destinationBuildParameters.toolchain.swiftCompilerPath.pathString,
"-lc++",
"-L", buildPath.pathString,
"-o", buildPath.appending(components: "liblib.dylib").pathString,
"-module-name", "lib",
Expand Down Expand Up @@ -3336,7 +3322,6 @@ final class BuildPlanTests: XCTestCase {
#else
XCTAssertEqual(try result.buildProduct(for: "lib").linkArguments(), [
result.plan.destinationBuildParameters.toolchain.swiftCompilerPath.pathString,
"-lstdc++",
"-L", buildPath.pathString,
"-o", buildPath.appending(components: "liblib.so").pathString,
"-module-name", "lib",
Expand Down