-
Notifications
You must be signed in to change notification settings - Fork 54
/
build.rs
105 lines (86 loc) · 2.73 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use std::{
env, fs,
path::Path,
process::{Command, ExitStatus},
};
fn main() {
let ac = autocfg::new();
// https://github.com/rust-lang/rust/issues/99301 [nightly]
//
// Autocfg does currently not support custom probes, or `nightly` only features
match compile_probe(GENERIC_MEMBER_ACCESS_PROBE) {
Some(status) if status.success() => autocfg::emit("generic_member_access"),
_ => {}
}
// https://github.com/rust-lang/rust/issues/47809 [rustc-1.46]
ac.emit_expression_cfg("std::panic::Location::caller", "track_caller");
if ac.probe_rustc_version(1, 52) {
autocfg::emit("eyre_no_fmt_arguments_as_str");
}
if ac.probe_rustc_version(1, 58) {
autocfg::emit("eyre_no_fmt_args_capture");
}
if ac.probe_rustc_version(1, 65) {
autocfg::emit("backtrace")
}
}
// This code exercises the surface area or the generic member access feature for the `std::error::Error` trait.
//
// This is use to detect and supply backtrace information through different errors types.
const GENERIC_MEMBER_ACCESS_PROBE: &str = r#"
#![feature(error_generic_member_access)]
#![allow(dead_code)]
use std::error::{Error, Request};
use std::fmt::{self, Display};
#[derive(Debug)]
struct E {
backtrace: MyBacktrace,
}
#[derive(Debug)]
struct MyBacktrace;
impl Display for E {
fn fmt(&self, _formatter: &mut fmt::Formatter) -> fmt::Result {
unimplemented!()
}
}
impl Error for E {
fn provide<'a>(&'a self, request: &mut Request<'a>) {
request
.provide_ref::<MyBacktrace>(&self.backtrace);
}
}
"#;
fn compile_probe(probe: &str) -> Option<ExitStatus> {
let rustc = env::var_os("RUSTC")?;
let out_dir = env::var_os("OUT_DIR")?;
let probefile = Path::new(&out_dir).join("probe.rs");
fs::write(&probefile, probe).ok()?;
// Supports invoking rustc thrugh a wrapper
let mut cmd = if let Some(wrapper) = env::var_os("RUSTC_WRAPPER") {
let mut cmd = Command::new(wrapper);
cmd.arg(rustc);
cmd
} else {
Command::new(rustc)
};
if let Some(target) = env::var_os("TARGET") {
cmd.arg("--target").arg(target);
}
// If Cargo wants to set RUSTFLAGS, use that.
if let Ok(rustflags) = env::var("CARGO_ENCODED_RUSTFLAGS") {
if !rustflags.is_empty() {
for arg in rustflags.split('\x1f') {
cmd.arg(arg);
}
}
}
cmd.arg("--edition=2018")
.arg("--crate-name=eyre_build")
.arg("--crate-type=lib")
.arg("--emit=metadata")
.arg("--out-dir")
.arg(out_dir)
.arg(probefile)
.status()
.ok()
}