11import Foundation
22import PackagePlugin
33
4+ /// Plans the build for packaging.
45struct 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