import os
import sys
from pathlib import Path
from typing import TYPE_CHECKING
from emscripten_helpers import (
add_js_externs,
add_js_libraries,
add_js_post,
add_js_pre,
create_engine_file,
create_template_zip,
get_template_zip_path,
run_closure_compiler,
)
from SCons.Util import WhereIs
from methods import get_compiler_version, print_error, print_info, print_warning
from platform_methods import validate_arch
if TYPE_CHECKING:
from SCons.Script.SConscript import SConsEnvironment
def get_name():
return "Web"
def can_build():
return WhereIs("emcc") is not None
def get_tools(env: "SConsEnvironment"):
return ["cc", "c++", "ar", "link", "textfile", "zip"]
def get_opts():
from SCons.Variables import BoolVariable
return [
("initial_memory", "Initial WASM memory (in MiB)", 32),
("stack_size", "WASM stack size (in KiB)", 5120),
("default_pthread_stack_size", "WASM pthread default stack size (in KiB)", 2048),
BoolVariable("use_assertions", "Use Emscripten runtime assertions", False),
BoolVariable("use_ubsan", "Use Emscripten undefined behavior sanitizer (UBSAN)", False),
BoolVariable("use_asan", "Use Emscripten address sanitizer (ASAN)", False),
BoolVariable("use_lsan", "Use Emscripten leak sanitizer (LSAN)", False),
BoolVariable("use_safe_heap", "Use Emscripten SAFE_HEAP sanitizer", False),
BoolVariable("javascript_eval", "Enable JavaScript eval interface", True),
BoolVariable(
"dlink_enabled", "Enable WebAssembly dynamic linking (GDExtension support). Produces bigger binaries", False
),
BoolVariable("use_closure_compiler", "Use closure compiler to minimize JavaScript code", False),
BoolVariable(
"proxy_to_pthread",
"Use Emscripten PROXY_TO_PTHREAD option to run the main application code to a separate thread",
False,
),
BoolVariable("wasm_simd", "Use WebAssembly SIMD to improve CPU performance", True),
]
def get_doc_classes():
return [
"EditorExportPlatformWeb",
]
def get_doc_path():
return "doc_classes"
def get_flags():
return {
"arch": "wasm32",
"target": "template_debug",
"builtin_pcre2_with_jit": False,
"vulkan": False,
"module_raycast_enabled": False,
"optimize": "size",
}
def library_emitter(target, source, env):
env.Depends(source, env.Value(get_compiler_version(env)))
return target, source
def configure(env: "SConsEnvironment"):
env["CC"] = "emcc"
env["CXX"] = "em++"
env["AR"] = "emar"
env["RANLIB"] = "emranlib"
cc_version = get_compiler_version(env)
cc_semver = (cc_version["major"], cc_version["minor"], cc_version["patch"])
if cc_semver < (4, 0, 0):
print_error("The minimum Emscripten version to build Godot is 4.0.0, detected: %s.%s.%s" % cc_semver)
sys.exit(255)
env.Append(LIBEMITTER=[library_emitter])
env["EXPORTED_FUNCTIONS"] = ["_main"]
env["EXPORTED_RUNTIME_METHODS"] = []
supported_arches = ["wasm32"]
validate_arch(env["arch"], get_name(), supported_arches)
try:
env["initial_memory"] = int(env["initial_memory"])
except Exception:
print_error("Initial memory must be a valid integer")
sys.exit(255)
emcc_path = Path(str(WhereIs("emcc")))
while emcc_path.is_symlink():
emcc_path = emcc_path.readlink()
emscripten_include_path = emcc_path.parent.joinpath("cache", "sysroot", "include")
env.Append(CPPPATH=[emscripten_include_path])
if env.debug_features:
env.Append(LINKFLAGS=["--profiling-funcs"])
else:
env["use_assertions"] = True
if env["use_assertions"]:
env.Append(LINKFLAGS=["-sASSERTIONS=1"])
if env.editor_build and env["initial_memory"] < 64:
print_info("Forcing `initial_memory=64` as it is required for the web editor.")
env["initial_memory"] = 64
env.Append(LINKFLAGS=["-sINITIAL_MEMORY=%sMB" % env["initial_memory"]])
env["ENV"] = os.environ
if env["werror"]:
env.Append(LINKFLAGS=["-Wl,--fatal-warnings"])
if env["lto"] == "auto":
env["lto"] = "thin"
if env["lto"] == "thin" and cc_semver < (4, 0, 9):
print_warning(
'"lto=thin" support requires Emscripten 4.0.9 (detected %s.%s.%s), using "lto=full" instead.' % cc_semver
)
env["lto"] = "full"
if env["lto"] != "none":
if env["lto"] == "thin":
env.Append(CCFLAGS=["-flto=thin"])
env.Append(LINKFLAGS=["-flto=thin"])
else:
env.Append(CCFLAGS=["-flto"])
env.Append(LINKFLAGS=["-flto"])
if env["use_ubsan"]:
env.Append(CCFLAGS=["-fsanitize=undefined"])
env.Append(LINKFLAGS=["-fsanitize=undefined"])
if env["use_asan"]:
env.Append(CCFLAGS=["-fsanitize=address"])
env.Append(LINKFLAGS=["-fsanitize=address"])
if env["use_lsan"]:
env.Append(CCFLAGS=["-fsanitize=leak"])
env.Append(LINKFLAGS=["-fsanitize=leak"])
if env["use_safe_heap"]:
env.Append(LINKFLAGS=["-sSAFE_HEAP=1"])
if env["use_closure_compiler"] and cc_semver < (4, 0, 11):
print_warning(
'"use_closure_compiler=yes" support requires Emscripten 4.0.11 (detected %s.%s.%s), using "use_closure_compiler=no" instead.'
% cc_semver
)
env["use_closure_compiler"] = False
if env["use_closure_compiler"]:
env.Append(LINKFLAGS=["--closure", "1"])
jscc = env.Builder(generator=run_closure_compiler, suffix=".cc.js", src_suffix=".js")
env.Append(BUILDERS={"BuildJS": jscc})
env["JS_LIBS"] = []
env["JS_PRE"] = []
env["JS_POST"] = []
env["JS_EXTERNS"] = []
env.AddMethod(add_js_libraries, "AddJSLibraries")
env.AddMethod(add_js_pre, "AddJSPre")
env.AddMethod(add_js_post, "AddJSPost")
env.AddMethod(add_js_externs, "AddJSExterns")
env.AddMethod(create_engine_file, "CreateEngineFile")
env.AddMethod(get_template_zip_path, "GetTemplateZipPath")
env.AddMethod(create_template_zip, "CreateTemplateZip")
env["ARCOM_POSIX"] = env["ARCOM"].replace("$TARGET", "$TARGET.posix").replace("$SOURCES", "$SOURCES.posix")
env["ARCOM"] = "${TEMPFILE('$ARCOM_POSIX','$ARCOMSTR')}"
env["OBJPREFIX"] = ""
env["OBJSUFFIX"] = ".o"
env["PROGPREFIX"] = ""
env["PROGSUFFIX"] = ""
env["LIBPREFIX"] = "lib"
env["LIBSUFFIX"] = ".a"
env["LIBPREFIXES"] = ["$LIBPREFIX"]
env["LIBSUFFIXES"] = ["$LIBSUFFIX"]
env.Prepend(CPPPATH=["#platform/web"])
env.Append(CPPDEFINES=["WEB_ENABLED", "UNIX_ENABLED", "UNIX_SOCKET_UNAVAILABLE"])
if env["opengl3"]:
env.AppendUnique(CPPDEFINES=["GLES3_ENABLED"])
env.Append(LINKFLAGS=["-sMAX_WEBGL_VERSION=2"])
env.Append(LINKFLAGS=["-sOFFSCREEN_FRAMEBUFFER=1"])
env.Append(LINKFLAGS=["-sGL_ENABLE_GET_PROC_ADDRESS=0"])
if env["javascript_eval"]:
env.Append(CPPDEFINES=["JAVASCRIPT_EVAL_ENABLED"])
env.Append(LINKFLAGS=["-s%s=%sKB" % ("STACK_SIZE", env["stack_size"])])
if env["threads"]:
env.Append(CPPDEFINES=["PTHREAD_NO_RENAME"])
env.Append(CCFLAGS=["-sUSE_PTHREADS=1"])
env.Append(LINKFLAGS=["-sUSE_PTHREADS=1"])
env.Append(LINKFLAGS=["-sDEFAULT_PTHREAD_STACK_SIZE=%sKB" % env["default_pthread_stack_size"]])
env.Append(LINKFLAGS=["-sPTHREAD_POOL_SIZE=\"Module['emscriptenPoolSize']||8\""])
env.Append(LINKFLAGS=["-sWASM_MEM_MAX=2048MB"])
if not env["dlink_enabled"]:
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.')
env["proxy_to_pthread"] = False
if env["lto"] != "none":
env.Append(LINKFLAGS=["-Wl,-u,_emscripten_run_callback_on_thread"])
if env["dlink_enabled"]:
if env["proxy_to_pthread"]:
print_warning("GDExtension support requires proxy_to_pthread=no, disabling proxy to pthread.")
env["proxy_to_pthread"] = False
env.Append(CPPDEFINES=["WEB_DLINK_ENABLED"])
env.Append(CCFLAGS=["-sSIDE_MODULE=2"])
env.Append(LINKFLAGS=["-sSIDE_MODULE=2"])
env.Append(CCFLAGS=["-fvisibility=hidden"])
env.Append(LINKFLAGS=["-fvisibility=hidden"])
env.extra_suffix = ".dlink" + env.extra_suffix
env.Append(LINKFLAGS=["-sWASM_BIGINT"])
if env["proxy_to_pthread"]:
env.Append(LINKFLAGS=["-sPROXY_TO_PTHREAD=1"])
env.Append(CPPDEFINES=["PROXY_TO_PTHREAD_ENABLED"])
env["EXPORTED_RUNTIME_METHODS"] += ["_emscripten_proxy_main"]
env.Append(LINKFLAGS=["-sTEXTDECODER=0"])
if env["wasm_simd"]:
env.Append(CCFLAGS=["-msimd128"])
env.Append(LINKFLAGS=["-sENVIRONMENT=web,worker"])
env.Append(LINKFLAGS=["-sMODULARIZE=1", "-sEXPORT_NAME='Godot'"])
env.Append(CCFLAGS=["-sSUPPORT_LONGJMP='wasm'"])
env.Append(LINKFLAGS=["-sSUPPORT_LONGJMP='wasm'"])
env.Append(LINKFLAGS=["-sALLOW_MEMORY_GROWTH=1"])
env.Append(LINKFLAGS=["-sINVOKE_RUN=0"])
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"]
env.Append(LINKFLAGS=["-sEXIT_RUNTIME=1"])
env.Append(LINKFLAGS=["-sGL_WORKAROUND_SAFARI_GETCONTEXT_BUG=0"])
env.Append(CPPDEFINES=["GDSCRIPT_NO_LSP"])