From 64b0d5c1c25128e0e8abdc92976d08243111158c Mon Sep 17 00:00:00 2001 From: Adam Scott Date: Tue, 22 Apr 2025 18:37:16 -0400 Subject: [PATCH] [Web] Add required exported functions and runtime methods for emscripten --- platform/web/SCsub | 5 +++++ platform/web/detect.py | 15 ++++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/platform/web/SCsub b/platform/web/SCsub index 3abaffd5c3c..14f52dca337 100644 --- a/platform/web/SCsub +++ b/platform/web/SCsub @@ -76,6 +76,11 @@ for ext in sys_env["JS_EXTERNS"]: sys_env["ENV"]["EMCC_CLOSURE_ARGS"] += " --externs " + ext.abspath sys_env["ENV"]["EMCC_CLOSURE_ARGS"] = sys_env["ENV"]["EMCC_CLOSURE_ARGS"].strip() +if len(env["EXPORTED_FUNCTIONS"]): + sys_env.Append(LINKFLAGS=["-sEXPORTED_FUNCTIONS=" + repr(sorted(list(set(env["EXPORTED_FUNCTIONS"]))))]) +if len(env["EXPORTED_RUNTIME_METHODS"]): + sys_env.Append(LINKFLAGS=["-sEXPORTED_RUNTIME_METHODS=" + repr(sorted(list(set(env["EXPORTED_RUNTIME_METHODS"]))))]) + build = [] build_targets = ["#bin/godot${PROGSUFFIX}.js", "#bin/godot${PROGSUFFIX}.wasm"] if env["dlink_enabled"]: diff --git a/platform/web/detect.py b/platform/web/detect.py index d02e039ca6e..a26c17d218a 100644 --- a/platform/web/detect.py +++ b/platform/web/detect.py @@ -102,6 +102,9 @@ def library_emitter(target, source, env): def configure(env: "SConsEnvironment"): env.Append(LIBEMITTER=[library_emitter]) + env["EXPORTED_FUNCTIONS"] = ["_main"] + env["EXPORTED_RUNTIME_METHODS"] = [] + # Validate arch. supported_arches = ["wasm32"] validate_arch(env["arch"], get_name(), supported_arches) @@ -249,7 +252,7 @@ def configure(env: "SConsEnvironment"): if not env["dlink_enabled"]: # Workaround https://github.com/emscripten-core/emscripten/issues/21844#issuecomment-2116936414. # Not needed (and potentially dangerous) when dlink_enabled=yes, since we set EXPORT_ALL=1 in that case. - env.Append(LINKFLAGS=["-sEXPORTED_FUNCTIONS=['__emscripten_thread_crashed','_main']"]) + env["EXPORTED_FUNCTIONS"] += ["__emscripten_thread_crashed"] elif env["proxy_to_pthread"]: print_warning('"threads=no" support requires "proxy_to_pthread=no", disabling proxy to pthread.') @@ -276,7 +279,7 @@ def configure(env: "SConsEnvironment"): if env["proxy_to_pthread"]: env.Append(LINKFLAGS=["-sPROXY_TO_PTHREAD=1"]) env.Append(CPPDEFINES=["PROXY_TO_PTHREAD_ENABLED"]) - env.Append(LINKFLAGS=["-sEXPORTED_RUNTIME_METHODS=['_emscripten_proxy_main']"]) + env["EXPORTED_RUNTIME_METHODS"] += ["_emscripten_proxy_main"] # https://github.com/emscripten-core/emscripten/issues/18034#issuecomment-1277561925 env.Append(LINKFLAGS=["-sTEXTDECODER=0"]) @@ -303,7 +306,13 @@ def configure(env: "SConsEnvironment"): env.Append(LINKFLAGS=["-sINVOKE_RUN=0"]) # callMain for manual start, cwrap for the mono version. - env.Append(LINKFLAGS=["-sEXPORTED_RUNTIME_METHODS=['callMain','cwrap']"]) + # Make sure also to have those memory-related functions available. + heap_arrays = [f"HEAP{heap_type}{heap_size}" for heap_size in [8, 16, 32, 64] for heap_type in ["", "U"]] + [ + "HEAPF32", + "HEAPF64", + ] + env["EXPORTED_RUNTIME_METHODS"] += ["callMain", "cwrap"] + heap_arrays + env["EXPORTED_FUNCTIONS"] += ["_malloc", "_free"] # Add code that allow exiting runtime. env.Append(LINKFLAGS=["-sEXIT_RUNTIME=1"])