Skip to content

Commit 53ef052

Browse files
committed
Integrate mdbook-spec for the reference.
This updates the reference which is now using a new mdbook plugin. This requires a little extra work than a normal book because the plugin uses `rustdoc` to generate links to the standard library. It also ensures that the submodule is available for *any* command that uses rustbook, since it is now part of the rustbook workspace.
1 parent a20db06 commit 53ef052

File tree

8 files changed

+102
-13
lines changed

8 files changed

+102
-13
lines changed

src/bootstrap/src/core/build_steps/doc.rs

+63-9
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
1010
use std::io::{self, Write};
1111
use std::path::{Path, PathBuf};
12-
use std::{fs, mem};
12+
use std::{env, fs, mem};
1313

1414
use crate::core::build_steps::compile;
1515
use crate::core::build_steps::tool::{self, prepare_tool_cargo, SourceType, Tool};
@@ -62,6 +62,7 @@ macro_rules! book {
6262
src: builder.src.join($path),
6363
parent: Some(self),
6464
languages: $lang.into(),
65+
rustdoc: None,
6566
})
6667
}
6768
}
@@ -80,7 +81,6 @@ book!(
8081
EditionGuide, "src/doc/edition-guide", "edition-guide", &[], submodule;
8182
EmbeddedBook, "src/doc/embedded-book", "embedded-book", &[], submodule;
8283
Nomicon, "src/doc/nomicon", "nomicon", &[], submodule;
83-
Reference, "src/doc/reference", "reference", &[], submodule;
8484
RustByExample, "src/doc/rust-by-example", "rust-by-example", &["ja"], submodule;
8585
RustdocBook, "src/doc/rustdoc", "rustdoc", &[];
8686
StyleGuide, "src/doc/style-guide", "style-guide", &[];
@@ -112,6 +112,7 @@ impl Step for UnstableBook {
112112
src: builder.md_doc_out(self.target).join("unstable-book"),
113113
parent: Some(self),
114114
languages: vec![],
115+
rustdoc: None,
115116
})
116117
}
117118
}
@@ -123,6 +124,7 @@ struct RustbookSrc<P: Step> {
123124
src: PathBuf,
124125
parent: Option<P>,
125126
languages: Vec<&'static str>,
127+
rustdoc: Option<PathBuf>,
126128
}
127129

128130
impl<P: Step> Step for RustbookSrc<P> {
@@ -153,13 +155,18 @@ impl<P: Step> Step for RustbookSrc<P> {
153155
builder.info(&format!("Rustbook ({target}) - {name}"));
154156
let _ = fs::remove_dir_all(&out);
155157

156-
builder
157-
.tool_cmd(Tool::Rustbook)
158-
.arg("build")
159-
.arg(&src)
160-
.arg("-d")
161-
.arg(&out)
162-
.run(builder);
158+
let mut rustbook_cmd = builder.tool_cmd(Tool::Rustbook);
159+
if let Some(mut rustdoc) = self.rustdoc {
160+
rustdoc.pop();
161+
let old_path = env::var_os("PATH").unwrap_or_default();
162+
let new_path =
163+
env::join_paths(std::iter::once(rustdoc).chain(env::split_paths(&old_path)))
164+
.expect("could not add rustdoc to PATH");
165+
166+
rustbook_cmd.env("PATH", new_path);
167+
}
168+
169+
rustbook_cmd.arg("build").arg(&src).arg("-d").arg(&out).run(builder);
163170

164171
for lang in &self.languages {
165172
let out = out.join(lang);
@@ -232,6 +239,7 @@ impl Step for TheBook {
232239
src: absolute_path.clone(),
233240
parent: Some(self),
234241
languages: vec![],
242+
rustdoc: None,
235243
});
236244

237245
// building older edition redirects
@@ -244,6 +252,7 @@ impl Step for TheBook {
244252
// treat the other editions as not having a parent.
245253
parent: Option::<Self>::None,
246254
languages: vec![],
255+
rustdoc: None,
247256
});
248257
}
249258

@@ -1214,6 +1223,51 @@ impl Step for RustcBook {
12141223
src: out_base,
12151224
parent: Some(self),
12161225
languages: vec![],
1226+
rustdoc: None,
1227+
});
1228+
}
1229+
}
1230+
1231+
#[derive(Ord, PartialOrd, Debug, Clone, Hash, PartialEq, Eq)]
1232+
pub struct Reference {
1233+
pub compiler: Compiler,
1234+
pub target: TargetSelection,
1235+
}
1236+
1237+
impl Step for Reference {
1238+
type Output = ();
1239+
const DEFAULT: bool = true;
1240+
const ONLY_HOSTS: bool = true;
1241+
1242+
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
1243+
let builder = run.builder;
1244+
run.path("src/doc/reference").default_condition(builder.config.docs)
1245+
}
1246+
1247+
fn make_run(run: RunConfig<'_>) {
1248+
run.builder.ensure(Reference {
1249+
compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
1250+
target: run.target,
1251+
});
1252+
}
1253+
1254+
/// Builds the reference book.
1255+
fn run(self, builder: &Builder<'_>) {
1256+
builder.require_and_update_submodule("src/doc/reference", None);
1257+
1258+
// This is needed for generating links to the standard library using
1259+
// the mdbook-spec plugin.
1260+
builder.ensure(compile::Std::new(self.compiler, builder.config.build));
1261+
let rustdoc = builder.rustdoc(self.compiler);
1262+
1263+
// Run rustbook/mdbook to generate the HTML pages.
1264+
builder.ensure(RustbookSrc {
1265+
target: self.target,
1266+
name: "reference".to_owned(),
1267+
src: builder.src.join("src/doc/reference"),
1268+
parent: Some(self),
1269+
languages: vec![],
1270+
rustdoc: Some(rustdoc),
12171271
});
12181272
}
12191273
}

