Skip to content

Commit 5a826c7

Browse files
committed
Add internal RUSTC_INTERNAL_FORCE_PANIC_ABORT env var for libpanic_abort
Currently, we require `libpanic_abort` to always be build with the `abort` panic strategy, regardless of what panic strategy would normally be passed to `rustc` (e.g. `unwind` when building `libstd`). This ensures that when downstream crates build against `libpanic_abort`, it is properly detected as the panic runtime for the `abort` strategy. Previously, this was done by special-casing `libpanic_abort` in bootstrap. This meant that building `libpanic_abort` without using `x.py` (e.g by using `xargo`) would result in the wrong panic strategy being applied, leading to an error when trying to build against it: ``` error: the crate `panic_abort` does not have the panic strategy `abort` ``` This is a problem for tools like Miri, which require a custom-build libstd, and cannot use `x.py`. To fix this, we add a special environment variable `RUSTC_INTERNAL_FORCE_PANIC_ABORT`. This is set in the `build.rs` for `libpanic_abort`, and checked by the compiler when determining the panic strategy of the current crate. While this is still a hack, it's a much better one - the special case is now represented in the `libpanic_abort` crate itself, rather than in `bootstrap`. Ideally, this would be an internal attribute (e.g. `#[rustc_panic_abort]`) that we apply to `libpanic_abort`. Unfortunately, `emscripten` targets require that we be able to determine the panic strategy very early on, before we've even started parsing the crate: https://github.com/rust-lang/rust/blob/3fc30d884ae0c988d98452a06737705cfe34806a/src/librustc_codegen_llvm/llvm_util.rs#L77 To avoid invasive changes to emscripten and/or the codewgen backend infrastructure, I chose to add a new environment variable. Note that the hack in bootstrap needs to remain until this changes makes its way into the bootstrap compiler, to ensure that we can still build a correct `libpanic_abort` with a bootstrap compiler that doesn't know about this special environment variable.
1 parent 56237d7 commit 5a826c7

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

src/bootstrap/bin/rustc.rs

+5
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ fn main() {
9494
// other crate intentionally as this is the only crate for now that we
9595
// ship with panic=abort.
9696
//
97+
// FIXME: Remove the special case for "panic_abort"
98+
// once https://github.com/rust-lang/rust/pull/60026
99+
// rides the release train into the bootstrap compiler.
100+
// `cfg(bootstrap)`: (added to make this easier to grep for)
101+
//
97102
// This... is a bit of a hack how we detect this. Ideally this
98103
// information should be encoded in the crate I guess? Would likely
99104
// require an RFC amendment to RFC 1513, however.

src/libpanic_abort/build.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn main() {
2+
// Hack to force this crate to be compiled with the `abort`
3+
// panic strategy, regardless of what strategy Cargo
4+
// passes to the compiler.
5+
// See `rustc::session::Session::panic_strategy` for more details
6+
println!("cargo:rustc-env=RUSTC_INTERNAL_FORCE_PANIC_ABORT=1");
7+
}

src/librustc/session/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,18 @@ impl Session {
563563
/// Returns the panic strategy for this compile session. If the user explicitly selected one
564564
/// using '-C panic', use that, otherwise use the panic strategy defined by the target.
565565
pub fn panic_strategy(&self) -> PanicStrategy {
566+
// This is a hack to support `libpanic_abort`. We require `libpanic_abort`
567+
// to always be built with `PanicStrategy::Abort`, regardless of what panic
568+
// strategy is suppplied to the compiler. Ideally, this would be an intenral
569+
// attribute in `libpanic_abort` - howevver, emscripten needs to access
570+
// the panic strategy before we even start parsing the crate:
571+
// https://github.com/rust-lang/rust/blob/3fc30d8/src/librustc_codegen_llvm/llvm_util.rs#L77
572+
//
573+
// As a result, we need to use a special environment variable, which is set from
574+
// the `build.rs` of `libpanic_abort`
575+
if env::var("RUSTC_INTERNAL_FORCE_PANIC_ABORT").is_ok() {
576+
return PanicStrategy::Abort;
577+
}
566578
self.opts
567579
.cg
568580
.panic

0 commit comments

Comments
 (0)