detect.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import os
  2. from emscripten_helpers import run_closure_compiler, create_engine_file, add_js_libraries
  3. from SCons.Util import WhereIs
  4. def is_active():
  5. return True
  6. def get_name():
  7. return "JavaScript"
  8. def can_build():
  9. return WhereIs("emcc") is not None
  10. def get_opts():
  11. from SCons.Variables import BoolVariable
  12. return [
  13. # eval() can be a security concern, so it can be disabled.
  14. BoolVariable("javascript_eval", "Enable JavaScript eval interface", True),
  15. BoolVariable("threads_enabled", "Enable WebAssembly Threads support (limited browser support)", False),
  16. BoolVariable("gdnative_enabled", "Enable WebAssembly GDNative support (produces bigger binaries)", False),
  17. BoolVariable("use_closure_compiler", "Use closure compiler to minimize JavaScript code", False),
  18. ]
  19. def get_flags():
  20. return [
  21. ("tools", False),
  22. ("builtin_pcre2_with_jit", False),
  23. # Disabling the mbedtls module reduces file size.
  24. # The module has little use due to the limited networking functionality
  25. # in this platform. For the available networking methods, the browser
  26. # manages TLS.
  27. ("module_mbedtls_enabled", False),
  28. ]
  29. def configure(env):
  30. ## Build type
  31. if env["target"] == "release":
  32. # Use -Os to prioritize optimizing for reduced file size. This is
  33. # particularly valuable for the web platform because it directly
  34. # decreases download time.
  35. # -Os reduces file size by around 5 MiB over -O3. -Oz only saves about
  36. # 100 KiB over -Os, which does not justify the negative impact on
  37. # run-time performance.
  38. env.Append(CCFLAGS=["-Os"])
  39. env.Append(LINKFLAGS=["-Os"])
  40. elif env["target"] == "release_debug":
  41. env.Append(CCFLAGS=["-Os"])
  42. env.Append(LINKFLAGS=["-Os"])
  43. env.Append(CPPDEFINES=["DEBUG_ENABLED"])
  44. # Retain function names for backtraces at the cost of file size.
  45. env.Append(LINKFLAGS=["--profiling-funcs"])
  46. else: # "debug"
  47. env.Append(CPPDEFINES=["DEBUG_ENABLED"])
  48. env.Append(CCFLAGS=["-O1", "-g"])
  49. env.Append(LINKFLAGS=["-O1", "-g"])
  50. env.Append(LINKFLAGS=["-s", "ASSERTIONS=1"])
  51. if env["tools"]:
  52. if not env["threads_enabled"]:
  53. raise RuntimeError(
  54. "Threads must be enabled to build the editor. Please add the 'threads_enabled=yes' option"
  55. )
  56. # Tools need more memory. Initial stack memory in bytes. See `src/settings.js` in emscripten repository (will be renamed to INITIAL_MEMORY).
  57. env.Append(LINKFLAGS=["-s", "TOTAL_MEMORY=33554432"])
  58. elif env["builtin_icu"]:
  59. env.Append(CCFLAGS=["-frtti"])
  60. else:
  61. # Disable exceptions and rtti on non-tools (template) builds
  62. # These flags help keep the file size down.
  63. env.Append(CCFLAGS=["-fno-exceptions", "-fno-rtti"])
  64. # Don't use dynamic_cast, necessary with no-rtti.
  65. env.Append(CPPDEFINES=["NO_SAFE_CAST"])
  66. ## Copy env variables.
  67. env["ENV"] = os.environ
  68. # LTO
  69. if env["use_lto"]:
  70. env.Append(CCFLAGS=["-flto=full"])
  71. env.Append(LINKFLAGS=["-flto=full"])
  72. # Closure compiler
  73. if env["use_closure_compiler"]:
  74. # For emscripten support code.
  75. env.Append(LINKFLAGS=["--closure", "1"])
  76. # Register builder for our Engine files
  77. jscc = env.Builder(generator=run_closure_compiler, suffix=".cc.js", src_suffix=".js")
  78. env.Append(BUILDERS={"BuildJS": jscc})
  79. # Add helper method for adding libraries.
  80. env.AddMethod(add_js_libraries, "AddJSLibraries")
  81. # Add method that joins/compiles our Engine files.
  82. env.AddMethod(create_engine_file, "CreateEngineFile")
  83. # Closure compiler extern and support for ecmascript specs (const, let, etc).
  84. env["ENV"]["EMCC_CLOSURE_ARGS"] = "--language_in ECMASCRIPT6"
  85. env["CC"] = "emcc"
  86. env["CXX"] = "em++"
  87. env["AR"] = "emar"
  88. env["RANLIB"] = "emranlib"
  89. # Use TempFileMunge since some AR invocations are too long for cmd.exe.
  90. # Use POSIX-style paths, required with TempFileMunge.
  91. env["ARCOM_POSIX"] = env["ARCOM"].replace("$TARGET", "$TARGET.posix").replace("$SOURCES", "$SOURCES.posix")
  92. env["ARCOM"] = "${TEMPFILE(ARCOM_POSIX)}"
  93. # All intermediate files are just LLVM bitcode.
  94. env["OBJPREFIX"] = ""
  95. env["OBJSUFFIX"] = ".bc"
  96. env["PROGPREFIX"] = ""
  97. # Program() output consists of multiple files, so specify suffixes manually at builder.
  98. env["PROGSUFFIX"] = ""
  99. env["LIBPREFIX"] = "lib"
  100. env["LIBSUFFIX"] = ".bc"
  101. env["LIBPREFIXES"] = ["$LIBPREFIX"]
  102. env["LIBSUFFIXES"] = ["$LIBSUFFIX"]
  103. env.Prepend(CPPPATH=["#platform/javascript"])
  104. env.Append(CPPDEFINES=["JAVASCRIPT_ENABLED", "UNIX_ENABLED"])
  105. if env["javascript_eval"]:
  106. env.Append(CPPDEFINES=["JAVASCRIPT_EVAL_ENABLED"])
  107. if env["threads_enabled"] and env["gdnative_enabled"]:
  108. raise Exception("Threads and GDNative support can't be both enabled due to WebAssembly limitations")
  109. # Thread support (via SharedArrayBuffer).
  110. if env["threads_enabled"]:
  111. env.Append(CPPDEFINES=["PTHREAD_NO_RENAME"])
  112. env.Append(CCFLAGS=["-s", "USE_PTHREADS=1"])
  113. env.Append(LINKFLAGS=["-s", "USE_PTHREADS=1"])
  114. env.Append(LINKFLAGS=["-s", "PTHREAD_POOL_SIZE=8"])
  115. env.Append(LINKFLAGS=["-s", "WASM_MEM_MAX=2048MB"])
  116. env.extra_suffix = ".threads" + env.extra_suffix
  117. else:
  118. env.Append(CPPDEFINES=["NO_THREADS"])
  119. if env["gdnative_enabled"]:
  120. env.Append(CCFLAGS=["-s", "RELOCATABLE=1"])
  121. env.Append(LINKFLAGS=["-s", "RELOCATABLE=1"])
  122. env.extra_suffix = ".gdnative" + env.extra_suffix
  123. # Reduce code size by generating less support code (e.g. skip NodeJS support).
  124. env.Append(LINKFLAGS=["-s", "ENVIRONMENT=web,worker"])
  125. # Wrap the JavaScript support code around a closure named Godot.
  126. env.Append(LINKFLAGS=["-s", "MODULARIZE=1", "-s", "EXPORT_NAME='Godot'"])
  127. # Allow increasing memory buffer size during runtime. This is efficient
  128. # when using WebAssembly (in comparison to asm.js) and works well for
  129. # us since we don't know requirements at compile-time.
  130. env.Append(LINKFLAGS=["-s", "ALLOW_MEMORY_GROWTH=1"])
  131. # This setting just makes WebGL 2 APIs available, it does NOT disable WebGL 1.
  132. env.Append(LINKFLAGS=["-s", "USE_WEBGL2=1"])
  133. # Do not call main immediately when the support code is ready.
  134. env.Append(LINKFLAGS=["-s", "INVOKE_RUN=0"])
  135. # Allow use to take control of swapping WebGL buffers.
  136. env.Append(LINKFLAGS=["-s", "OFFSCREEN_FRAMEBUFFER=1"])
  137. # callMain for manual start.
  138. env.Append(LINKFLAGS=["-s", "EXTRA_EXPORTED_RUNTIME_METHODS=['callMain']"])
  139. # Add code that allow exiting runtime.
  140. env.Append(LINKFLAGS=["-s", "EXIT_RUNTIME=1"])