generate_protobuf.py 1018 B

12345678910111213141516171819202122232425262728293031323334
  1. #!/usr/bin/env python3
  2. from pathlib import Path
  3. import os
  4. import shutil
  5. import subprocess
  6. import sys
  7. def run():
  8. SCRIPT_PATH = Path(__file__).absolute().parent
  9. REPO_ROOT = SCRIPT_PATH.parent
  10. proto_path = REPO_ROOT / 'proto'
  11. nanopb_path = REPO_ROOT / 'thirdparty' / 'nanopb'
  12. # Make sure nanopb submodule is available
  13. if not os.path.isdir(nanopb_path):
  14. print(f'Nanopb checkout not found! Make sure you have inited/updated the submodule located at {nanopb_path}', file=sys.stderr)
  15. exit(1)
  16. nanopb_generator_path = nanopb_path / 'generator' / 'nanopb_generator.py'
  17. c_generated_output_path = REPO_ROOT / 'firmware' / 'src' / 'proto_gen'
  18. proto_files = [f for f in os.listdir(proto_path) if f.endswith('.proto')]
  19. assert len(proto_files) > 0, 'No proto files found!'
  20. # Generate C files via nanopb
  21. subprocess.check_call(['python3', nanopb_generator_path, '-D', c_generated_output_path] + proto_files, cwd=proto_path)
  22. if __name__ == '__main__':
  23. run()