1
0

prebuild.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import os
  2. def get_sources():
  3. sources = {}
  4. for file in sorted(os.listdir("python")):
  5. if not file.endswith(".py"):
  6. continue
  7. key = file.split(".")[0]
  8. const_char_array = []
  9. with open("python/" + file) as f:
  10. specials = { 10: '\\n', 34: '\\"' }
  11. for c in f.read().encode('utf-8'):
  12. if c in specials:
  13. const_char_array.append(specials[c])
  14. elif c >= 32 and c <= 126 and c != 92:
  15. const_char_array.append(chr(c))
  16. else:
  17. const_char_array.append(f'\\x{c:02x}')
  18. const_char_array = ''.join(const_char_array)
  19. sources[key] = '"' + const_char_array + '"'
  20. return sources
  21. sources = get_sources()
  22. # use LF line endings instead of CRLF
  23. with open("include/pocketpy/_generated.h", "wt", encoding='utf-8', newline='\n') as f:
  24. data = '''#pragma once
  25. // generated by prebuild.py
  26. namespace pkpy{
  27. '''
  28. for key in sorted(sources.keys()):
  29. value = sources[key]
  30. data += f' extern const char kPythonLibs_{key}[];\n'
  31. data += '} // namespace pkpy\n'
  32. f.write(data)
  33. with open("src/_generated.cpp", "wt", encoding='utf-8', newline='\n') as f:
  34. data = '''// generated by prebuild.py
  35. #include "pocketpy/_generated.h"
  36. namespace pkpy{
  37. '''
  38. for key in sorted(sources.keys()):
  39. value = sources[key]
  40. data += f' const char kPythonLibs_{key}[] = {value};\n'
  41. data += '} // namespace pkpy\n'
  42. f.write(data)