compile_db.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/usr/bin/env python3
  2. from flipper.app import App
  3. import json
  4. import pathlib
  5. class Main(App):
  6. def init(self):
  7. self.subparsers = self.parser.add_subparsers(help="sub-command help")
  8. # generate
  9. self.parser_generate = self.subparsers.add_parser(
  10. "generate", help="Check source code format and file names"
  11. )
  12. self.parser_generate.add_argument("-p", dest="path", required=True)
  13. self.parser_generate.set_defaults(func=self.generate)
  14. def parse_sources(self, path, source_path, flags_path):
  15. flags = ""
  16. with open(path + "/" + flags_path) as f:
  17. for line in f:
  18. if line.strip():
  19. flags += line.strip() + " "
  20. local_path = str(pathlib.Path().resolve())
  21. data = []
  22. with open(path + "/" + source_path) as f:
  23. for line in f:
  24. if line.strip():
  25. file = line.strip()
  26. data.append(
  27. {
  28. "directory": local_path,
  29. "command": flags + "-c " + file,
  30. "file": file,
  31. }
  32. )
  33. return data
  34. def generate(self):
  35. DB_SOURCE = [
  36. {
  37. "name": "ASM",
  38. "source": "db.asm_source.list",
  39. "flags": "db.asm_flags.list",
  40. },
  41. {"name": "C", "source": "db.c_source.list", "flags": "db.c_flags.list"},
  42. {
  43. "name": "CPP",
  44. "source": "db.cpp_source.list",
  45. "flags": "db.cpp_flags.list",
  46. },
  47. ]
  48. path = self.args.path
  49. out_data = []
  50. out_path = path + "/" + "compile_commands.json"
  51. out_file = open(out_path, mode="w")
  52. for record in DB_SOURCE:
  53. self.logger.info(
  54. f"Processing {record['name']} ({record['source']}, {record['flags']})"
  55. )
  56. data = self.parse_sources(path, record["source"], record["flags"])
  57. out_data += data
  58. self.logger.info(f"Saving")
  59. json.dump(out_data, out_file, indent=2)
  60. self.logger.info(f"Compilation DB written to " + out_path)
  61. return 0
  62. if __name__ == "__main__":
  63. Main()()