Merge pull request #24 from dtolnay/probe
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Replace version check with probing Backtrace api
authored by David Tolnay · committed by GitHub · parents d7c37d6, 6d0a019 · Browse files · 3 files·+64 added−26 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+64 added−26 removed
- tests:+13 added−0 removed
- other:+51 added−26 removed
Provenance
- Commit
2191a63- Tree
f5e8a1e- Author
David Tolnay <dtolnay@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
d7c37d6- Parent 2
6d0a019
3 files changed+64 added−26 removed
added:tests/test_backtrace.rs+13 added
1+#[rustversion::not(nightly)]2+#[ignore]3+#[test]4+fn test_backtrace() {}5+6+#[rustversion::nightly]7+#[test]8+fn test_backtrace() {9+ use anyhow::anyhow;10+11+ let error = anyhow!("oh no!");12+ let _ = error.backtrace();13+}modified:build.rs+50 added-26 removed
1 use std::env;2-use std::process::Command;3-use std::str::{self, FromStr};2+use std::fs;3+use std::path::Path;4+use std::process::{Command, ExitStatus};5 5-fn main() {6- let compiler = match rustc_version() {7- Some(compiler) => compiler,8- None => return,9- };6+// This code exercises the surface area that we expect of the std Backtrace7+// type. If the current toolchain is able to compile it, we go ahead and use8+// backtrace in anyhow.9+const PROBE: &str = r#"10+ #![feature(backtrace)]11+ #![allow(dead_code)]12 11- if compiler.minor >= 40 && compiler.nightly {12- println!("cargo:rustc-cfg=backtrace");13- }14-}13+ use std::backtrace::{Backtrace, BacktraceStatus};14+ use std::error::Error;15+ use std::fmt::{self, Display};16 16-struct Compiler {17- minor: u32,18- nightly: bool,19-}17+ #[derive(Debug)]18+ struct E;19 21-fn rustc_version() -> Option<Compiler> {22- let rustc = env::var_os("RUSTC")?;23- let output = Command::new(rustc).arg("--version").output().ok()?;24- let version = str::from_utf8(&output.stdout).ok()?;20+ impl Display for E {21+ fn fmt(&self, _formatter: &mut fmt::Formatter) -> fmt::Result {22+ unimplemented!()23+ }24+ }25 26- let mut pieces = version.split('.');27- if pieces.next() != Some("rustc 1") {28- return None;26+ impl Error for E {27+ fn backtrace(&self) -> Option<&Backtrace> {28+ let backtrace = Backtrace::capture();29+ match backtrace.status() {30+ BacktraceStatus::Captured | BacktraceStatus::Disabled | _ => {}31+ }32+ unimplemented!()33+ }34 }35+"#;36 31- let next = pieces.next()?;32- let minor = u32::from_str(next).ok()?;33- let nightly = version.contains("nightly") || version.contains("dev");34- Some(Compiler { minor, nightly })37+fn main() {38+ match compile_probe() {39+ Some(status) if status.success() => println!("cargo:rustc-cfg=backtrace"),40+ _ => {}41+ }42 }43+44+fn compile_probe() -> Option<ExitStatus> {45+ let rustc = env::var_os("RUSTC")?;46+ let out_dir = env::var_os("OUT_DIR")?;47+ let probefile = Path::new(&out_dir).join("lib.rs");48+ fs::write(&probefile, PROBE).ok()?;49+ Command::new(rustc)50+ .arg("--edition=2018")51+ .arg("--crate-name=anyhow_build")52+ .arg("--crate-type=lib")53+ .arg("--emit=metadata")54+ .arg("--out-dir")55+ .arg(out_dir)56+ .arg(probefile)57+ .status()58+ .ok()59+}modified:Cargo.toml+1 added
12 unmodified lines13141516171812 unmodified linesdefault = ["std"]std = []
[dev-dependencies]rustversion = "1.0"thiserror = "1.0"