Re: [PATCH] rust: support `srctree`-relative links

From: Martin Rodriguez Reboredo
Date: Sat Dec 16 2023 - 08:56:42 EST


On 12/15/23 20:54, Miguel Ojeda wrote:
Some of our links use relative paths in order to point to files in the
source tree, e.g.:

//! C header: [`include/linux/printk.h`](../../../../include/linux/printk.h)
/// [`struct mutex`]: ../../../../include/linux/mutex.h

These are problematic because they are hard to maintain and do not support
`O=` builds.

Instead, provide support for `srctree`-relative links, e.g.:

//! C header: [`include/linux/printk.h`](srctree/include/linux/printk.h)
/// [`struct mutex`]: srctree/include/linux/mutex.h

The links are fixed after `rustdoc` generation to be based on the absolute
path to the source tree.

Essentially, this is the automatic version of Tomonori's fix [1],
suggested by Gary [2].

Suggested-by: Gary Guo <gary@xxxxxxxxxxx>
Reported-by: FUJITA Tomonori <fujita.tomonori@xxxxxxxxx>
Closes: https://lore.kernel.org/r/20231026.204058.2167744626131849993.fujita.tomonori@xxxxxxxxx [1]
Fixes: 48fadf440075 ("docs: Move rustdoc output, cross-reference it")
Link: https://lore.kernel.org/rust-for-linux/20231026154525.6d14b495@eugeo/ [2]
Signed-off-by: Miguel Ojeda <ojeda@xxxxxxxxxx>
---
Documentation/rust/coding-guidelines.rst | 13 +++++++++++++
rust/Makefile | 3 ++-
rust/kernel/error.rs | 2 +-
rust/kernel/ioctl.rs | 2 +-
rust/kernel/kunit.rs | 2 +-
rust/kernel/print.rs | 8 ++++----
rust/kernel/sync/condvar.rs | 2 +-
rust/kernel/sync/lock/mutex.rs | 2 +-
rust/kernel/sync/lock/spinlock.rs | 2 +-
rust/kernel/task.rs | 2 +-
rust/kernel/workqueue.rs | 2 +-
rust/macros/lib.rs | 2 +-
12 files changed, 28 insertions(+), 14 deletions(-)

diff --git a/Documentation/rust/coding-guidelines.rst b/Documentation/rust/coding-guidelines.rst
index aa8ed082613e..05542840b16c 100644
[...]
diff --git a/rust/Makefile b/rust/Makefile
index 543b37f6c77f..73ea24117f07 100644
--- a/rust/Makefile
+++ b/rust/Makefile
@@ -98,7 +98,8 @@ rustdoc: rustdoc-core rustdoc-macros rustdoc-compiler_builtins \
$(Q)find $(rustdoc_output) -name '*.html' -type f -print0 | xargs -0 sed -Ei \
-e 's:rust-logo-[0-9a-f]+\.svg:logo.svg:g' \
-e 's:favicon-[0-9a-f]+\.svg:logo.svg:g' \
- -e 's:<link rel="alternate icon" type="image/png" href="[/.]+/static\.files/favicon-(16x16|32x32)-[0-9a-f]+\.png">::g'
+ -e 's:<link rel="alternate icon" type="image/png" href="[/.]+/static\.files/favicon-(16x16|32x32)-[0-9a-f]+\.png">::g' \
+ -e 's:<a href="srctree/([^"]+)">:<a href="$(abs_srctree)/\1">:g'

Another way would be to not put `srctree` in the link and add
`abs_srctree` here to directories that are in `srctree`. But that might
be too wonky to rely on so this will be the way to go.

$(Q)for f in $(rustdoc_output)/static.files/rustdoc-*.css; do \
echo ".logo-container > img { object-fit: contain; }" >> $$f; done
[...]

Reviewed-by: Martin Rodriguez Reboredo <yakoyoku@xxxxxxxxx>