use anyhow::{Context, Result};
#[cfg(feature = "build-metadata")]
use built::write_built_file;
fn main() -> Result<()> {
println!("cargo:rerun-if-changed=build.rs");
let out_dir = std::env::var("OUT_DIR")?;
let out_path = std::path::PathBuf::from(&out_dir);
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR")?;
if std::env::var("CARGO_CFG_TARGET_OS")? == "windows" {
println!("cargo:rerun-if-changed=src/hyperlight_surrogate/src/main.rs");
println!("cargo:rerun-if-changed=src/hyperlight_surrogate/build.rs");
println!("cargo:rerun-if-changed=src/hyperlight_surrogate/Cargo.toml_temp_name");
std::fs::create_dir_all(format!("{out_dir}/hyperlight_surrogate/src"))?;
std::fs::copy(
format!("{manifest_dir}/src/hyperlight_surrogate/src/main.rs"),
format!("{out_dir}/hyperlight_surrogate/src/main.rs"),
)?;
std::fs::copy(
format!("{manifest_dir}/src/hyperlight_surrogate/build.rs"),
format!("{out_dir}/hyperlight_surrogate/build.rs"),
)?;
std::fs::copy(
format!("{manifest_dir}/src/hyperlight_surrogate/Cargo.toml_temp_name"),
format!("{out_dir}/hyperlight_surrogate/Cargo.toml"),
)?;
let target_manifest_path = format!("{out_dir}/hyperlight_surrogate/Cargo.toml");
let target_dir = out_path.join("../../hls");
let profile = std::env::var("PROFILE")?;
let build_profile = if profile.to_lowercase() == "debug" {
"dev".to_string()
} else {
profile.clone()
};
let target_triple = std::env::var("TARGET")?;
let status = std::process::Command::new("cargo")
.env("CARGO_TARGET_DIR", &target_dir)
.arg("build")
.arg("--manifest-path")
.arg(&target_manifest_path)
.arg("--target")
.arg(&target_triple)
.arg("--profile")
.arg(build_profile)
.arg("--verbose")
.status()
.expect("Failed to execute cargo build for surrogate");
if !status.success() {
panic!("Failed to build hyperlight surrogate");
}
println!("cargo:rustc-env=PROFILE={}", profile);
let surrogate_binary_dir = std::path::PathBuf::from(&target_dir)
.join(&target_triple)
.join(profile);
println!(
"cargo:rustc-env=HYPERLIGHT_SURROGATE_DIR={}",
surrogate_binary_dir.display()
);
}
if std::env::var("CARGO_CFG_TARGET_OS")? == "macos"
&& std::env::var("CARGO_FEATURE_HVF").is_ok()
{
println!("cargo:rerun-if-env-changed=BINDGEN_EXTRA_CLANG_ARGS");
println!(
"cargo:rerun-if-changed=src/hyperlight_host/src/hypervisor/virtual_machine/hvf/bindings.h"
);
println!(
"cargo:rerun-if-changed=src/hyperlight_host/src/hypervisor/virtual_machine/hvf/fp_abi.c"
);
println!("cargo:rustc-link-lib=framework=Hypervisor");
let sdk_path = std::process::Command::new("xcrun")
.args(["-sdk", "macosx", "-show-sdk-path"])
.output()?
.stdout;
let sdk_path = String::from_utf8_lossy(&sdk_path);
eprintln!("sdk path: {}", sdk_path.trim());
bindgen::Builder::default()
.clang_args(&[
"-isysroot",
sdk_path.trim(),
"-I",
&format!("{manifest_dir}/src/hypervisor/virtual_machine/hvf"),
])
.blocklist_type("hv_simd_fp_uchar16_t")
.blocklist_function("hv_vcpu_get_simd_fp_reg")
.blocklist_function("hv_vcpu_set_simd_fp_reg")
.default_alias_style(bindgen::AliasVariation::NewType)
.type_alias("hv_memory_flags_t")
.newtype_enum("hv_reg_t")
.newtype_enum("hv_simd_fp_reg_t")
.newtype_enum("hv_sys_reg_t")
.newtype_enum("hv_exit_reason_t")
.no_debug("hv_return_t")
.formatter(bindgen::Formatter::Prettyplease)
.header(format!(
"{manifest_dir}/src/hypervisor/virtual_machine/hvf/bindings.h"
))
.generate()
.context("Unable to generate hvf bindings")?
.write_to_file(out_path.join("hvf_bindings.rs"))
.context("Couldn't write hvf bindings")?;
cc::Build::new()
.opt_level(3)
.file(format!(
"{manifest_dir}/src/hypervisor/virtual_machine/hvf/fp_abi.c"
))
.compile("hyperlight_host_hvf_abi_wrapper");
}
cfg_aliases::cfg_aliases! {
gdb: { all(feature = "gdb", debug_assertions, target_arch = "x86_64") },
kvm: { all(feature = "kvm", target_os = "linux") },
mshv3: { all(feature = "mshv3", target_os = "linux") },
hvf: { all(feature = "hvf", target_os = "macos") },
crashdump: { all(feature = "crashdump", target_arch = "x86_64") },
print_debug: { all(feature = "print_debug", debug_assertions) },
unshared_snapshot_mem: { gdb },
}
#[cfg(feature = "build-metadata")]
write_built_file()?;
Ok(())
}