detect.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 'EM_CONFIG' in os.environ or os.path.exists(os.path.expanduser('~/.emscripten'))
  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. em_config_file = os.getenv('EM_CONFIG') or os.path.expanduser('~/.emscripten')
  53. if not os.path.exists(em_config_file):
  54. raise RuntimeError("Emscripten configuration file '%s' does not exist" % em_config_file)
  55. with open(em_config_file) as f:
  56. em_config = {}
  57. try:
  58. # Emscripten configuration file is a Python file with simple assignments.
  59. exec(f.read(), em_config)
  60. except StandardError as e:
  61. raise RuntimeError("Emscripten configuration file '%s' is invalid:\n%s" % (em_config_file, e))
  62. if 'EMSCRIPTEN_ROOT' not in em_config:
  63. raise RuntimeError("'EMSCRIPTEN_ROOT' missing in Emscripten configuration file '%s'" % em_config_file)
  64. env.PrependENVPath('PATH', em_config['EMSCRIPTEN_ROOT'])
  65. env['CC'] = 'emcc'
  66. env['CXX'] = 'em++'
  67. env['LINK'] = 'emcc'
  68. # Emscripten's ar has issues with duplicate file names, so use cc.
  69. env['AR'] = 'emcc'
  70. env['ARFLAGS'] = '-o'
  71. # emranlib is a noop, so it's safe to use with AR=emcc.
  72. env['RANLIB'] = 'emranlib'
  73. # Use TempFileMunge since some AR invocations are too long for cmd.exe.
  74. # Use POSIX-style paths, required with TempFileMunge.
  75. env['ARCOM_POSIX'] = env['ARCOM'].replace(
  76. '$TARGET', '$TARGET.posix').replace(
  77. '$SOURCES', '$SOURCES.posix')
  78. env['ARCOM'] = '${TEMPFILE(ARCOM_POSIX)}'
  79. # All intermediate files are just LLVM bitcode.
  80. env['OBJPREFIX'] = ''
  81. env['OBJSUFFIX'] = '.bc'
  82. env['PROGPREFIX'] = ''
  83. # Program() output consists of multiple files, so specify suffixes manually at builder.
  84. env['PROGSUFFIX'] = ''
  85. env['LIBPREFIX'] = 'lib'
  86. env['LIBSUFFIX'] = '.bc'
  87. env['LIBPREFIXES'] = ['$LIBPREFIX']
  88. env['LIBSUFFIXES'] = ['$LIBSUFFIX']
  89. ## Compile flags
  90. env.Append(CPPPATH=['#platform/javascript'])
  91. env.Append(CPPDEFINES=['JAVASCRIPT_ENABLED', 'UNIX_ENABLED'])
  92. # No multi-threading (SharedArrayBuffer) available yet,
  93. # once feasible also consider memory buffer size issues.
  94. env.Append(CPPDEFINES=['NO_THREADS'])
  95. # These flags help keep the file size down.
  96. env.Append(CCFLAGS=['-fno-exceptions', '-fno-rtti'])
  97. # Don't use dynamic_cast, necessary with no-rtti.
  98. env.Append(CPPDEFINES=['NO_SAFE_CAST'])
  99. if env['javascript_eval']:
  100. env.Append(CPPDEFINES=['JAVASCRIPT_EVAL_ENABLED'])
  101. ## Link flags
  102. env.Append(LINKFLAGS=['-s', 'BINARYEN=1'])
  103. # Allow increasing memory buffer size during runtime. This is efficient
  104. # when using WebAssembly (in comparison to asm.js) and works well for
  105. # us since we don't know requirements at compile-time.
  106. env.Append(LINKFLAGS=['-s', 'ALLOW_MEMORY_GROWTH=1'])
  107. # This setting just makes WebGL 2 APIs available, it does NOT disable WebGL 1.
  108. env.Append(LINKFLAGS=['-s', 'USE_WEBGL2=1'])
  109. env.Append(LINKFLAGS=['-s', 'INVOKE_RUN=0'])
  110. # TODO: Reevaluate usage of this setting now that engine.js manages engine runtime.
  111. env.Append(LINKFLAGS=['-s', 'NO_EXIT_RUNTIME=1'])
  112. # TODO: Move that to opus module's config.
  113. if 'module_opus_enabled' in env and env['module_opus_enabled']:
  114. env.opus_fixed_point = 'yes'