Skip to content

Commit 0ea624b

Browse files
committed
devices: build init binary in Cargo OUT_DIR
As per previous discussion [0], adjust the build script to build the init binary in Cargo's OUT_DIR, which is meant precisely for use cases like this. This way, we don't leak a random binary into the source tree, where it would have to be removed manually. [0] containers#578 (comment) Signed-off-by: Daniel Müller <deso@posteo.net>
1 parent 53920d2 commit 0ea624b

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

src/devices/build.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ fn build_default_init() -> PathBuf {
66
let manifest_dir = PathBuf::from(std::env::var_os("CARGO_MANIFEST_DIR").unwrap());
77
let libkrun_root = manifest_dir.join("../..");
88
let init_src = libkrun_root.join("init/init.c");
9-
let init_bin = libkrun_root.join("init/init");
9+
10+
let out_dir = PathBuf::from(std::env::var_os("OUT_DIR").unwrap());
11+
let init_bin = out_dir.join("init");
1012

1113
println!("cargo:rerun-if-env-changed=CC_LINUX");
1214
println!("cargo:rerun-if-env-changed=CC");
@@ -45,7 +47,12 @@ fn build_default_init() -> PathBuf {
4547
fn main() {
4648
let init_binary_path = std::env::var_os("KRUN_INIT_BINARY_PATH")
4749
.map(PathBuf::from)
48-
.unwrap_or_else(build_default_init);
50+
.unwrap_or_else(|| {
51+
let init_path = build_default_init();
52+
// SAFETY: The build script is single threaded.
53+
unsafe { std::env::set_var("KRUN_INIT_BINARY_PATH", &init_path) };
54+
init_path
55+
});
4956
println!(
5057
"cargo:rustc-env=KRUN_INIT_BINARY_PATH={}",
5158
init_binary_path.display()

0 commit comments

Comments
 (0)