Skip to content

Commit e48fae8

Browse files
committed
Add optimize_wasm build option for Web
1 parent 215acd5 commit e48fae8

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

platform/web/SCsub

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,15 @@ js_wrapped = env.NoCache(
118118
env.Textfile("#bin/godot", [env.File(f) for f in wrap_list], TEXTFILESUFFIX="${PROGSUFFIX}.wrapped.js")
119119
)
120120

121+
main_wasm = build[1]
122+
side_wasm = build[2] if len(build) > 2 else None
123+
124+
if env["optimize_wasm"]:
125+
main_wasm = env.OptimizeWASM(main_wasm)
126+
if side_wasm is not None:
127+
side_wasm = env.OptimizeWASM(side_wasm)
128+
121129
# 0 - unwrapped js file (use wrapped one instead)
122130
# 1 - wasm file
123131
# 2 - wasm side (when dlink is enabled).
124-
env.CreateTemplateZip(js_wrapped, build[1], build[2] if len(build) > 2 else None)
132+
env.CreateTemplateZip(js_wrapped, main_wasm, side_wasm)

platform/web/detect.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
create_template_zip,
1111
get_template_zip_path,
1212
run_closure_compiler,
13+
run_optimize_wasm,
1314
)
1415
from SCons.Util import WhereIs
1516

@@ -57,6 +58,11 @@ def get_opts():
5758
"Use Emscripten PROXY_TO_PTHREAD option to run the main application code to a separate thread",
5859
False,
5960
),
61+
BoolVariable(
62+
"optimize_wasm",
63+
"Use binaryen `wasm-opt` to reduce the output .wasm file sizes",
64+
False,
65+
),
6066
]
6167

6268

@@ -303,3 +309,9 @@ def configure(env: "SConsEnvironment"):
303309
# This workaround creates a closure that prevents the garbage collector from freeing the WebGL context.
304310
# We also only use WebGL2, and changing context version is not widely supported anyway.
305311
env.Append(LINKFLAGS=["-sGL_WORKAROUND_SAFARI_GETCONTEXT_BUG=0"])
312+
313+
# Optimize WASM file
314+
if env["optimize_wasm"]:
315+
optimize_wasm_action = env.Action(run_optimize_wasm)
316+
optimize_wasm_builder = env.Builder(action=optimize_wasm_action, suffix=".opt.wasm", src_suffix=".wasm")
317+
env.Append(BUILDERS={"OptimizeWASM": optimize_wasm_builder})

platform/web/emscripten_helpers.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import json
22
import os
3+
import subprocess
34

4-
from SCons.Util import WhereIs
5+
from SCons.Errors import UserError
6+
from SCons.Util import WhereIs, splitext
57

8+
from misc.utility.color import print_info
69
from platform_methods import get_build_version
710

811

@@ -127,3 +130,19 @@ def add_js_pre(env, js_pre):
127130

128131
def add_js_externs(env, externs):
129132
env.Append(JS_EXTERNS=env.File(externs))
133+
134+
135+
def run_optimize_wasm(target, source, env):
136+
wasmopt_bin_path = WhereIs("wasm-opt")
137+
if wasmopt_bin_path is None:
138+
raise UserError(
139+
'[wasm-opt] Cannot find `wasm-opt` command line utility. That binary is usually packed with the "binaryen" package.'
140+
)
141+
142+
for s in source:
143+
source_path = str(s)
144+
target_path = splitext(source_path)[0] + ".opt.wasm"
145+
146+
if target_path in [str(t) for t in target]:
147+
print_info(f"[wasm-opt] Optimizing {source_path}")
148+
subprocess.run([wasmopt_bin_path, source_path, "-o", target_path, "-all", "--post-emscripten", "-Oz"])

0 commit comments

Comments
 (0)