detect.py 5.1 KB

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