detect.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. def is_active():
  14. return True
  15. def get_name():
  16. return "JavaScript"
  17. def can_build():
  18. return WhereIs("emcc") is not None
  19. def get_opts():
  20. from SCons.Variables import BoolVariable
  21. return [
  22. ("initial_memory", "Initial WASM memory (in MiB)", 32),
  23. BoolVariable("use_assertions", "Use Emscripten runtime assertions", False),
  24. BoolVariable("use_ubsan", "Use Emscripten undefined behavior sanitizer (UBSAN)", False),
  25. BoolVariable("use_asan", "Use Emscripten address sanitizer (ASAN)", False),
  26. BoolVariable("use_lsan", "Use Emscripten leak sanitizer (LSAN)", False),
  27. BoolVariable("use_safe_heap", "Use Emscripten SAFE_HEAP sanitizer", False),
  28. # eval() can be a security concern, so it can be disabled.
  29. BoolVariable("javascript_eval", "Enable JavaScript eval interface", True),
  30. BoolVariable("threads_enabled", "Enable WebAssembly Threads support (limited browser support)", False),
  31. BoolVariable("gdnative_enabled", "Enable WebAssembly GDNative support (produces bigger binaries)", False),
  32. BoolVariable("use_closure_compiler", "Use closure compiler to minimize JavaScript code", False),
  33. ]
  34. def get_flags():
  35. return [
  36. ("tools", False),
  37. ("builtin_pcre2_with_jit", False),
  38. ]
  39. def configure(env):
  40. try:
  41. env["initial_memory"] = int(env["initial_memory"])
  42. except Exception:
  43. print("Initial memory must be a valid integer")
  44. sys.exit(255)
  45. ## Build type
  46. if env["target"].startswith("release"):
  47. # Use -Os to prioritize optimizing for reduced file size. This is
  48. # particularly valuable for the web platform because it directly
  49. # decreases download time.
  50. # -Os reduces file size by around 5 MiB over -O3. -Oz only saves about
  51. # 100 KiB over -Os, which does not justify the negative impact on
  52. # run-time performance.
  53. if env["optimize"] != "none":
  54. env.Append(CCFLAGS=["-Os"])
  55. env.Append(LINKFLAGS=["-Os"])
  56. if env["target"] == "release_debug":
  57. # Retain function names for backtraces at the cost of file size.
  58. env.Append(LINKFLAGS=["--profiling-funcs"])
  59. else: # "debug"
  60. env.Append(CCFLAGS=["-O1", "-g"])
  61. env.Append(LINKFLAGS=["-O1", "-g"])
  62. env["use_assertions"] = True
  63. if env["use_assertions"]:
  64. env.Append(LINKFLAGS=["-s", "ASSERTIONS=1"])
  65. if env["tools"]:
  66. if not env["threads_enabled"]:
  67. print('Note: Forcing "threads_enabled=yes" as it is required for the web editor.')
  68. env["threads_enabled"] = "yes"
  69. if env["initial_memory"] < 64:
  70. print('Note: Forcing "initial_memory=64" as it is required for the web editor.')
  71. env["initial_memory"] = 64
  72. else:
  73. # Disable rtti on non-tools (template) builds.
  74. # This helps keep the file size down.
  75. env.Append(CCFLAGS=["-fno-rtti"])
  76. # Don't use dynamic_cast, necessary with no-rtti.
  77. env.Append(CPPDEFINES=["NO_SAFE_CAST"])
  78. env.Append(LINKFLAGS=["-s", "INITIAL_MEMORY=%sMB" % env["initial_memory"]])
  79. ## Copy env variables.
  80. env["ENV"] = os.environ
  81. # LTO
  82. if env["lto"] == "auto": # Full LTO for production.
  83. env["lto"] = "full"
  84. if env["lto"] != "none":
  85. if env["lto"] == "thin":
  86. env.Append(CCFLAGS=["-flto=thin"])
  87. env.Append(LINKFLAGS=["-flto=thin"])
  88. else:
  89. env.Append(CCFLAGS=["-flto"])
  90. env.Append(LINKFLAGS=["-flto"])
  91. # Workaround https://github.com/emscripten-core/emscripten/issues/19781.
  92. cc_semver = tuple(get_compiler_version(env))
  93. if cc_semver >= (3, 1, 42):
  94. env.Append(LINKFLAGS=["-Wl,-u,scalbnf"])
  95. # Sanitizers
  96. if env["use_ubsan"]:
  97. env.Append(CCFLAGS=["-fsanitize=undefined"])
  98. env.Append(LINKFLAGS=["-fsanitize=undefined"])
  99. if env["use_asan"]:
  100. env.Append(CCFLAGS=["-fsanitize=address"])
  101. env.Append(LINKFLAGS=["-fsanitize=address"])
  102. if env["use_lsan"]:
  103. env.Append(CCFLAGS=["-fsanitize=leak"])
  104. env.Append(LINKFLAGS=["-fsanitize=leak"])
  105. if env["use_safe_heap"]:
  106. env.Append(LINKFLAGS=["-s", "SAFE_HEAP=1"])
  107. # Closure compiler
  108. if env["use_closure_compiler"]:
  109. # For emscripten support code.
  110. env.Append(LINKFLAGS=["--closure", "1"])
  111. # Register builder for our Engine files
  112. jscc = env.Builder(generator=run_closure_compiler, suffix=".cc.js", src_suffix=".js")
  113. env.Append(BUILDERS={"BuildJS": jscc})
  114. # Add helper method for adding libraries, externs, pre-js.
  115. env["JS_LIBS"] = []
  116. env["JS_PRE"] = []
  117. env["JS_EXTERNS"] = []
  118. env.AddMethod(add_js_libraries, "AddJSLibraries")
  119. env.AddMethod(add_js_pre, "AddJSPre")
  120. env.AddMethod(add_js_externs, "AddJSExterns")
  121. # Add method that joins/compiles our Engine files.
  122. env.AddMethod(create_engine_file, "CreateEngineFile")
  123. # Add method for creating the final zip file
  124. env.AddMethod(create_template_zip, "CreateTemplateZip")
  125. # Closure compiler extern and support for ecmascript specs (const, let, etc).
  126. env["ENV"]["EMCC_CLOSURE_ARGS"] = "--language_in ECMASCRIPT6"
  127. env["CC"] = "emcc"
  128. env["CXX"] = "em++"
  129. env["AR"] = "emar"
  130. env["RANLIB"] = "emranlib"
  131. # Use TempFileMunge since some AR invocations are too long for cmd.exe.
  132. # Use POSIX-style paths, required with TempFileMunge.
  133. env["ARCOM_POSIX"] = env["ARCOM"].replace("$TARGET", "$TARGET.posix").replace("$SOURCES", "$SOURCES.posix")
  134. env["ARCOM"] = "${TEMPFILE(ARCOM_POSIX)}"
  135. # All intermediate files are just object files.
  136. env["OBJPREFIX"] = ""
  137. env["OBJSUFFIX"] = ".o"
  138. env["PROGPREFIX"] = ""
  139. # Program() output consists of multiple files, so specify suffixes manually at builder.
  140. env["PROGSUFFIX"] = ""
  141. env["LIBPREFIX"] = "lib"
  142. env["LIBSUFFIX"] = ".a"
  143. env["LIBPREFIXES"] = ["$LIBPREFIX"]
  144. env["LIBSUFFIXES"] = ["$LIBSUFFIX"]
  145. env.Prepend(CPPPATH=["#platform/javascript"])
  146. env.Append(CPPDEFINES=["JAVASCRIPT_ENABLED", "UNIX_ENABLED"])
  147. if env["javascript_eval"]:
  148. env.Append(CPPDEFINES=["JAVASCRIPT_EVAL_ENABLED"])
  149. # Thread support (via SharedArrayBuffer).
  150. if env["threads_enabled"]:
  151. env.Append(CPPDEFINES=["PTHREAD_NO_RENAME"])
  152. env.Append(CCFLAGS=["-s", "USE_PTHREADS=1"])
  153. env.Append(LINKFLAGS=["-s", "USE_PTHREADS=1"])
  154. env.Append(LINKFLAGS=["-s", "PTHREAD_POOL_SIZE=8"])
  155. env.Append(LINKFLAGS=["-s", "WASM_MEM_MAX=2048MB"])
  156. env.extra_suffix = ".threads" + env.extra_suffix
  157. else:
  158. env.Append(CPPDEFINES=["NO_THREADS"])
  159. if env["gdnative_enabled"]:
  160. cc_semver = tuple(get_compiler_version(env))
  161. if cc_semver < (2, 0, 10):
  162. print("GDNative support requires emscripten >= 2.0.10, detected: %s.%s.%s" % cc_semver)
  163. sys.exit(255)
  164. if env["threads_enabled"] and cc_semver < (3, 1, 14):
  165. print("Threads and GDNative requires emscripten => 3.1.14, detected: %s.%s.%s" % cc_semver)
  166. sys.exit(255)
  167. env.Append(CCFLAGS=["-s", "RELOCATABLE=1"])
  168. env.Append(LINKFLAGS=["-s", "RELOCATABLE=1"])
  169. # Weak symbols are broken upstream: https://github.com/emscripten-core/emscripten/issues/12819
  170. env.Append(CPPDEFINES=["ZSTD_HAVE_WEAK_SYMBOLS=0"])
  171. env.extra_suffix = ".gdnative" + env.extra_suffix
  172. # Reduce code size by generating less support code (e.g. skip NodeJS support).
  173. env.Append(LINKFLAGS=["-s", "ENVIRONMENT=web,worker"])
  174. # Wrap the JavaScript support code around a closure named Godot.
  175. env.Append(LINKFLAGS=["-s", "MODULARIZE=1", "-s", "EXPORT_NAME='Godot'"])
  176. # Allow increasing memory buffer size during runtime. This is efficient
  177. # when using WebAssembly (in comparison to asm.js) and works well for
  178. # us since we don't know requirements at compile-time.
  179. env.Append(LINKFLAGS=["-s", "ALLOW_MEMORY_GROWTH=1"])
  180. # This setting just makes WebGL 2 APIs available, it does NOT disable WebGL 1.
  181. env.Append(LINKFLAGS=["-s", "USE_WEBGL2=1"])
  182. # Do not call main immediately when the support code is ready.
  183. env.Append(LINKFLAGS=["-s", "INVOKE_RUN=0"])
  184. # Allow use to take control of swapping WebGL buffers.
  185. env.Append(LINKFLAGS=["-s", "OFFSCREEN_FRAMEBUFFER=1"])
  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"])