prebuild.py 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. import os
  2. def generate_python_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. # convert to char array (signed)
  11. for c in f.read().encode('utf-8'):
  12. if c < 128:
  13. const_char_array.append(str(c))
  14. else:
  15. const_char_array.append(str(c - 256))
  16. const_char_array.append('0')
  17. const_char_array = ','.join(const_char_array)
  18. sources[key] = '{' + const_char_array + '}'
  19. header = '''#pragma once
  20. // generated by prebuild.py
  21. namespace pkpy{
  22. '''
  23. for key, value in sources.items():
  24. header += f' inline const char kPythonLibs_{key}[] = {value};\n'
  25. header += '}\n'
  26. return header
  27. # use LF line endings instead of CRLF
  28. with open("include/pocketpy/_generated.h", "wt", encoding='utf-8', newline='\n') as f:
  29. f.write(generate_python_sources())