parse-headers.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/env python3
  2. # SPDX-License-Identifier: GPL-2.0
  3. # Copyright (c) 2016, 2025 by Mauro Carvalho Chehab <mchehab@kernel.org>.
  4. # pylint: disable=C0103
  5. """
  6. Convert a C header or source file ``FILE_IN``, into a ReStructured Text
  7. included via ..parsed-literal block with cross-references for the
  8. documentation files that describe the API. It accepts an optional
  9. ``FILE_RULES`` file to describes what elements will be either ignored or
  10. be pointed to a non-default reference type/name.
  11. The output is written at ``FILE_OUT``.
  12. It is capable of identifying defines, functions, structs, typedefs,
  13. enums and enum symbols and create cross-references for all of them.
  14. It is also capable of distinguish #define used for specifying a Linux
  15. ioctl.
  16. The optional ``FILE_RULES`` contains a set of rules like:
  17. ignore ioctl VIDIOC_ENUM_FMT
  18. replace ioctl VIDIOC_DQBUF vidioc_qbuf
  19. replace define V4L2_EVENT_MD_FL_HAVE_FRAME_SEQ :c:type:`v4l2_event_motion_det`
  20. """
  21. import argparse, sys
  22. import os.path
  23. src_dir = os.path.dirname(os.path.realpath(__file__))
  24. sys.path.insert(0, os.path.join(src_dir, '../lib/python'))
  25. from kdoc.parse_data_structs import ParseDataStructs
  26. from kdoc.enrich_formatter import EnrichFormatter
  27. def main():
  28. """Main function"""
  29. parser = argparse.ArgumentParser(description=__doc__,
  30. formatter_class=EnrichFormatter)
  31. parser.add_argument("-d", "--debug", action="count", default=0,
  32. help="Increase debug level. Can be used multiple times")
  33. parser.add_argument("-t", "--toc", action="store_true",
  34. help="instead of a literal block, outputs a TOC table at the RST file")
  35. parser.add_argument("file_in", help="Input C file")
  36. parser.add_argument("file_out", help="Output RST file")
  37. parser.add_argument("file_rules", nargs="?",
  38. help="Exceptions file (optional)")
  39. args = parser.parse_args()
  40. parser = ParseDataStructs(debug=args.debug)
  41. parser.parse_file(args.file_in, args.file_rules)
  42. parser.debug_print()
  43. parser.write_output(args.file_in, args.file_out, args.toc)
  44. if __name__ == "__main__":
  45. main()