Skip to content

Commit

Permalink
[wip] Update boostrap tests to support book dependencies
Browse files Browse the repository at this point in the history
Since TRPL now depends on a `trpl` crate, the test needs to be able to
build that crate to run mdbook against it, and also to invoke mdbook
with `--library-path` in that case. Use the support for that flag added
to `rustbook` in the previous change to invoke it with the path to the
dependencies it will need to run `rustdoc` tests which reference `trpl`.

Update the allowed dependencies for `rustbook` to include the licenses
for the `trpl` crate's dependencies.

Still to-do:

- [ ] Test this to make sure it works (!) and that it does not generate
      any extra noise during build.
  • Loading branch information
chriskrycho committed Oct 25, 2024
1 parent 9de0522 commit e4c2b0f
Show file tree
Hide file tree
Showing 5 changed files with 1,053 additions and 5 deletions.
77 changes: 76 additions & 1 deletion src/bootstrap/src/core/build_steps/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
//! `./x.py test` (aka [`Kind::Test`]) is currently allowed to reach build steps in other modules.
//! However, this contains ~all test parts we expect people to be able to build and run locally.
use std::collections::HashSet;
use std::ffi::{OsStr, OsString};
use std::path::{Path, PathBuf};
use std::{env, fs, iter};

use clap_complete::shells;

use crate::core::build_steps::compile::run_cargo;
use crate::core::build_steps::doc::DocumentationFormat;
use crate::core::build_steps::synthetic_targets::MirOptPanicAbortSyntheticTarget;
use crate::core::build_steps::tool::{self, SourceType, Tool};
Expand Down Expand Up @@ -2168,9 +2170,11 @@ NOTE: if you're sure you want to do this, please open an issue as to why. In the
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct BookTest {
compiler: Compiler,
target: TargetSelection,
path: PathBuf,
name: &'static str,
is_ext_doc: bool,
dependencies: Vec<&'static str>,
}

impl Step for BookTest {
Expand Down Expand Up @@ -2223,6 +2227,63 @@ impl BookTest {
// Books often have feature-gated example text.
rustbook_cmd.env("RUSTC_BOOTSTRAP", "1");
rustbook_cmd.env("PATH", new_path).arg("test").arg(path);

// Books may also need to build dependencies. For example, `TheBook` has
// code samples which use the `trpl` crate. For the `rustdoc` invocation
// to find them them successfully, they need to be built first and their
// paths used to generate the
let libs = if !self.dependencies.is_empty() {
let mut lib_paths = vec![];
for dep in self.dependencies {
let mode = Mode::ToolRustc;
let target = builder.config.build;
// CHECKME: is this correct, or should it be using `builder::Cargo::new`?
let cargo = tool::prepare_tool_cargo(
builder,
self.compiler,
mode,
builder.config.build,
Kind::Build,
dep,
SourceType::Submodule,
&[],
);
// CHECKME: this is used for the "stamp" for this `run_cargo`;
// is there a better way to do this?!?
let dep_path = PathBuf::from(dep);
let file_name = dep_path.file_name().unwrap();
let file_name = PathBuf::from(file_name);

let stamp = builder
.cargo_out(self.compiler, mode, target)
.join(file_name)
.with_extension("stamp");

let output_paths = run_cargo(builder, cargo, vec![], &stamp, vec![], false, false);
let directories = output_paths
.into_iter()
.filter_map(|p| p.parent().map(ToOwned::to_owned))
.fold(HashSet::new(), |mut set, dir| {
set.insert(dir);
set
});
lib_paths.extend(directories);
}
lib_paths
} else {
vec![]
};

if !libs.is_empty() {
let comma_separated_paths = libs
.into_iter()
.map(|path| format!("{}", path.display()))
.collect::<Vec<String>>()
.join(",");

rustbook_cmd.args(vec![String::from("--library-path"), comma_separated_paths]);
}

builder.add_rust_test_threads(&mut rustbook_cmd);
let _guard = builder.msg(
Kind::Test,
Expand Down Expand Up @@ -2281,12 +2342,14 @@ macro_rules! test_book {
$name:ident, $path:expr, $book_name:expr,
default=$default:expr
$(,submodules = $submodules:expr)?
$(,dependencies=$dependencies:expr)?
;
)+) => {
$(
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct $name {
compiler: Compiler,
target: TargetSelection,
}

impl Step for $name {
Expand All @@ -2301,6 +2364,7 @@ macro_rules! test_book {
fn make_run(run: RunConfig<'_>) {
run.builder.ensure($name {
compiler: run.builder.compiler(run.builder.top_stage, run.target),
target: run.target,
});
}

Expand All @@ -2310,11 +2374,22 @@ macro_rules! test_book {
builder.require_submodule(submodule, None);
}
)*

let dependencies = vec![];
$(
let mut dependencies = dependencies;
for dep in $dependencies {
dependencies.push(dep);
}
)?

builder.ensure(BookTest {
compiler: self.compiler,
target: self.target,
path: PathBuf::from($path),
name: $book_name,
is_ext_doc: !$default,
dependencies,
});
}
}
Expand All @@ -2329,7 +2404,7 @@ test_book!(
RustcBook, "src/doc/rustc", "rustc", default=true;
RustByExample, "src/doc/rust-by-example", "rust-by-example", default=false, submodules=["src/doc/rust-by-example"];
EmbeddedBook, "src/doc/embedded-book", "embedded-book", default=false, submodules=["src/doc/embedded-book"];
TheBook, "src/doc/book", "book", default=false, submodules=["src/doc/book"];
TheBook, "src/doc/book", "book", default=false, submodules=["src/doc/book"], dependencies=["src/doc/book/packages/trpl"];
UnstableBook, "src/doc/unstable-book", "unstable-book", default=true;
EditionGuide, "src/doc/edition-guide", "edition-guide", default=false, submodules=["src/doc/edition-guide"];
);
Expand Down
Loading

0 comments on commit e4c2b0f

Please sign in to comment.