-
Notifications
You must be signed in to change notification settings - Fork 17
/
build.rs
131 lines (117 loc) · 4.03 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright © 2018–2019 Trevor Spiteri
// Copying and distribution of this file, with or without
// modification, are permitted in any medium without royalty provided
// the copyright notice and this notice are preserved. This file is
// offered as-is, without any warranty.
#![allow(dead_code)]
#![allow(unused_variables)]
#![cfg_attr(feature = "fail-on-warnings", deny(warnings))]
use std::{
env,
ffi::OsString,
fs::{self, File},
io::{Result as IoResult, Write},
path::{Path, PathBuf},
process::Command,
};
struct Environment {
out_dir: PathBuf,
rustc: OsString,
}
fn main() {
let env = Environment {
out_dir: PathBuf::from(cargo_env("OUT_DIR")),
rustc: cargo_env("RUSTC"),
};
}
#[derive(PartialEq)]
struct Optional(bool);
impl Environment {
// 1. If optional feature is availble (both with and without flag), output:
// cargo:rustc-cfg=<name>
// 2. If feature is available with flag (both optional and not), output:
// cargo:rustc-cfg=nightly_<name>
// 3. If non-optional feature is not available, panic.
fn check_feature(
&self,
name: &str,
optional: Optional,
contents: &str,
nightly_features: Option<&str>,
) {
let try_dir = self.out_dir.join(format!("try_{}", name));
let filename = format!("try_{}.rs", name);
create_dir_or_panic(&try_dir);
println!("$ cd {:?}", try_dir);
#[derive(PartialEq)]
enum Iteration {
Stable,
Unstable,
}
let mut found = false;
for i in &[Iteration::Stable, Iteration::Unstable] {
let s;
let file_contents = match *i {
Iteration::Stable => contents,
Iteration::Unstable => match nightly_features {
Some(features) => {
s = format!("#![feature({})]\n{}", features, contents);
&s
}
None => continue,
},
};
create_file_or_panic(&try_dir.join(&filename), file_contents);
let mut cmd = Command::new(&self.rustc);
cmd.current_dir(&try_dir)
.args(&[&filename, "--emit=dep-info,metadata"]);
println!("$ {:?}", cmd);
let status = cmd
.status()
.unwrap_or_else(|_| panic!("Unable to execute: {:?}", cmd));
if status.success() {
if optional.0 {
println!("cargo:rustc-cfg={}", name);
}
if *i == Iteration::Unstable {
println!("cargo:rustc-cfg=nightly_{}", name);
}
found = true;
break;
}
}
remove_dir_or_panic(&try_dir);
if !found && !optional.0 {
panic!("essential feature not supported by compiler: {}", name);
}
}
}
fn cargo_env(name: &str) -> OsString {
env::var_os(name)
.unwrap_or_else(|| panic!("environment variable not found: {}, please use cargo", name))
}
fn remove_dir(dir: &Path) -> IoResult<()> {
if !dir.exists() {
return Ok(());
}
assert!(dir.is_dir(), "Not a directory: {:?}", dir);
println!("$ rm -r {:?}", dir);
fs::remove_dir_all(dir)
}
fn remove_dir_or_panic(dir: &Path) {
remove_dir(dir).unwrap_or_else(|_| panic!("Unable to remove directory: {:?}", dir));
}
fn create_dir(dir: &Path) -> IoResult<()> {
println!("$ mkdir -p {:?}", dir);
fs::create_dir_all(dir)
}
fn create_dir_or_panic(dir: &Path) {
create_dir(dir).unwrap_or_else(|_| panic!("Unable to create directory: {:?}", dir));
}
fn create_file_or_panic(filename: &Path, contents: &str) {
println!("$ printf '%s' {:?}... > {:?}", &contents[0..20], filename);
let mut file =
File::create(filename).unwrap_or_else(|_| panic!("Unable to create file: {:?}", filename));
file.write_all(contents.as_bytes())
.unwrap_or_else(|_| panic!("Unable to write to file: {:?}", filename));
}