src/bootstrap/src/core/build_steps/tool.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ bootstrap_tool!(
348348

349349
/// These are the submodules that are required for rustbook to work due to
350350
/// depending on mdbook plugins.
351-
pub static SUBMODULES_FOR_RUSTBOOK: &[&str] = &["src/doc/book"];
351+
pub static SUBMODULES_FOR_RUSTBOOK: &[&str] = &["src/doc/book", "src/doc/reference"];
352352

353353
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
354354
pub struct OptimizedDist {

src/bootstrap/src/core/build_steps/vendor.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::core::build_steps::tool::SUBMODULES_FOR_RUSTBOOK;
12
use crate::core::builder::{Builder, RunConfig, ShouldRun, Step};
23
use crate::utils::exec::command;
34
use std::path::PathBuf;
@@ -35,7 +36,7 @@ impl Step for Vendor {
3536
}
3637

3738
// These submodules must be present for `x vendor` to work.
38-
for submodule in ["src/tools/cargo", "src/doc/book"] {
39+
for submodule in SUBMODULES_FOR_RUSTBOOK.iter().chain(["src/tools/cargo"].iter()) {
3940
builder.build.require_and_update_submodule(submodule, None);
4041
}
4142

src/tools/rustbook/Cargo.lock

+21
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,20 @@ dependencies = [
661661
"textwrap",
662662
]
663663

664+
[[package]]
665+
name = "mdbook-spec"
666+
version = "0.1.2"
667+
dependencies = [
668+
"anyhow",
669+
"mdbook",
670+
"once_cell",
671+
"pathdiff",
672+
"regex",
673+
"semver",
674+
"serde_json",
675+
"tempfile",
676+
]
677+
664678
[[package]]
665679
name = "mdbook-trpl-listing"
666680
version = "0.1.0"
@@ -794,6 +808,12 @@ dependencies = [
794808
"windows-targets 0.52.6",
795809
]
796810

811+
[[package]]
812+
name = "pathdiff"
813+
version = "0.2.1"
814+
source = "registry+https://github.com/rust-lang/crates.io-index"
815+
checksum = "8835116a5c179084a830efb3adc117ab007512b535bc1a21c991d3b32a6b44dd"
816+
797817
[[package]]
798818
name = "percent-encoding"
799819
version = "2.3.1"
@@ -1079,6 +1099,7 @@ dependencies = [
10791099
"env_logger",
10801100
"mdbook",
10811101
"mdbook-i18n-helpers",
1102+
"mdbook-spec",
10821103
"mdbook-trpl-listing",
10831104
"mdbook-trpl-note",
10841105
]

src/tools/rustbook/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ env_logger = "0.11"
1212
mdbook-trpl-listing = { path = "../../doc/book/packages/mdbook-trpl-listing" }
1313
mdbook-trpl-note = { path = "../../doc/book/packages/mdbook-trpl-note" }
1414
mdbook-i18n-helpers = "0.3.3"
15+
mdbook-spec = { path = "../../doc/reference/mdbook-spec"}
1516

1617
[dependencies.mdbook]
1718
version = "0.4.37"

src/tools/rustbook/src/main.rs

+12
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use mdbook::errors::Result as Result3;
99
use mdbook::MDBook;
1010
use mdbook_i18n_helpers::preprocessors::Gettext;
1111

12+
use mdbook_spec::Spec;
1213
use mdbook_trpl_listing::TrplListing;
1314
use mdbook_trpl_note::TrplNote;
1415

@@ -83,6 +84,13 @@ pub fn build(args: &ArgMatches) -> Result3<()> {
8384
book.config.build.build_dir = dest_dir.into();
8485
}
8586

87+
// NOTE: Replacing preprocessors using this technique causes error
88+
// messages to be displayed when the original preprocessor doesn't work
89+
// (but it otherwise succeeds).
90+
//
91+
// This should probably be fixed in mdbook to remove the existing
92+
// preprocessor, or this should modify the config and use
93+
// MDBook::load_with_config.
8694
if book.config.get_preprocessor("trpl-note").is_some() {
8795
book.with_preprocessor(TrplNote);
8896
}
@@ -91,6 +99,10 @@ pub fn build(args: &ArgMatches) -> Result3<()> {
9199
book.with_preprocessor(TrplListing);
92100
}
93101

102+
if book.config.get_preprocessor("spec").is_some() {
103+
book.with_preprocessor(Spec::new());
104+
}
105+
94106
book.build()?;
95107

96108
Ok(())

src/tools/tidy/src/deps.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub(crate) const WORKSPACES: &[(&str, ExceptionList, Option<(&[&str], &[&str])>,
7272
//("src/tools/miri/test-cargo-miri", &[], None), // FIXME uncomment once all deps are vendored
7373
//("src/tools/miri/test_dependencies", &[], None), // FIXME uncomment once all deps are vendored
7474
("src/tools/rust-analyzer", EXCEPTIONS_RUST_ANALYZER, None, &[]),
75-
("src/tools/rustbook", EXCEPTIONS_RUSTBOOK, None, &["src/doc/book"]),
75+
("src/tools/rustbook", EXCEPTIONS_RUSTBOOK, None, &["src/doc/book", "src/doc/reference"]),
7676
("src/tools/rustc-perf", EXCEPTIONS_RUSTC_PERF, None, &["src/tools/rustc-perf"]),
7777
("src/tools/x", &[], None, &[]),
7878
// tidy-alphabetical-end

0 commit comments

Comments
 (0)