make-stl-wrappers.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # This Source Code Form is subject to the terms of the Mozilla Public
  2. # License, v. 2.0. If a copy of the MPL was not distributed with this
  3. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. from __future__ import print_function
  5. import os, re, string, sys
  6. from mozbuild.util import FileAvoidWrite
  7. def find_in_path(file, searchpath):
  8. for dir in searchpath.split(os.pathsep):
  9. f = os.path.join(dir, file)
  10. if os.path.exists(f):
  11. return f
  12. return ''
  13. def header_path(header, compiler):
  14. if compiler == 'gcc':
  15. # we use include_next on gcc
  16. return header
  17. elif compiler == 'msvc':
  18. return find_in_path(header, os.environ.get('INCLUDE', ''))
  19. else:
  20. # hope someone notices this ...
  21. raise NotImplementedError(compiler)
  22. def is_comment(line):
  23. return re.match(r'\s*#.*', line)
  24. def main(outdir, compiler, template_file, header_list_file):
  25. if not os.path.isdir(outdir):
  26. os.mkdir(outdir)
  27. template = open(template_file, 'r').read()
  28. for header in open(header_list_file, 'r'):
  29. header = header.rstrip()
  30. if 0 == len(header) or is_comment(header):
  31. continue
  32. path = header_path(header, compiler)
  33. with FileAvoidWrite(os.path.join(outdir, header)) as f:
  34. f.write(string.Template(template).substitute(HEADER=header,
  35. HEADER_PATH=path))
  36. if __name__ == '__main__':
  37. if 5 != len(sys.argv):
  38. print("""Usage:
  39. python {0} OUT_DIR ('msvc'|'gcc') TEMPLATE_FILE HEADER_LIST_FILE
  40. """.format(sys.argv[0]), file=sys.stderr)
  41. sys.exit(1)
  42. main(*sys.argv[1:])