Skip to content

Commit 92ee0c3

Browse files
committed
Add --disable-dead-strip flag
1 parent 441c859 commit 92ee0c3

File tree

5 files changed

+71
-2
lines changed

5 files changed

+71
-2
lines changed

Sources/Build/BuildPlan.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,6 +1201,10 @@ public final class ProductBuildDescription {
12011201
}
12021202

12031203
private var deadStripArguments: [String] {
1204+
if buildParameters.disableDeadStrip {
1205+
return []
1206+
}
1207+
12041208
switch buildParameters.configuration {
12051209
case .debug:
12061210
return []

Sources/Commands/Options.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,9 @@ public struct SwiftToolOptions: ParsableArguments {
375375
@Option(name: .customLong("resolver-fingerprint-checking"))
376376
var resolverFingerprintCheckingMode: FingerprintCheckingMode = .warn
377377

378+
@Flag(name: .customLong("disable-dead-strip"), help: "Disable dead code stripping by the linker")
379+
var disableDeadStrip: Bool = false
380+
378381
@Flag(name: .customLong("netrc"), help: .hidden)
379382
var _deprecated_netrc: Bool = false
380383

Sources/Commands/SwiftTool.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,7 @@ public class SwiftTool {
842842
isXcodeBuildSystemEnabled: options.buildSystem == .xcode,
843843
printManifestGraphviz: options.printManifestGraphviz,
844844
forceTestDiscovery: options.enableTestDiscovery, // backwards compatibility, remove with --enable-test-discovery
845+
disableDeadStrip: options.disableDeadStrip,
845846
isTTY: isTTY
846847
)
847848
})

Sources/SPMBuildCore/BuildParameters.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ public struct BuildParameters: Encodable {
172172
// What strategy to use to discover tests
173173
public var testDiscoveryStrategy: TestDiscoveryStrategy
174174

175+
/// Whether to disable dead code stripping by the linker
176+
public var disableDeadStrip: Bool
177+
175178
public var isTTY: Bool
176179

177180
public init(
@@ -200,6 +203,7 @@ public struct BuildParameters: Encodable {
200203
printManifestGraphviz: Bool = false,
201204
enableTestability: Bool? = nil,
202205
forceTestDiscovery: Bool = false,
206+
disableDeadStrip: Bool = false,
203207
isTTY: Bool = false
204208
) {
205209
let triple = destinationTriple ?? .getHostTriple(usingSwiftCompiler: toolchain.swiftCompiler)
@@ -237,6 +241,7 @@ public struct BuildParameters: Encodable {
237241
self.enableTestability = enableTestability ?? (.debug == configuration)
238242
// decide if to enable the use of test manifests based on platform. this is likely to change in the future
239243
self.testDiscoveryStrategy = triple.isDarwin() ? .objectiveC : .manifest(generate: forceTestDiscovery)
244+
self.disableDeadStrip = disableDeadStrip
240245
self.isTTY = isTTY
241246
}
242247

Tests/BuildTests/BuildPlanTests.swift

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ final class BuildPlanTests: XCTestCase {
6969
canRenameEntrypointFunctionName: Bool = false,
7070
destinationTriple: TSCUtility.Triple = hostTriple,
7171
indexStoreMode: BuildParameters.IndexStoreMode = .off,
72-
useExplicitModuleBuild: Bool = false
72+
useExplicitModuleBuild: Bool = false,
73+
disableDeadStrip: Bool = false
7374
) -> BuildParameters {
7475
return BuildParameters(
7576
dataPath: buildPath,
@@ -82,7 +83,8 @@ final class BuildPlanTests: XCTestCase {
8283
shouldLinkStaticSwiftStdlib: shouldLinkStaticSwiftStdlib,
8384
canRenameEntrypointFunctionName: canRenameEntrypointFunctionName,
8485
indexStoreMode: indexStoreMode,
85-
useExplicitModuleBuild: useExplicitModuleBuild
86+
useExplicitModuleBuild: useExplicitModuleBuild,
87+
disableDeadStrip: disableDeadStrip
8688
)
8789
}
8890

@@ -472,6 +474,60 @@ final class BuildPlanTests: XCTestCase {
472474
"-Xlinker", "-rpath", "-Xlinker", "/fake/path/lib/swift-5.5/macosx",
473475
"-target", defaultTargetTriple,
474476
])
477+
#else
478+
XCTAssertEqual(try result.buildProduct(for: "exe").linkArguments(), [
479+
"/fake/path/to/swiftc", "-g", "-L", "/path/to/build/release",
480+
"-o", "/path/to/build/release/exe", "-module-name", "exe", "-emit-executable",
481+
"-Xlinker", "--gc-sections", "-Xlinker", "-rpath=$ORIGIN",
482+
"@/path/to/build/release/exe.product/Objects.LinkFileList",
483+
"-target", defaultTargetTriple,
484+
])
485+
#endif
486+
}
487+
488+
func testBasicReleasePackageNoDeadStrip() throws {
489+
let fs = InMemoryFileSystem(emptyFiles:
490+
"/Pkg/Sources/exe/main.swift"
491+
)
492+
493+
let observability = ObservabilitySystem.makeForTesting()
494+
let graph = try loadPackageGraph(
495+
fs: fs,
496+
manifests: [
497+
Manifest.createRootManifest(
498+
name: "Pkg",
499+
path: .init("/Pkg"),
500+
targets: [
501+
TargetDescription(name: "exe", dependencies: []),
502+
]),
503+
],
504+
observabilityScope: observability.topScope
505+
)
506+
XCTAssertNoDiagnostics(observability.diagnostics)
507+
508+
let result = try BuildPlanResult(plan: BuildPlan(
509+
buildParameters: mockBuildParameters(config: .release, disableDeadStrip: true),
510+
graph: graph,
511+
fileSystem: fs,
512+
observabilityScope: observability.topScope
513+
))
514+
515+
result.checkProductsCount(1)
516+
result.checkTargetsCount(1)
517+
518+
let exe = try result.target(for: "exe").swiftTarget().compileArguments()
519+
XCTAssertMatch(exe, ["-swift-version", "4", "-O", "-g", .equal(j), "-DSWIFT_PACKAGE", "-module-cache-path", "/path/to/build/release/ModuleCache", .anySequence])
520+
521+
#if os(macOS)
522+
XCTAssertEqual(try result.buildProduct(for: "exe").linkArguments(), [
523+
"/fake/path/to/swiftc", "-g", "-L", "/path/to/build/release",
524+
"-o", "/path/to/build/release/exe", "-module-name", "exe", "-emit-executable",
525+
"-Xlinker", "-rpath", "-Xlinker", "@loader_path",
526+
"@/path/to/build/release/exe.product/Objects.LinkFileList",
527+
"-Xlinker", "-rpath", "-Xlinker", "/fake/path/lib/swift/macosx",
528+
"-Xlinker", "-rpath", "-Xlinker", "/fake/path/lib/swift-5.5/macosx",
529+
"-target", defaultTargetTriple,
530+
])
475531
#else
476532
XCTAssertEqual(try result.buildProduct(for: "exe").linkArguments(), [
477533
"/fake/path/to/swiftc", "-g", "-L", "/path/to/build/release",

0 commit comments

Comments
 (0)