detect.py 8.6 KB

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