detect.py 8.8 KB

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