Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/bootstrap/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::time::Instant;
use std::{env, process};

use bootstrap::{
Build, CONFIG_CHANGE_HISTORY, ChangeId, Config, Flags, Subcommand, debug,
Build, CONFIG_CHANGE_HISTORY, ChangeId, Config, Flags, StepStack, Subcommand, debug,
find_recent_config_change_ids, human_readable_changes, t,
};

Expand All @@ -27,6 +27,25 @@ fn main() {

let _start_time = Instant::now();

// Always print backtraces to provide richer errors, to help debug hard-to-reproduce panics
// when the user didn't specify RUST_BACKTRACE
if std::env::var("RUST_BACKTRACE").is_err() {
unsafe {
std::env::set_var("RUST_BACKTRACE", "1");
}
}

let default_panic_hook = std::panic::take_hook();
std::panic::set_hook(Box::new(move |info| {
default_panic_hook(info);
StepStack::with_current(|stack| {
eprintln!("\nBootstrap has panicked, currently active steps:");
for step in stack.get_active_steps() {
eprintln!("{} at {}", step.info, step.location);
}
});
}));

let args = env::args().skip(1).collect::<Vec<_>>();

if Flags::try_parse_verbose_help(&args) {
Expand Down
4 changes: 1 addition & 3 deletions src/bootstrap/src/core/build_steps/clippy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
//! to pass a prebuilt Clippy from the outside when running `cargo clippy`, but that would be
//! (as usual) a massive undertaking/refactoring.

use build_helper::exit;

use super::compile::{ArtifactKeepMode, run_cargo, rustc_cargo, std_cargo};
use super::tool::{SourceType, prepare_tool_cargo};
use crate::builder::{Builder, ShouldRun};
Expand All @@ -23,7 +21,7 @@ use crate::core::build_steps::compile::std_crates_for_run_make;
use crate::core::builder;
use crate::core::builder::{Alias, Kind, RunConfig, Step, StepMetadata, crate_description};
use crate::utils::build_stamp::{self, BuildStamp};
use crate::{Compiler, Mode, Subcommand, TargetSelection};
use crate::{Compiler, Mode, Subcommand, TargetSelection, exit};

/// Disable the most spammy clippy lints
const IGNORED_RULES_FOR_STD_AND_RUSTC: &[&str] = &[
Expand Down
6 changes: 3 additions & 3 deletions src/bootstrap/src/core/build_steps/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use crate::utils::helpers::{
};
use crate::{
CLang, CodegenBackendKind, Compiler, DependencyType, FileType, GitRepo, LLVM_TOOLS, Mode,
debug, trace,
debug, exit, trace,
};

/// Build a standard library for the given `target` using the given `build_compiler`.
Expand Down Expand Up @@ -2068,7 +2068,7 @@ impl Step for Sysroot {
sysroot_lib_rustlib_src_rust.display(),
);
}
build_helper::exit!(1);
exit!(1);
}
}

Expand All @@ -2086,7 +2086,7 @@ impl Step for Sysroot {
builder.src.display(),
e,
);
build_helper::exit!(1);
exit!(1);
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/bootstrap/src/core/build_steps/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use std::path::{Path, PathBuf};
use std::sync::OnceLock;
use std::{env, fs};

use build_helper::exit;
use build_helper::git::PathFreshness;

use crate::core::build_steps::llvm;
Expand All @@ -25,7 +24,7 @@ use crate::utils::exec::command;
use crate::utils::helpers::{
self, exe, get_clang_cl_resource_dir, libdir, t, unhashed_basename, up_to_date,
};
use crate::{CLang, GitRepo, Kind, trace};
use crate::{CLang, GitRepo, Kind, exit, trace};

