forked from enarx-archive/enarx-keepldr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
242 lines (200 loc) · 7.13 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// SPDX-License-Identifier: Apache-2.0
use std::collections::HashMap;
use std::ffi::OsStr;
use std::fs::OpenOptions;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use walkdir::WalkDir;
const CRATE: &str = env!("CARGO_MANIFEST_DIR");
const TEST_BINS_IN: &str = "tests/bin";
fn find_files_with_extensions<'a>(
exts: &'a [&'a str],
path: impl AsRef<Path>,
) -> impl Iterator<Item = PathBuf> + 'a {
WalkDir::new(&path)
.into_iter()
.filter_map(|e| e.ok())
.filter(move |e| {
e.path()
.extension()
.and_then(OsStr::to_str)
.map(|ext| exts.contains(&ext))
.unwrap_or(false)
})
.map(|x| x.path().to_owned())
}
fn rerun_src(path: impl AsRef<Path>) -> std::io::Result<()> {
for entry in find_files_with_extensions(&["rs", "s", "S"], &path) {
if let Some(path) = entry.to_str() {
println!("cargo:rerun-if-changed={}", path)
}
}
Ok(())
}
fn build_rs_tests(in_path: &Path, out_path: &Path) {
let filtered_env: HashMap<String, String> = std::env::vars()
.filter(|&(ref k, _)| {
k == "TERM" || k == "TZ" || k == "LANG" || k == "PATH" || k == "RUSTUP_HOME"
})
.collect();
let target_name = "x86_64-unknown-linux-musl";
for in_source in find_files_with_extensions(&["rs"], &in_path) {
let stdout: Stdio = OpenOptions::new()
.write(true)
.open("/dev/tty")
.map(Stdio::from)
.unwrap_or_else(|_| Stdio::inherit());
let stderr: Stdio = OpenOptions::new()
.write(true)
.open("/dev/tty")
.map(Stdio::from)
.unwrap_or_else(|_| Stdio::inherit());
let output = in_source.file_stem().unwrap();
let status = Command::new("rustc")
.current_dir(&out_path)
.env_clear()
.envs(&filtered_env)
.stdout(stdout)
.stderr(stderr)
.arg("--target")
.arg(target_name)
.arg(&in_source)
.arg("-o")
.arg(output)
.status()
.unwrap_or_else(|_| panic!("failed to compile {:#?}", &in_source));
if !status.success() {
panic!("Failed to compile {:?}", &in_source);
}
}
}
fn build_cc_tests(in_path: &Path, out_path: &Path) {
for in_source in find_files_with_extensions(&["c", "s", "S"], &in_path) {
let output = in_source.file_stem().unwrap();
let mut cmd = cc::Build::new()
.no_default_flags(true)
.get_compiler()
.to_command();
let status = cmd
.current_dir(&out_path)
.arg("-nostdlib")
.arg("-static")
.arg("-pie")
.arg("-fPIC")
.arg("-o")
.arg(output)
.arg(&in_source)
.status()
.unwrap_or_else(|_| panic!("failed to compile {:#?}", &in_source));
if !status.success() {
panic!("Failed to compile {:?}", &in_source);
}
}
}
fn main() {
println!("cargo:rerun-if-env-changed=OUT_DIR");
println!("cargo:rerun-if-env-changed=PROFILE");
let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap());
let out_dir_bin = out_dir.join("bin");
match std::fs::create_dir(&out_dir_bin) {
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {}
Err(e) => {
eprintln!("Can't create {:#?} : {:#?}", out_dir_bin, e);
std::process::exit(1);
}
Ok(_) => {}
}
build_cc_tests(&Path::new(CRATE).join(TEST_BINS_IN), &out_dir_bin);
build_rs_tests(&Path::new(CRATE).join(TEST_BINS_IN), &out_dir_bin);
let profile: &[&str] = match std::env::var("PROFILE").unwrap().as_str() {
"release" => &["--release"],
_ => &[],
};
let target_name = "x86_64-unknown-linux-musl";
let filtered_env: HashMap<String, String> = std::env::vars()
.filter(|&(ref k, _)| {
k == "TERM" || k == "TZ" || k == "LANG" || k == "PATH" || k == "RUSTUP_HOME"
})
.collect();
// internal crates are not included, if there is a `Cargo.toml` file
// trick cargo by renaming the `Cargo.toml` to `Cargo.tml` before
// publishing and rename it back here.
for entry in std::fs::read_dir("internal").unwrap() {
let path = entry.unwrap().path();
let cargo_toml = path.join("Cargo.toml");
let cargo_tml = path.join("Cargo.tml");
if cargo_tml.exists() {
std::fs::copy(cargo_tml, cargo_toml).unwrap();
}
}
for entry in std::fs::read_dir("internal").unwrap() {
let path_buf = entry.unwrap().path();
let shim_name = path_buf.clone();
let shim_name = shim_name
.file_name()
.unwrap()
.to_os_string()
.into_string()
.unwrap();
let shim_out_dir = out_dir.join(&path_buf);
let path: String = path_buf.into_os_string().into_string().unwrap();
println!("cargo:rerun-if-changed={}/Cargo.tml", path);
println!("cargo:rerun-if-changed={}/Cargo.toml", path);
println!("cargo:rerun-if-changed={}/Cargo.lock", path);
println!("cargo:rerun-if-changed={}/.cargo/config", path);
rerun_src(&path).unwrap();
if !shim_name.starts_with("shim-") {
continue;
}
let target_dir = shim_out_dir.clone().into_os_string().into_string().unwrap();
let stdout: Stdio = OpenOptions::new()
.write(true)
.open("/dev/tty")
.map(Stdio::from)
.unwrap_or_else(|_| Stdio::inherit());
let stderr: Stdio = OpenOptions::new()
.write(true)
.open("/dev/tty")
.map(Stdio::from)
.unwrap_or_else(|_| Stdio::inherit());
let status = Command::new("cargo")
.current_dir(&path)
.env_clear()
.envs(&filtered_env)
.stdout(stdout)
.stderr(stderr)
.arg("+nightly")
.arg("build")
.args(profile)
.arg("--target-dir")
.arg(&target_dir)
.arg("--target")
.arg(target_name)
.arg("--bin")
.arg(&shim_name)
.status()
.expect("failed to build shim");
if !status.success() {
eprintln!("Failed to build shim {}", path);
std::process::exit(1);
}
let out_bin = out_dir_bin.join(&shim_name);
let shim_out_bin = shim_out_dir
.join(&target_name)
.join(&std::env::var("PROFILE").unwrap())
.join(&shim_name);
let status = Command::new("strip")
.arg("--strip-unneeded")
.arg("-o")
.arg(&out_bin)
.arg(&shim_out_bin)
.status();
match status {
Ok(status) if status.success() => {}
_ => {
println!("cargo:warning=Failed to run `strip` on {:?}", &shim_out_bin);
std::fs::rename(&shim_out_bin, &out_bin).expect("move failed")
}
}
}
}