detect.py 6.2 KB

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