forked from enarx/enarx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
293 lines (246 loc) · 8.58 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// SPDX-License-Identifier: Apache-2.0
use std::collections::HashMap;
use std::ffi::OsStr;
use std::fs;
use std::os::unix::fs::FileTypeExt;
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/c-tests";
const AESM_SOCKET: &str = "/var/run/aesmd/aesm.socket";
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>) {
for entry in find_files_with_extensions(&["rs", "s", "S"], &path) {
if let Some(path) = entry.to_str() {
println!("cargo:rerun-if-changed={}", path)
}
if let Some(Some(path)) = entry.parent().map(Path::to_str) {
println!("cargo:rerun-if-changed={}", path)
}
}
}
fn build_cc_tests(in_path: &Path, out_path: &Path) {
for in_source in find_files_with_extensions(&["c", "s", "S"], &in_path) {
if let Some(path) = in_source.to_str() {
println!("cargo:rerun-if-changed={}", path)
}
if let Some(Some(path)) = in_source.parent().map(Path::to_str) {
println!("cargo:rerun-if-changed={}", 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-pie")
.arg("-fPIC")
.arg("-fno-omit-frame-pointer")
.arg("-fno-stack-protector")
.arg("-g")
.arg("-o")
.arg(output)
.arg(&in_source)
.status()
.unwrap_or_else(|_| panic!("failed to compile {:#?}", &in_source));
assert!(status.success(), "Failed to compile {:?}", &in_source);
}
}
// Build a binary named `bin_name` from the crate located at `in_dir`,
// targeting `target_name`, then strip the resulting binary and place it
// at `out_dir`/bin/`bin_name`.
fn cargo_build_bin(
in_dir: &Path,
out_dir: &Path,
target_name: &str,
bin_name: &str,
) -> std::io::Result<()> {
// And here's where we'd like to place the final (stripped) binary
let out_bin = out_dir.join("bin").join(bin_name);
// Don't run the build if ENARX_PREBUILT_${bin_name} is set
let prebuilt_env_name = format!("ENARX_PREBUILT_{}", bin_name);
if let Ok(prebuilt_path) = std::env::var(&prebuilt_env_name) {
println!(
"cargo:warning=Using prebuilt {} binary from {}: {}",
bin_name, prebuilt_env_name, &prebuilt_path
);
std::fs::copy(prebuilt_path, out_bin)?;
return Ok(());
}
let profile: &[&str] = match std::env::var("PROFILE").unwrap().as_str() {
"release" => &["--release"],
_ => &[],
};
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 path = in_dir.as_os_str().to_str().unwrap();
for p in [
"src",
"tests",
"build.rs",
"Cargo.tml",
"Cargo.toml",
"Cargo.lock",
"layout.ld",
".cargo",
".cargo/config",
] {
let file = in_dir.join(p);
if file.exists() {
println!("cargo:rerun-if-changed={}/{}", path, p);
}
}
rerun_src(&path);
let target_dir = out_dir.join(path);
let stdout: Stdio = fs::OpenOptions::new()
.write(true)
.open("/dev/tty")
.map(Stdio::from)
.unwrap_or_else(|_| Stdio::inherit());
let stderr: Stdio = fs::OpenOptions::new()
.write(true)
.open("/dev/tty")
.map(Stdio::from)
.unwrap_or_else(|_| Stdio::inherit());
let mut cmd = Command::new("cargo");
let cmd = cmd
.current_dir(&path)
.env_clear()
.envs(&filtered_env)
.stdout(stdout)
.stderr(stderr)
.arg("build")
.args(profile)
.arg("--target-dir")
.arg(&target_dir)
.arg("--target")
.arg(target_name)
.arg("--bin")
.arg(bin_name);
#[cfg(feature = "gdb")]
let cmd = cmd.arg("--features=gdb");
#[cfg(feature = "dbg")]
let cmd = cmd.arg("--features=dbg");
let status = cmd.status()?;
if !status.success() {
eprintln!("Failed to build in {}", path);
std::process::exit(1);
}
// This is the path to the newly-built binary.
// See https://doc.rust-lang.org/cargo/guide/build-cache.html for details.
let target_bin = target_dir
.join(target_name)
.join(std::env::var("PROFILE").unwrap())
.join(bin_name);
// Strip the binary
let status = Command::new("strip")
.arg("--strip-unneeded")
.arg("-o")
.arg(&out_bin)
.arg(&target_bin)
.status()?;
// Failing that, just copy it into place
if !status.success() {
println!("cargo:warning=Failed to run `strip` on {:?}", target_bin);
std::fs::rename(&target_bin, &out_bin)?;
}
Ok(())
}
fn create(path: &Path) {
match std::fs::create_dir(&path) {
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {}
Err(e) => {
eprintln!("Can't create {:#?} : {:#?}", path, e);
std::process::exit(1);
}
Ok(_) => {}
}
}
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_proto = out_dir.join("protos");
create(&out_dir_proto);
protobuf_codegen_pure::Codegen::new()
.out_dir(&out_dir_proto)
.inputs(&["src/protobuf/aesm-proto.proto"])
.include("src/protobuf")
.customize(protobuf_codegen_pure::Customize {
gen_mod_rs: Some(true),
..Default::default()
})
.run()
.expect("Protobuf codegen failed");
let out_dir_bin = out_dir.join("bin");
create(&out_dir_bin);
build_cc_tests(&Path::new(CRATE).join(TEST_BINS_IN), &out_dir_bin);
let target = "x86_64-unknown-linux-musl";
// 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();
}
let dir_name = path.file_name().unwrap().to_str().unwrap_or_default();
match dir_name {
#[cfg(feature = "wasmldr")]
"wasmldr" => cargo_build_bin(&path, &out_dir, target, "wasmldr").unwrap(),
#[cfg(feature = "backend-kvm")]
"shim-kvm" => cargo_build_bin(&path, &out_dir, target, "shim-kvm").unwrap(),
#[cfg(feature = "backend-sgx")]
"shim-sgx" => cargo_build_bin(&path, &out_dir, target, "shim-sgx").unwrap(),
_ => eprintln!("Unknown internal directory: {}", dir_name),
}
if cargo_tml.exists() {
std::fs::remove_file(&cargo_toml).unwrap()
}
}
if std::path::Path::new("/dev/sgx_enclave").exists()
&& fs::metadata("/dev/sgx_enclave")
.unwrap()
.file_type()
.is_char_device()
{
println!("cargo:rustc-cfg=host_can_test_sgx");
if std::path::Path::new(AESM_SOCKET).exists()
&& fs::metadata(AESM_SOCKET).unwrap().file_type().is_socket()
{
println!("cargo:rustc-cfg=host_can_test_attestation");
}
}
if std::path::Path::new("/dev/sev").exists() {
// Not expected to fail, as the file exists.
let metadata = fs::metadata("/dev/sev").unwrap();
let file_type = metadata.file_type();
if file_type.is_char_device() {
println!("cargo:rustc-cfg=host_can_test_sev");
println!("cargo:rustc-cfg=host_can_test_attestation");
}
}
}