Skip to content
Myr

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

3 files+64 added−26 removed

added:tests/test_backtrace.rs+13 added
1#[rustversion::not(nightly)]2#[ignore]3#[test]4fn test_backtrace() {}56#[rustversion::nightly]7#[test]8fn test_backtrace() {9 use anyhow::anyhow;1011 let error = anyhow!("oh no!");12 let _ = error.backtrace();13}
modified:build.rs+50 added-26 removed
1use std::env;2use std::process::Command;3use std::str::{self, FromStr};2use std::fs;3use std::path::Path;4use std::process::{Command, ExitStatus};55fn 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.9const PROBE: &str = r#"10 #![feature(backtrace)]11 #![allow(dead_code)]1211 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};1616struct Compiler {17 minor: u32,18 nightly: bool,19}17 #[derive(Debug)]18 struct E;1921fn 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 }2526 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"#;3631 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 })37fn main() {38 match compile_probe() {39 Some(status) if status.success() => println!("cargo:rustc-cfg=backtrace"),40 _ => {}41 }42}4344fn 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