kernel-doc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. #!/usr/bin/env python3
  2. # SPDX-License-Identifier: GPL-2.0
  3. # Copyright(c) 2025: Mauro Carvalho Chehab <mchehab@kernel.org>.
  4. #
  5. # pylint: disable=C0103,R0912,R0914,R0915
  6. #
  7. # NOTE: While kernel-doc requires at least version 3.6 to run, the
  8. # command line should work with Python 3.2+ (tested with 3.4).
  9. # The rationale is that it shall fail gracefully during Kernel
  10. # compilation with older Kernel versions. Due to that:
  11. # - encoding line is needed here;
  12. # - f-strings cannot be used in this file.
  13. # - libraries that require newer versions can only be included
  14. # after the Python version has been checked.
  15. #
  16. # Converted from the kernel-doc script originally written in Perl
  17. # under GPLv2, copyrighted since 1998 by the following authors:
  18. #
  19. # Aditya Srivastava <yashsri421@gmail.com>
  20. # Akira Yokosawa <akiyks@gmail.com>
  21. # Alexander A. Klimov <grandmaster@al2klimov.de>
  22. # Alexander Lobakin <aleksander.lobakin@intel.com>
  23. # André Almeida <andrealmeid@igalia.com>
  24. # Andy Shevchenko <andriy.shevchenko@linux.intel.com>
  25. # Anna-Maria Behnsen <anna-maria@linutronix.de>
  26. # Armin Kuster <akuster@mvista.com>
  27. # Bart Van Assche <bart.vanassche@sandisk.com>
  28. # Ben Hutchings <ben@decadent.org.uk>
  29. # Borislav Petkov <bbpetkov@yahoo.de>
  30. # Chen-Yu Tsai <wenst@chromium.org>
  31. # Coco Li <lixiaoyan@google.com>
  32. # Conchúr Navid <conchur@web.de>
  33. # Daniel Santos <daniel.santos@pobox.com>
  34. # Danilo Cesar Lemes de Paula <danilo.cesar@collabora.co.uk>
  35. # Dan Luedtke <mail@danrl.de>
  36. # Donald Hunter <donald.hunter@gmail.com>
  37. # Gabriel Krisman Bertazi <krisman@collabora.co.uk>
  38. # Greg Kroah-Hartman <gregkh@linuxfoundation.org>
  39. # Harvey Harrison <harvey.harrison@gmail.com>
  40. # Horia Geanta <horia.geanta@freescale.com>
  41. # Ilya Dryomov <idryomov@gmail.com>
  42. # Jakub Kicinski <kuba@kernel.org>
  43. # Jani Nikula <jani.nikula@intel.com>
  44. # Jason Baron <jbaron@redhat.com>
  45. # Jason Gunthorpe <jgg@nvidia.com>
  46. # Jérémy Bobbio <lunar@debian.org>
  47. # Johannes Berg <johannes.berg@intel.com>
  48. # Johannes Weiner <hannes@cmpxchg.org>
  49. # Jonathan Cameron <Jonathan.Cameron@huawei.com>
  50. # Jonathan Corbet <corbet@lwn.net>
  51. # Jonathan Neuschäfer <j.neuschaefer@gmx.net>
  52. # Kamil Rytarowski <n54@gmx.com>
  53. # Kees Cook <kees@kernel.org>
  54. # Laurent Pinchart <laurent.pinchart@ideasonboard.com>
  55. # Levin, Alexander (Sasha Levin) <alexander.levin@verizon.com>
  56. # Linus Torvalds <torvalds@linux-foundation.org>
  57. # Lucas De Marchi <lucas.demarchi@profusion.mobi>
  58. # Mark Rutland <mark.rutland@arm.com>
  59. # Markus Heiser <markus.heiser@darmarit.de>
  60. # Martin Waitz <tali@admingilde.org>
  61. # Masahiro Yamada <masahiroy@kernel.org>
  62. # Matthew Wilcox <willy@infradead.org>
  63. # Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
  64. # Michal Wajdeczko <michal.wajdeczko@intel.com>
  65. # Michael Zucchi
  66. # Mike Rapoport <rppt@linux.ibm.com>
  67. # Niklas Söderlund <niklas.soderlund@corigine.com>
  68. # Nishanth Menon <nm@ti.com>
  69. # Paolo Bonzini <pbonzini@redhat.com>
  70. # Pavan Kumar Linga <pavan.kumar.linga@intel.com>
  71. # Pavel Pisa <pisa@cmp.felk.cvut.cz>
  72. # Peter Maydell <peter.maydell@linaro.org>
  73. # Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
  74. # Randy Dunlap <rdunlap@infradead.org>
  75. # Richard Kennedy <richard@rsk.demon.co.uk>
  76. # Rich Walker <rw@shadow.org.uk>
  77. # Rolf Eike Beer <eike-kernel@sf-tec.de>
  78. # Sakari Ailus <sakari.ailus@linux.intel.com>
  79. # Silvio Fricke <silvio.fricke@gmail.com>
  80. # Simon Huggins
  81. # Tim Waugh <twaugh@redhat.com>
  82. # Tomasz Warniełło <tomasz.warniello@gmail.com>
  83. # Utkarsh Tripathi <utripathi2002@gmail.com>
  84. # valdis.kletnieks@vt.edu <valdis.kletnieks@vt.edu>
  85. # Vegard Nossum <vegard.nossum@oracle.com>
  86. # Will Deacon <will.deacon@arm.com>
  87. # Yacine Belkadi <yacine.belkadi.1@gmail.com>
  88. # Yujie Liu <yujie.liu@intel.com>
  89. """
  90. Print formatted kernel documentation to stdout.
  91. Read C language source or header FILEs, extract embedded
  92. documentation comments, and print formatted documentation
  93. to standard output.
  94. The documentation comments are identified by the ``/**``
  95. opening comment mark.
  96. See Documentation/doc-guide/kernel-doc.rst for the
  97. documentation comment syntax.
  98. """
  99. import argparse
  100. import logging
  101. import os
  102. import sys
  103. # Import Python modules
  104. LIB_DIR = "../lib/python"
  105. SRC_DIR = os.path.dirname(os.path.realpath(__file__))
  106. sys.path.insert(0, os.path.join(SRC_DIR, LIB_DIR))
  107. WERROR_RETURN_CODE = 3
  108. DESC = """
  109. Read C language source or header FILEs, extract embedded documentation comments,
  110. and print formatted documentation to standard output.
  111. The documentation comments are identified by the "/**" opening comment mark.
  112. See Documentation/doc-guide/kernel-doc.rst for the documentation comment syntax.
  113. """
  114. EXPORT_FILE_DESC = """
  115. Specify an additional FILE in which to look for EXPORT_SYMBOL information.
  116. May be used multiple times.
  117. """
  118. EXPORT_DESC = """
  119. Only output documentation for symbols that have been
  120. exported using EXPORT_SYMBOL() and related macros in any input
  121. FILE or -export-file FILE.
  122. """
  123. INTERNAL_DESC = """
  124. Only output documentation for symbols that have NOT been
  125. exported using EXPORT_SYMBOL() and related macros in any input
  126. FILE or -export-file FILE.
  127. """
  128. FUNCTION_DESC = """
  129. Only output documentation for the given function or DOC: section
  130. title. All other functions and DOC: sections are ignored.
  131. May be used multiple times.
  132. """
  133. NOSYMBOL_DESC = """
  134. Exclude the specified symbol from the output documentation.
  135. May be used multiple times.
  136. """
  137. FILES_DESC = """
  138. Header and C source files to be parsed.
  139. """
  140. WARN_CONTENTS_BEFORE_SECTIONS_DESC = """
  141. Warn if there are contents before sections (deprecated).
  142. This option is kept just for backward-compatibility, but it does nothing,
  143. neither here nor at the original Perl script.
  144. """
  145. EPILOG = """
  146. The return value is:
  147. - 0: success or Python version is not compatible with
  148. kernel-doc. If -Werror is not used, it will also
  149. return 0 if there are issues at kernel-doc markups;
  150. - 1: an abnormal condition happened;
  151. - 2: argparse issued an error;
  152. - 3: When -Werror is used, it means that one or more unfiltered parse
  153. warnings happened.
  154. """
  155. class MsgFormatter(logging.Formatter):
  156. """
  157. Helper class to capitalize errors and warnings, the same way
  158. the venerable (now retired) kernel-doc.pl used to do.
  159. """
  160. def format(self, record):
  161. record.levelname = record.levelname.capitalize()
  162. return logging.Formatter.format(self, record)
  163. def main():
  164. """
  165. Main program.
  166. """
  167. parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter,
  168. description=DESC, epilog=EPILOG)
  169. #
  170. # Normal arguments
  171. #
  172. parser.add_argument("-v", "-verbose", "--verbose", action="store_true",
  173. help="Verbose output, more warnings and other information.")
  174. parser.add_argument("-d", "-debug", "--debug", action="store_true",
  175. help="Enable debug messages")
  176. parser.add_argument("-M", "-modulename", "--modulename",
  177. default="Kernel API",
  178. help="Allow setting a module name at the output.")
  179. parser.add_argument("-l", "-enable-lineno", "--enable_lineno",
  180. action="store_true",
  181. help="Enable line number output (only in ReST mode)")
  182. #
  183. # Arguments to control the warning behavior
  184. #
  185. parser.add_argument("-Wreturn", "--wreturn", action="store_true",
  186. help="Warns about the lack of a return markup on functions.")
  187. parser.add_argument("-Wshort-desc", "-Wshort-description", "--wshort-desc",
  188. action="store_true",
  189. help="Warns if initial short description is missing")
  190. parser.add_argument("-Wcontents-before-sections",
  191. "--wcontents-before-sections", action="store_true",
  192. help=WARN_CONTENTS_BEFORE_SECTIONS_DESC)
  193. parser.add_argument("-Wall", "--wall", action="store_true",
  194. help="Enable all types of warnings")
  195. parser.add_argument("-Werror", "--werror", action="store_true",
  196. help="Treat warnings as errors.")
  197. parser.add_argument("-export-file", "--export-file", action='append',
  198. help=EXPORT_FILE_DESC)
  199. #
  200. # Output format mutually-exclusive group
  201. #
  202. out_group = parser.add_argument_group("Output format selection (mutually exclusive)")
  203. out_fmt = out_group.add_mutually_exclusive_group()
  204. out_fmt.add_argument("-m", "-man", "--man", action="store_true",
  205. help="Output troff manual page format.")
  206. out_fmt.add_argument("-r", "-rst", "--rst", action="store_true",
  207. help="Output reStructuredText format (default).")
  208. out_fmt.add_argument("-N", "-none", "--none", action="store_true",
  209. help="Do not output documentation, only warnings.")
  210. #
  211. # Output selection mutually-exclusive group
  212. #
  213. sel_group = parser.add_argument_group("Output selection (mutually exclusive)")
  214. sel_mut = sel_group.add_mutually_exclusive_group()
  215. sel_mut.add_argument("-e", "-export", "--export", action='store_true',
  216. help=EXPORT_DESC)
  217. sel_mut.add_argument("-i", "-internal", "--internal", action='store_true',
  218. help=INTERNAL_DESC)
  219. sel_mut.add_argument("-s", "-function", "--symbol", action='append',
  220. help=FUNCTION_DESC)
  221. #
  222. # Those are valid for all 3 types of filter
  223. #
  224. parser.add_argument("-n", "-nosymbol", "--nosymbol", action='append',
  225. help=NOSYMBOL_DESC)
  226. parser.add_argument("-D", "-no-doc-sections", "--no-doc-sections",
  227. action='store_true', help="Don't output DOC sections")
  228. parser.add_argument("files", metavar="FILE",
  229. nargs="+", help=FILES_DESC)
  230. args = parser.parse_args()
  231. if args.wall:
  232. args.wreturn = True
  233. args.wshort_desc = True
  234. args.wcontents_before_sections = True
  235. logger = logging.getLogger()
  236. if not args.debug:
  237. logger.setLevel(logging.INFO)
  238. else:
  239. logger.setLevel(logging.DEBUG)
  240. formatter = MsgFormatter('%(levelname)s: %(message)s')
  241. handler = logging.StreamHandler()
  242. handler.setFormatter(formatter)
  243. logger.addHandler(handler)
  244. python_ver = sys.version_info[:2]
  245. if python_ver < (3,6):
  246. #
  247. # Depending on the Kernel configuration, kernel-doc --none is called at
  248. # build time. As we don't want to break compilation due to the
  249. # usage of an old Python version, return 0 here.
  250. #
  251. if args.none:
  252. logger.error("Python 3.6 or later is required by kernel-doc. Skipping checks")
  253. sys.exit(0)
  254. sys.exit("Python 3.6 or later is required by kernel-doc. Aborting.")
  255. if python_ver < (3,7):
  256. logger.warning("Python 3.7 or later is required for correct results")
  257. #
  258. # Import kernel-doc libraries only after checking the Python version
  259. #
  260. from kdoc.kdoc_files import KernelFiles # pylint: disable=C0415
  261. from kdoc.kdoc_output import RestFormat, ManFormat # pylint: disable=C0415
  262. if args.man:
  263. out_style = ManFormat(modulename=args.modulename)
  264. elif args.none:
  265. out_style = None
  266. else:
  267. out_style = RestFormat()
  268. kfiles = KernelFiles(verbose=args.verbose,
  269. out_style=out_style, werror=args.werror,
  270. wreturn=args.wreturn, wshort_desc=args.wshort_desc,
  271. wcontents_before_sections=args.wcontents_before_sections)
  272. kfiles.parse(args.files, export_file=args.export_file)
  273. for t in kfiles.msg(enable_lineno=args.enable_lineno, export=args.export,
  274. internal=args.internal, symbol=args.symbol,
  275. nosymbol=args.nosymbol, export_file=args.export_file,
  276. no_doc_sections=args.no_doc_sections):
  277. msg = t[1]
  278. if msg:
  279. print(msg)
  280. error_count = kfiles.errors
  281. if not error_count:
  282. sys.exit(0)
  283. if args.werror:
  284. print("%s warnings as errors" % error_count) # pylint: disable=C0209
  285. sys.exit(WERROR_RETURN_CODE)
  286. if args.verbose:
  287. print("%s errors" % error_count) # pylint: disable=C0209
  288. sys.exit(0)
  289. #
  290. # Call main method
  291. #
  292. if __name__ == "__main__":
  293. main()