detect.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import os
  2. import string
  3. import sys
  4. def is_active():
  5. return True
  6. def get_name():
  7. return 'JavaScript'
  8. def can_build():
  9. return 'EMSCRIPTEN_ROOT' in os.environ or 'EMSCRIPTEN' in os.environ
  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. ]
  16. def get_flags():
  17. return [
  18. ('tools', False),
  19. ('module_theora_enabled', False),
  20. # Disabling the mbedtls module reduces file size.
  21. # The module has little use due to the limited networking functionality
  22. # in this platform. For the available networking methods, the browser
  23. # manages TLS.
  24. ('module_mbedtls_enabled', False),
  25. ]
  26. def configure(env):
  27. ## Build type
  28. if env['target'] == 'release' or env['target'] == 'profile':
  29. # Use -Os to prioritize optimizing for reduced file size. This is
  30. # particularly valuable for the web platform because it directly
  31. # decreases download time.
  32. # -Os reduces file size by around 5 MiB over -O3. -Oz only saves about
  33. # 100 KiB over -Os, which does not justify the negative impact on
  34. # run-time performance.
  35. env.Append(CCFLAGS=['-Os'])
  36. env.Append(LINKFLAGS=['-Os'])
  37. if env['target'] == 'profile':
  38. env.Append(LINKFLAGS=['--profiling-funcs'])
  39. elif env['target'] == 'release_debug':
  40. env.Append(CPPDEFINES=['DEBUG_ENABLED'])
  41. env.Append(CCFLAGS=['-O2'])
  42. env.Append(LINKFLAGS=['-O2'])
  43. # Retain function names for backtraces at the cost of file size.
  44. env.Append(LINKFLAGS=['--profiling-funcs'])
  45. elif env['target'] == '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. ## Compiler configuration
  51. env['ENV'] = os.environ
  52. if 'EMSCRIPTEN_ROOT' in os.environ:
  53. env.PrependENVPath('PATH', os.environ['EMSCRIPTEN_ROOT'])
  54. elif 'EMSCRIPTEN' in os.environ:
  55. env.PrependENVPath('PATH', os.environ['EMSCRIPTEN'])
  56. env['CC'] = 'emcc'
  57. env['CXX'] = 'em++'
  58. env['LINK'] = 'emcc'
  59. # Emscripten's ar has issues with duplicate file names, so use cc.
  60. env['AR'] = 'emcc'
  61. env['ARFLAGS'] = '-o'
  62. # emranlib is a noop, so it's safe to use with AR=emcc.
  63. env['RANLIB'] = 'emranlib'
  64. # Use TempFileMunge since some AR invocations are too long for cmd.exe.
  65. # Use POSIX-style paths, required with TempFileMunge.
  66. env['ARCOM_POSIX'] = env['ARCOM'].replace(
  67. '$TARGET', '$TARGET.posix').replace(
  68. '$SOURCES', '$SOURCES.posix')
  69. env['ARCOM'] = '${TEMPFILE(ARCOM_POSIX)}'
  70. # All intermediate files are just LLVM bitcode.
  71. env['OBJPREFIX'] = ''
  72. env['OBJSUFFIX'] = '.bc'
  73. env['PROGPREFIX'] = ''
  74. # Program() output consists of multiple files, so specify suffixes manually at builder.
  75. env['PROGSUFFIX'] = ''
  76. env['LIBPREFIX'] = 'lib'
  77. env['LIBSUFFIX'] = '.bc'
  78. env['LIBPREFIXES'] = ['$LIBPREFIX']
  79. env['LIBSUFFIXES'] = ['$LIBSUFFIX']
  80. ## Compile flags
  81. env.Append(CPPPATH=['#platform/javascript'])
  82. env.Append(CPPDEFINES=['JAVASCRIPT_ENABLED', 'UNIX_ENABLED'])
  83. # No multi-threading (SharedArrayBuffer) available yet,
  84. # once feasible also consider memory buffer size issues.
  85. env.Append(CPPDEFINES=['NO_THREADS'])
  86. # These flags help keep the file size down.
  87. env.Append(CCFLAGS=['-fno-exceptions', '-fno-rtti'])
  88. # Don't use dynamic_cast, necessary with no-rtti.
  89. env.Append(CPPDEFINES=['NO_SAFE_CAST'])
  90. if env['javascript_eval']:
  91. env.Append(CPPDEFINES=['JAVASCRIPT_EVAL_ENABLED'])
  92. ## Link flags
  93. env.Append(LINKFLAGS=['-s', 'BINARYEN=1'])
  94. # Allow increasing memory buffer size during runtime. This is efficient
  95. # when using WebAssembly (in comparison to asm.js) and works well for
  96. # us since we don't know requirements at compile-time.
  97. env.Append(LINKFLAGS=['-s', 'ALLOW_MEMORY_GROWTH=1'])
  98. # This setting just makes WebGL 2 APIs available, it does NOT disable WebGL 1.
  99. env.Append(LINKFLAGS=['-s', 'USE_WEBGL2=1'])
  100. # engine.js uses FS but is not currently evaluated by Emscripten, so export FS.
  101. # TODO: Getting rid of this export is desirable.
  102. extra_exports = [
  103. 'FS',
  104. ]
  105. env.Append(LINKFLAGS=['-s', 'EXTRA_EXPORTED_RUNTIME_METHODS="%s"' % repr(extra_exports)])
  106. env.Append(LINKFLAGS=['-s', 'INVOKE_RUN=0'])
  107. # TODO: Reevaluate usage of this setting now that engine.js manages engine runtime.
  108. env.Append(LINKFLAGS=['-s', 'NO_EXIT_RUNTIME=1'])
  109. # TODO: Move that to opus module's config.
  110. if 'module_opus_enabled' in env and env['module_opus_enabled']:
  111. env.opus_fixed_point = 'yes'