Skip to content

Commit b609547

Browse files
committed
std: always depend on backtrace, but only enable its features on demand
1 parent 3287a65 commit b609547

File tree

9 files changed

+23
-21
lines changed

9 files changed

+23
-21
lines changed

src/bootstrap/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ impl Build {
490490
features.push_str(" llvm-libunwind");
491491
}
492492
if self.config.backtrace {
493-
features.push_str(" backtrace");
493+
features.push_str(" backtrace_support");
494494
}
495495
if self.config.profiler {
496496
features.push_str(" profiler");

src/libstd/Cargo.toml

+9-9
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,8 @@ hashbrown = { version = "0.5.0", features = ['rustc-dep-of-std'] }
2727

2828
[dependencies.backtrace]
2929
version = "0.3.37"
30-
default-features = false # don't use coresymbolication on OSX
31-
features = [
32-
"rustc-dep-of-std", # enable build support for integrating into libstd
33-
"dbghelp", # backtrace/symbolize on MSVC
34-
"libbacktrace", # symbolize on most platforms
35-
"libunwind", # backtrace on most platforms
36-
"dladdr", # symbolize on platforms w/o libbacktrace
37-
]
38-
optional = true
30+
default-features = false # without the libstd `backtrace` feature, stub out everything
31+
features = [ "rustc-dep-of-std" ] # enable build support for integrating into libstd
3932

4033
[dev-dependencies]
4134
rand = "0.7"
@@ -65,6 +58,13 @@ cc = "1.0"
6558
[features]
6659
default = ["std_detect_file_io", "std_detect_dlsym_getauxval"]
6760

61+
backtrace_support = [
62+
"backtrace/dbghelp", # backtrace/symbolize on MSVC
63+
"backtrace/libbacktrace", # symbolize on most platforms
64+
"backtrace/libunwind", # backtrace on most platforms
65+
"backtrace/dladdr", # symbolize on platforms w/o libbacktrace
66+
]
67+
6868
panic-unwind = ["panic_unwind"]
6969
profiler = ["profiler_builtins"]
7070
compiler-builtins-c = ["alloc/compiler-builtins-c"]

src/libstd/build.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ fn main() {
4949
println!("cargo:rustc-link-lib=zircon");
5050
println!("cargo:rustc-link-lib=fdio");
5151
} else if target.contains("cloudabi") {
52-
if cfg!(feature = "backtrace") {
52+
if cfg!(feature = "backtrace_support") {
5353
println!("cargo:rustc-link-lib=unwind");
5454
}
5555
println!("cargo:rustc-link-lib=c");

src/libstd/panicking.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,12 @@ pub fn take_hook() -> Box<dyn Fn(&PanicInfo<'_>) + 'static + Sync + Send> {
157157
}
158158

159159
fn default_hook(info: &PanicInfo<'_>) {
160-
#[cfg(feature = "backtrace")]
160+
#[cfg(feature = "backtrace_support")]
161161
use crate::sys_common::{backtrace as backtrace_mod};
162162

163163
// If this is a double panic, make sure that we print a backtrace
164164
// for this panic. Otherwise only print it if logging is enabled.
165-
#[cfg(feature = "backtrace")]
165+
#[cfg(feature = "backtrace_support")]
166166
let log_backtrace = {
167167
let panics = update_panic_count(0);
168168

@@ -190,7 +190,7 @@ fn default_hook(info: &PanicInfo<'_>) {
190190
let _ = writeln!(err, "thread '{}' panicked at '{}', {}",
191191
name, msg, location);
192192

193-
#[cfg(feature = "backtrace")]
193+
#[cfg(feature = "backtrace_support")]
194194
{
195195
use crate::sync::atomic::{AtomicBool, Ordering};
196196

src/libstd/rt.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ fn lang_start_internal(main: &(dyn Fn() -> i32 + Sync + crate::panic::RefUnwindS
4444
sys::args::init(argc, argv);
4545

4646
// Let's run some code!
47-
#[cfg(feature = "backtrace")]
47+
#[cfg(feature = "backtrace_support")]
4848
let exit_code = panic::catch_unwind(|| {
4949
sys_common::backtrace::__rust_begin_short_backtrace(move || main())
5050
});
51-
#[cfg(not(feature = "backtrace"))]
51+
#[cfg(not(feature = "backtrace_support"))]
5252
let exit_code = panic::catch_unwind(move || main());
5353

5454
sys_common::cleanup();

src/libstd/sys_common/backtrace.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use crate::io;
77
use crate::borrow::Cow;
88
use crate::io::prelude::*;
99
use crate::path::{self, Path, PathBuf};
10-
use crate::sync::atomic::{self, Ordering};
1110
use crate::sys::mutex::Mutex;
1211

1312
use backtrace::{BacktraceFmt, BytesOrWideString, PrintFmt};
@@ -34,6 +33,7 @@ pub fn lock() -> impl Drop {
3433
}
3534

3635
/// Prints the current backtrace.
36+
#[cfg(feature = "backtrace_support")]
3737
pub fn print(w: &mut dyn Write, format: PrintFmt) -> io::Result<()> {
3838
// There are issues currently linking libbacktrace into tests, and in
3939
// general during libstd's own unit tests we're not testing this path. In
@@ -129,7 +129,10 @@ where
129129

130130
// For now logging is turned off by default, and this function checks to see
131131
// whether the magical environment variable is present to see if it's turned on.
132+
#[cfg(feature = "backtrace_support")]
132133
pub fn log_enabled() -> Option<PrintFmt> {
134+
use crate::sync::atomic::{self, Ordering};
135+
133136
// Setting environment variables for Fuchsia components isn't a standard
134137
// or easily supported workflow. For now, always display backtraces.
135138
if cfg!(target_os = "fuchsia") {

src/libstd/sys_common/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ macro_rules! rtunwrap {
4141

4242
pub mod alloc;
4343
pub mod at_exit_imp;
44-
#[cfg(feature = "backtrace")]
4544
pub mod backtrace;
4645
pub mod condvar;
4746
pub mod io;

src/libstd/thread/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -465,11 +465,11 @@ impl Builder {
465465
}
466466

467467
thread_info::set(imp::guard::current(), their_thread);
468-
#[cfg(feature = "backtrace")]
468+
#[cfg(feature = "backtrace_support")]
469469
let try_result = panic::catch_unwind(panic::AssertUnwindSafe(|| {
470470
crate::sys_common::backtrace::__rust_begin_short_backtrace(f)
471471
}));
472-
#[cfg(not(feature = "backtrace"))]
472+
#[cfg(not(feature = "backtrace_support"))]
473473
let try_result = panic::catch_unwind(panic::AssertUnwindSafe(f));
474474
*their_packet.get() = Some(try_result);
475475
};

src/libtest/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ proc_macro = { path = "../libproc_macro" }
2323

2424
# Forward features to the `std` crate as necessary
2525
[features]
26-
backtrace = ["std/backtrace"]
26+
backtrace_support = ["std/backtrace_support"]
2727
compiler-builtins-c = ["std/compiler-builtins-c"]
2828
llvm-libunwind = ["std/llvm-libunwind"]
2929
panic-unwind = ["std/panic_unwind"]

0 commit comments

Comments
 (0)