Skip to contentSkip to conversation

add a make target to generate man pages from kernel-doc

PATCH In review Mauro Carvalho Chehab 2 participants 5 messages Aug 27, 2025 – Aug 28, 2025

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v3 cover add a make target to generate man pages from kernel-doc

Hi Jon,

Currently, generation of manpages is hacky: one needs to run kernel-doc over c source files and then use an extra script to split results.

The new kernel-doc tool supports multiple files and even dirs, so there's no need to use git ls-files anymore.

Yet, it produces a single output. Change the logic to add, instead a target to docs Makefile to produce them, moving the split and build logic to sphinx-build-wrapper.

That allows honoring SPHINXDIRS when they point to a subdir, while scanning all files by default.

This series comes after:

    https://lore.kernel.org/linux-doc/[email protected]/T/#t

-

It should be noticed that, while Sphinx does have a man pages builder, it requires a completely different set of ReST entries that would need to be designed specially for that:

- for each man page, an entry at conf.py is required;
- the ReST page should be in a format closer to a real man page;
- exactly one .rst file for each man page.

This is completely different than the approach we take where we have, where kernel-doc direcly generates groff output formated data.

So, yeah, this is still a little bit hackish, but to avoid that we would need a completely different setup with, frankly, would be a headache to setup and maintain.

With that in mind, there are a couple of possible alternatives:

1. change kernel-doc to produce one file per man page. A change like that would require adding file directory parameter for output, and/or teaching it about Documentation/output and O=;
2. make sphinx-build-wrapper do that for us. That's IMO the best alternative, which is what I actually implemented;
3. add a man pages Sphinx extension that would be tricking sphinx, making it produce one man page .rst file for each kernel-doc markup entry.
This is probably doable, and could eventually bring some advantages, as we could also have man pages for pages that are only in ReST format, but it will require some extra development, and likely require several changes at the existing .rst files. Also, generating extra output files out of the blue is something that Sphinx doesn't like to do, as it likes to have a 1:1 mapping between source files and output ones.
Not sure if it worth the efforts, and will still be hackish.

I opted for (2), as it means that ones needing to generate docs can benefit from SPHINXDIRS, and we don't need anything extra at docs Makefile. By doing that, we can later improve the man pages logic without needing to touch anything else but kernel-doc and/or the wrapper tool.

There are two points to consider:

- when SPHINXDIRS=. (the default), it searches fror all files treewide, even the ones without kernel-doc entries at docs, which is aligned with the procedure used by the current documentation about how to generate man pages;

- when SHINXDIRS=subdirs, it searches only for the files that come just after kernel-doc markups. It doesn't take into account any kernel-doc markup parameters to filter out internal/external functions or to point to an extra header file.

---

v3:
- fixed a badly-solved conflict on v2;
- improved documentation about manpages generation;
- added SEE ALSO at the man pages.

v2:
- no actual changes, but it was rebased on the top of the actual pacth series, in order to apply cleanly.
*** BLURB HERE ***

Mauro Carvalho Chehab (2):
docs: add support to build manpages from kerneldoc output
tools: kernel-doc: add a see also section at man pages

 Documentation/Makefile                 |  3 +-
 Documentation/doc-guide/kernel-doc.rst | 29 +++++----
 Makefile                               |  2 +-
 scripts/lib/kdoc/kdoc_files.py         |  5 +-
 scripts/lib/kdoc/kdoc_output.py        | 84 ++++++++++++++++++++++++--
 scripts/split-man.pl                   | 28 ---------
 tools/docs/sphinx-build-wrapper        | 81 +++++++++++++++++++++++--
 7 files changed, 178 insertions(+), 54 deletions(-)
 delete mode 100755 scripts/split-man.pl
Signature · 1 line
2.51.0
#1 lore raw

PATCH v3 1/2 docs: add support to build manpages from kerneldoc output

Generating man files currently requires running a separate script. The target also doesn't appear at the docs Makefile.

Add support for mandocs at the Makefile, adding the build logic inside sphinx-build-wrapper, updating documentation and dropping the ancillary script.

---
 Documentation/Makefile                 |  3 +-
 Documentation/doc-guide/kernel-doc.rst | 29 ++++-----
 Makefile                               |  2 +-
 scripts/split-man.pl                   | 28 ---------
 tools/docs/sphinx-build-wrapper        | 81 ++++++++++++++++++++++++--
 5 files changed, 95 insertions(+), 48 deletions(-)
 delete mode 100755 scripts/split-man.pl
Documentation/Makefile +2 -1
index 3e1cb44a5fbb..22e39e5ed07d 100644
@@ -53,7 +53,7 @@ ifeq ($(HAVE_SPHINX),0)
 else # HAVE_SPHINX
 
 # Common documentation targets
