prebuild.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  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 in sorted(sources.keys()):
  24. value = sources[key]
  25. header += f' inline const char kPythonLibs_{key}[] = {value};\n'
  26. header += '}\n'
  27. return header
  28. # use LF line endings instead of CRLF
  29. with open("include/pocketpy/_generated.h", "wt", encoding='utf-8', newline='\n') as f:
  30. f.write(generate_python_sources())