Skip to content

eBPF-based security platform

docs: rebuild the preview when source files change

Andrea Lattuadaeaf5dc81dd4f
diff --git a/Documentation/Makefile b/Documentation/Makefile
index 557f7f8d..a36a7c7e 100644
--- a/Documentation/Makefile
+++ b/Documentation/Makefile
@@ -48,7 +48,7 @@ RUN_SERVE = $(CONTAINER) run --rm --read-only --cap-drop=ALL \
help:
@printf '%s\n' \
'make -C Documentation html render the HTML into $(DOCS_OUT)/html' \
- 'make -C Documentation serve render, then serve it on $(DOCS_BIND):$(DOCS_PORT)' \
+ 'make -C Documentation serve render, serve on $(DOCS_BIND):$(DOCS_PORT), watch with fswatch or inotifywait' \
'make -C Documentation check render, then check the docs against the code' \
'make -C Documentation linkcheck check external links (needs the network)' \
'make -C Documentation figures draw the SVGs of images/ from figures/ (needs TeX)' \
@@ -107,7 +107,8 @@ html-docs: html
serve: html
@printf 'shirudo: serving http://%s:%s/ (Ctrl-C stops it)\n' \
'$(DOCS_BIND)' '$(DOCS_PORT)'
- $(Q)$(RUN_SERVE) python3 -m http.server --directory /srv --bind 0.0.0.0 8000
+ +$(Q)bash "$(DOCS)/serve.sh" "$(MAKE)" "$(OUT)" $(RUN_SERVE) \
+ python3 -m http.server --directory /srv --bind 0.0.0.0 8000
check: html
$(Q)$(ROOT)/.github/scripts/check-docs.sh
diff --git a/Documentation/contribute/code/developer-guide.rst b/Documentation/contribute/code/developer-guide.rst
index ff3f3011..2cae1cd3 100644
--- a/Documentation/contribute/code/developer-guide.rst
+++ b/Documentation/contribute/code/developer-guide.rst
@@ -349,7 +349,8 @@ Other make targets
:doc:`/start/install`.
* - ``make docs``
- Renders the documentation and serves it on
- ``http://127.0.0.1:8000``. See
+ ``http://127.0.0.1:8000``. Rebuilds on changes with ``fswatch`` or
+ ``inotifywait``. See
:doc:`/contribute/documentation/writing-docs`.
* - ``make tags``, ``make cscope``
- Index the C sources, untracked ones and the generated
diff --git a/Documentation/contribute/documentation/writing-docs.rst b/Documentation/contribute/documentation/writing-docs.rst
index d7d91c02..de5b1031 100644
--- a/Documentation/contribute/documentation/writing-docs.rst
+++ b/Documentation/contribute/documentation/writing-docs.rst
@@ -253,6 +253,11 @@ Render and serve the documentation on ``http://127.0.0.1:8000``:
$ make docs
+If ``fswatch`` or ``inotifywait`` is on your ``PATH``, the preview rebuilds
+when files under ``Documentation/`` change. Refresh the page to see the
+new build. Install ``fswatch`` or the ``inotify-tools`` package to enable
+automatic rebuilds.
+
Render it and check it against the code, as CI does:
.. code-block:: console
diff --git a/Documentation/serve.sh b/Documentation/serve.sh
new file mode 100644
index 00000000..391ec4b8
--- /dev/null
+++ b/Documentation/serve.sh
@@ -0,0 +1,72 @@
+#!/usr/bin/env bash
+# SPDX-License-Identifier: BSD-2-Clause
+# Copyright (c) 2026 Authors of shirudo
+
+set -eu
+
+# Make runs recursive recipes even in dry-run, question and touch modes.
+# Older versions put long options before the group of short flags.
+read -ra makeflags <<< "${MAKEFLAGS-}"
+for flag in "${makeflags[@]}"; do
+ case $flag in
+ --) break ;;
+ --*) continue ;;
+ *[nqt]*) exit 0 ;;
+ *) break ;;
+ esac
+done
+
+make=$1
+out=$2
+shift 2
+
+if command -v fswatch >/dev/null 2>&1; then
+ watcher=fswatch
+elif command -v inotifywait >/dev/null 2>&1; then
+ watcher=inotifywait
+else
+ printf '%s\n' 'hint: install fswatch or inotify-tools to rebuild docs on changes' >&2
+ exec "$@"
+fi
+
+# Both watchers report absolute paths. Ignore generated files so a
+# rebuild cannot trigger another.
+docs=$(pwd -P)
+out=$(cd "$out" && pwd -P)
+out_pattern=$(printf '%s\n' "$out" | sed 's/[][(){}.^$*+?|\\]/\\&/g')
+exclude="^${out_pattern}(/|$)"
+
+if [[ $watcher == fswatch ]]; then
+ watch=(fswatch -orE -e "$exclude" "$docs")
+else
+ watch=(inotifywait -qmr -e close_write,create,delete,move --exclude "$exclude"
+ --format '%e' "$docs" "@$out")
+fi
+
+# Separate process groups let us stop the watcher and any rebuild it is
+# running when the server exits, or stop both jobs on Ctrl-C.
+set -m
+(
+ set +m
+ "${watch[@]}" | while IFS= read -r event; do
+ "$make" html || printf '%s\n' 'docs serve: docs rebuild failed; watching for the next change' >&2
+ done
+ printf 'docs serve: %s stopped; docs will no longer rebuild automatically\n' "$watcher" >&2
+) &
+watch_pid=$!
+
+"$@" &
+server_pid=$!
+
+cleanup()
+{
+ kill -TERM -- "-$watch_pid" 2>/dev/null || :
+ kill -INT -- "-$server_pid" 2>/dev/null || :
+ wait "$watch_pid" "$server_pid" 2>/dev/null || :
+}
+trap cleanup EXIT
+trap 'exit 130' INT
+trap 'exit 143' TERM
+
+printf 'docs serve: watching Documentation/ for changes with %s\n' "$watcher"
+wait "$server_pid"
diff --git a/Makefile b/Makefile
index 27bc5235..cff65fc0 100644
--- a/Makefile
+++ b/Makefile
@@ -96,7 +96,7 @@ help:
' Package: make -C packaging help' \
' Tests: make tests (all of them), make -C testing help' \
' Proofs: make proofs (CBMC and Lean 4), make proofs-cbmc [PROOFS=<proof>...], make proofs-lean4' \
- ' Documentation: make docs (render and serve), make -C Documentation help' \
+ ' Documentation: make docs (render, serve and watch), make -C Documentation help' \
' Watch lists: make -C upatches/kernel-obs lists'
shirudo-build: