c_emscripten.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 vi:ts=4:noexpandtab
  3. import subprocess, shlex, sys
  4. from waflib.Tools import ccroot, gcc, gxx
  5. from waflib.Configure import conf
  6. from waflib.TaskGen import after_method, feature
  7. from waflib.Tools.compiler_c import c_compiler
  8. from waflib.Tools.compiler_cxx import cxx_compiler
  9. for supported_os in ('linux', 'darwin', 'gnu', 'aix'):
  10. c_compiler[supported_os].append('c_emscripten')
  11. cxx_compiler[supported_os].append('c_emscripten')
  12. @conf
  13. def get_emscripten_version(conf, cc):
  14. """
  15. Emscripten doesn't support processing '-' like clang/gcc
  16. """
  17. dummy = conf.cachedir.parent.make_node("waf-emscripten.c")
  18. dummy.write("")
  19. cmd = cc + ['-dM', '-E', '-x', 'c', dummy.abspath()]
  20. env = conf.env.env or None
  21. try:
  22. p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env)
  23. out = p.communicate()[0]
  24. except Exception as e:
  25. conf.fatal('Could not determine emscripten version %r: %s' % (cmd, e))
  26. if not isinstance(out, str):
  27. out = out.decode(sys.stdout.encoding or 'latin-1')
  28. k = {}
  29. out = out.splitlines()
  30. for line in out:
  31. lst = shlex.split(line)
  32. if len(lst)>2:
  33. key = lst[1]
  34. val = lst[2]
  35. k[key] = val
  36. if not ('__clang__' in k and 'EMSCRIPTEN' in k):
  37. conf.fatal('Could not determine the emscripten compiler version.')
  38. conf.env.DEST_OS = 'generic'
  39. conf.env.DEST_BINFMT = 'elf'
  40. conf.env.DEST_CPU = 'asm-js'
  41. conf.env.CC_VERSION = (k['__clang_major__'], k['__clang_minor__'], k['__clang_patchlevel__'])
  42. return k
  43. @conf
  44. def find_emscripten(conf):
  45. cc = conf.find_program(['emcc'], var='CC')
  46. conf.get_emscripten_version(cc)
  47. conf.env.CC = cc
  48. conf.env.CC_NAME = 'emscripten'
  49. cxx = conf.find_program(['em++'], var='CXX')
  50. conf.env.CXX = cxx
  51. conf.env.CXX_NAME = 'emscripten'
  52. conf.find_program(['emar'], var='AR')
  53. def configure(conf):
  54. conf.find_emscripten()
  55. conf.find_ar()
  56. conf.gcc_common_flags()
  57. conf.gxx_common_flags()
  58. conf.cc_load_tools()
  59. conf.cc_add_flags()
  60. conf.cxx_load_tools()
  61. conf.cxx_add_flags()
  62. conf.link_add_flags()
  63. conf.env.ARFLAGS = ['rcs']
  64. conf.env.cshlib_PATTERN = '%s.js'
  65. conf.env.cxxshlib_PATTERN = '%s.js'
  66. conf.env.cstlib_PATTERN = '%s.a'
  67. conf.env.cxxstlib_PATTERN = '%s.a'
  68. conf.env.cprogram_PATTERN = '%s.html'
  69. conf.env.cxxprogram_PATTERN = '%s.html'
  70. conf.env.CXX_TGT_F = ['-c', '-o', '']
  71. conf.env.CC_TGT_F = ['-c', '-o', '']
  72. conf.env.CXXLNK_TGT_F = ['-o', '']
  73. conf.env.CCLNK_TGT_F = ['-o', '']
  74. conf.env.append_value('LINKFLAGS',['-Wl,--enable-auto-import'])