# SPDX-License-Identifier: BSD-3-Clause # Copyright (c) 2026, The OpenROAD Authors """Bazel rule that invokes docs/Makefile to produce cat/ and html/ man pages. The output filenames aren't known at analysis time (they depend on which modules exist under src/*/), so outputs are declared as TreeArtifacts and the real work is delegated to the `bazel-manpages` Makefile target. Host requirements: pandoc, nroff (groff), col (bsdextrautils), python3>=3.10. """ def _man_pages_resource_set(_os, _num_inputs): # The 'cat web' make below fans out with -j$(nproc), so this action uses # the whole host. Reserve all local CPUs to keep Bazel from co-scheduling # other heavy actions alongside it and oversubscribing the machine. Bazel # clamps the request to the cores actually available, so this is safe on # small CI hosts too. return {"cpu": 512.0} def _man_pages_impl(ctx): cat_dir = ctx.actions.declare_directory("cat") html_dir = ctx.actions.declare_directory("html") command = """ set -euo pipefail CAT_OUT="$PWD/{cat_out}" HTML_OUT="$PWD/{html_out}" # The messages.txt files that man3 is built from are generated, so they live # under bazel-out rather than in the source tree the execroot symlinks in. # md_roff_compat.py looks for /src//messages.txt and # /messages.txt, which is exactly the bin dir's layout. export MESSAGES_ROOT_DIR="$PWD/{bin_dir}" # Two phases: 'preprocess' (serial) generates the md/man*/*.md sources, then # 'cat web' fan out pandoc/nroff in parallel. They cannot share one -j make # invocation: cat/web read the md files preprocess produces, and a parallel # build has no dependency edge forcing preprocess to finish first. Running # 'cat web' as a second invocation also re-parses the Makefile so its # $(wildcard md/man*/*.md) picks up the freshly generated sources. # nproc is GNU coreutils (absent on stock macOS); fall back to sysctl, then 4. JOBS="$(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 4)" # -s (silent) suppresses make's per-recipe echo. There are ~3400 man pages and # four recipes each, so echoing them emits megabytes of stdout, past Bazel's # --experimental_ui_max_stdouterr_bytes, which then drops the whole log -- # including the pandoc/groff diagnostics that are worth seeing. Those tools # name the file they failed on, so the echoed command adds nothing. make -s --no-print-directory -C docs -f Makefile preprocess \\ CAT_ROOT_DIR="$CAT_OUT" HTML_ROOT_DIR="$HTML_OUT" make -s --no-print-directory -j"$JOBS" -C docs -f Makefile cat web \\ CAT_ROOT_DIR="$CAT_OUT" HTML_ROOT_DIR="$HTML_OUT" """.format( bin_dir = ctx.bin_dir.path, cat_out = cat_dir.path, html_out = html_dir.path, ) ctx.actions.run_shell( resource_set = _man_pages_resource_set, outputs = [cat_dir, html_dir], inputs = ctx.files.docs_srcs + ctx.files.scripts + ctx.files.readmes + ctx.files.messages, command = command, mnemonic = "ManPages", progress_message = "Generating man pages (cat + html)", use_default_shell_env = True, execution_requirements = {"no-sandbox": "1"}, ) return [DefaultInfo( files = depset([cat_dir, html_dir]), runfiles = ctx.runfiles(files = [cat_dir, html_dir]), )] man_pages = rule( implementation = _man_pages_impl, attrs = { "docs_srcs": attr.label_list( doc = "All source files under docs/ needed by the Makefile.", allow_files = True, ), "messages": attr.label_list( doc = "Module messages.txt files needed for man3 page generation.", allow_files = [".txt"], ), "readmes": attr.label_list( doc = "Module README.md files (src/*/README.md).", allow_files = [".md"], ), "scripts": attr.label_list( doc = "Python/shell scripts for man page generation.", allow_files = True, ), }, )