Skip to content

Commit a0c6b27

Browse files
committed
Cleanup some unnecessary build setting
- fix adding -lc++ to OTHER_LDFLAGS to match native build system. - remove OTHER_LDRFLAGS setting
1 parent d89692b commit a0c6b27

File tree

7 files changed

+71
-11
lines changed

7 files changed

+71
-11
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// swift-tools-version: 6.2
2+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "CCPackage",
7+
products: [
8+
.library(name: "CCTarget", type: .static, targets: ["CCTarget"]),
9+
],
10+
targets: [
11+
.target(name: "CCTarget", ),
12+
.executableTarget(name: "executable", dependencies: ["CCTarget"]),
13+
]
14+
)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
#include <string>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// The Swift Programming Language
2+
// https://docs.swift.org/swift-book
3+
4+
@main
5+
struct Executable {
6+
static func main() {
7+
print("Hello, world!")
8+
}
9+
}

Fixtures/PIFBuilder/UnknownPlatforms/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ let package = Package(
99
name: "UnknownPlatforms",
1010
swiftSettings: [
1111
.define("FOO", .when(platforms: [.custom("DoesNotExist")])),
12-
.define("BAR", .when(platforms: [.linux])),
12+
.define("BAR", .when(platforms: [.linux])),
1313
.define("BAZ", .when(platforms: [.macOS])),
1414
],
1515
),

Sources/SwiftBuildSupport/PackagePIFBuilder.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,6 @@ public final class PackagePIFBuilder {
552552
// We currently deliberately do not support Swift ObjC interface headers.
553553
settings[.SWIFT_INSTALL_OBJC_HEADER] = "NO"
554554
settings[.SWIFT_OBJC_INTERFACE_HEADER_NAME] = ""
555-
settings[.OTHER_LDRFLAGS] = []
556555

557556
// Packages use the SwiftPM workspace's cache directory as a compiler working directory to maximize module
558557
// sharing.

Sources/SwiftBuildSupport/PackagePIFProjectBuilder+Modules.swift

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -488,14 +488,16 @@ extension PackagePIFProjectBuilder {
488488
if enableDuplicateLinkageCulling {
489489
impartedSettings[.LD_WARN_DUPLICATE_LIBRARIES] = "NO"
490490
}
491-
impartedSettings[.OTHER_LDFLAGS] = (sourceModule.isCxx ? ["-lc++"] : []) + ["$(inherited)"]
492-
impartedSettings[.OTHER_LDRFLAGS] = []
493-
log(
494-
.debug,
495-
indent: 1,
496-
"Added '\(impartedSettings[.OTHER_LDFLAGS]!)' to imparted OTHER_LDFLAGS"
497-
)
498-
491+
if sourceModule.isCxx {
492+
for platform in ProjectModel.BuildSettings.Platform.allCases {
493+
// darwin & freebsd
494+
if [.macOS, .macCatalyst, .iOS, .watchOS, .tvOS, .xrOS, .driverKit, .freebsd].contains(platform) {
495+
settings[.OTHER_LDFLAGS, platform] = ["-lc++", "$(inherited)"]
496+
} else if [.android, .linux, .wasi, .openbsd].contains(platform) {
497+
settings[.OTHER_LDFLAGS, platform] = ["-lstdc++", "$(inherited)"]
498+
}
499+
}
500+
}
499501
// This should be only for dynamic targets, but that isn't possible today.
500502
// Improvement is tracked by rdar://77403529 (Only impart `PackageFrameworks` search paths to clients of dynamic
501503
// package targets and products).
@@ -820,7 +822,6 @@ extension PackagePIFProjectBuilder {
820822
impartedSettings[.OTHER_CFLAGS] = ["-fmodule-map-file=\(systemLibrary.modulemapFileAbsolutePath)"] +
821823
pkgConfig.cFlags.prepending("$(inherited)")
822824
impartedSettings[.OTHER_LDFLAGS] = pkgConfig.libs.prepending("$(inherited)")
823-
impartedSettings[.OTHER_LDRFLAGS] = []
824825
impartedSettings[.OTHER_SWIFT_FLAGS] = ["-Xcc"] + impartedSettings[.OTHER_CFLAGS]!
825826
log(.debug, indent: 1, "Added '\(systemLibrary.path.pathString)' to imparted HEADER_SEARCH_PATHS")
826827

Tests/SwiftBuildSupportTests/PIFBuilderTests.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,19 @@ extension SwiftBuildSupport.PIF.Workspace {
8585
}
8686

8787
extension SwiftBuildSupport.PIF.Project {
88+
fileprivate func target(id: String) throws -> ProjectModel.BaseTarget {
89+
let matchingTargets: [ProjectModel.BaseTarget] = underlying.targets.filter {
90+
return $0.common.id.value == String(id)
91+
}
92+
if matchingTargets.isEmpty {
93+
throw StringError("No target named \(id) in PIF project")
94+
} else if matchingTargets.count > 1 {
95+
throw StringError("Multiple target named \(id) in PIF project")
96+
} else {
97+
return matchingTargets[0]
98+
}
99+
}
100+
88101
fileprivate func target(named name: String) throws -> ProjectModel.BaseTarget {
89102
let matchingTargets = underlying.targets.filter {
90103
$0.common.name == name
@@ -135,6 +148,28 @@ struct PIFBuilderTests {
135148
}
136149
}
137150

151+
@Test func platformCCLibrary() async throws {
152+
try await withGeneratedPIF(fromFixture: "PIFBuilder/CCPackage") { pif, observabilitySystem in
153+
let releaseConfig = try pif.workspace
154+
.project(named: "CCPackage")
155+
.target(id: "PACKAGE-TARGET:CCTarget")
156+
.buildConfig(named: "Release")
157+
158+
for platform in ProjectModel.BuildSettings.Platform.allCases {
159+
let ld_flags = releaseConfig.settings[.OTHER_LDFLAGS, platform]
160+
if [.macOS, .macCatalyst, .iOS, .watchOS, .tvOS, .xrOS, .driverKit, .freebsd].contains(platform) {
161+
#expect(ld_flags == ["-lc++", "$(inherited)"], "for platform \(platform)")
162+
} else if [.android, .linux, .wasi, .openbsd].contains(platform) {
163+
#expect(ld_flags == ["-lstdc++", "$(inherited)"], "for platform \(platform)")
164+
} else if [.windows, ._iOSDevice].contains(platform) {
165+
#expect(ld_flags == nil, "for platform \(platform)")
166+
} else {
167+
Issue.record("Unexpected platform \(platform)")
168+
}
169+
}
170+
}
171+
}
172+
138173
@Test func pluginWithBinaryTargetDependency() async throws {
139174
try await withGeneratedPIF(fromFixture: "Miscellaneous/Plugins/BinaryTargetExePlugin") { pif, observabilitySystem in
140175
// Verify that PIF generation succeeds for a package with a plugin that depends on a binary target

0 commit comments

Comments
 (0)