prebuild.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. # 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. return sources
  20. sources = get_sources()
  21. # use LF line endings instead of CRLF
  22. with open("include/pocketpy/_generated.h", "wt", encoding='utf-8', newline='\n') as f:
  23. data = '''#pragma once
  24. // generated by prebuild.py
  25. namespace pkpy{
  26. '''
  27. for key in sorted(sources.keys()):
  28. value = sources[key]
  29. data += f' extern const char kPythonLibs_{key}[];\n'
  30. data += '} // namespace pkpy\n'
  31. f.write(data)
  32. with open("src/_generated.cpp", "wt", encoding='utf-8', newline='\n') as f:
  33. data = '''// generated by prebuild.py
  34. #include "pocketpy/_generated.h"
  35. namespace pkpy{
  36. '''
  37. for key in sorted(sources.keys()):
  38. value = sources[key]
  39. data += f' const char kPythonLibs_{key}[] = {value};\n'
  40. data += '} // namespace pkpy\n'
  41. f.write(data)