detect.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 'EMSCRIPTEN_ROOT' not in em_config:
  56. raise RuntimeError("'EMSCRIPTEN_ROOT' missing in Emscripten configuration file '%s'" % em_config_file)
  57. env.PrependENVPath('PATH', em_config['EMSCRIPTEN_ROOT'])
  58. env['CC'] = 'emcc'
  59. env['CXX'] = 'em++'
  60. env['LINK'] = 'emcc'
  61. env['AR'] = 'emar'
  62. env['RANLIB'] = 'emranlib'
  63. # Use TempFileMunge since some AR invocations are too long for cmd.exe.
  64. # Use POSIX-style paths, required with TempFileMunge.
  65. env['ARCOM_POSIX'] = env['ARCOM'].replace(
  66. '$TARGET', '$TARGET.posix').replace(
  67. '$SOURCES', '$SOURCES.posix')
  68. env['ARCOM'] = '${TEMPFILE(ARCOM_POSIX)}'
  69. # All intermediate files are just LLVM bitcode.
  70. env['OBJPREFIX'] = ''
  71. env['OBJSUFFIX'] = '.bc'
  72. env['PROGPREFIX'] = ''
  73. # Program() output consists of multiple files, so specify suffixes manually at builder.
  74. env['PROGSUFFIX'] = ''
  75. env['LIBPREFIX'] = 'lib'
  76. env['LIBSUFFIX'] = '.bc'
  77. env['LIBPREFIXES'] = ['$LIBPREFIX']
  78. env['LIBSUFFIXES'] = ['$LIBSUFFIX']
  79. ## Compile flags
  80. env.Append(CPPPATH=['#platform/javascript'])
  81. env.Append(CPPDEFINES=['JAVASCRIPT_ENABLED', 'UNIX_ENABLED'])
  82. # No multi-threading (SharedArrayBuffer) available yet,
  83. # once feasible also consider memory buffer size issues.
  84. env.Append(CPPDEFINES=['NO_THREADS'])
  85. # Disable exceptions and rtti on non-tools (template) builds
  86. if not env['tools']:
  87. # These flags help keep the file size down.
  88. env.Append(CCFLAGS=['-fno-exceptions', '-fno-rtti'])
  89. # Don't use dynamic_cast, necessary with no-rtti.
  90. env.Append(CPPDEFINES=['NO_SAFE_CAST'])
  91. if env['javascript_eval']:
  92. env.Append(CPPDEFINES=['JAVASCRIPT_EVAL_ENABLED'])
  93. ## Link flags
  94. # We use IDBFS in javascript_main.cpp. Since Emscripten 1.39.1 it needs to
  95. # be linked explicitly.
  96. env.Append(LIBS=['idbfs.js'])
  97. env.Append(LINKFLAGS=['-s', 'BINARYEN=1'])
  98. # This needs to be defined for Emscripten using 'fastcomp' (default pre-1.39.0)
  99. # and undefined if using 'upstream'. And to make things simple, earlier
  100. # Emscripten versions didn't include 'fastcomp' in their path, so we check
  101. # against the presence of 'upstream' to conditionally add the flag.
  102. if not "upstream" in em_config['EMSCRIPTEN_ROOT']:
  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'])
  113. #adding flag due to issue with emscripten 1.38.41 callMain method https://github.com/emscripten-core/emscripten/blob/incoming/ChangeLog.md#v13841-08072019
  114. env.Append(LINKFLAGS=['-s', 'EXTRA_EXPORTED_RUNTIME_METHODS=["callMain"]'])