Skip to content

Commit 3e1e26a

Browse files
committed
Fix linking error on linux
- libstdc++ needs to be added when linking if any dependency has a cpp dependency
1 parent 04b0249 commit 3e1e26a

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

Sources/SwiftBuildSupport/PackagePIFBuilder+Helpers.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,14 @@ extension PackageGraph.ResolvedProduct {
796796
self.modules.anySatisfy { !$0.isBinary }
797797
}
798798

799+
var hasCXXModules: Bool {
800+
let ret = (try? self.recursiveModuleDependencies())?.contains {
801+
guard let clangModule = $0.underlying as? ClangModule else { return false }
802+
return clangModule.isCXX
803+
} ?? false
804+
return ret
805+
}
806+
799807
/// Returns the corresponding *system library* module, if this is a system library product.
800808
var systemModule: SystemLibraryModule? {
801809
guard self.isSystemLibraryProduct else { return nil }

Sources/SwiftBuildSupport/PackagePIFProjectBuilder+Modules.swift

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -495,16 +495,6 @@ extension PackagePIFProjectBuilder {
495495
if enableDuplicateLinkageCulling {
496496
impartedSettings[.LD_WARN_DUPLICATE_LIBRARIES] = "NO"
497497
}
498-
if sourceModule.isCxx {
499-
for platform in ProjectModel.BuildSettings.Platform.allCases {
500-
// darwin & freebsd
501-
if [.macOS, .macCatalyst, .iOS, .watchOS, .tvOS, .xrOS, .driverKit, .freebsd].contains(platform) {
502-
settings[.OTHER_LDFLAGS, platform] = ["-lc++", "$(inherited)"]
503-
} else if [.android, .linux, .wasi, .openbsd].contains(platform) {
504-
settings[.OTHER_LDFLAGS, platform] = ["-lstdc++", "$(inherited)"]
505-
}
506-
}
507-
}
508498
// This should be only for dynamic targets, but that isn't possible today.
509499
// Improvement is tracked by rdar://77403529 (Only impart `PackageFrameworks` search paths to clients of dynamic
510500
// package targets and products).

Sources/SwiftBuildSupport/PackagePIFProjectBuilder+Products.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ extension PackagePIFProjectBuilder {
329329
}
330330
}
331331

332+
var needsCppLib: Bool = false
332333
// Handle the main target's dependencies (and link against them).
333334
mainModule.recursivelyTraverseDependencies { dependency in
334335
switch dependency {
@@ -453,6 +454,8 @@ extension PackagePIFProjectBuilder {
453454
indent: 1,
454455
"Added \(shouldLinkProduct ? "linked " : "")dependency on target '\(dependencyGUID)'"
455456
)
457+
needsCppLib = moduleDependency.sources.containsCXXFiles || needsCppLib
458+
456459
}
457460

458461
case .product(let productDependency, let packageConditions):
@@ -464,6 +467,18 @@ extension PackagePIFProjectBuilder {
464467
targetKeyPath: mainModuleTargetKeyPath,
465468
settings: &settings
466469
)
470+
needsCppLib = (productDependency.hasCXXModules || needsCppLib)
471+
}
472+
}
473+
474+
if needsCppLib {
475+
for platform in ProjectModel.BuildSettings.Platform.allCases {
476+
// darwin & freebsd
477+
if [.macOS, .macCatalyst, .iOS, .watchOS, .tvOS, .xrOS, .driverKit, .freebsd].contains(platform) {
478+
settings[.OTHER_LDFLAGS, platform] = ["-lc++", "$(inherited)"]
479+
} else if [.android, .linux, .wasi, .openbsd].contains(platform) {
480+
settings[.OTHER_LDFLAGS, platform] = ["-lstdc++", "$(inherited)"]
481+
}
467482
}
468483
}
469484

Tests/SwiftBuildSupportTests/PIFBuilderTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ struct PIFBuilderTests {
159159
try await withGeneratedPIF(fromFixture: "PIFBuilder/CCPackage") { pif, observabilitySystem in
160160
let releaseConfig = try pif.workspace
161161
.project(named: "CCPackage")
162-
.target(id: "PACKAGE-TARGET:CCTarget")
162+
.target(id: "PACKAGE-PRODUCT:pifbuilder_ccpackage_executable.executable")
163163
.buildConfig(named: "Release")
164164

165165
for platform in ProjectModel.BuildSettings.Platform.allCases {

0 commit comments

Comments
 (0)