Skip to content

Commit c3c297a

Browse files
authored
Prepare for this crate to go into libstd (#349)
* Prepare for this crate to go into libstd This commit is a preparation of this crate to be included as a submodule into the standard library. I'm not 100% sold on this yet but I'm somewhat convinced that this is going to happen this way. This is progress on #328 and a preview of what it might look like to implement this strategy. Currently I don't plan to merge this to the `master` branch unless it's decided to move forward with this integration strategy of the gimli feature of the backtrace crate. * Update as-if-std integration
1 parent 2ad451a commit c3c297a

File tree

21 files changed

+156
-90
lines changed

21 files changed

+156
-90
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ jobs:
8282
if: contains(matrix.os, 'ubuntu')
8383
- run: RUSTFLAGS="-C link-arg=-Wl,--compress-debug-sections=zlib-gnu" cargo test --features gimli-symbolize
8484
if: contains(matrix.os, 'ubuntu')
85+
- run: cargo build --manifest-path crates/as-if-std/Cargo.toml
8586

8687
windows_arm64:
8788
name: Windows AArch64

Cargo.toml

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ autotests = true
1515
edition = "2018"
1616

1717
[workspace]
18-
members = ['crates/cpp_smoke_test']
18+
members = ['crates/cpp_smoke_test', 'crates/as-if-std']
1919
exclude = ['crates/without_debuginfo', 'crates/macos_frames_test', 'crates/line-tables-only']
2020

2121
[dependencies]
@@ -32,9 +32,6 @@ rustc-serialize = { version = "0.3", optional = true }
3232
# Optionally demangle C++ frames' symbols in backtraces.
3333
cpp_demangle = { default-features = false, version = "0.3.0", optional = true }
3434

35-
# Internal dependencies when built as a dependency of libstd, do not use.
36-
core = { version = "1.0.0", optional = true, package = 'rustc-std-workspace-core' }
37-
compiler_builtins = { version = '0.1.2', optional = true }
3835

3936
# Optional dependencies enabled through the `gimli-symbolize` feature, do not
4037
# use these features directly.
@@ -72,7 +69,7 @@ std = []
7269
# be affected by feature selection here. Also note that it's highly unlikely you
7370
# want to configure this. If you're having trouble getting backtraces it's
7471
# likely best to open an issue.
75-
gimli-symbolize = ["addr2line", "miniz_oxide", "object", "std"]
72+
gimli-symbolize = ["addr2line", "miniz_oxide", "object"]
7673
libbacktrace = ["backtrace-sys/backtrace-sys"]
7774

7875
#=======================================
@@ -105,14 +102,6 @@ verify-winapi = [
105102
'winapi/winbase',
106103
'winapi/winnt',
107104
]
108-
rustc-dep-of-std = [
109-
'backtrace-sys/rustc-dep-of-std',
110-
'cfg-if/rustc-dep-of-std',
111-
'core',
112-
'compiler_builtins',
113-
'libc/rustc-dep-of-std',
114-
'rustc-demangle/rustc-dep-of-std',
115-
]
116105

117106
[[example]]
118107
name = "backtrace"

ci/run.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
set -ex
44

55
cargo test --target $TARGET
6+
cargo build --target $TARGET --manifest-path crates/as-if-std/Cargo.toml

crates/as-if-std/Cargo.toml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[package]
2+
name = "as-if-std"
3+
version = "0.1.0"
4+
authors = ["Alex Crichton <alex@alexcrichton.com>"]
5+
edition = "2018"
6+
publish = false
7+
8+
[lib]
9+
test = false
10+
doc = false
11+
doctest = false
12+
bench = false
13+
14+
[dependencies]
15+
cfg-if = "0.1.10"
16+
rustc-demangle = "0.1.4"
17+
libc = { version = "0.2.45", default-features = false }
18+
addr2line = { version = "0.12.0", default-features = false }
19+
miniz_oxide = { version = "0.4.0", default-features = false }
20+
21+
[dependencies.object]
22+
version = "0.20.0"
23+
default-features = false
24+
features = ['read_core', 'elf', 'macho', 'pe', 'unaligned']
25+
26+
[features]
27+
default = ['gimli-symbolize']
28+
gimli-symbolize = []

crates/as-if-std/build.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
println!("cargo:rustc-cfg=backtrace_in_libstd");
3+
}

