detect.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. import os
  2. import sys
  3. from emscripten_helpers import (
  4. run_closure_compiler,
  5. create_engine_file,
  6. add_js_libraries,
  7. add_js_pre,
  8. add_js_externs,
  9. create_template_zip,
  10. )
  11. from methods import get_compiler_version
  12. from SCons.Util import WhereIs
  13. from typing import TYPE_CHECKING
  14. if TYPE_CHECKING:
  15. from SCons import Environment
  16. def get_name():
  17. return "Web"
  18. def can_build():
  19. return WhereIs("emcc") is not None
  20. def get_opts():
  21. from SCons.Variables import BoolVariable
  22. return [
  23. ("initial_memory", "Initial WASM memory (in MiB)", 32),
  24. BoolVariable("use_assertions", "Use Emscripten runtime assertions", False),
  25. BoolVariable("use_ubsan", "Use Emscripten undefined behavior sanitizer (UBSAN)", False),
  26. BoolVariable("use_asan", "Use Emscripten address sanitizer (ASAN)", False),
  27. BoolVariable("use_lsan", "Use Emscripten leak sanitizer (LSAN)", False),
  28. BoolVariable("use_safe_heap", "Use Emscripten SAFE_HEAP sanitizer", False),
  29. # eval() can be a security concern, so it can be disabled.
  30. BoolVariable("javascript_eval", "Enable JavaScript eval interface", True),
  31. BoolVariable(
  32. "dlink_enabled", "Enable WebAssembly dynamic linking (GDExtension support). Produces bigger binaries", False
  33. ),
  34. BoolVariable("use_closure_compiler", "Use closure compiler to minimize JavaScript code", False),
  35. ]
  36. def get_doc_classes():
  37. return [
  38. "EditorExportPlatformWeb",
  39. ]
  40. def get_doc_path():
  41. return "doc_classes"
  42. def get_flags():
  43. return [
  44. ("arch", "wasm32"),
  45. ("target", "template_debug"),
  46. ("builtin_pcre2_with_jit", False),
  47. ("vulkan", False),
  48. # Embree is heavy and requires too much memory (GH-70621).
  49. ("module_raycast_enabled", False),
  50. # Use -Os to prioritize optimizing for reduced file size. This is
  51. # particularly valuable for the web platform because it directly
  52. # decreases download time.
  53. # -Os reduces file size by around 5 MiB over -O3. -Oz only saves about
  54. # 100 KiB over -Os, which does not justify the negative impact on
  55. # run-time performance.
  56. ("optimize", "size"),
  57. ]
  58. def configure(env: "Environment"):
  59. # Validate arch.
  60. supported_arches = ["wasm32"]
  61. if env["arch"] not in supported_arches:
  62. print(
  63. 'Unsupported CPU architecture "%s" for iOS. Supported architectures are: %s.'
  64. % (env["arch"], ", ".join(supported_arches))
  65. )
  66. sys.exit()
  67. try:
  68. env["initial_memory"] = int(env["initial_memory"])
  69. except Exception:
  70. print("Initial memory must be a valid integer")
  71. sys.exit(255)
  72. ## Build type
  73. if env.debug_features:
  74. # Retain function names for backtraces at the cost of file size.
  75. env.Append(LINKFLAGS=["--profiling-funcs"])
  76. else:
  77. env["use_assertions"] = True
  78. if env["use_assertions"]:
  79. env.Append(LINKFLAGS=["-s", "ASSERTIONS=1"])
  80. if env.editor_build and env["initial_memory"] < 64:
  81. print('Note: Forcing "initial_memory=64" as it is required for the web editor.')
  82. env["initial_memory"] = 64
  83. env.Append(LINKFLAGS=["-s", "INITIAL_MEMORY=%sMB" % env["initial_memory"]])
  84. ## Copy env variables.
  85. env["ENV"] = os.environ
  86. # LTO
  87. if env["lto"] == "auto": # Full LTO for production.
  88. env["lto"] = "full"
  89. if env["lto"] != "none":
  90. if env["lto"] == "thin":
  91. env.Append(CCFLAGS=["-flto=thin"])
  92. env.Append(LINKFLAGS=["-flto=thin"])
  93. else:
  94. env.Append(CCFLAGS=["-flto"])
  95. env.Append(LINKFLAGS=["-flto"])
  96. # Sanitizers
  97. if env["use_ubsan"]:
  98. env.Append(CCFLAGS=["-fsanitize=undefined"])
  99. env.Append(LINKFLAGS=["-fsanitize=undefined"])
  100. if env["use_asan"]:
  101. env.Append(CCFLAGS=["-fsanitize=address"])
  102. env.Append(LINKFLAGS=["-fsanitize=address"])
  103. if env["use_lsan"]:
  104. env.Append(CCFLAGS=["-fsanitize=leak"])
  105. env.Append(LINKFLAGS=["-fsanitize=leak"])
  106. if env["use_safe_heap"]:
  107. env.Append(LINKFLAGS=["-s", "SAFE_HEAP=1"])
  108. # Closure compiler
  109. if env["use_closure_compiler"]:
  110. # For emscripten support code.
  111. env.Append(LINKFLAGS=["--closure", "1"])
  112. # Register builder for our Engine files
  113. jscc = env.Builder(generator=run_closure_compiler, suffix=".cc.js", src_suffix=".js")
  114. env.Append(BUILDERS={"BuildJS": jscc})
  115. # Add helper method for adding libraries, externs, pre-js.
  116. env["JS_LIBS"] = []
  117. env["JS_PRE"] = []
  118. env["JS_EXTERNS"] = []
  119. env.AddMethod(add_js_libraries, "AddJSLibraries")
  120. env.AddMethod(add_js_pre, "AddJSPre")
  121. env.AddMethod(add_js_externs, "AddJSExterns")
  122. # Add method that joins/compiles our Engine files.
  123. env.AddMethod(create_engine_file, "CreateEngineFile")
  124. # Add method for creating the final zip file
  125. env.AddMethod(create_template_zip, "CreateTemplateZip")
  126. # Closure compiler extern and support for ecmascript specs (const, let, etc).
  127. env["ENV"]["EMCC_CLOSURE_ARGS"] = "--language_in ECMASCRIPT6"
  128. env["CC"] = "emcc"
  129. env["CXX"] = "em++"
  130. env["AR"] = "emar"
  131. env["RANLIB"] = "emranlib"
  132. # Use TempFileMunge since some AR invocations are too long for cmd.exe.
  133. # Use POSIX-style paths, required with TempFileMunge.
  134. env["ARCOM_POSIX"] = env["ARCOM"].replace("$TARGET", "$TARGET.posix").replace("$SOURCES", "$SOURCES.posix")
  135. env["ARCOM"] = "${TEMPFILE(ARCOM_POSIX)}"
  136. # All intermediate files are just object files.
  137. env["OBJPREFIX"] = ""
  138. env["OBJSUFFIX"] = ".o"
  139. env["PROGPREFIX"] = ""
  140. # Program() output consists of multiple files, so specify suffixes manually at builder.
  141. env["PROGSUFFIX"] = ""
  142. env["LIBPREFIX"] = "lib"
  143. env["LIBSUFFIX"] = ".a"
  144. env["LIBPREFIXES"] = ["$LIBPREFIX"]
  145. env["LIBSUFFIXES"] = ["$LIBSUFFIX"]
  146. env.Prepend(CPPPATH=["#platform/web"])
  147. env.Append(CPPDEFINES=["WEB_ENABLED", "UNIX_ENABLED"])
  148. if env["opengl3"]:
  149. env.AppendUnique(CPPDEFINES=["GLES3_ENABLED"])
  150. # This setting just makes WebGL 2 APIs available, it does NOT disable WebGL 1.
  151. env.Append(LINKFLAGS=["-s", "USE_WEBGL2=1"])
  152. # Allow use to take control of swapping WebGL buffers.
  153. env.Append(LINKFLAGS=["-s", "OFFSCREEN_FRAMEBUFFER=1"])
  154. if env["javascript_eval"]:
  155. env.Append(CPPDEFINES=["JAVASCRIPT_EVAL_ENABLED"])
  156. # Thread support (via SharedArrayBuffer).
  157. env.Append(CPPDEFINES=["PTHREAD_NO_RENAME"])
  158. env.Append(CCFLAGS=["-s", "USE_PTHREADS=1"])
  159. env.Append(LINKFLAGS=["-s", "USE_PTHREADS=1"])
  160. env.Append(LINKFLAGS=["-s", "PTHREAD_POOL_SIZE=8"])
  161. env.Append(LINKFLAGS=["-s", "WASM_MEM_MAX=2048MB"])
  162. # Get version info for checks below.
  163. cc_version = get_compiler_version(env)
  164. cc_semver = (int(cc_version["major"]), int(cc_version["minor"]), int(cc_version["patch"]))
  165. if env["lto"] != "none":
  166. # Workaround https://github.com/emscripten-core/emscripten/issues/19781.
  167. if cc_semver >= (3, 1, 42) and cc_semver < (3, 1, 46):
  168. env.Append(LINKFLAGS=["-Wl,-u,scalbnf"])
  169. if env["dlink_enabled"]:
  170. if cc_semver < (3, 1, 14):
  171. print("GDExtension support requires emscripten >= 3.1.14, detected: %s.%s.%s" % cc_semver)
  172. sys.exit(255)
  173. env.Append(CCFLAGS=["-s", "SIDE_MODULE=2"])
  174. env.Append(LINKFLAGS=["-s", "SIDE_MODULE=2"])
  175. env.Append(CCFLAGS=["-fvisibility=hidden"])
  176. env.Append(LINKFLAGS=["-fvisibility=hidden"])
  177. env.extra_suffix = ".dlink" + env.extra_suffix
  178. # Reduce code size by generating less support code (e.g. skip NodeJS support).
  179. env.Append(LINKFLAGS=["-s", "ENVIRONMENT=web,worker"])
  180. # Wrap the JavaScript support code around a closure named Godot.
  181. env.Append(LINKFLAGS=["-s", "MODULARIZE=1", "-s", "EXPORT_NAME='Godot'"])
  182. # Allow increasing memory buffer size during runtime. This is efficient
  183. # when using WebAssembly (in comparison to asm.js) and works well for
  184. # us since we don't know requirements at compile-time.
  185. env.Append(LINKFLAGS=["-s", "ALLOW_MEMORY_GROWTH=1"])
  186. # Do not call main immediately when the support code is ready.
  187. env.Append(LINKFLAGS=["-s", "INVOKE_RUN=0"])
  188. # callMain for manual start, cwrap for the mono version.
  189. env.Append(LINKFLAGS=["-s", "EXPORTED_RUNTIME_METHODS=['callMain','cwrap']"])
  190. # Add code that allow exiting runtime.
  191. env.Append(LINKFLAGS=["-s", "EXIT_RUNTIME=1"])
  192. # This workaround creates a closure that prevents the garbage collector from freeing the WebGL context.
  193. # We also only use WebGL2, and changing context version is not widely supported anyway.
  194. env.Append(LINKFLAGS=["-s", "GL_WORKAROUND_SAFARI_GETCONTEXT_BUG=0"])