Skip to content

Commit cf82323

Browse files
Add --split-debug option to PackageToJS plugin
1 parent f0f205c commit cf82323

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

Diff for: Plugins/PackageToJS/PackageToJS.swift

+6-2
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,15 @@ struct PackageToJS: CommandPlugin {
2424
struct BuildOptions {
2525
/// Product to build (default: executable target if there's only one)
2626
var product: String?
27+
/// Whether to split debug information into a separate file (default: false)
28+
var splitDebug: Bool
2729
var options: Options
2830

2931
static func parse(from extractor: inout ArgumentExtractor) -> BuildOptions {
3032
let product = extractor.extractOption(named: "product").last
33+
let splitDebug = extractor.extractFlag(named: "split-debug")
3134
let options = Options.parse(from: &extractor)
32-
return BuildOptions(product: product, options: options)
35+
return BuildOptions(product: product, splitDebug: splitDebug != 0, options: options)
3336
}
3437

3538
static func help() -> String {
@@ -43,6 +46,7 @@ struct PackageToJS: CommandPlugin {
4346
--output <path> Path to the output directory (default: .build/plugins/PackageToJS/outputs/Package)
4447
--package-name <name> Name of the package (default: lowercased Package.swift name)
4548
--explain Whether to explain the build plan
49+
--split-debug Whether to split debug information into a separate .wasm.debug file (default: false)
4650
4751
SUBCOMMANDS:
4852
test Builds and runs tests
@@ -202,7 +206,7 @@ struct PackageToJS: CommandPlugin {
202206
options: buildOptions.options, context: context, selfPackage: selfPackage,
203207
outputDir: outputDir)
204208
let rootTask = try planner.planBuild(
205-
make: &make, wasmProductArtifact: productArtifact)
209+
make: &make, splitDebug: buildOptions.splitDebug, wasmProductArtifact: productArtifact)
206210
cleanIfBuildGraphChanged(root: rootTask, make: make, context: context)
207211
print("Packaging...")
208212
try make.build(output: rootTask)

Diff for: Plugins/PackageToJS/PackagingPlanner.swift

+18-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Foundation
22
import PackagePlugin
33

4+
/// Plans the build for packaging.
45
struct PackagingPlanner {
56
let options: PackageToJS.Options
67
let context: PluginContext
@@ -20,6 +21,8 @@ struct PackagingPlanner {
2021
self.selfPath = String(#filePath)
2122
}
2223

24+
// MARK: - Primitive build operations
25+
2326
private static func syncFile(from: String, to: String) throws {
2427
if FileManager.default.fileExists(atPath: to) {
2528
try FileManager.default.removeItem(atPath: to)
@@ -46,19 +49,25 @@ struct PackagingPlanner {
4649
}
4750
}
4851

52+
// MARK: - Build plans
53+
4954
/// Construct the build plan and return the root task key
5055
func planBuild(
5156
make: inout MiniMake,
57+
splitDebug: Bool,
5258
wasmProductArtifact: URL
5359
) throws -> MiniMake.TaskKey {
54-
let (allTasks, _) = try planBuildInternal(make: &make, wasmProductArtifact: wasmProductArtifact)
60+
let (allTasks, _) = try planBuildInternal(
61+
make: &make, splitDebug: splitDebug, wasmProductArtifact: wasmProductArtifact
62+
)
5563
return make.addTask(
5664
inputTasks: allTasks, output: "all", attributes: [.phony, .silent]
5765
) { _ in }
5866
}
5967

6068
private func planBuildInternal(
6169
make: inout MiniMake,
70+
splitDebug: Bool,
6271
wasmProductArtifact: URL
6372
) throws -> (allTasks: [MiniMake.TaskKey], outputDirTask: MiniMake.TaskKey) {
6473
// Prepare output directory
@@ -95,14 +104,16 @@ struct PackagingPlanner {
95104
) {
96105
try Self.createDirectory(atPath: $0.output)
97106
}
98-
let stripWasmPath = tmpDir.appending(path: wasmFilename + ".strip").path
107+
// If splitDebug is true, we need to place the DWARF-stripped wasm file (but "name" section remains)
108+
// in the output directory.
109+
let stripWasmPath = (splitDebug ? outputDir : tmpDir).appending(path: wasmFilename + ".debug").path
99110

100111
// First, strip DWARF sections as their existence enables DWARF preserving mode in wasm-opt
101112
let stripWasm = make.addTask(
102113
inputFiles: [selfPath, wasmProductArtifact.path], inputTasks: [outputDirTask, tmpDirTask],
103114
output: stripWasmPath
104115
) {
105-
print("Stripping debug information...")
116+
print("Stripping DWARF debug info...")
106117
try Self.runCommand(wasmOptPath, [wasmProductArtifact.path, "--strip-dwarf", "--debuginfo", "-o", $0.output])
107118
}
108119
// Then, run wasm-opt with all optimizations
@@ -111,7 +122,7 @@ struct PackagingPlanner {
111122
output: outputDir.appending(path: wasmFilename).path
112123
) {
113124
print("Optimizing the wasm file...")
114-
try Self.runCommand(wasmOptPath, [stripWasmPath, "--debuginfo", "-Os", "-o", $0.output])
125+
try Self.runCommand(wasmOptPath, [stripWasmPath, "-Os", "-o", $0.output])
115126
}
116127
} else {
117128
// Copy the wasm product artifact
@@ -168,7 +179,9 @@ struct PackagingPlanner {
168179
make: inout MiniMake,
169180
wasmProductArtifact: URL
170181
) throws -> (rootTask: MiniMake.TaskKey, binDir: URL) {
171-
var (allTasks, outputDirTask) = try planBuildInternal(make: &make, wasmProductArtifact: wasmProductArtifact)
182+
var (allTasks, outputDirTask) = try planBuildInternal(
183+
make: &make, splitDebug: false, wasmProductArtifact: wasmProductArtifact
184+
)
172185

173186
let binDir = outputDir.appending(path: "bin")
174187
let binDirTask = make.addTask(

0 commit comments

Comments
 (0)