-infodocs texinfodocs latexdocs epubdocs xmldocs pdfdocs linkcheckdocs:
+mandocs infodocs texinfodocs latexdocs epubdocs xmldocs pdfdocs linkcheckdocs:
 	$(Q)@$(srctree)/tools/docs/sphinx-pre-install --version-check
 	+$(Q)$(PYTHON3) $(BUILD_WRAPPER) $@ \
 		--sphinxdirs="$(SPHINXDIRS)" --conf=$(SPHINX_CONF) \
@@ -104,6 +104,7 @@ dochelp:
 	@echo  '  htmldocs        - HTML'
 	@echo  '  texinfodocs     - Texinfo'
 	@echo  '  infodocs        - Info'
+	@echo  '  mandocs         - Man pages'
 	@echo  '  latexdocs       - LaTeX'
 	@echo  '  pdfdocs         - PDF'
 	@echo  '  epubdocs        - EPUB'
Documentation/doc-guide/kernel-doc.rst +16 -13
index af9697e60165..4370cc8fbcf5 100644
@@ -579,20 +579,23 @@ source.
 How to use kernel-doc to generate man pages
 -------------------------------------------
 
-If you just want to use kernel-doc to generate man pages you can do this
-from the kernel git tree::
+To generate man pages for all files that contain kernel-doc markups, run::
 
-  $ scripts/kernel-doc -man \
-    $(git grep -l '/\*\*' -- :^Documentation :^tools) \
-    | scripts/split-man.pl /tmp/man
+  $ make mandocs
 
-Some older versions of git do not support some of the variants of syntax for
-path exclusion.  One of the following commands may work for those versions::
+Or calling ``script-build-wrapper`` directly::
 
-  $ scripts/kernel-doc -man \
-    $(git grep -l '/\*\*' -- . ':!Documentation' ':!tools') \
-    | scripts/split-man.pl /tmp/man
+  $ ./tools/docs/sphinx-build-wrapper mandocs
 
