sphinx-build-wrapper 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. #!/usr/bin/env python3
  2. # SPDX-License-Identifier: GPL-2.0
  3. # Copyright (C) 2025 Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
  4. #
  5. # pylint: disable=R0902, R0912, R0913, R0914, R0915, R0917, C0103
  6. #
  7. # Converted from docs Makefile and parallel-wrapper.sh, both under
  8. # GPLv2, copyrighted since 2008 by the following authors:
  9. #
  10. # Akira Yokosawa <akiyks@gmail.com>
  11. # Arnd Bergmann <arnd@arndb.de>
  12. # Breno Leitao <leitao@debian.org>
  13. # Carlos Bilbao <carlos.bilbao@amd.com>
  14. # Dave Young <dyoung@redhat.com>
  15. # Donald Hunter <donald.hunter@gmail.com>
  16. # Geert Uytterhoeven <geert+renesas@glider.be>
  17. # Jani Nikula <jani.nikula@intel.com>
  18. # Jan Stancek <jstancek@redhat.com>
  19. # Jonathan Corbet <corbet@lwn.net>
  20. # Joshua Clayton <stillcompiling@gmail.com>
  21. # Kees Cook <keescook@chromium.org>
  22. # Linus Torvalds <torvalds@linux-foundation.org>
  23. # Magnus Damm <damm+renesas@opensource.se>
  24. # Masahiro Yamada <masahiroy@kernel.org>
  25. # Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
  26. # Maxim Cournoyer <maxim.cournoyer@gmail.com>
  27. # Peter Foley <pefoley2@pefoley.com>
  28. # Randy Dunlap <rdunlap@infradead.org>
  29. # Rob Herring <robh@kernel.org>
  30. # Shuah Khan <shuahkh@osg.samsung.com>
  31. # Thorsten Blum <thorsten.blum@toblux.com>
  32. # Tomas Winkler <tomas.winkler@intel.com>
  33. """
  34. Sphinx build wrapper that handles Kernel-specific business rules:
  35. - it gets the Kernel build environment vars;
  36. - it determines what's the best parallelism;
  37. - it handles SPHINXDIRS
  38. This tool ensures that MIN_PYTHON_VERSION is satisfied. If version is
  39. below that, it seeks for a new Python version. If found, it re-runs using
  40. the newer version.
  41. """
  42. import argparse
  43. import locale
  44. import os
  45. import re
  46. import shlex
  47. import shutil
  48. import subprocess
  49. import sys
  50. from concurrent import futures
  51. from glob import glob
  52. LIB_DIR = "../lib/python"
  53. SRC_DIR = os.path.dirname(os.path.realpath(__file__))
  54. sys.path.insert(0, os.path.join(SRC_DIR, LIB_DIR))
  55. from kdoc.python_version import PythonVersion
  56. from kdoc.latex_fonts import LatexFontChecker
  57. from jobserver import JobserverExec # pylint: disable=C0413,C0411,E0401
  58. #
  59. # Some constants
  60. #
  61. VENV_DEFAULT = "sphinx_latest"
  62. MIN_PYTHON_VERSION = PythonVersion("3.7").version
  63. PAPER = ["", "a4", "letter"]
  64. TARGETS = {
  65. "cleandocs": { "builder": "clean" },
  66. "linkcheckdocs": { "builder": "linkcheck" },
  67. "htmldocs": { "builder": "html" },
  68. "epubdocs": { "builder": "epub", "out_dir": "epub" },
  69. "texinfodocs": { "builder": "texinfo", "out_dir": "texinfo" },
  70. "infodocs": { "builder": "texinfo", "out_dir": "texinfo" },
  71. "mandocs": { "builder": "man", "out_dir": "man" },
  72. "latexdocs": { "builder": "latex", "out_dir": "latex" },
  73. "pdfdocs": { "builder": "latex", "out_dir": "latex" },
  74. "xmldocs": { "builder": "xml", "out_dir": "xml" },
  75. }
  76. #
  77. # SphinxBuilder class
  78. #
  79. class SphinxBuilder:
  80. """
  81. Handles a sphinx-build target, adding needed arguments to build
  82. with the Kernel.
  83. """
  84. def get_path(self, path, use_cwd=False, abs_path=False):
  85. """
  86. Ancillary routine to handle patches the right way, as shell does.
  87. It first expands "~" and "~user". Then, if patch is not absolute,
  88. join self.srctree. Finally, if requested, convert to abspath.
  89. """
  90. path = os.path.expanduser(path)
  91. if not path.startswith("/"):
  92. if use_cwd:
  93. base = os.getcwd()
  94. else:
  95. base = self.srctree
  96. path = os.path.join(base, path)
  97. if abs_path:
  98. return os.path.abspath(path)
  99. return path
  100. def check_rust(self, sphinxdirs):
  101. """
  102. Checks if Rust is enabled
  103. """
  104. config = os.path.join(self.srctree, ".config")
  105. if not {'.', 'rust'}.intersection(sphinxdirs):
  106. return False
  107. if not os.path.isfile(config):
  108. return False
  109. re_rust = re.compile(r"CONFIG_RUST=(m|y)")
  110. try:
  111. with open(config, "r", encoding="utf-8") as fp:
  112. for line in fp:
  113. if re_rust.match(line):
  114. return True
  115. except OSError as e:
  116. print(f"Failed to open {config}", file=sys.stderr)
  117. return False
  118. return False
  119. def get_sphinx_extra_opts(self, n_jobs):
  120. """
  121. Get the number of jobs to be used for docs build passed via command
  122. line and desired sphinx verbosity.
  123. The number of jobs can be on different places:
  124. 1) It can be passed via "-j" argument;
  125. 2) The SPHINXOPTS="-j8" env var may have "-j";
  126. 3) if called via GNU make, -j specifies the desired number of jobs.
  127. with GNU makefile, this number is available via POSIX jobserver;
  128. 4) if none of the above is available, it should default to "-jauto",
  129. and let sphinx decide the best value.
  130. """
  131. #
  132. # SPHINXOPTS env var, if used, contains extra arguments to be used
  133. # by sphinx-build time. Among them, it may contain sphinx verbosity
  134. # and desired number of parallel jobs.
  135. #
  136. parser = argparse.ArgumentParser()
  137. parser.add_argument('-j', '--jobs', type=int)
  138. parser.add_argument('-q', '--quiet', action='store_true')
  139. parser.add_argument('-v', '--verbose', default=0, action='count')
  140. #
  141. # Other sphinx-build arguments go as-is, so place them
  142. # at self.sphinxopts, using shell parser
  143. #
  144. sphinxopts = shlex.split(os.environ.get("SPHINXOPTS", ""))
  145. #
  146. # Build a list of sphinx args, honoring verbosity here if specified
  147. #
  148. sphinx_args, self.sphinxopts = parser.parse_known_args(sphinxopts)
  149. verbose = sphinx_args.verbose
  150. if self.verbose:
  151. verbose += 1
  152. if sphinx_args.quiet is True:
  153. verbose = 0
  154. #
  155. # If the user explicitly sets "-j" at command line, use it.
  156. # Otherwise, pick it from SPHINXOPTS args
  157. #
  158. if n_jobs:
  159. self.n_jobs = n_jobs
  160. elif sphinx_args.jobs:
  161. self.n_jobs = sphinx_args.jobs
  162. else:
  163. self.n_jobs = None
  164. if verbose < 1:
  165. self.sphinxopts += ["-q"]
  166. else:
  167. for i in range(1, sphinx_args.verbose):
  168. self.sphinxopts += ["-v"]
  169. def __init__(self, builddir, venv=None, verbose=False, n_jobs=None,
  170. interactive=None):
  171. """Initialize internal variables"""
  172. self.venv = venv
  173. self.verbose = None
  174. #
  175. # Normal variables passed from Kernel's makefile
  176. #
  177. self.kernelversion = os.environ.get("KERNELVERSION", "unknown")
  178. self.kernelrelease = os.environ.get("KERNELRELEASE", "unknown")
  179. self.pdflatex = os.environ.get("PDFLATEX", "xelatex")
  180. #
  181. # Kernel main Makefile defines a PYTHON3 variable whose default is
  182. # "python3". When set to a different value, it allows running a
  183. # diferent version than the default official python3 package.
  184. # Several distros package python3xx-sphinx packages with newer
  185. # versions of Python and sphinx-build.
  186. #
  187. # Honor such variable different than default
  188. #
  189. self.python = os.environ.get("PYTHON3")
  190. if self.python == "python3":
  191. self.python = None
  192. if not interactive:
  193. self.latexopts = os.environ.get("LATEXOPTS", "-interaction=batchmode -no-shell-escape")
  194. else:
  195. self.latexopts = os.environ.get("LATEXOPTS", "")
  196. if not verbose:
  197. verbose = bool(os.environ.get("KBUILD_VERBOSE", "") != "")
  198. if verbose is not None:
  199. self.verbose = verbose
  200. #
  201. # Source tree directory. This needs to be at os.environ, as
  202. # Sphinx extensions use it
  203. #
  204. self.srctree = os.environ.get("srctree")
  205. if not self.srctree:
  206. self.srctree = "."
  207. os.environ["srctree"] = self.srctree
  208. #
  209. # Now that we can expand srctree, get other directories as well
  210. #
  211. self.sphinxbuild = os.environ.get("SPHINXBUILD", "sphinx-build")
  212. self.kerneldoc = self.get_path(os.environ.get("KERNELDOC",
  213. "tools/docs/kernel-doc"))
  214. self.builddir = self.get_path(builddir, use_cwd=True, abs_path=True)
  215. #
  216. # Get directory locations for LaTeX build toolchain
  217. #
  218. self.pdflatex_cmd = shutil.which(self.pdflatex)
  219. self.latexmk_cmd = shutil.which("latexmk")
  220. self.env = os.environ.copy()
  221. self.get_sphinx_extra_opts(n_jobs)
  222. #
  223. # If venv command line argument is specified, run Sphinx from venv
  224. #
  225. if venv:
  226. bin_dir = os.path.join(venv, "bin")
  227. if not os.path.isfile(os.path.join(bin_dir, "activate")):
  228. sys.exit(f"Venv {venv} not found.")
  229. # "activate" virtual env
  230. self.env["PATH"] = bin_dir + ":" + self.env["PATH"]
  231. self.env["VIRTUAL_ENV"] = venv
  232. if "PYTHONHOME" in self.env:
  233. del self.env["PYTHONHOME"]
  234. print(f"Setting venv to {venv}")
  235. def run_sphinx(self, sphinx_build, build_args, *args, **pwargs):
  236. """
  237. Executes sphinx-build using current python3 command.
  238. When calling via GNU make, POSIX jobserver is used to tell how
  239. many jobs are still available from a job pool. claim all remaining
  240. jobs, as we don't want sphinx-build to run in parallel with other
  241. jobs.
  242. Despite that, the user may actually force a different value than
  243. the number of available jobs via command line.
  244. The "with" logic here is used to ensure that the claimed jobs will
  245. be freed once subprocess finishes
  246. """
  247. with JobserverExec() as jobserver:
  248. if jobserver.claim:
  249. #
  250. # when GNU make is used, claim available jobs from jobserver
  251. #
  252. n_jobs = str(jobserver.claim)
  253. else:
  254. #
  255. # Otherwise, let sphinx decide by default
  256. #
  257. n_jobs = "auto"
  258. #
  259. # If explicitly requested via command line, override default
  260. #
  261. if self.n_jobs:
  262. n_jobs = str(self.n_jobs)
  263. #
  264. # We can't simply call python3 sphinx-build, as OpenSUSE
  265. # Tumbleweed uses an ELF binary file (/usr/bin/alts) to switch
  266. # between different versions of sphinx-build. So, only call it
  267. # prepending "python3.xx" when PYTHON3 variable is not default.
  268. #
  269. if self.python:
  270. cmd = [self.python]
  271. else:
  272. cmd = []
  273. cmd += [sphinx_build]
  274. cmd += [f"-j{n_jobs}"]
  275. cmd += build_args
  276. cmd += self.sphinxopts
  277. if self.verbose:
  278. print(" ".join(cmd))
  279. return subprocess.call(cmd, *args, **pwargs)
  280. def handle_html(self, css, output_dir):
  281. """
  282. Extra steps for HTML and epub output.
  283. For such targets, we need to ensure that CSS will be properly
  284. copied to the output _static directory
  285. """
  286. if css:
  287. css = os.path.expanduser(css)
  288. if not css.startswith("/"):
  289. css = os.path.join(self.srctree, css)
  290. static_dir = os.path.join(output_dir, "_static")
  291. os.makedirs(static_dir, exist_ok=True)
  292. try:
  293. shutil.copy2(css, static_dir)
  294. except (OSError, IOError) as e:
  295. print(f"Warning: Failed to copy CSS: {e}", file=sys.stderr)
  296. def build_pdf_file(self, latex_cmd, from_dir, path):
  297. """Builds a single pdf file using latex_cmd"""
  298. try:
  299. subprocess.run(latex_cmd + [path],
  300. cwd=from_dir, check=True, env=self.env)
  301. return True
  302. except subprocess.CalledProcessError:
  303. return False
  304. def pdf_parallel_build(self, tex_suffix, latex_cmd, tex_files, n_jobs):
  305. """Build PDF files in parallel if possible"""
  306. builds = {}
  307. build_failed = False
  308. max_len = 0
  309. has_tex = False
  310. #
  311. # LaTeX PDF error code is almost useless for us:
  312. # any warning makes it non-zero. For kernel doc builds it always return
  313. # non-zero even when build succeeds. So, let's do the best next thing:
  314. # Ignore build errors. At the end, check if all PDF files were built,
  315. # printing a summary with the built ones and returning 0 if all of
  316. # them were actually built.
  317. #
  318. with futures.ThreadPoolExecutor(max_workers=n_jobs) as executor:
  319. jobs = {}
  320. for from_dir, pdf_dir, entry in tex_files:
  321. name = entry.name
  322. if not name.endswith(tex_suffix):
  323. continue
  324. name = name[:-len(tex_suffix)]
  325. has_tex = True
  326. future = executor.submit(self.build_pdf_file, latex_cmd,
  327. from_dir, entry.path)
  328. jobs[future] = (from_dir, pdf_dir, name)
  329. for future in futures.as_completed(jobs):
  330. from_dir, pdf_dir, name = jobs[future]
  331. pdf_name = name + ".pdf"
  332. pdf_from = os.path.join(from_dir, pdf_name)
  333. pdf_to = os.path.join(pdf_dir, pdf_name)
  334. out_name = os.path.relpath(pdf_to, self.builddir)
  335. max_len = max(max_len, len(out_name))
  336. try:
  337. success = future.result()
  338. if success and os.path.exists(pdf_from):
  339. os.rename(pdf_from, pdf_to)
  340. #
  341. # if verbose, get the name of built PDF file
  342. #
  343. if self.verbose:
  344. builds[out_name] = "SUCCESS"
  345. else:
  346. builds[out_name] = "FAILED"
  347. build_failed = True
  348. except futures.Error as e:
  349. builds[out_name] = f"FAILED ({repr(e)})"
  350. build_failed = True
  351. #
  352. # Handle case where no .tex files were found
  353. #
  354. if not has_tex:
  355. out_name = "LaTeX files"
  356. max_len = max(max_len, len(out_name))
  357. builds[out_name] = "FAILED: no .tex files were generated"
  358. build_failed = True
  359. return builds, build_failed, max_len
  360. def handle_pdf(self, output_dirs, deny_vf):
  361. """
  362. Extra steps for PDF output.
  363. As PDF is handled via a LaTeX output, after building the .tex file,
  364. a new build is needed to create the PDF output from the latex
  365. directory.
  366. """
  367. builds = {}
  368. max_len = 0
  369. tex_suffix = ".tex"
  370. tex_files = []
  371. #
  372. # Since early 2024, Fedora and openSUSE tumbleweed have started
  373. # deploying variable-font format of "Noto CJK", causing LaTeX
  374. # to break with CJK. Work around it, by denying the variable font
  375. # usage during xelatex build by passing the location of a config
  376. # file with a deny list.
  377. #
  378. # See tools/docs/lib/latex_fonts.py for more details.
  379. #
  380. if deny_vf:
  381. deny_vf = os.path.expanduser(deny_vf)
  382. if os.path.isdir(deny_vf):
  383. self.env["XDG_CONFIG_HOME"] = deny_vf
  384. for from_dir in output_dirs:
  385. pdf_dir = os.path.join(from_dir, "../pdf")
  386. os.makedirs(pdf_dir, exist_ok=True)
  387. if self.latexmk_cmd:
  388. latex_cmd = [self.latexmk_cmd, f"-{self.pdflatex}"]
  389. else:
  390. latex_cmd = [self.pdflatex]
  391. latex_cmd.extend(shlex.split(self.latexopts))
  392. # Get a list of tex files to process
  393. with os.scandir(from_dir) as it:
  394. for entry in it:
  395. if entry.name.endswith(tex_suffix):
  396. tex_files.append((from_dir, pdf_dir, entry))
  397. #
  398. # When using make, this won't be used, as the number of jobs comes
  399. # from POSIX jobserver. So, this covers the case where build comes
  400. # from command line. On such case, serialize by default, except if
  401. # the user explicitly sets the number of jobs.
  402. #
  403. n_jobs = 1
  404. # n_jobs is either an integer or "auto". Only use it if it is a number
  405. if self.n_jobs:
  406. try:
  407. n_jobs = int(self.n_jobs)
  408. except ValueError:
  409. pass
  410. #
  411. # When using make, jobserver.claim is the number of jobs that were
  412. # used with "-j" and that aren't used by other make targets
  413. #
  414. with JobserverExec() as jobserver:
  415. n_jobs = 1
  416. #
  417. # Handle the case when a parameter is passed via command line,
  418. # using it as default, if jobserver doesn't claim anything
  419. #
  420. if self.n_jobs:
  421. try:
  422. n_jobs = int(self.n_jobs)
  423. except ValueError:
  424. pass
  425. if jobserver.claim:
  426. n_jobs = jobserver.claim
  427. builds, build_failed, max_len = self.pdf_parallel_build(tex_suffix,
  428. latex_cmd,
  429. tex_files,
  430. n_jobs)
  431. #
  432. # In verbose mode, print a summary with the build results per file.
  433. # Otherwise, print a single line with all failures, if any.
  434. # On both cases, return code 1 indicates build failures,
  435. #
  436. if self.verbose:
  437. msg = "Summary"
  438. msg += "\n" + "=" * len(msg)
  439. print()
  440. print(msg)
  441. for pdf_name, pdf_file in builds.items():
  442. print(f"{pdf_name:<{max_len}}: {pdf_file}")
  443. print()
  444. if build_failed:
  445. msg = LatexFontChecker().check()
  446. if msg:
  447. print(msg)
  448. sys.exit("Error: not all PDF files were created.")
  449. elif build_failed:
  450. n_failures = len(builds)
  451. failures = ", ".join(builds.keys())
  452. msg = LatexFontChecker().check()
  453. if msg:
  454. print(msg)
  455. sys.exit(f"Error: Can't build {n_failures} PDF file(s): {failures}")
  456. def handle_info(self, output_dirs):
  457. """
  458. Extra steps for Info output.
  459. For texinfo generation, an additional make is needed from the
  460. texinfo directory.
  461. """
  462. for output_dir in output_dirs:
  463. try:
  464. subprocess.run(["make", "info"], cwd=output_dir, check=True)
  465. except subprocess.CalledProcessError as e:
  466. sys.exit(f"Error generating info docs: {e}")
  467. def handle_man(self, kerneldoc, docs_dir, src_dir, output_dir):
  468. """
  469. Create man pages from kernel-doc output
  470. """
  471. re_kernel_doc = re.compile(r"^\.\.\s+kernel-doc::\s*(\S+)")
  472. re_man = re.compile(r'^\.TH "[^"]*" (\d+) "([^"]*)"')
  473. if docs_dir == src_dir:
  474. #
  475. # Pick the entire set of kernel-doc markups from the entire tree
  476. #
  477. kdoc_files = set([self.srctree])
  478. else:
  479. kdoc_files = set()
  480. for fname in glob(os.path.join(src_dir, "**"), recursive=True):
  481. if os.path.isfile(fname) and fname.endswith(".rst"):
  482. with open(fname, "r", encoding="utf-8") as in_fp:
  483. data = in_fp.read()
  484. for line in data.split("\n"):
  485. match = re_kernel_doc.match(line)
  486. if match:
  487. if os.path.isfile(match.group(1)):
  488. kdoc_files.add(match.group(1))
  489. if not kdoc_files:
  490. sys.exit(f"Directory {src_dir} doesn't contain kernel-doc tags")
  491. cmd = [ kerneldoc, "-m" ] + sorted(kdoc_files)
  492. try:
  493. if self.verbose:
  494. print(" ".join(cmd))
  495. result = subprocess.run(cmd, stdout=subprocess.PIPE, text= True)
  496. if result.returncode:
  497. print(f"Warning: kernel-doc returned {result.returncode} warnings")
  498. except (OSError, ValueError, subprocess.SubprocessError) as e:
  499. sys.exit(f"Failed to create man pages for {src_dir}: {repr(e)}")
  500. fp = None
  501. try:
  502. for line in result.stdout.split("\n"):
  503. match = re_man.match(line)
  504. if not match:
  505. if fp:
  506. fp.write(line + '\n')
  507. continue
  508. if fp:
  509. fp.close()
  510. fname = f"{output_dir}/{match.group(2)}.{match.group(1)}"
  511. if self.verbose:
  512. print(f"Creating {fname}")
  513. fp = open(fname, "w", encoding="utf-8")
  514. fp.write(line + '\n')
  515. finally:
  516. if fp:
  517. fp.close()
  518. def cleandocs(self, builder): # pylint: disable=W0613
  519. """Remove documentation output directory"""
  520. shutil.rmtree(self.builddir, ignore_errors=True)
  521. def build(self, target, sphinxdirs=None,
  522. theme=None, css=None, paper=None, deny_vf=None,
  523. skip_sphinx=False):
  524. """
  525. Build documentation using Sphinx. This is the core function of this
  526. module. It prepares all arguments required by sphinx-build.
  527. """
  528. builder = TARGETS[target]["builder"]
  529. out_dir = TARGETS[target].get("out_dir", "")
  530. #
  531. # Cleandocs doesn't require sphinx-build
  532. #
  533. if target == "cleandocs":
  534. self.cleandocs(builder)
  535. return
  536. if theme:
  537. os.environ["DOCS_THEME"] = theme
  538. #
  539. # Other targets require sphinx-build, so check if it exists
  540. #
  541. if not skip_sphinx:
  542. sphinxbuild = shutil.which(self.sphinxbuild, path=self.env["PATH"])
  543. if not sphinxbuild and target != "mandocs":
  544. sys.exit(f"Error: {self.sphinxbuild} not found in PATH.\n")
  545. if target == "pdfdocs":
  546. if not self.pdflatex_cmd and not self.latexmk_cmd:
  547. sys.exit("Error: pdflatex or latexmk required for PDF generation")
  548. docs_dir = os.path.abspath(os.path.join(self.srctree, "Documentation"))
  549. #
  550. # Fill in base arguments for Sphinx build
  551. #
  552. kerneldoc = self.kerneldoc
  553. if kerneldoc.startswith(self.srctree):
  554. kerneldoc = os.path.relpath(kerneldoc, self.srctree)
  555. if not sphinxdirs:
  556. sphinxdirs = os.environ.get("SPHINXDIRS", ".")
  557. #
  558. # sphinxdirs can be a list or a whitespace-separated string
  559. #
  560. sphinxdirs_list = []
  561. for sphinxdir in sphinxdirs:
  562. if isinstance(sphinxdir, list):
  563. sphinxdirs_list += sphinxdir
  564. else:
  565. sphinxdirs_list += sphinxdir.split()
  566. args = [ "-b", builder, "-c", docs_dir ]
  567. if builder == "latex":
  568. if not paper:
  569. paper = PAPER[1]
  570. args.extend(["-D", f"latex_elements.papersize={paper}paper"])
  571. rustdoc = self.check_rust(sphinxdirs_list)
  572. if rustdoc:
  573. args.extend(["-t", "rustdoc"])
  574. #
  575. # The sphinx-build tool has a bug: internally, it tries to set
  576. # locale with locale.setlocale(locale.LC_ALL, ''). This causes a
  577. # crash if language is not set. Detect and fix it.
  578. #
  579. try:
  580. locale.setlocale(locale.LC_ALL, '')
  581. except locale.Error:
  582. self.env["LC_ALL"] = "C"
  583. #
  584. # Step 1: Build each directory in separate.
  585. #
  586. # This is not the best way of handling it, as cross-references between
  587. # them will be broken, but this is what we've been doing since
  588. # the beginning.
  589. #
  590. output_dirs = []
  591. for sphinxdir in sphinxdirs_list:
  592. src_dir = os.path.join(docs_dir, sphinxdir)
  593. doctree_dir = os.path.join(self.builddir, ".doctrees")
  594. output_dir = os.path.join(self.builddir, sphinxdir, out_dir)
  595. #
  596. # Make directory names canonical
  597. #
  598. src_dir = os.path.normpath(src_dir)
  599. doctree_dir = os.path.normpath(doctree_dir)
  600. output_dir = os.path.normpath(output_dir)
  601. os.makedirs(doctree_dir, exist_ok=True)
  602. os.makedirs(output_dir, exist_ok=True)
  603. output_dirs.append(output_dir)
  604. build_args = args + [
  605. "-d", doctree_dir,
  606. "-D", f"version={self.kernelversion}",
  607. "-D", f"release={self.kernelrelease}",
  608. "-D", f"kerneldoc_srctree={self.srctree}",
  609. src_dir,
  610. output_dir,
  611. ]
  612. if target == "mandocs":
  613. self.handle_man(kerneldoc, docs_dir, src_dir, output_dir)
  614. elif not skip_sphinx:
  615. try:
  616. result = self.run_sphinx(sphinxbuild, build_args,
  617. env=self.env)
  618. if result:
  619. sys.exit(f"Build failed: return code: {result}")
  620. except (OSError, ValueError, subprocess.SubprocessError) as e:
  621. sys.exit(f"Build failed: {repr(e)}")
  622. #
  623. # Ensure that each html/epub output will have needed static files
  624. #
  625. if target in ["htmldocs", "epubdocs"]:
  626. self.handle_html(css, output_dir)
  627. #
  628. # Step 2: Some targets (PDF and info) require an extra step once
  629. # sphinx-build finishes
  630. #
  631. if target == "pdfdocs":
  632. self.handle_pdf(output_dirs, deny_vf)
  633. elif target == "infodocs":
  634. self.handle_info(output_dirs)
  635. if rustdoc and target in ["htmldocs", "epubdocs"]:
  636. print("Building rust docs")
  637. if "MAKE" in self.env:
  638. cmd = [self.env["MAKE"]]
  639. else:
  640. cmd = ["make", "LLVM=1"]
  641. cmd += [ "rustdoc"]
  642. if self.verbose:
  643. print(" ".join(cmd))
  644. try:
  645. subprocess.run(cmd, check=True)
  646. except subprocess.CalledProcessError as e:
  647. print(f"Ignored errors when building rustdoc: {e}. Is RUST enabled?",
  648. file=sys.stderr)
  649. def jobs_type(value):
  650. """
  651. Handle valid values for -j. Accepts Sphinx "-jauto", plus a number
  652. equal or bigger than one.
  653. """
  654. if value is None:
  655. return None
  656. if value.lower() == 'auto':
  657. return value.lower()
  658. try:
  659. if int(value) >= 1:
  660. return value
  661. raise argparse.ArgumentTypeError(f"Minimum jobs is 1, got {value}")
  662. except ValueError:
  663. raise argparse.ArgumentTypeError(f"Must be 'auto' or positive integer, got {value}") # pylint: disable=W0707
  664. EPILOG="""
  665. Besides the command line arguments, several environment variables affect its
  666. default behavior, meant to be used when called via Kernel Makefile:
  667. - KERNELVERSION: Kernel major version
  668. - KERNELRELEASE: Kernel release
  669. - KBUILD_VERBOSE: Contains the value of "make V=[0|1] variable.
  670. When V=0 (KBUILD_VERBOSE=0), sets verbose level to "-q".
  671. - SPHINXBUILD: Documentation build tool (default: "sphinx-build").
  672. - SPHINXOPTS: Extra options pased to SPHINXBUILD
  673. (default: "-j auto" and "-q" if KBUILD_VERBOSE=0).
  674. The "-v" flag can be used to increase verbosity.
  675. If V=0, the first "-v" will drop "-q".
  676. - PYTHON3: Python command to run SPHINXBUILD
  677. - PDFLATEX: LaTeX PDF engine. (default: "xelatex")
  678. - LATEXOPTS: Optional set of command line arguments to the LaTeX engine
  679. - srctree: Location of the Kernel root directory (default: ".").
  680. """
  681. def main():
  682. """
  683. Main function. The only mandatory argument is the target. If not
  684. specified, the other arguments will use default values if not
  685. specified at os.environ.
  686. """
  687. parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter,
  688. description=__doc__,
  689. epilog=EPILOG)
  690. parser.add_argument("target", choices=list(TARGETS.keys()),
  691. help="Documentation target to build")
  692. parser.add_argument("--sphinxdirs", nargs="+",
  693. help="Specific directories to build")
  694. parser.add_argument("--builddir", default="output",
  695. help="Sphinx configuration file (default: %(default)s)")
  696. parser.add_argument("--theme", help="Sphinx theme to use")
  697. parser.add_argument("--css", help="Custom CSS file for HTML/EPUB")
  698. parser.add_argument("--paper", choices=PAPER, default=PAPER[0],
  699. help="Paper size for LaTeX/PDF output")
  700. parser.add_argument('--deny-vf',
  701. help="Configuration to deny variable fonts on pdf builds")
  702. parser.add_argument("-v", "--verbose", action='store_true',
  703. help="place build in verbose mode")
  704. parser.add_argument('-j', '--jobs', type=jobs_type,
  705. help="Sets number of jobs to use with sphinx-build(default: auto)")
  706. parser.add_argument('-i', '--interactive', action='store_true',
  707. help="Change latex default to run in interactive mode")
  708. parser.add_argument('-s', '--skip-sphinx-build', action='store_true',
  709. help="Skip sphinx-build step")
  710. parser.add_argument("-V", "--venv", nargs='?', const=f'{VENV_DEFAULT}',
  711. default=None,
  712. help=f'If used, run Sphinx from a venv dir (default dir: {VENV_DEFAULT})')
  713. args = parser.parse_args()
  714. PythonVersion.check_python(MIN_PYTHON_VERSION, show_alternatives=True,
  715. bail_out=True)
  716. builder = SphinxBuilder(builddir=args.builddir, venv=args.venv,
  717. verbose=args.verbose, n_jobs=args.jobs,
  718. interactive=args.interactive)
  719. builder.build(args.target, sphinxdirs=args.sphinxdirs,
  720. theme=args.theme, css=args.css, paper=args.paper,
  721. deny_vf=args.deny_vf,
  722. skip_sphinx=args.skip_sphinx_build)
  723. if __name__ == "__main__":
  724. main()