Skip to content

Commit 0f042ce

Browse files
committed
Add klint-rustdoc command as a helper to invoke rustdoc
1 parent e1e8226 commit 0f042ce

5 files changed

Lines changed: 34 additions & 2 deletions

File tree

Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ name = "klint"
77
version = "0.1.0"
88
edition = "2024"
99
license = "MIT OR Apache-2.0"
10+
default-run = "klint"
1011

1112
[dependencies]
1213
rusqlite = "0.37"
@@ -18,6 +19,14 @@ iced-x86 = { version = "1.21.0", default-features = false, features = ["std", "d
1819
[dev-dependencies]
1920
compiletest_rs = { version = "0.11", features = [ "tmp" ] }
2021

22+
[[bin]]
23+
name = "klint"
24+
path = "src/main.rs"
25+
26+
[[bin]]
27+
name = "klint-rustdoc"
28+
path = "src/rustdoc.rs"
29+
2130
[package.metadata.rust-analyzer]
2231
# This crate uses #![feature(rustc_private)]
2332
rustc_private = true

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ This patch can be used even if plain rustc or clippy is used for kernel build.
4141
To run this tool for Linux kernel build, use `make RUSTC=<path to klint>` to use klint in place of a Rust compiler.
4242

4343
If you compile kernel with rustdoc tests as kunit tests, you also need a matching version of `rustdoc`.
44-
For Nix users, this is available as symlink `klint-rustdoc`, you can use add `RUSTDOC=klint-rustdoc` to make command line.
44+
As it can be tricky to get the versions to match, `klint` provides a `klint-rustdoc` binary will execute the correct rustdoc.
45+
You can add `RUSTDOC="<path to klint>-rustdoc"` to the make command.
4546

4647
`klint`'s atomic context checker is not lint-clean on Linux kernel tree.
4748
If you want to check it out, you can opt into it with `-Dklint::atomic_context`.

build.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,16 @@ fn main() {
1717
// binary would be missing the necessary RPATH so it cannot run without using Cargo.
1818
let sysroot = probe_sysroot();
1919
println!("cargo::rustc-link-arg=-Wl,-rpath={sysroot}/lib");
20+
21+
// If the RUSTDOC environment variable is just a plain "rustdoc", we want to discover the full path.
22+
// NB: this is the case when built from nix.
23+
let mut rustdoc = std::env::var("RUSTDOC").unwrap_or_default();
24+
if rustdoc.is_empty() || rustdoc == "rustdoc" {
25+
rustdoc = format!("{sysroot}/bin/rustdoc");
26+
assert!(
27+
std::fs::exists(&rustdoc).unwrap_or_default(),
28+
"Cannot find RUSTDOC. This is an unknown environment, please file a bug report."
29+
);
30+
}
31+
println!("cargo::rustc-env=RUSTDOC={rustdoc}");
2032
}

flake.nix

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,10 @@
6464
doCheck = false;
6565

6666
# If kernel rustdoc tests are enabled, user would need a matching version of rustdoc.
67+
# klint provides a klint-rustdoc binary to ease the process. However, for nix, we already
68+
# know the path to the rustdoc binary, so just symlink and replace the wrapper.
6769
postInstall = ''
68-
ln -s "${lib.getExe' rustc "rustdoc"}" $out/bin/klint-rustdoc
70+
ln -sf "${lib.getExe' rustc "rustdoc"}" $out/bin/klint-rustdoc
6971
'';
7072

7173
passthru.rustc = rustc;

src/rustdoc.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use std::io::Result;
2+
use std::os::unix::process::CommandExt;
3+
4+
fn main() -> Result<()> {
5+
Err(std::process::Command::new(env!("RUSTDOC"))
6+
.args(std::env::args().skip(1))
7+
.exec())
8+
}

0 commit comments

Comments
 (0)