#[derive(Clone)]
pub struct LlvmResult {
Expand Down
3 changes: 1 addition & 2 deletions src/bootstrap/src/core/build_steps/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

use std::path::PathBuf;

use build_helper::exit;
use build_helper::git::get_git_untracked_files;
use clap_complete::{Generator, shells};

Expand All @@ -17,7 +16,7 @@ use crate::core::builder::{Builder, Kind, RunConfig, ShouldRun, Step, StepMetada
use crate::core::config::TargetSelection;
use crate::core::config::flags::{get_completion, top_level_help};
use crate::utils::exec::command;
use crate::{Mode, t};
use crate::{Mode, exit, t};

#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct BuildManifest;
Expand Down
4 changes: 1 addition & 3 deletions src/bootstrap/src/core/build_steps/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ use std::path::{Path, PathBuf};
use std::process::Command;
use std::{env, fs, iter};

use build_helper::exit;

use crate::core::build_steps::compile::{ArtifactKeepMode, Std, run_cargo};
use crate::core::build_steps::doc::{DocumentationFormat, prepare_doc_compiler};
use crate::core::build_steps::gcc::{Gcc, GccTargetPair, add_cg_gcc_cargo_flags};
Expand Down Expand Up @@ -44,7 +42,7 @@ use crate::utils::helpers::{
up_to_date,
};
use crate::utils::render_tests::{add_flags_and_try_run_tests, try_run_tests};
use crate::{CLang, CodegenBackendKind, GitRepo, Mode, PathSet, TestTarget, envify};
use crate::{CLang, CodegenBackendKind, GitRepo, Mode, PathSet, TestTarget, envify, exit};

mod compiletest;
pub mod failed_tests;
Expand Down
17 changes: 16 additions & 1 deletion src/bootstrap/src/core/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,20 @@ use crate::core::build_steps::{
check, clean, clippy, compile, dist, doc, gcc, install, llvm, run, setup, test, tool, vendor,
};
use crate::core::builder::cli_paths::CLIStepPath;
use crate::core::builder::step_stack::StepRecord;
pub use crate::core::builder::step_stack::StepStack;
use crate::core::config::flags::Subcommand;
use crate::core::config::{DryRun, TargetSelection};
use crate::utils::build_stamp::BuildStamp;
use crate::utils::cache::Cache;
use crate::utils::exec::{BootstrapCommand, ExecutionContext, command};
use crate::utils::helpers::{self, LldThreads, add_dylib_path, exe, libdir, linker_args, t};
use crate::utils::tracing::format_location;
use crate::{Build, Crate, trace};

mod cargo;
mod cli_paths;
mod step_stack;
#[cfg(test)]
mod tests;

Expand Down Expand Up @@ -1112,6 +1116,7 @@ impl<'a> Builder<'a> {
Subcommand::Perf { .. } => (Kind::Perf, &paths[..]),
};

StepStack::with_current(|stack| stack.clear());
Self::new_internal(build, kind, paths.to_owned())
}

Expand Down Expand Up @@ -1574,6 +1579,12 @@ Alternatively, you can set `build.local-rebuild=true` and use a stage0 compiler
graph.register_step_execution(&step, parent, self.config.dry_run());
}

// The location has to be gathered in this function, to be correctly propagated with
// #[track_caller].
let location = format_location(*std::panic::Location::caller());
StepStack::with_current(|stack| {
stack.push(StepRecord { info: pretty_print_step(&step), location });
});
stack.push(Box::new(step.clone()));
}

Expand All @@ -1599,7 +1610,7 @@ Alternatively, you can set `build.local-rebuild=true` and use a stage0 compiler
"step",
step_name = pretty_step_name::<S>(),
args = step_debug_args(&step),
location = crate::utils::tracing::format_location(*std::panic::Location::caller())
location = format_location(*std::panic::Location::caller())
);
span.entered()
};
Expand All @@ -1626,6 +1637,10 @@ Alternatively, you can set `build.local-rebuild=true` and use a stage0 compiler
let mut stack = self.stack.borrow_mut();
let cur_step = stack.pop().expect("step stack empty");
assert_eq!(cur_step.downcast_ref(), Some(&step));

StepStack::with_current(|stack| {
stack.pop();
});
}
self.cache.put(step, out.clone());
out
Expand Down
48 changes: 48 additions & 0 deletions src/bootstrap/src/core/builder/step_stack.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use std::cell::RefCell;

thread_local! {
static STEP_STACK: RefCell<StepStack> = const { RefCell::new(StepStack::new()) };
}

/// This type serves for recording the stack of executed steps in a thread-local variable.
///
/// It is used to print the currently running step stack in places where we do not have easy
/// access to the currently used builder, e.g. in exit! macros or the panic handler.
pub struct StepStack {
stack: Vec<StepRecord>,
}

pub struct StepRecord {
pub info: String,
pub location: String,
}

impl StepStack {
/// Return the currently active step stack for this thread.
pub fn with_current<F>(func: F)
where
F: FnOnce(&mut StepStack),
{
STEP_STACK.with(|stack| func(&mut stack.borrow_mut()));
}

const fn new() -> Self {
Self { stack: Vec::new() }
}

pub fn get_active_steps(&self) -> impl Iterator<Item = &StepRecord> {
self.stack.iter()
}

pub fn clear(&mut self) {
self.stack.clear();
}

pub fn push(&mut self, record: StepRecord) {
self.stack.push(record);
}

pub fn pop(&mut self) {
self.stack.pop();
}
}
11 changes: 5 additions & 6 deletions src/bootstrap/src/core/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use std::sync::{Arc, Mutex};
use std::{cmp, env, fs};

