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
#[cfg(feature = "sandbox-microvm")]
use std::path::Path;
#[cfg(feature = "sandbox-microvm")]
/// Search for a shared library using ldconfig and common directory paths.
/// Returns the directory containing the library if found.
fn find_library_dir(name: &str) -> Option<String> {
// Try ldconfig -p first.
if let Ok(out) = std::process::Command::new("ldconfig").arg("-p").output() {
let stdout = String::from_utf8_lossy(&out.stdout);
for line in stdout.lines() {
if line.contains(name) {
// ldconfig -p output format: "libkrun.so (libc6,x86-64) => /usr/lib/libkrun.so"
if let Some(idx) = line.find("=> ") {
let path = &line[idx + 3..].trim();
if let Some(parent) = Path::new(path).parent() {
return Some(parent.to_string_lossy().to_string());
}
}
}
}
}
// Fall back to common install paths.
for dir in &[
"/usr/lib",
"/usr/lib64",
"/usr/local/lib",
"/usr/local/lib64",
] {
if Path::new(dir).join(name).exists() {
return Some(dir.to_string());
}
}
None
}
fn main() {
#[cfg(feature = "sandbox-microvm")]
{
// Search for both libraries before emitting any link directives.
// If either is missing, warn and skip linking — the main binary and
// library-level unit tests can still compile and run without libkrun.
// The runner binary (dirge-microvm-runner) will fail to link, but
// that's expected: it requires libkrun.so + libkrunfw.so at link time.
// On CI, only `cargo test --bin dirge` is run, so the runner is never
// built and the missing libkrun is not an error.
match (
find_library_dir("libkrun.so"),
find_library_dir("libkrunfw.so"),
) {
(Some(krun_dir), Some(krunfw_dir)) => {
println!("cargo:rustc-link-search=native={krun_dir}");
println!("cargo:rustc-link-lib=krun");
if krunfw_dir != krun_dir {
println!("cargo:rustc-link-search=native={krunfw_dir}");
}
println!("cargo:rustc-link-lib=krunfw");
}
_ => {
println!(
"cargo:warning=libkrun.so and/or libkrunfw.so not found — \
the microVM runner binary will not be buildable, but \
library-level tests and the main binary will work fine"
);
}
}
}
}