crates/as-if-std/src/lib.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// A crate which builds the `backtrace` crate as-if it's included as a
2+
// submodule into the standard library. We try to set this crate up similarly
3+
// to the standard library itself to minimize the likelihood of issues when
4+
// updating the `backtrace` crate.
5+
6+
#![no_std]
7+
8+
extern crate alloc;
9+
10+
// We want to `pub use std::*` in the root but we don't want `std` available in
11+
// the root namespace, so do this in a funky inner module.
12+
mod __internal {
13+
extern crate std;
14+
pub use std::*;
15+
}
16+
17+
pub use __internal::*;
18+
19+
// This is the magical part which we hope works.
20+
#[path = "../../../src/lib.rs"]
21+
mod the_backtrace_crate;

src/backtrace/dbghelp.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
2222
#![allow(bad_style)]
2323

24-
use crate::dbghelp;
25-
use crate::windows::*;
24+
use super::super::{dbghelp, windows::*};
2625
use core::ffi::c_void;
2726
use core::mem;
2827

src/backtrace/libunwind.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
//!
2626
//! This is the default unwinding API for all non-Windows platforms currently.
2727
28+
use super::super::Bomb;
2829
use core::ffi::c_void;
2930

3031
pub enum Frame {
@@ -103,7 +104,7 @@ pub unsafe fn trace(mut cb: &mut dyn FnMut(&super::Frame) -> bool) {
103104
inner: Frame::Raw(ctx),
104105
};
105106

106-
let mut bomb = crate::Bomb { enabled: true };
107+
let mut bomb = Bomb { enabled: true };
107108
let keep_going = cb(&cx);
108109
bomb.enabled = false;
109110

src/dbghelp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
2424
#![allow(non_snake_case)]
2525

26-
use crate::windows::*;
26+
use super::windows::*;
2727
use core::mem;
2828
use core::ptr;
2929

src/lib.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,32 +49,39 @@
4949
feature(sgx_platform)
5050
)]
5151
#![warn(rust_2018_idioms)]
52+
// When we're building as part of libstd, silence all warnings since they're
53+
// irrelevant as this crate is developed out-of-tree.
54+
#![cfg_attr(backtrace_in_libstd, allow(warnings))]
5255

5356
#[cfg(feature = "std")]
5457
#[macro_use]
5558
extern crate std;
5659

57-
pub use crate::backtrace::{trace_unsynchronized, Frame};
60+
// This is only used for gimli right now, so silence warnings elsewhere.
61+
#[cfg_attr(not(target_os = "linux"), allow(unused_extern_crates))]
62+
extern crate alloc;
63+
64+
pub use self::backtrace::{trace_unsynchronized, Frame};
5865
mod backtrace;
5966

60-
pub use crate::symbolize::resolve_frame_unsynchronized;
61-
pub use crate::symbolize::{resolve_unsynchronized, Symbol, SymbolName};
67+
pub use self::symbolize::resolve_frame_unsynchronized;
68+
pub use self::symbolize::{resolve_unsynchronized, Symbol, SymbolName};
6269
mod symbolize;
6370

64-
pub use crate::types::BytesOrWideString;
71+
pub use self::types::BytesOrWideString;
6572
mod types;
6673

6774
#[cfg(feature = "std")]
68-
pub use crate::symbolize::clear_symbol_cache;
75+
pub use self::symbolize::clear_symbol_cache;
6976

7077
mod print;
7178
pub use print::{BacktraceFmt, BacktraceFrameFmt, PrintFmt};
7279

7380
cfg_if::cfg_if! {
7481
if #[cfg(feature = "std")] {
75-
pub use crate::backtrace::trace;
76-
pub use crate::symbolize::{resolve, resolve_frame};
77-
pub use crate::capture::{Backtrace, BacktraceFrame, BacktraceSymbol};
82+
pub use self::backtrace::trace;
83+
pub use self::symbolize::{resolve, resolve_frame};
84+
pub use self::capture::{Backtrace, BacktraceFrame, BacktraceSymbol};
7885
mod capture;
7986
}
8087
}

0 commit comments

Comments
 (0)