use build_helper::ci::CiEnv;
use build_helper::exit;
use build_helper::git::{GitConfig, PathFreshness, check_path_modifications};
use serde::Deserialize;
#[cfg(feature = "tracing")]
Expand Down Expand Up @@ -57,8 +56,10 @@ use crate::core::download::{
};
use crate::utils::channel;
use crate::utils::exec::{ExecutionContext, command};
use crate::utils::helpers::{exe, get_host_target};
use crate::{CodegenBackendKind, GitInfo, OnceLock, TargetSelection, check_ci_llvm, helpers, t};
use crate::utils::helpers::{exe, fail, get_host_target};
use crate::{
CodegenBackendKind, GitInfo, OnceLock, TargetSelection, check_ci_llvm, exit, helpers, t,
};

/// Each path in this list is considered "allowed" in the `download-rustc="if-unchanged"` logic.
/// This means they can be modified and changes to these paths should never trigger a compiler build
Expand Down Expand Up @@ -2173,7 +2174,7 @@ fn postprocess_toml(
}
}
eprintln!("failed to parse override `{option}`: `{err}");
exit!(2)
exit!(2);
}
toml.merge(None, &mut Default::default(), override_toml, ReplaceOpt::Override);
}
Expand All @@ -2195,8 +2196,6 @@ pub fn check_stage0_version(
src_dir: &Path,
exec_ctx: &ExecutionContext,
) {
use build_helper::util::fail;

if exec_ctx.dry_run() {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/bootstrap/src/core/config/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ pub fn get_completion(shell: &dyn Generator, path: &Path) -> Option<String> {
} else {
std::fs::read_to_string(path).unwrap_or_else(|_| {
eprintln!("couldn't read {}", path.display());
crate::exit!(1)
crate::exit!(1);
})
};
let mut buf = Vec::new();
Expand Down
3 changes: 1 addition & 2 deletions src/bootstrap/src/core/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ pub mod toml;
use std::collections::HashSet;
use std::path::PathBuf;

use build_helper::exit;
pub use config::*;
use serde::de::Unexpected;
use serde::{Deserialize, Deserializer};
Expand All @@ -41,8 +40,8 @@ pub use toml::change_id::ChangeId;
pub use toml::rust::BootstrapOverrideLld;
pub use toml::target::Target;

use crate::Display;
use crate::str::FromStr;
use crate::{Display, exit};

// We are using a decl macro instead of a derive proc macro here to reduce the compile time of bootstrap.
#[macro_export]
Expand Down
2 changes: 1 addition & 1 deletion src/bootstrap/src/core/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,11 @@ impl Config {

#[cfg(not(test))]
pub(crate) fn maybe_download_ci_llvm(&self) {
use build_helper::exit;
use build_helper::git::PathFreshness;

use crate::core::build_steps::llvm::detect_llvm_freshness;
use crate::core::config::toml::llvm::check_incompatible_options_for_ci_llvm;
use crate::exit;

if !self.llvm_from_ci {
return;
Expand Down
10 changes: 8 additions & 2 deletions src/bootstrap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use std::time::{Instant, SystemTime};
use std::{env, fs, io, str};

use build_helper::ci::gha;
use build_helper::exit;
use cc::Tool;
use termcolor::{ColorChoice, StandardStream, WriteColor};
use utils::build_stamp::BuildStamp;
Expand All @@ -42,9 +41,9 @@ use crate::utils::helpers::{self, dir_is_empty, exe, libdir, set_file_times, spl
mod core;
mod utils;

pub use core::builder::PathSet;
#[cfg(feature = "tracing")]
pub use core::builder::STEP_SPAN_TARGET;
pub use core::builder::{PathSet, StepStack};
pub use core::config::flags::{Flags, Subcommand};
pub use core::config::{ChangeId, Config};

Expand Down Expand Up @@ -2149,3 +2148,10 @@ pub fn prepare_behaviour_dump_dir(build: &Build) {
t!(INITIALIZED.set(true));
}
}

#[macro_export]
macro_rules! exit {
($code:expr) => {
$crate::utils::helpers::detail_exit($code, cfg!(test));
};
}
3 changes: 1 addition & 2 deletions src/bootstrap/src/utils/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

use build_helper::drop_bomb::DropBomb;
use build_helper::exit;

use crate::core::config::DryRun;
use crate::{PathBuf, t};
use crate::{PathBuf, exit, t};

/// What should be done when the command fails.
#[derive(Debug, Copy, Clone)]
Expand Down
Loading
Loading