|
| 1 | +//! Auxiliary tools for checking various documentation. |
| 2 | +
|
| 3 | +use super::test_helpers::run_cargo_test; |
| 4 | +use crate::core::build_steps::tool::{self, SourceType, Tool}; |
| 5 | +use crate::core::builder::{Builder, Kind, RunConfig, ShouldRun, Step}; |
| 6 | +use crate::core::config::TargetSelection; |
| 7 | +use crate::utils::exec::command; |
| 8 | +use crate::utils::helpers::{self}; |
| 9 | +use crate::{DocTests, Mode}; |
| 10 | + |
| 11 | +#[derive(Debug, Clone, PartialEq, Eq, Hash)] |
| 12 | +pub struct Linkcheck { |
| 13 | + host: TargetSelection, |
| 14 | +} |
| 15 | + |
| 16 | +impl Step for Linkcheck { |
| 17 | + type Output = (); |
| 18 | + const ONLY_HOSTS: bool = true; |
| 19 | + const DEFAULT: bool = true; |
| 20 | + |
| 21 | + /// Runs the `linkchecker` tool as compiled in `stage` by the `host` compiler. |
| 22 | + /// |
| 23 | + /// This tool in `src/tools` will verify the validity of all our links in the |
| 24 | + /// documentation to ensure we don't have a bunch of dead ones. |
| 25 | + fn run(self, builder: &Builder<'_>) { |
| 26 | + let host = self.host; |
| 27 | + let hosts = &builder.hosts; |
| 28 | + let targets = &builder.targets; |
| 29 | + |
| 30 | + // if we have different hosts and targets, some things may be built for |
| 31 | + // the host (e.g. rustc) and others for the target (e.g. std). The |
| 32 | + // documentation built for each will contain broken links to |
| 33 | + // docs built for the other platform (e.g. rustc linking to cargo) |
| 34 | + if (hosts != targets) && !hosts.is_empty() && !targets.is_empty() { |
| 35 | + panic!( |
| 36 | + "Linkcheck currently does not support builds with different hosts and targets. |
| 37 | +You can skip linkcheck with --skip src/tools/linkchecker" |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + builder.info(&format!("Linkcheck ({host})")); |
| 42 | + |
| 43 | + // Test the linkchecker itself. |
| 44 | + let bootstrap_host = builder.config.build; |
| 45 | + let compiler = builder.compiler(0, bootstrap_host); |
| 46 | + |
| 47 | + let cargo = tool::prepare_tool_cargo( |
| 48 | + builder, |
| 49 | + compiler, |
| 50 | + Mode::ToolBootstrap, |
| 51 | + bootstrap_host, |
| 52 | + Kind::Test, |
| 53 | + "src/tools/linkchecker", |
| 54 | + SourceType::InTree, |
| 55 | + &[], |
| 56 | + ); |
| 57 | + run_cargo_test( |
| 58 | + cargo, |
| 59 | + &[], |
| 60 | + &[], |
| 61 | + "linkchecker", |
| 62 | + "linkchecker self tests", |
| 63 | + bootstrap_host, |
| 64 | + builder, |
| 65 | + ); |
| 66 | + |
| 67 | + if builder.doc_tests == DocTests::No { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + // Build all the default documentation. |
| 72 | + builder.default_doc(&[]); |
| 73 | + |
| 74 | + // Build the linkchecker before calling `msg`, since GHA doesn't support nested groups. |
| 75 | + let linkchecker = builder.tool_cmd(Tool::Linkchecker); |
| 76 | + |
| 77 | + // Run the linkchecker. |
| 78 | + let _guard = |
| 79 | + builder.msg(Kind::Test, compiler.stage, "Linkcheck", bootstrap_host, bootstrap_host); |
| 80 | + let _time = helpers::timeit(builder); |
| 81 | + linkchecker.delay_failure().arg(builder.out.join(host).join("doc")).run(builder); |
| 82 | + } |
| 83 | + |
| 84 | + fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { |
| 85 | + let builder = run.builder; |
| 86 | + let run = run.path("src/tools/linkchecker"); |
| 87 | + run.default_condition(builder.config.docs) |
| 88 | + } |
| 89 | + |
| 90 | + fn make_run(run: RunConfig<'_>) { |
| 91 | + run.builder.ensure(Linkcheck { host: run.target }); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +fn check_if_tidy_is_installed(builder: &Builder<'_>) -> bool { |
| 96 | + command("tidy").allow_failure().arg("--version").run_capture_stdout(builder).is_success() |
| 97 | +} |
| 98 | + |
| 99 | +#[derive(Debug, Clone, PartialEq, Eq, Hash)] |
| 100 | +pub struct HtmlCheck { |
| 101 | + target: TargetSelection, |
| 102 | +} |
| 103 | + |
| 104 | +impl Step for HtmlCheck { |
| 105 | + type Output = (); |
| 106 | + const DEFAULT: bool = true; |
| 107 | + const ONLY_HOSTS: bool = true; |
| 108 | + |
| 109 | + fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { |
| 110 | + let builder = run.builder; |
| 111 | + let run = run.path("src/tools/html-checker"); |
| 112 | + run.lazy_default_condition(Box::new(|| check_if_tidy_is_installed(builder))) |
| 113 | + } |
| 114 | + |
| 115 | + fn make_run(run: RunConfig<'_>) { |
| 116 | + run.builder.ensure(HtmlCheck { target: run.target }); |
| 117 | + } |
| 118 | + |
| 119 | + fn run(self, builder: &Builder<'_>) { |
| 120 | + if !check_if_tidy_is_installed(builder) { |
| 121 | + eprintln!("not running HTML-check tool because `tidy` is missing"); |
| 122 | + eprintln!( |
| 123 | + "You need the HTML tidy tool https://www.html-tidy.org/, this tool is *not* part of the rust project and needs to be installed separately, for example via your package manager." |
| 124 | + ); |
| 125 | + panic!("Cannot run html-check tests"); |
| 126 | + } |
| 127 | + // Ensure that a few different kinds of documentation are available. |
| 128 | + builder.default_doc(&[]); |
| 129 | + builder.ensure(crate::core::build_steps::doc::Rustc::new( |
| 130 | + builder.top_stage, |
| 131 | + self.target, |
| 132 | + builder, |
| 133 | + )); |
| 134 | + |
| 135 | + builder |
| 136 | + .tool_cmd(Tool::HtmlChecker) |
| 137 | + .delay_failure() |
| 138 | + .arg(builder.doc_out(self.target)) |
| 139 | + .run(builder); |
| 140 | + } |
| 141 | +} |
0 commit comments