Skip to content

Commit

Permalink
Fix toolchain mess
Browse files Browse the repository at this point in the history
  • Loading branch information
Noratrieb committed May 14, 2024
1 parent b381547 commit f9fe6a8
Showing 1 changed file with 32 additions and 25 deletions.
57 changes: 32 additions & 25 deletions builder/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
use std::{path::Path, thread};

use eyre::{Context, OptionExt};
use eyre::{ensure, Context, OptionExt};

fn main() -> eyre::Result<()> {
let root_dir = Path::new("..");
// Ensure rustup picks up the rust-toolchain.toml file properly and doesn't get confused by this cargo run.
std::env::remove_var("CARGO");
std::env::remove_var("RUSTUP_TOOLCHAIN");

let root_dir = Path::new("..")
.canonicalize()
.wrap_err("canonicalizing ..")?;
let examples_dir = root_dir.join("code").join("examples");

// Change the current directory to ensure the correct rustup toolchains are used.
std::env::set_current_dir(&examples_dir)
.wrap_err("changing current directory to code/examples")?;

let examples = std::fs::read_dir(&examples_dir)
.wrap_err("opening ../code/examples, script must be run in ./builder")?;

install_toolchain(&examples_dir).wrap_err("install toolchain")?;

install_toolchain().wrap_err("install toolchain")?;
// Setup miri to avoid race condition in `cargo miri run` below...
eprintln!("Setting up miri");
std::process::Command::new("cargo")
.arg("miri")
.arg("setup")
.output()
.wrap_err("setting up miri")?;
setup_miri(&examples_dir).wrap_err("setting up miri sysroot")?;

thread::scope(|scope| {
let mut handles = Vec::new();
Expand Down Expand Up @@ -52,6 +56,20 @@ fn main() -> eyre::Result<()> {
Ok(())
}

fn setup_miri(dir: &Path) -> eyre::Result<()> {
eprintln!("Setting up miri");
let output = std::process::Command::new("cargo")
.arg("miri")
.arg("setup")
.current_dir(dir)
.spawn()
.wrap_err("spawning miri")?
.wait()
.wrap_err("waiting for miri setup")?;
ensure!(output.success());
Ok(())
}

fn run_example(examples_dir: &Path, filename: &str) -> eyre::Result<()> {
let use_miri = filename.starts_with("unsafe_");

Expand All @@ -62,15 +80,12 @@ fn run_example(examples_dir: &Path, filename: &str) -> eyre::Result<()> {
eprintln!("Running {example_name}");

let mut cmd = std::process::Command::new("cargo");
cmd.current_dir(examples_dir);
if use_miri {
cmd.arg("miri");
}
cmd.arg("run").arg("--quiet").arg("--example");
cmd.arg(example_name);

remove_rustup_vars(&mut cmd);

let out = cmd.output().wrap_err("spawning cargo")?;
let stderr = String::from_utf8(out.stderr).wrap_err("stderr was invalid UTF-8")?;

Expand All @@ -82,23 +97,15 @@ fn run_example(examples_dir: &Path, filename: &str) -> eyre::Result<()> {
Ok(())
}

// Ensure there is output for the toolchain and that the installation doesn't pollute stderr.
fn install_toolchain(examples_dir: &Path) -> eyre::Result<()> {
/// Ensures there is output for the toolchain and that the installation doesn't pollute stderr.
fn install_toolchain() -> eyre::Result<()> {
let mut toolchain_install = std::process::Command::new("rustc");
toolchain_install.arg("-V");
toolchain_install.current_dir(examples_dir);
remove_rustup_vars(&mut toolchain_install);
toolchain_install
let output = toolchain_install
.spawn()
.wrap_err("failed to spawn rustc")?
.wait()
.wrap_err("failed to wait for rustc")?;

ensure!(output.success());
Ok(())
}

fn remove_rustup_vars(cmd: &mut std::process::Command) {
// Ensure rustup picks up the rust-toolchain.toml file properly and doesn't get confused by this cargo run.
cmd.env_remove("CARGO");
cmd.env_remove("RUSTUP_TOOLCHAIN");
}

0 comments on commit f9fe6a8

Please sign in to comment.