detect.py 8.6 KB

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