detect.py 5.3 KB

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