@@ -14,11 +14,22 @@ struct PackageToJS {
1414 var enableCodeCoverage : Bool = false
1515 }
1616
17+ enum DebugInfoFormat : String , CaseIterable {
18+ /// No debug info
19+ case none
20+ /// The all DWARF sections and "name" section
21+ case dwarf
22+ /// Only "name" section
23+ case name
24+ }
25+
1726 struct BuildOptions {
1827 /// Product to build (default: executable target if there's only one)
1928 var product : String ?
2029 /// Whether to apply wasm-opt optimizations in release mode (default: true)
2130 var noOptimize : Bool
31+ /// The format of debug info to keep in the final wasm file (default: none)
32+ var debugInfoFormat : DebugInfoFormat
2233 /// The options for packaging
2334 var packageOptions : PackageOptions
2435 }
@@ -388,7 +399,7 @@ struct PackagingPlanner {
388399 buildOptions: PackageToJS . BuildOptions
389400 ) throws -> MiniMake . TaskKey {
390401 let ( allTasks, _, _, _) = try planBuildInternal (
391- make: & make, noOptimize: buildOptions. noOptimize
402+ make: & make, noOptimize: buildOptions. noOptimize, debugInfoFormat : buildOptions . debugInfoFormat
392403 )
393404 return make. addTask (
394405 inputTasks: allTasks, output: BuildPath ( phony: " all " ) , attributes: [ . phony, . silent]
@@ -397,7 +408,8 @@ struct PackagingPlanner {
397408
398409 private func planBuildInternal(
399410 make: inout MiniMake ,
400- noOptimize: Bool
411+ noOptimize: Bool ,
412+ debugInfoFormat: PackageToJS . DebugInfoFormat
401413 ) throws -> (
402414 allTasks: [ MiniMake . TaskKey ] ,
403415 outputDirTask: MiniMake . TaskKey ,
@@ -432,24 +444,32 @@ struct PackagingPlanner {
432444 let finalWasmPath = outputDir. appending ( path: wasmFilename)
433445
434446 if shouldOptimize {
435- // Optimize the wasm in release mode
436- let wasmWithoutDwarfPath = intermediatesDir. appending ( path: wasmFilename + " .no-dwarf " )
437-
438- // First, strip DWARF sections as their existence enables DWARF preserving mode in wasm-opt
439- let wasmWithoutDwarf = make. addTask (
440- inputFiles: [ selfPath, wasmProductArtifact] , inputTasks: [ outputDirTask, intermediatesDirTask] ,
441- output: wasmWithoutDwarfPath
442- ) {
443- print ( " Stripping DWARF debug info... " )
444- try system. wasmOpt ( [ " --strip-dwarf " , " --debuginfo " ] , input: $1. resolve ( path: wasmProductArtifact) . path, output: $1. resolve ( path: $0. output) . path)
447+ let wasmOptInputFile : BuildPath
448+ let wasmOptInputTask : MiniMake . TaskKey ?
449+ switch debugInfoFormat {
450+ case . dwarf:
451+ // Keep the original wasm file
452+ wasmOptInputFile = wasmProductArtifact
453+ wasmOptInputTask = nil
454+ case . name, . none:
455+ // Optimize the wasm in release mode
456+ wasmOptInputFile = intermediatesDir. appending ( path: wasmFilename + " .no-dwarf " )
457+ // First, strip DWARF sections as their existence enables DWARF preserving mode in wasm-opt
458+ wasmOptInputTask = make. addTask (
459+ inputFiles: [ selfPath, wasmProductArtifact] , inputTasks: [ outputDirTask, intermediatesDirTask] ,
460+ output: wasmOptInputFile
461+ ) {
462+ print ( " Stripping DWARF debug info... " )
463+ try system. wasmOpt ( [ " --strip-dwarf " , " --debuginfo " ] , input: $1. resolve ( path: wasmProductArtifact) . path, output: $1. resolve ( path: $0. output) . path)
464+ }
445465 }
446466 // Then, run wasm-opt with all optimizations
447467 wasm = make. addTask (
448- inputFiles: [ selfPath, wasmWithoutDwarfPath ] , inputTasks: [ outputDirTask, wasmWithoutDwarf ] ,
468+ inputFiles: [ selfPath, wasmOptInputFile ] , inputTasks: [ outputDirTask] + ( wasmOptInputTask . map { [ $0 ] } ?? [ ] ) ,
449469 output: finalWasmPath
450470 ) {
451471 print ( " Optimizing the wasm file... " )
452- try system. wasmOpt ( [ " -Os " , " --debuginfo " ] , input: $1. resolve ( path: wasmWithoutDwarfPath ) . path, output: $1. resolve ( path: $0. output) . path)
472+ try system. wasmOpt ( [ " -Os " ] + ( debugInfoFormat != . none ? [ " --debuginfo " ] : [ ] ) , input: $1. resolve ( path: wasmOptInputFile ) . path, output: $1. resolve ( path: $0. output) . path)
453473 }
454474 } else {
455475 // Copy the wasm product artifact
@@ -518,7 +538,7 @@ struct PackagingPlanner {
518538 make: inout MiniMake
519539 ) throws -> ( rootTask: MiniMake . TaskKey , binDir: BuildPath ) {
520540 var ( allTasks, outputDirTask, intermediatesDirTask, packageJsonTask) = try planBuildInternal (
521- make: & make, noOptimize: false
541+ make: & make, noOptimize: false , debugInfoFormat : . dwarf
522542 )
523543
524544 // Install npm dependencies used in the test harness
0 commit comments