Skip to content

Commit cd9716f

Browse files
Apply wasm-opt for release
1 parent 2b6aa1c commit cd9716f

File tree

2 files changed

+72
-15
lines changed

2 files changed

+72
-15
lines changed

Diff for: Plugins/PackageToJS/PackageToJS.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ struct PackageToJS: CommandPlugin {
201201
let planner = PackagingPlanner(
202202
options: buildOptions.options, context: context, selfPackage: selfPackage,
203203
outputDir: outputDir)
204-
let rootTask = planner.planBuild(
204+
let rootTask = try planner.planBuild(
205205
make: &make, wasmProductArtifact: productArtifact)
206206
cleanIfBuildGraphChanged(root: rootTask, make: make, context: context)
207207
print("Packaging...")
@@ -268,7 +268,7 @@ struct PackageToJS: CommandPlugin {
268268
let planner = PackagingPlanner(
269269
options: testOptions.options, context: context, selfPackage: selfPackage,
270270
outputDir: outputDir)
271-
let (rootTask, binDir) = planner.planTestBuild(
271+
let (rootTask, binDir) = try planner.planTestBuild(
272272
make: &make, wasmProductArtifact: productArtifact)
273273
cleanIfBuildGraphChanged(root: rootTask, make: make, context: context)
274274
print("Packaging tests...")
@@ -414,7 +414,7 @@ private func printStderr(_ message: String) {
414414
fputs(message + "\n", stderr)
415415
}
416416

417-
private func which(_ executable: String) throws -> URL {
417+
func which(_ executable: String) throws -> URL {
418418
let pathSeparator: Character
419419
#if os(Windows)
420420
pathSeparator = ";"
@@ -431,7 +431,7 @@ private func which(_ executable: String) throws -> URL {
431431
throw PackageToJSError("Executable \(executable) not found in PATH")
432432
}
433433

434-
private struct PackageToJSError: Swift.Error, CustomStringConvertible {
434+
struct PackageToJSError: Swift.Error, CustomStringConvertible {
435435
let description: String
436436

437437
init(_ message: String) {

Diff for: Plugins/PackageToJS/PackagingPlanner.swift

+68-11
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,24 @@ struct PackagingPlanner {
3434
)
3535
}
3636

37+
private static func runCommand(_ command: URL, _ arguments: [String]) throws {
38+
let task = Process()
39+
task.executableURL = command
40+
task.arguments = arguments
41+
task.currentDirectoryURL = URL(fileURLWithPath: FileManager.default.currentDirectoryPath)
42+
try task.run()
43+
task.waitUntilExit()
44+
guard task.terminationStatus == 0 else {
45+
throw PackageToJSError("Command failed with status \(task.terminationStatus)")
46+
}
47+
}
48+
3749
/// Construct the build plan and return the root task key
3850
func planBuild(
3951
make: inout MiniMake,
4052
wasmProductArtifact: URL
41-
) -> MiniMake.TaskKey {
42-
let (allTasks, _) = planBuildInternal(make: &make, wasmProductArtifact: wasmProductArtifact)
53+
) throws -> MiniMake.TaskKey {
54+
let (allTasks, _) = try planBuildInternal(make: &make, wasmProductArtifact: wasmProductArtifact)
4355
return make.addTask(
4456
inputTasks: allTasks, output: "all", attributes: [.phony, .silent]
4557
) { _ in }
@@ -48,7 +60,7 @@ struct PackagingPlanner {
4860
private func planBuildInternal(
4961
make: inout MiniMake,
5062
wasmProductArtifact: URL
51-
) -> (allTasks: [MiniMake.TaskKey], outputDirTask: MiniMake.TaskKey) {
63+
) throws -> (allTasks: [MiniMake.TaskKey], outputDirTask: MiniMake.TaskKey) {
5264
// Prepare output directory
5365
let outputDirTask = make.addTask(
5466
inputFiles: [selfPath], output: outputDir.path, attributes: [.silent]
@@ -58,12 +70,57 @@ struct PackagingPlanner {
5870

5971
var packageInputs: [MiniMake.TaskKey] = []
6072

61-
// Copy the wasm product artifact
62-
let wasm = make.addTask(
63-
inputFiles: [selfPath, wasmProductArtifact.path], inputTasks: [outputDirTask],
64-
output: outputDir.appending(path: wasmFilename).path
65-
) {
66-
try Self.syncFile(from: wasmProductArtifact.path, to: $0.output)
73+
// Guess the build configuration from the parent directory name of .wasm file
74+
let buildConfiguration = wasmProductArtifact.deletingLastPathComponent().lastPathComponent
75+
let wasm: MiniMake.TaskKey
76+
77+
let shouldOptimize: Bool
78+
let wasmOptPath = try? which("wasm-opt")
79+
if buildConfiguration == "debug" {
80+
shouldOptimize = false
81+
} else {
82+
if wasmOptPath != nil {
83+
shouldOptimize = true
84+
} else {
85+
print("Warning: wasm-opt not found in PATH, skipping optimizations")
86+
shouldOptimize = false
87+
}
88+
}
89+
90+
if let wasmOptPath = wasmOptPath, shouldOptimize {
91+
// Optimize the wasm in release mode
92+
let tmpDir = outputDir.deletingLastPathComponent().appending(path: "\(outputDir.lastPathComponent).tmp")
93+
let tmpDirTask = make.addTask(
94+
inputFiles: [selfPath], output: tmpDir.path, attributes: [.silent]
95+
) {
96+
try Self.createDirectory(atPath: $0.output)
97+
}
98+
let stripWasmPath = tmpDir.appending(path: wasmFilename + ".strip").path
99+
100+
// First, strip DWARF sections as their existence enables DWARF preserving mode in wasm-opt
101+
let stripWasm = make.addTask(
102+
inputFiles: [selfPath, wasmProductArtifact.path], inputTasks: [outputDirTask, tmpDirTask],
103+
output: stripWasmPath
104+
) {
105+
print("Stripping debug information...")
106+
try Self.runCommand(wasmOptPath, [wasmProductArtifact.path, "--strip-dwarf", "--debuginfo", "-o", $0.output])
107+
}
108+
// Then, run wasm-opt with all optimizations
109+
wasm = make.addTask(
110+
inputFiles: [selfPath], inputTasks: [outputDirTask, stripWasm],
111+
output: outputDir.appending(path: wasmFilename).path
112+
) {
113+
print("Optimizing the wasm file...")
114+
try Self.runCommand(wasmOptPath, [stripWasmPath, "--debuginfo", "-Os", "-o", $0.output])
115+
}
116+
} else {
117+
// Copy the wasm product artifact
118+
wasm = make.addTask(
119+
inputFiles: [selfPath, wasmProductArtifact.path], inputTasks: [outputDirTask],
120+
output: outputDir.appending(path: wasmFilename).path
121+
) {
122+
try Self.syncFile(from: wasmProductArtifact.path, to: $0.output)
123+
}
67124
}
68125
packageInputs.append(wasm)
69126

@@ -110,8 +167,8 @@ struct PackagingPlanner {
110167
func planTestBuild(
111168
make: inout MiniMake,
112169
wasmProductArtifact: URL
113-
) -> (rootTask: MiniMake.TaskKey, binDir: URL) {
114-
var (allTasks, outputDirTask) = planBuildInternal(make: &make, wasmProductArtifact: wasmProductArtifact)
170+
) throws -> (rootTask: MiniMake.TaskKey, binDir: URL) {
171+
var (allTasks, outputDirTask) = try planBuildInternal(make: &make, wasmProductArtifact: wasmProductArtifact)
115172

116173
let binDir = outputDir.appending(path: "bin")
117174
let binDirTask = make.addTask(

0 commit comments

Comments
 (0)