1
1
import Foundation
2
2
import PackagePlugin
3
3
4
+ /// Plans the build for packaging.
4
5
struct PackagingPlanner {
5
6
let options : PackageToJS . Options
6
7
let context : PluginContext
@@ -20,6 +21,8 @@ struct PackagingPlanner {
20
21
self . selfPath = String ( #filePath)
21
22
}
22
23
24
+ // MARK: - Primitive build operations
25
+
23
26
private static func syncFile( from: String , to: String ) throws {
24
27
if FileManager . default. fileExists ( atPath: to) {
25
28
try FileManager . default. removeItem ( atPath: to)
@@ -46,19 +49,25 @@ struct PackagingPlanner {
46
49
}
47
50
}
48
51
52
+ // MARK: - Build plans
53
+
49
54
/// Construct the build plan and return the root task key
50
55
func planBuild(
51
56
make: inout MiniMake ,
57
+ splitDebug: Bool ,
52
58
wasmProductArtifact: URL
53
59
) 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
+ )
55
63
return make. addTask (
56
64
inputTasks: allTasks, output: " all " , attributes: [ . phony, . silent]
57
65
) { _ in }
58
66
}
59
67
60
68
private func planBuildInternal(
61
69
make: inout MiniMake ,
70
+ splitDebug: Bool ,
62
71
wasmProductArtifact: URL
63
72
) throws -> ( allTasks: [ MiniMake . TaskKey ] , outputDirTask: MiniMake . TaskKey ) {
64
73
// Prepare output directory
@@ -95,14 +104,16 @@ struct PackagingPlanner {
95
104
) {
96
105
try Self . createDirectory ( atPath: $0. output)
97
106
}
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
99
110
100
111
// First, strip DWARF sections as their existence enables DWARF preserving mode in wasm-opt
101
112
let stripWasm = make. addTask (
102
113
inputFiles: [ selfPath, wasmProductArtifact. path] , inputTasks: [ outputDirTask, tmpDirTask] ,
103
114
output: stripWasmPath
104
115
) {
105
- print ( " Stripping debug information ... " )
116
+ print ( " Stripping DWARF debug info ... " )
106
117
try Self . runCommand ( wasmOptPath, [ wasmProductArtifact. path, " --strip-dwarf " , " --debuginfo " , " -o " , $0. output] )
107
118
}
108
119
// Then, run wasm-opt with all optimizations
@@ -111,7 +122,7 @@ struct PackagingPlanner {
111
122
output: outputDir. appending ( path: wasmFilename) . path
112
123
) {
113
124
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] )
115
126
}
116
127
} else {
117
128
// Copy the wasm product artifact
@@ -168,7 +179,9 @@ struct PackagingPlanner {
168
179
make: inout MiniMake ,
169
180
wasmProductArtifact: URL
170
181
) 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
+ )
172
185
173
186
let binDir = outputDir. appending ( path: " bin " )
174
187
let binDirTask = make. addTask (
0 commit comments