use std::io::{self, Write};
use std::path::{Path, PathBuf};
use std::{env, fs, mem};
use crate::Mode;
use crate::core::build_steps::compile;
use crate::core::build_steps::tool::{self, SourceType, Tool, prepare_tool_cargo};
use crate::core::builder::{
self, Alias, Builder, Compiler, Kind, RunConfig, ShouldRun, Step, crate_description,
};
use crate::core::config::{Config, TargetSelection};
use crate::utils::helpers::{symlink_dir, t, up_to_date};
macro_rules! submodule_helper {
($path:expr, submodule) => {
$path
};
($path:expr, submodule = $submodule:literal) => {
$submodule
};
}
macro_rules! book {
($($name:ident, $path:expr, $book_name:expr, $lang:expr $(, submodule $(= $submodule:literal)? )? ;)+) => {
$(
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct $name {
target: TargetSelection,
}
impl Step for $name {
type Output = ();
const DEFAULT: bool = true;
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
let builder = run.builder;
run.path($path).default_condition(builder.config.docs)
}
fn make_run(run: RunConfig<'_>) {
run.builder.ensure($name {
target: run.target,
});
}
fn run(self, builder: &Builder<'_>) {
$(
let path = submodule_helper!( $path, submodule $( = $submodule )? );
builder.require_submodule(path, None);
)?
builder.ensure(RustbookSrc {
target: self.target,
name: $book_name.to_owned(),
src: builder.src.join($path),
parent: Some(self),
languages: $lang.into(),
rustdoc_compiler: None,
})
}
}
)+
}
}
book!(
CargoBook, "src/tools/cargo/src/doc", "cargo", &[], submodule = "src/tools/cargo";
ClippyBook, "src/tools/clippy/book", "clippy", &[];
EditionGuide, "src/doc/edition-guide", "edition-guide", &[], submodule;
EmbeddedBook, "src/doc/embedded-book", "embedded-book", &[], submodule;
Nomicon, "src/doc/nomicon", "nomicon", &[], submodule;
RustByExample, "src/doc/rust-by-example", "rust-by-example", &["ja", "zh"], submodule;
RustdocBook, "src/doc/rustdoc", "rustdoc", &[];
StyleGuide, "src/doc/style-guide", "style-guide", &[];
);
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct UnstableBook {
target: TargetSelection,
}
impl Step for UnstableBook {
type Output = ();
const DEFAULT: bool = true;
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
let builder = run.builder;
run.path("src/doc/unstable-book").default_condition(builder.config.docs)
}
fn make_run(run: RunConfig<'_>) {
run.builder.ensure(UnstableBook { target: run.target });
}
fn run(self, builder: &Builder<'_>) {
builder.ensure(UnstableBookGen { target: self.target });
builder.ensure(RustbookSrc {
target: self.target,
name: "unstable-book".to_owned(),
src: builder.md_doc_out(self.target).join("unstable-book"),
parent: Some(self),
languages: vec![],
rustdoc_compiler: None,
})
}
}
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
struct RustbookSrc<P: Step> {
target: TargetSelection,
name: String,
src: PathBuf,
parent: Option<P>,
languages: Vec<&'static str>,
rustdoc_compiler: Option<Compiler>,
}
impl<P: Step> Step for RustbookSrc<P> {
type Output = ();
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
run.never()
}
fn run(self, builder: &Builder<'_>) {
let target = self.target;
let name = self.name;
let src = self.src;
let out = builder.doc_out(target);
t!(fs::create_dir_all(&out));
let out = out.join(&name);
let index = out.join("index.html");
let rustbook = builder.tool_exe(Tool::Rustbook);
if !builder.config.dry_run()
&& (!up_to_date(&src, &index) || !up_to_date(&rustbook, &index))
{
builder.info(&format!("Rustbook ({target}) - {name}"));
let _ = fs::remove_dir_all(&out);
let mut rustbook_cmd = builder.tool_cmd(Tool::Rustbook);
if let Some(compiler) = self.rustdoc_compiler {
let mut rustdoc = builder.rustdoc(compiler);
rustdoc.pop();
let old_path = env::var_os("PATH").unwrap_or_default();
let new_path =
env::join_paths(std::iter::once(rustdoc).chain(env::split_paths(&old_path)))
.expect("could not add rustdoc to PATH");
rustbook_cmd.env("PATH", new_path);
builder.add_rustc_lib_path(compiler, &mut rustbook_cmd);
}
rustbook_cmd
.arg("build")
.arg(&src)
.arg("-d")
.arg(&out)
.arg("--rust-root")
.arg(&builder.src)
.run(builder);
for lang in &self.languages {
let out = out.join(lang);
builder.info(&format!("Rustbook ({target}) - {name} - {lang}"));
let _ = fs::remove_dir_all(&out);
builder
.tool_cmd(Tool::Rustbook)
.arg("build")
.arg(&src)
.arg("-d")
.arg(&out)
.arg("-l")
.arg(lang)
.run(builder);
}
}
if self.parent.is_some() {
builder.maybe_open_in_browser::<P>(index)
}
}
}
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct TheBook {
compiler: Compiler,
target: TargetSelection,
}
impl Step for TheBook {
type Output = ();
const DEFAULT: bool = true;
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
let builder = run.builder;
run.path("src/doc/book").default_condition(builder.config.docs)
}
fn make_run(run: RunConfig<'_>) {
run.builder.ensure(TheBook {
compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
target: run.target,
});
}
fn run(self, builder: &Builder<'_>) {
builder.require_submodule("src/doc/book", None);
let compiler = self.compiler;
let target = self.target;
let absolute_path = builder.src.join("src/doc/book");
let redirect_path = absolute_path.join("redirects");
builder.ensure(RustbookSrc {
target,
name: "book".to_owned(),
src: absolute_path.clone(),
parent: Some(self),
languages: vec![],
rustdoc_compiler: None,
});
for edition in &["first-edition", "second-edition", "2018-edition"] {
builder.ensure(RustbookSrc {
target,
name: format!("book/{edition}"),
src: absolute_path.join(edition),
parent: Option::<Self>::None,
languages: vec![],
rustdoc_compiler: None,
});
}
let shared_assets = builder.ensure(SharedAssets { target });
let _guard = builder.msg_doc(compiler, "book redirect pages", target);
for file in t!(fs::read_dir(redirect_path)) {
let file = t!(file);
let path = file.path();
let path = path.to_str().unwrap();
invoke_rustdoc(builder, compiler, &shared_assets, target, path);
}
}
}
fn invoke_rustdoc(
builder: &Builder<'_>,
compiler: Compiler,
shared_assets: &SharedAssetsPaths,
target: TargetSelection,
markdown: &str,
) {
let out = builder.doc_out(target);
let path = builder.src.join("src/doc").join(markdown);
let header = builder.src.join("src/doc/redirect.inc");
let footer = builder.src.join("src/doc/footer.inc");
let mut cmd = builder.rustdoc_cmd(compiler);
let out = out.join("book");
cmd.arg("--html-after-content")
.arg(&footer)
.arg("--html-before-content")
.arg(&shared_assets.version_info)
.arg("--html-in-header")
.arg(&header)
.arg("--markdown-no-toc")
.arg("--markdown-playground-url")
.arg("https://play.rust-lang.org/")
.arg("-o")
.arg(&out)
.arg(&path)
.arg("--markdown-css")
.arg("../rust.css")
.arg("-Zunstable-options");
if !builder.config.docs_minification {
cmd.arg("--disable-minification");
}
cmd.run(builder);
}
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct Standalone {
compiler: Compiler,
target: TargetSelection,
}
impl Step for Standalone {
type Output = ();
const DEFAULT: bool = true;
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
let builder = run.builder;
run.path("src/doc").alias("standalone").default_condition(builder.config.docs)
}
fn make_run(run: RunConfig<'_>) {
run.builder.ensure(Standalone {
compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
target: run.target,
});
}
fn run(self, builder: &Builder<'_>) {
let target = self.target;
let compiler = self.compiler;
let _guard = builder.msg_doc(compiler, "standalone", target);
let out = builder.doc_out(target);
t!(fs::create_dir_all(&out));
let version_info = builder.ensure(SharedAssets { target: self.target }).version_info;
let favicon = builder.src.join("src/doc/favicon.inc");
let footer = builder.src.join("src/doc/footer.inc");
let full_toc = builder.src.join("src/doc/full-toc.inc");
for file in t!(fs::read_dir(builder.src.join("src/doc"))) {
let file = t!(file);
let path = file.path();
let filename = path.file_name().unwrap().to_str().unwrap();
if !filename.ends_with(".md") || filename == "README.md" {
continue;
}
let html = out.join(filename).with_extension("html");
let rustdoc = builder.rustdoc(compiler);
if up_to_date(&path, &html)
&& up_to_date(&footer, &html)
&& up_to_date(&favicon, &html)
&& up_to_date(&full_toc, &html)
&& (builder.config.dry_run() || up_to_date(&version_info, &html))
&& (builder.config.dry_run() || up_to_date(&rustdoc, &html))
{
continue;
}
let mut cmd = builder.rustdoc_cmd(compiler);
cmd.arg("--html-after-content")
.arg(&footer)
.arg("--html-before-content")
.arg(&version_info)
.arg("--html-in-header")
.arg(&favicon)
.arg("--markdown-no-toc")
.arg("-Zunstable-options")
.arg("--index-page")
.arg(builder.src.join("src/doc/index.md"))
.arg("--markdown-playground-url")
.arg("https://play.rust-lang.org/")
.arg("-o")
.arg(&out)
.arg(&path);
if !builder.config.docs_minification {
cmd.arg("--disable-minification");
}
if filename == "not_found.md" {
cmd.arg("--markdown-css").arg("https://doc.rust-lang.org/rust.css");
} else {
cmd.arg("--markdown-css").arg("rust.css");
}
cmd.run(builder);
}
if builder.paths.is_empty() || builder.was_invoked_explicitly::<Self>(Kind::Doc) {
let index = out.join("index.html");
builder.open_in_browser(index);
}
}
}
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct Releases {
compiler: Compiler,
target: TargetSelection,
}
impl Step for Releases {
type Output = ();
const DEFAULT: bool = true;
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
let builder = run.builder;
run.path("RELEASES.md").alias("releases").default_condition(builder.config.docs)
}
fn make_run(run: RunConfig<'_>) {
run.builder.ensure(Releases {
compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
target: run.target,
});
}
fn run(self, builder: &Builder<'_>) {
let target = self.target;
let compiler = self.compiler;
let _guard = builder.msg_doc(compiler, "releases", target);
let out = builder.doc_out(target);
t!(fs::create_dir_all(&out));
builder.ensure(Standalone {
compiler: builder.compiler(builder.top_stage, builder.config.build),
target,
});
let version_info = builder.ensure(SharedAssets { target: self.target }).version_info;
let favicon = builder.src.join("src/doc/favicon.inc");
let footer = builder.src.join("src/doc/footer.inc");
let full_toc = builder.src.join("src/doc/full-toc.inc");
let html = out.join("releases.html");
let tmppath = out.join("releases.md");
let inpath = builder.src.join("RELEASES.md");
let rustdoc = builder.rustdoc(compiler);
if !up_to_date(&inpath, &html)
|| !up_to_date(&footer, &html)
|| !up_to_date(&favicon, &html)
|| !up_to_date(&full_toc, &html)
|| !(builder.config.dry_run()
|| up_to_date(&version_info, &html)
|| up_to_date(&rustdoc, &html))
{
let mut tmpfile = t!(fs::File::create(&tmppath));
t!(tmpfile.write_all(b"% Rust Release Notes\n\n"));
t!(io::copy(&mut t!(fs::File::open(&inpath)), &mut tmpfile));
mem::drop(tmpfile);
let mut cmd = builder.rustdoc_cmd(compiler);
cmd.arg("--html-after-content")
.arg(&footer)
.arg("--html-before-content")
.arg(&version_info)
.arg("--html-in-header")
.arg(&favicon)
.arg("--markdown-no-toc")
.arg("--markdown-css")
.arg("rust.css")
.arg("-Zunstable-options")
.arg("--index-page")
.arg(builder.src.join("src/doc/index.md"))
.arg("--markdown-playground-url")
.arg("https://play.rust-lang.org/")
.arg("-o")
.arg(&out)
.arg(&tmppath);
if !builder.config.docs_minification {
cmd.arg("--disable-minification");
}
cmd.run(builder);
}
if builder.was_invoked_explicitly::<Self>(Kind::Doc) {
builder.open_in_browser(&html);
}
}
}
#[derive(Debug, Clone)]
pub struct SharedAssetsPaths {
pub version_info: PathBuf,
}
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct SharedAssets {
target: TargetSelection,
}
impl Step for SharedAssets {
type Output = SharedAssetsPaths;
const DEFAULT: bool = false;
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
run.never()
}
fn run(self, builder: &Builder<'_>) -> Self::Output {
let out = builder.doc_out(self.target);
let version_input = builder.src.join("src").join("doc").join("version_info.html.template");
let version_info = out.join("version_info.html");
if !builder.config.dry_run() && !up_to_date(&version_input, &version_info) {
let info = t!(fs::read_to_string(&version_input))
.replace("VERSION", &builder.rust_release())
.replace("SHORT_HASH", builder.rust_info().sha_short().unwrap_or(""))
.replace("STAMP", builder.rust_info().sha().unwrap_or(""));
t!(fs::write(&version_info, info));
}
builder.copy_link(
&builder.src.join("src").join("doc").join("rust.css"),
&out.join("rust.css"),
);
SharedAssetsPaths { version_info }
}
}
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Std {
pub stage: u32,
pub target: TargetSelection,
pub format: DocumentationFormat,
crates: Vec<String>,
}
impl Std {
pub(crate) fn new(stage: u32, target: TargetSelection, format: DocumentationFormat) -> Self {
Std { stage, target, format, crates: vec![] }
}
}
impl Step for Std {
type Output = ();
const DEFAULT: bool = true;
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
let builder = run.builder;
run.crate_or_deps("sysroot").path("library").default_condition(builder.config.docs)
}
fn make_run(run: RunConfig<'_>) {
let crates = compile::std_crates_for_run_make(&run);
run.builder.ensure(Std {
stage: run.builder.top_stage,
target: run.target,
format: if run.builder.config.cmd.json() {
DocumentationFormat::Json
} else {
DocumentationFormat::Html
},
crates,
});
}
fn run(self, builder: &Builder<'_>) {
let stage = self.stage;
let target = self.target;
let crates = if self.crates.is_empty() {
builder
.in_tree_crates("sysroot", Some(target))
.iter()
.map(|c| c.name.to_string())
.collect()
} else {
self.crates
};
let out = match self.format {
DocumentationFormat::Html => builder.doc_out(target),
DocumentationFormat::Json => builder.json_doc_out(target),
};
t!(fs::create_dir_all(&out));
if self.format == DocumentationFormat::Html {
builder.ensure(SharedAssets { target: self.target });
}
let index_page = builder
.src
.join("src/doc/index.md")
.into_os_string()
.into_string()
.expect("non-utf8 paths are unsupported");
let mut extra_args = match self.format {
DocumentationFormat::Html => {
vec!["--markdown-css", "rust.css", "--markdown-no-toc", "--index-page", &index_page]
}
DocumentationFormat::Json => vec!["--output-format", "json"],
};
if !builder.config.docs_minification {
extra_args.push("--disable-minification");
}
extra_args.push("-Zunstable-options");
doc_std(builder, self.format, stage, target, &out, &extra_args, &crates);
if let DocumentationFormat::Json = self.format {
return;
}
if builder.paths.iter().any(|path| path.ends_with("library")) {
let index = out.join("std").join("index.html");
builder.open_in_browser(index);
} else {
for requested_crate in crates {
if STD_PUBLIC_CRATES.iter().any(|&k| k == requested_crate) {
let index = out.join(requested_crate).join("index.html");
builder.open_in_browser(index);
break;
}
}
}
}
}
const STD_PUBLIC_CRATES: [&str; 5] = ["core", "alloc", "std", "proc_macro", "test"];
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum DocumentationFormat {
Html,
Json,
}
impl DocumentationFormat {
fn as_str(&self) -> &str {
match self {
DocumentationFormat::Html => "HTML",
DocumentationFormat::Json => "JSON",
}
}
}
fn doc_std(
builder: &Builder<'_>,
format: DocumentationFormat,
stage: u32,
target: TargetSelection,
out: &Path,
extra_args: &[&str],
requested_crates: &[String],
) {
let compiler = builder.compiler(stage, builder.config.build);
let target_doc_dir_name = if format == DocumentationFormat::Json { "json-doc" } else { "doc" };
let target_dir = builder.stage_out(compiler, Mode::Std).join(target).join(target_doc_dir_name);
let out_dir = target_dir.join(target).join("doc");
let mut cargo =
builder::Cargo::new(builder, compiler, Mode::Std, SourceType::InTree, target, Kind::Doc);
compile::std_cargo(builder, target, compiler.stage, &mut cargo);
cargo
.arg("--no-deps")
.arg("--target-dir")
.arg(&*target_dir.to_string_lossy())
.arg("-Zskip-rustdoc-fingerprint")
.arg("-Zrustdoc-map")
.rustdocflag("--extern-html-root-url")
.rustdocflag("std_detect=https://docs.rs/std_detect/latest/")
.rustdocflag("--extern-html-root-takes-precedence")
.rustdocflag("--resource-suffix")
.rustdocflag(&builder.version);
for arg in extra_args {
cargo.rustdocflag(arg);
}
if builder.config.library_docs_private_items {
cargo.rustdocflag("--document-private-items").rustdocflag("--document-hidden-items");
}
for krate in requested_crates {
if krate == "sysroot" {
continue;
}
cargo.arg("-p").arg(krate);
}
let description =
format!("library{} in {} format", crate_description(requested_crates), format.as_str());
let _guard = builder.msg_doc(compiler, description, target);
cargo.into_cmd().run(builder);
builder.cp_link_r(&out_dir, out);
}
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct Rustc {
pub stage: u32,
pub target: TargetSelection,
crates: Vec<String>,
}
impl Rustc {
pub(crate) fn new(stage: u32, target: TargetSelection, builder: &Builder<'_>) -> Self {
let crates = builder
.in_tree_crates("rustc-main", Some(target))
.into_iter()
.map(|krate| krate.name.to_string())
.collect();
Self { stage, target, crates }
}
}
impl Step for Rustc {
type Output = ();
const DEFAULT: bool = true;
const ONLY_HOSTS: bool = true;
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
let builder = run.builder;
run.crate_or_deps("rustc-main")
.path("compiler")
.default_condition(builder.config.compiler_docs)
}
fn make_run(run: RunConfig<'_>) {
run.builder.ensure(Rustc {
stage: run.builder.top_stage,
target: run.target,
crates: run.make_run_crates(Alias::Compiler),
});
}
fn run(self, builder: &Builder<'_>) {
let stage = self.stage;
let target = self.target;
let out = builder.compiler_doc_out(target);
t!(fs::create_dir_all(&out));
let compiler = builder.compiler(stage, builder.config.build);
builder.ensure(compile::Std::new(compiler, builder.config.build));
let _guard = builder.msg_sysroot_tool(
Kind::Doc,
stage,
format!("compiler{}", crate_description(&self.crates)),
compiler.host,
target,
);
let mut cargo = builder::Cargo::new(
builder,
compiler,
Mode::Rustc,
SourceType::InTree,
target,
Kind::Doc,
);
cargo.rustdocflag("--document-private-items");
cargo.rustdocflag("-Arustdoc::private-intra-doc-links");
cargo.rustdocflag("--enable-index-page");
cargo.rustdocflag("-Znormalize-docs");
cargo.rustdocflag("--show-type-layout");
compile::rustc_cargo(builder, &mut cargo, target, &compiler, &self.crates);
cargo.arg("-Zskip-rustdoc-fingerprint");
cargo.arg("--no-deps");
cargo.arg("-Zrustdoc-map");
cargo.rustdocflag("--extern-html-root-url");
cargo.rustdocflag("ena=https://docs.rs/ena/latest/");
let mut to_open = None;
let out_dir = builder.stage_out(compiler, Mode::Rustc).join(target).join("doc");
for krate in &*self.crates {
let dir_name = krate.replace('-', "_");
t!(fs::create_dir_all(out_dir.join(&*dir_name)));
cargo.arg("-p").arg(krate);
if to_open.is_none() {
to_open = Some(dir_name);
}
}
symlink_dir_force(&builder.config, &out, &out_dir);
let proc_macro_out_dir = builder.stage_out(compiler, Mode::Rustc).join("doc");
symlink_dir_force(&builder.config, &out, &proc_macro_out_dir);
cargo.into_cmd().run(builder);
if !builder.config.dry_run() {
for krate in &*self.crates {
let dir_name = krate.replace('-', "_");
assert!(out.join(&*dir_name).read_dir().unwrap().next().is_some());
}
}
if builder.paths.iter().any(|path| path.ends_with("compiler")) {
let index = out.join("rustc_middle").join("index.html");
builder.open_in_browser(index);
} else if let Some(krate) = to_open {
let index = out.join(krate).join("index.html");
builder.open_in_browser(index);
}
}
}
macro_rules! tool_doc {
(
$tool: ident,
$path: literal,
$(rustc_tool = $rustc_tool:literal, )?
$(is_library = $is_library:expr,)?
$(crates = $crates:expr)?
$(, submodule $(= $submodule:literal)? )?
) => {
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct $tool {
target: TargetSelection,
}
impl Step for $tool {
type Output = ();
const DEFAULT: bool = true;
const ONLY_HOSTS: bool = true;
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
let builder = run.builder;
run.path($path).default_condition(builder.config.compiler_docs)
}
fn make_run(run: RunConfig<'_>) {
run.builder.ensure($tool { target: run.target });
}
fn run(self, builder: &Builder<'_>) {
let source_type = SourceType::InTree;
$(
let _ = source_type; let source_type = SourceType::Submodule;
let path = submodule_helper!( $path, submodule $( = $submodule )? );
builder.require_submodule(path, None);
)?
let stage = builder.top_stage;
let target = self.target;
let out = builder.compiler_doc_out(target);
t!(fs::create_dir_all(&out));
let compiler = builder.compiler(stage, builder.config.build);
builder.ensure(compile::Std::new(compiler, target));
if true $(&& $rustc_tool)? {
builder.ensure(Rustc::new(stage, target, builder));
builder.ensure(compile::Rustc::new(compiler, target));
}
let mut cargo = prepare_tool_cargo(
builder,
compiler,
Mode::ToolRustc,
target,
Kind::Doc,
$path,
source_type,
&[],
);
cargo.arg("-Zskip-rustdoc-fingerprint");
cargo.arg("--no-deps");
if false $(|| $is_library)? {
cargo.arg("--lib");
}
$(for krate in $crates {
cargo.arg("-p").arg(krate);
})?
cargo.rustdocflag("--document-private-items");
cargo.rustdocflag("-Arustdoc::private-intra-doc-links");
cargo.rustdocflag("--enable-index-page");
cargo.rustdocflag("--show-type-layout");
let out_dir = builder.stage_out(compiler, Mode::ToolRustc).join(target).join("doc");
$(for krate in $crates {
let dir_name = krate.replace("-", "_");
t!(fs::create_dir_all(out_dir.join(&*dir_name)));
})?
symlink_dir_force(&builder.config, &out, &out_dir);
let proc_macro_out_dir = builder.stage_out(compiler, Mode::ToolRustc).join("doc");
symlink_dir_force(&builder.config, &out, &proc_macro_out_dir);
let _guard = builder.msg_doc(compiler, stringify!($tool).to_lowercase(), target);
cargo.into_cmd().run(builder);
if !builder.config.dry_run() {
$(for krate in $crates {
let dir_name = krate.replace("-", "_");
assert!(out.join(&*dir_name).read_dir().unwrap().next().is_some());
})?
}
}
}
}
}
tool_doc!(
BuildHelper,
"src/build_helper",
rustc_tool = false,
is_library = true,
crates = ["build_helper"]
);
tool_doc!(Rustdoc, "src/tools/rustdoc", crates = ["rustdoc", "rustdoc-json-types"]);
tool_doc!(Rustfmt, "src/tools/rustfmt", crates = ["rustfmt-nightly", "rustfmt-config_proc_macro"]);
tool_doc!(Clippy, "src/tools/clippy", crates = ["clippy_config", "clippy_utils"]);
tool_doc!(Miri, "src/tools/miri", crates = ["miri"]);
tool_doc!(
Cargo,
"src/tools/cargo",
rustc_tool = false,
crates = [
"cargo",
"cargo-credential",
"cargo-platform",
"cargo-test-macro",
"cargo-test-support",
"cargo-util",
"cargo-util-schemas",
"crates-io",
"mdman",
"rustfix",
],
submodule = "src/tools/cargo"
);
tool_doc!(Tidy, "src/tools/tidy", rustc_tool = false, crates = ["tidy"]);
tool_doc!(
Bootstrap,
"src/bootstrap",
rustc_tool = false,
is_library = true,
crates = ["bootstrap"]
);
tool_doc!(
RunMakeSupport,
"src/tools/run-make-support",
rustc_tool = false,
is_library = true,
crates = ["run_make_support"]
);
tool_doc!(
Compiletest,
"src/tools/compiletest",
rustc_tool = false,
is_library = true,
crates = ["compiletest"]
);
#[derive(Ord, PartialOrd, Debug, Clone, Hash, PartialEq, Eq)]
pub struct ErrorIndex {
pub target: TargetSelection,
}
impl Step for ErrorIndex {
type Output = ();
const DEFAULT: bool = true;
const ONLY_HOSTS: bool = true;
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
let builder = run.builder;
run.path("src/tools/error_index_generator").default_condition(builder.config.docs)
}
fn make_run(run: RunConfig<'_>) {
let target = run.target;
run.builder.ensure(ErrorIndex { target });
}
fn run(self, builder: &Builder<'_>) {
builder.info(&format!("Documenting error index ({})", self.target));
let out = builder.doc_out(self.target);
t!(fs::create_dir_all(&out));
tool::ErrorIndex::command(builder).arg("html").arg(out).arg(&builder.version).run(builder);
}
}
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct UnstableBookGen {
target: TargetSelection,
}
impl Step for UnstableBookGen {
type Output = ();
const DEFAULT: bool = true;
const ONLY_HOSTS: bool = true;
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
let builder = run.builder;
run.path("src/tools/unstable-book-gen").default_condition(builder.config.docs)
}
fn make_run(run: RunConfig<'_>) {
run.builder.ensure(UnstableBookGen { target: run.target });
}
fn run(self, builder: &Builder<'_>) {
let target = self.target;
builder.info(&format!("Generating unstable book md files ({target})"));
let out = builder.md_doc_out(target).join("unstable-book");
builder.create_dir(&out);
builder.remove_dir(&out);
let mut cmd = builder.tool_cmd(Tool::UnstableBookGen);
cmd.arg(builder.src.join("library"));
cmd.arg(builder.src.join("compiler"));
cmd.arg(builder.src.join("src"));
cmd.arg(out);
cmd.run(builder);
}
}
fn symlink_dir_force(config: &Config, original: &Path, link: &Path) {
if config.dry_run() {
return;
}
if let Ok(m) = fs::symlink_metadata(link) {
if m.file_type().is_dir() {
t!(fs::remove_dir_all(link));
} else {
t!(fs::remove_file(link).or_else(|_| fs::remove_dir(link)));
}
}
t!(
symlink_dir(config, original, link),
format!("failed to create link from {} -> {}", link.display(), original.display())
);
}
#[derive(Ord, PartialOrd, Debug, Clone, Hash, PartialEq, Eq)]
pub struct RustcBook {
pub compiler: Compiler,
pub target: TargetSelection,
pub validate: bool,
}
impl Step for RustcBook {
type Output = ();
const DEFAULT: bool = true;
const ONLY_HOSTS: bool = true;
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
let builder = run.builder;
run.path("src/doc/rustc").default_condition(builder.config.docs)
}
fn make_run(run: RunConfig<'_>) {
run.builder.ensure(RustcBook {
compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
target: run.target,
validate: false,
});
}
fn run(self, builder: &Builder<'_>) {
let out_base = builder.md_doc_out(self.target).join("rustc");
t!(fs::create_dir_all(&out_base));
let out_listing = out_base.join("src/lints");
builder.cp_link_r(&builder.src.join("src/doc/rustc"), &out_base);
builder.info(&format!("Generating lint docs ({})", self.target));
let rustc = builder.rustc(self.compiler);
builder.ensure(compile::Std::new(self.compiler, self.target));
let mut cmd = builder.tool_cmd(Tool::LintDocs);
cmd.arg("--src");
cmd.arg(builder.src.join("compiler"));
cmd.arg("--out");
cmd.arg(&out_listing);
cmd.arg("--rustc");
cmd.arg(&rustc);
cmd.arg("--rustc-target").arg(self.target.rustc_target_arg());
if let Some(target_linker) = builder.linker(self.target) {
cmd.arg("--rustc-linker").arg(target_linker);
}
if builder.is_verbose() {
cmd.arg("--verbose");
}
if self.validate {
cmd.arg("--validate");
}
cmd.env("RUSTC_BOOTSTRAP", "1");
builder.add_rustc_lib_path(self.compiler, &mut cmd);
let doc_generator_guard = builder.msg(
Kind::Run,
self.compiler.stage,
"lint-docs",
self.compiler.host,
self.target,
);
cmd.run(builder);
drop(doc_generator_guard);
builder.ensure(RustbookSrc {
target: self.target,
name: "rustc".to_owned(),
src: out_base,
parent: Some(self),
languages: vec![],
rustdoc_compiler: None,
});
}
}
#[derive(Ord, PartialOrd, Debug, Clone, Hash, PartialEq, Eq)]
pub struct Reference {
pub compiler: Compiler,
pub target: TargetSelection,
}
impl Step for Reference {
type Output = ();
const DEFAULT: bool = true;
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
let builder = run.builder;
run.path("src/doc/reference").default_condition(builder.config.docs)
}
fn make_run(run: RunConfig<'_>) {
run.builder.ensure(Reference {
compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
target: run.target,
});
}
fn run(self, builder: &Builder<'_>) {
builder.require_submodule("src/doc/reference", None);
builder.ensure(compile::Std::new(self.compiler, builder.config.build));
builder.ensure(RustbookSrc {
target: self.target,
name: "reference".to_owned(),
src: builder.src.join("src/doc/reference"),
rustdoc_compiler: Some(self.compiler),
parent: Some(self),
languages: vec![],
});
}
}