gen_includes.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/usr/bin/env python3
  2. import sys
  3. import os
  4. import re
  5. import errno
  6. from os import listdir
  7. from os.path import dirname, relpath, join
  8. def ensure_dir_exists(path):
  9. try:
  10. os.makedirs(path)
  11. except OSError as e:
  12. if e.errno == errno.EEXIST and os.path.isdir(path):
  13. pass
  14. else:
  15. raise
  16. def make_dir(directory):
  17. if not os.path.exists(directory):
  18. os.makedirs(directory)
  19. def files(directory):
  20. for dirpath, dirnames, filenames in os.walk(directory):
  21. for name in filenames:
  22. yield relpath(join(dirpath, name), directory)
  23. def headers_set(directory):
  24. return {
  25. f for f in files(directory)
  26. if f.endswith('.h') and (not f.startswith('internal/') or f.startswith('internal/pycore_frame.h')) and not re.match(r'^pyconfig[.-].+\.h$', f)
  27. }
  28. if __name__ == "__main__":
  29. python2_path = sys.argv[1]
  30. python3_path = sys.argv[2]
  31. output_path = sys.argv[3]
  32. ensure_dir_exists(join('.', python2_path))
  33. ensure_dir_exists(join('.', python3_path))
  34. only_headers2 = headers_set(python2_path)
  35. only_headers3 = headers_set(python3_path)
  36. all_headers = only_headers2 | only_headers3
  37. for header in all_headers:
  38. path = join(output_path, header)
  39. make_dir(dirname(path))
  40. f = open(path, 'w')
  41. f.write('#pragma once\n\n')
  42. f.write('#ifdef USE_PYTHON3\n')
  43. if (header in only_headers3):
  44. f.write('#include <' + join(python3_path, header) + '>\n')
  45. else:
  46. f.write('#error "No <' + header + '> in Python3"\n')
  47. f.write('#else\n')
  48. if (header in only_headers2):
  49. f.write('#include <' + join(python2_path, header) + '>\n')
  50. else:
  51. f.write('#error "No <' + header + '> in Python2"\n')
  52. f.write('#endif\n')