-  $ scripts/kernel-doc -man \
-    $(git grep -l '/\*\*' -- . ":(exclude)Documentation" ":(exclude)tools") \
-    | scripts/split-man.pl /tmp/man
+The output will be at ``/man`` directory inside the output directory
+(by default: ``Documentation/output``).
+
+Optionally, it is possible to generate a partial set of man pages by
+using SPHINXDIRS:
+
+  $ make SPHINXDIRS=driver-api/media mandocs
+
+.. note::
+
+   When SPHINXDIRS={subdir} is used, it will only generate man pages for
+   the files explicitly inside a ``Documentation/{subdir}/.../*.rst`` file.
Makefile +1 -1
index 6bfe776bf3c5..9bd44afeda26 100644
@@ -1800,7 +1800,7 @@ $(help-board-dirs): help-%:
 # Documentation targets
 # ---------------------------------------------------------------------------
 DOC_TARGETS := xmldocs latexdocs pdfdocs htmldocs epubdocs cleandocs \
-	       linkcheckdocs dochelp refcheckdocs texinfodocs infodocs
+	       linkcheckdocs dochelp refcheckdocs texinfodocs infodocs mandocs
 PHONY += $(DOC_TARGETS)
 $(DOC_TARGETS):
 	$(Q)$(MAKE) $(build)=Documentation $@
scripts/split-man.pl deleted +0 -28
deleted file mode 100755
index 96bd99dc977a..000000000000
@@ -1,28 +0,0 @@
-#!/usr/bin/env perl
-# SPDX-License-Identifier: GPL-2.0
-#
-# Author: Mauro Carvalho Chehab <[email protected]>
-#
-# Produce manpages from kernel-doc.
-# See Documentation/doc-guide/kernel-doc.rst for instructions
-
-if ($#ARGV < 0) {
-   die "where do I put the results?\n";
-}
-
-mkdir $ARGV[0],0777;
-$state = 0;
-while (<STDIN>) {
-    if (/^\.TH \"[^\"]*\" 9 \"([^\"]*)\"/) {
-	if ($state == 1) { close OUT }
-	$state = 1;
-	$fn = "$ARGV[0]/$1.9";
-	print STDERR "Creating $fn\n";
-	open OUT, ">$fn" or die "can't open $fn: $!\n";
-	print OUT $_;
-    } elsif ($state != 0) {
-	print OUT $_;
-    }
-}
-
-close OUT;
tools/docs/sphinx-build-wrapper +76 -5

117 lines not on this page · raw message

Signature · 1 line
2.51.0
maintainer
#2 lore raw

Re: [PATCH v3 1/2] docs: add support to build manpages from kerneldoc output

Please check your mail tooling: something strange happened to all @kernel.org adresses:

Quoted 12 lines

Cc: Mauro Carvalho Chehab <[email protected]>,
Mauro Carvalho Chehab <[email protected]>, Thomas Weißschuh <[email protected]>, Alice Ryhl <[email protected]>, Masahiro Yamada <[email protected]>, Miguel Ojeda <[email protected]>, Nathan Chancellor <[email protected]>, Nicolas Schier <[email protected]>, Randy Dunlap <[email protected]>, Tamir Duberstein <[email protected]>, [email protected], [email protected]

Quoted 48 lines · Mauro Carvalho Chehab

Generating man files currently requires running a separate script. The target also doesn't appear at the docs Makefile.

Add support for mandocs at the Makefile, adding the build logic inside sphinx-build-wrapper, updating documentation and dropping the ancillary script.

---
 Documentation/Makefile                 |  3 +-
 Documentation/doc-guide/kernel-doc.rst | 29 ++++-----
 Makefile                               |  2 +-
 scripts/split-man.pl                   | 28 ---------
 tools/docs/sphinx-build-wrapper        | 81 ++++++++++++++++++++++++--
 5 files changed, 95 insertions(+), 48 deletions(-)
 delete mode 100755 scripts/split-man.pl
Documentation/Makefile +2 -1
index 3e1cb44a5fbb..22e39e5ed07d 100644
@@ -53,7 +53,7 @@ ifeq ($(HAVE_SPHINX),0)
 else # HAVE_SPHINX
 
 # Common documentation targets
-infodocs texinfodocs latexdocs epubdocs xmldocs pdfdocs linkcheckdocs:
+mandocs infodocs texinfodocs latexdocs epubdocs xmldocs pdfdocs linkcheckdocs:
 	$(Q)@$(srctree)/tools/docs/sphinx-pre-install --version-check
 	+$(Q)$(PYTHON3) $(BUILD_WRAPPER) $@ \
 		--sphinxdirs="$(SPHINXDIRS)" --conf=$(SPHINX_CONF) \
@@ -104,6 +104,7 @@ dochelp:
 	@echo  '  htmldocs        - HTML'
 	@echo  '  texinfodocs     - Texinfo'
 	@echo  '  infodocs        - Info'
+	@echo  '  mandocs         - Man pages'
 	@echo  '  latexdocs       - LaTeX'
 	@echo  '  pdfdocs         - PDF'
 	@echo  '  epubdocs        - EPUB'
Documentation/doc-guide/kernel-doc.rst +0 -2
index af9697e60165..4370cc8fbcf5 100644
@@ -579,20 +579,23 @@ source.
 How to use kernel-doc to generate man pages
 -------------------------------------------
 
-If you just want to use kernel-doc to generate man pages you can do this
-from the kernel git tree::

Documentation/doc-guide/kernel-doc.rst source. · line 586

Quoted 10 lines
Documentation/doc-guide/kernel-doc.rst +3 -5
+To generate man pages for all files that contain kernel-doc markups, run::
 
-  $ scripts/kernel-doc -man \
-    $(git grep -l '/\*\*' -- :^Documentation :^tools) \
-    | scripts/split-man.pl /tmp/man
+  $ make mandocs
 
-Some older versions of git do not support some of the variants of syntax for
-path exclusion.  One of the following commands may work for those versions::
+Or calling ``script-build-wrapper`` directly::
                ^^^^^^ sphinx-build-wrapper ?

Kind regards
Nicolas

#3 lore raw

Re: [PATCH v3 1/2] docs: add support to build manpages from kerneldoc output

Em Thu, 28 Aug 2025 00:04:08 +0200
Nicolas Schier <[email protected]> escreveu:

Quoted 15 lines

Please check your mail tooling: something strange happened to all @kernel.org adresses:

Quoted 12 lines

Cc: Mauro Carvalho Chehab <[email protected]>,
Mauro Carvalho Chehab <[email protected]>, Thomas Weißschuh <[email protected]>, Alice Ryhl <[email protected]>, Masahiro Yamada <[email protected]>, Miguel Ojeda <[email protected]>, Nathan Chancellor <[email protected]>, Nicolas Schier <[email protected]>, Randy Dunlap <[email protected]>, Tamir Duberstein <[email protected]>, [email protected], [email protected]

That's really weird! Thanks for letting me know. It was not supposed to do any e-mail changes.

Quoted 57 lines · Mauro Carvalho Chehab
Quoted 55 lines

Generating man files currently requires running a separate script. The target also doesn't appear at the docs Makefile.

Add support for mandocs at the Makefile, adding the build logic inside sphinx-build-wrapper, updating documentation and dropping the ancillary script.

---
 Documentation/Makefile                 |  3 +-
 Documentation/doc-guide/kernel-doc.rst | 29 ++++-----
 Makefile                               |  2 +-
 scripts/split-man.pl                   | 28 ---------
 tools/docs/sphinx-build-wrapper        | 81 ++++++++++++++++++++++++--
 5 files changed, 95 insertions(+), 48 deletions(-)
 delete mode 100755 scripts/split-man.pl
Documentation/Makefile +2 -1
index 3e1cb44a5fbb..22e39e5ed07d 100644
@@ -53,7 +53,7 @@ ifeq ($(HAVE_SPHINX),0)
 else # HAVE_SPHINX
 
 # Common documentation targets
-infodocs texinfodocs latexdocs epubdocs xmldocs pdfdocs linkcheckdocs:
+mandocs infodocs texinfodocs latexdocs epubdocs xmldocs pdfdocs linkcheckdocs:
 	$(Q)@$(srctree)/tools/docs/sphinx-pre-install --version-check
 	+$(Q)$(PYTHON3) $(BUILD_WRAPPER) $@ \
 		--sphinxdirs="$(SPHINXDIRS)" --conf=$(SPHINX_CONF) \
@@ -104,6 +104,7 @@ dochelp:
 	@echo  '  htmldocs        - HTML'
 	@echo  '  texinfodocs     - Texinfo'
 	@echo  '  infodocs        - Info'
+	@echo  '  mandocs         - Man pages'
 	@echo  '  latexdocs       - LaTeX'
 	@echo  '  pdfdocs         - PDF'
 	@echo  '  epubdocs        - EPUB'
Documentation/doc-guide/kernel-doc.rst +2 -5
index af9697e60165..4370cc8fbcf5 100644
@@ -579,20 +579,23 @@ source.
 How to use kernel-doc to generate man pages
 -------------------------------------------
 
-If you just want to use kernel-doc to generate man pages you can do this
-from the kernel git tree::
+To generate man pages for all files that contain kernel-doc markups, run::
 
-  $ scripts/kernel-doc -man \
-    $(git grep -l '/\*\*' -- :^Documentation :^tools) \
-    | scripts/split-man.pl /tmp/man
+  $ make mandocs
 
Quoted 5 lines

Documentation/doc-guide/kernel-doc.rst source. · line 586

Quoted 3 lines
Documentation/doc-guide/kernel-doc.rst +1 -2
-Some older versions of git do not support some of the variants of syntax for
-path exclusion.  One of the following commands may work for those versions::
+Or calling ``script-build-wrapper`` directly::  
                ^^^^^^ sphinx-build-wrapper ?

This is on a separate patch series:

https://lore.kernel.org/linux-doc/[email protected]/

It basically moves the complex logic around docs Makefile into a script that can either be called via Makefile or directly.

Thanks,
Mauro

#4 lore raw

PATCH v3 2/2 tools: kernel-doc: add a see also section at man pages

In reply to Mauro Carvalho Chehab

While cross-references are complex, as related ones can be on different files, we can at least correlate the ones that belong to the same file, adding a SEE ALSO section for them.

The result is not bad. See for instance:

	$ tools/docs/sphinx-build-wrapper --sphinxdirs driver-api/media -- mandocs
	$ man Documentation/output/driver-api/man/edac_pci_add_device.9

edac_pci_add_device(9) Kernel Hacker's Manual edac_pci_add_device(9)

NAME
edac_pci_add_device - Insert the 'edac_dev' structure into the edac_pci global list and create sysfs entries associated with edac_pci structure.

SYNOPSIS
int edac_pci_add_device (struct edac_pci_ctl_info *pci , int edac_idx );

ARGUMENTS
pci pointer to the edac_device structure to be added to
the list

edac_idx A unique numeric identifier to be assigned to the

RETURN
0 on Success, or an error code on failure

SEE ALSO
edac_pci_alloc_ctl_info(9), edac_pci_free_ctl_info(9),
edac_pci_alloc_index(9), edac_pci_del_device(9), edac_pci_cre‐
ate_generic_ctl(9), edac_pci_release_generic_ctl(9),
edac_pci_create_sysfs(9), edac_pci_remove_sysfs(9)

August 2025 edac_pci_add_device edac_pci_add_device(9)

---
 scripts/lib/kdoc/kdoc_files.py  |  5 +-
 scripts/lib/kdoc/kdoc_output.py | 84 +++++++++++++++++++++++++++++++--
 2 files changed, 83 insertions(+), 6 deletions(-)
scripts/lib/kdoc/kdoc_files.py +4 -1
index 9e09b45b02fa..061c033f32da 100644
@@ -275,7 +275,10 @@ class KernelFiles():
                 self.config.log.warning("No kernel-doc for file %s", fname)
                 continue
 
-            for arg in self.results[fname]:
+            symbols = self.results[fname]
+            self.out_style.set_symbols(symbols)
+
+            for arg in symbols:
                 m = self.out_msg(fname, arg.name, arg)
 
                 if m is None:
scripts/lib/kdoc/kdoc_output.py +79 -5

134 lines not on this page · raw message

Signature · 1 line
2.51.0