detect.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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("Threads must be enabled to build the editor. Please add the 'threads_enabled=yes' option")
  52. # Tools need more memory. Initial stack memory in bytes. See `src/settings.js` in emscripten repository (will be renamed to INITIAL_MEMORY).
  53. env.Append(LINKFLAGS=['-s', 'TOTAL_MEMORY=33554432'])
  54. else:
  55. # Disable exceptions and rtti on non-tools (template) builds
  56. # These flags help keep the file size down.
  57. env.Append(CCFLAGS=['-fno-exceptions', '-fno-rtti'])
  58. # Don't use dynamic_cast, necessary with no-rtti.
  59. env.Append(CPPDEFINES=['NO_SAFE_CAST'])
  60. ## Copy env variables.
  61. env['ENV'] = os.environ
  62. # LTO
  63. if env['use_lto']:
  64. env.Append(CCFLAGS=['-s', 'WASM_OBJECT_FILES=0'])
  65. env.Append(LINKFLAGS=['-s', 'WASM_OBJECT_FILES=0'])
  66. env.Append(LINKFLAGS=['--llvm-lto', '1'])
  67. # Closure compiler
  68. if env['use_closure_compiler']:
  69. # For emscripten support code.
  70. env.Append(LINKFLAGS=['--closure', '1'])
  71. # Register builder for our Engine files
  72. jscc = env.Builder(generator=run_closure_compiler, suffix='.cc.js', src_suffix='.js')
  73. env.Append(BUILDERS = {'BuildJS' : jscc})
  74. # Add method that joins/compiles our Engine files.
  75. env.AddMethod(create_engine_file, "CreateEngineFile")
  76. # Closure compiler extern and support for ecmascript specs (const, let, etc).
  77. env['ENV']['EMCC_CLOSURE_ARGS'] = '--language_in ECMASCRIPT6'
  78. em_config = parse_config()
  79. env.PrependENVPath('PATH', em_config['EMCC_ROOT'])
  80. env['CC'] = 'emcc'
  81. env['CXX'] = 'em++'
  82. env['LINK'] = 'emcc'
  83. env['AR'] = 'emar'
  84. env['RANLIB'] = 'emranlib'
  85. # Use TempFileMunge since some AR invocations are too long for cmd.exe.
  86. # Use POSIX-style paths, required with TempFileMunge.
  87. env['ARCOM_POSIX'] = env['ARCOM'].replace(
  88. '$TARGET', '$TARGET.posix').replace(
  89. '$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. else:
  113. env.Append(CPPDEFINES=['NO_THREADS'])
  114. # Reduce code size by generating less support code (e.g. skip NodeJS support).
  115. env.Append(LINKFLAGS=['-s', 'ENVIRONMENT=web,worker'])
  116. # We use IDBFS in javascript_main.cpp. Since Emscripten 1.39.1 it needs to
  117. # be linked explicitly.
  118. env.Append(LIBS=['idbfs.js'])
  119. env.Append(LINKFLAGS=['-s', 'BINARYEN=1'])
  120. env.Append(LINKFLAGS=['-s', 'MODULARIZE=1', '-s', 'EXPORT_NAME="Godot"'])
  121. # Allow increasing memory buffer size during runtime. This is efficient
  122. # when using WebAssembly (in comparison to asm.js) and works well for
  123. # us since we don't know requirements at compile-time.
  124. env.Append(LINKFLAGS=['-s', 'ALLOW_MEMORY_GROWTH=1'])
  125. # This setting just makes WebGL 2 APIs available, it does NOT disable WebGL 1.
  126. env.Append(LINKFLAGS=['-s', 'USE_WEBGL2=1'])
  127. env.Append(LINKFLAGS=['-s', 'INVOKE_RUN=0'])
  128. # callMain for manual start, FS for preloading.
  129. env.Append(LINKFLAGS=['-s', 'EXTRA_EXPORTED_RUNTIME_METHODS=["callMain", "FS"]'])