detect.py 6.4 KB

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