prebuild.py 913 B

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. with open("python/" + file) as f:
  9. value = f.read()
  10. const_array = ','.join([str(b) for b in value.encode('utf-8')])
  11. sources[key] = '(const char[]){' + const_array + '}'
  12. header = '''#pragma once
  13. // generated by prebuild.py
  14. #include <map>
  15. #include <string>
  16. namespace pkpy{
  17. inline const std::map<std::string, const char*> kPythonLibs = {
  18. '''
  19. for key, value in sources.items():
  20. header += f' {{ "{key}", {value} }},\n'
  21. header += ''' };
  22. } // namespace pkpy
  23. '''
  24. return header
  25. # use LF line endings instead of CRLF
  26. with open("include/pocketpy/_generated.h", "wt", encoding='utf-8', newline='\n') as f:
  27. f.write(generate_python_sources())