gen_periphery.py 885 B

123456789101112131415161718192021222324252627282930313233343536
  1. import pcpp
  2. import pycparser
  3. from ffigen.library import Library
  4. from ffigen.converters import set_vmath_converter, set_enum_converter
  5. from ffigen.meta import Header
  6. import os
  7. file_dir = os.path.dirname(os.path.abspath(__file__))
  8. path = '3rd/periphery/include/periphery.h'
  9. code = pcpp.CmdPreprocessor([None, path, '-o', 'tmp.h', '-I', os.path.join(file_dir, 'libc_include')])
  10. mapping = {
  11. 'enum serial_parity parity': 'serial_parity_t parity',
  12. }
  13. # remap tmp.h
  14. with open('tmp.h', 'r') as f:
  15. content = f.read()
  16. for k, v in mapping.items():
  17. content = content.replace(k, v)
  18. with open('tmp.h', 'w') as f:
  19. f.write(content)
  20. ast = pycparser.parse_file('tmp.h')
  21. os.remove('tmp.h')
  22. header = Header()
  23. header.build(ast)
  24. lib = Library.from_header('periphery', header)
  25. lib.build(
  26. includes=['periphery.h'],
  27. glue_dir='3rd/periphery/src',
  28. stub_dir='include/typings',
  29. )