forked from containers/libkrun
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
61 lines (54 loc) · 2.03 KB
/
build.rs
File metadata and controls
61 lines (54 loc) · 2.03 KB
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
use std::ffi::OsStr;
use std::path::PathBuf;
use std::process::Command;
fn build_default_init() -> PathBuf {
let manifest_dir = PathBuf::from(std::env::var_os("CARGO_MANIFEST_DIR").unwrap());
let libkrun_root = manifest_dir.join("../..");
let init_src = libkrun_root.join("init/init.c");
let out_dir = PathBuf::from(std::env::var_os("OUT_DIR").unwrap());
let init_bin = out_dir.join("init");
println!("cargo:rerun-if-env-changed=CC_LINUX");
println!("cargo:rerun-if-env-changed=CC");
println!("cargo:rerun-if-env-changed=TIMESYNC");
println!("cargo:rerun-if-changed={}", init_src.display());
println!(
"cargo:rerun-if-changed={}",
libkrun_root.join("init/jsmn.h").display()
);
let mut init_cc_flags = vec!["-O2", "-static", "-Wall"];
if std::env::var_os("TIMESYNC").as_deref() == Some(OsStr::new("1")) {
init_cc_flags.push("-D__TIMESYNC__");
}
let cc_value = std::env::var("CC_LINUX")
.or_else(|_| std::env::var("CC"))
.unwrap_or_else(|_| "cc".to_string());
let mut cc_parts = cc_value.split_ascii_whitespace();
let cc = cc_parts.next().expect("CC_LINUX/CC must not be empty");
let status = Command::new(cc)
.args(cc_parts)
.args(&init_cc_flags)
.arg("-o")
.arg(&init_bin)
.arg(&init_src)
.status()
.unwrap_or_else(|e| panic!("failed to execute {cc}: {e}"));
if !status.success() {
panic!("failed to compile init/init.c: {status}");
}
init_bin
}
fn main() {
let init_binary_path = std::env::var_os("KRUN_INIT_BINARY_PATH")
.map(PathBuf::from)
.unwrap_or_else(|| {
let init_path = build_default_init();
// SAFETY: The build script is single threaded.
unsafe { std::env::set_var("KRUN_INIT_BINARY_PATH", &init_path) };
init_path
});
println!(
"cargo:rustc-env=KRUN_INIT_BINARY_PATH={}",
init_binary_path.display()
);
println!("cargo:rerun-if-env-changed=KRUN_INIT_BINARY_PATH");
}