virt-sys 0.3.2

Native bindings to the libvirt C library
Documentation
use std::error::Error;
use std::fs;
use std::path::PathBuf;
use std::{env, process};

const LIBVIRT_VERSION: &str = "6.0.0";

fn main() {
    match run() {
        Ok(()) => {}
        Err(e) => {
            eprintln!("error: {e}");
            process::exit(1);
        }
    }
}

#[cfg(feature = "bindgen_regenerate")]
fn bindgen_regenerate(bindgen_out_file: &PathBuf) -> Result<(), Box<dyn Error>> {
    // We want to make sure that the generated bindings.rs file includes all libvirt APIs,
    // including the ones that are QEMU-specific
    if !cfg!(feature = "qemu") {
        return Err("qemu must be enabled along with bindgen_regenerate".into());
    }

    let bindings = bindgen::builder()
        .header("wrapper.h")
        .allowlist_var("^(VIR_|vir).*")
        .allowlist_type("^vir.*")
        .allowlist_function("^vir.*")
        // this is only false on esoteric platforms which libvirt does not support
        .size_t_is_usize(true)
        .generate_comments(false)
        .prepend_enum_name(false)
        .generate_cstr(true)
        // Put the oldest supported rust version here
        .rust_target(bindgen::RustTarget::stable(75, 0).expect("Uknown rust target version"))
        // Our public structs are not constant in size, layout changes between archs
        .layout_tests(false)
        .formatter(bindgen::Formatter::Prettyplease);

    bindings
        .generate()
        .map_err(|_| String::from("could not generate bindings"))?
        .write_to_file(bindgen_out_file)?;

    Ok(())
}

#[cfg(not(feature = "bindgen_regenerate"))]
fn bindgen_regenerate(_: &PathBuf) -> Result<(), Box<dyn Error>> {
    // We haven't been asked to regenerate bindings.rs, so nothing to do here
    Ok(())
}

fn run() -> Result<(), Box<dyn Error>> {
    println!("cargo:rerun-if-changed=wrapper.h");
    println!("cargo:rerun-if-env-changed=DOCS_RS");

    let mut config = pkg_config::Config::new();

    // On regular builds, we want to abort at compile-time if we do not find libvirt, since refusing
    // to do so moves the errors to link-time and breaks cargo's assumption that the crate compiled
    // successfully (so you have to remember to `cargo clean` yourself after installing libvirt).
    //
    // For docs.rs documentation builds we can ignore those errors. docs.rs sets DOCS_RS and
    // recommends using it for this purpose: https://docs.rs/about/builds
    let docs = env::var_os("DOCS_RS").is_some();

    let found = config.atleast_version(LIBVIRT_VERSION).probe("libvirt");
    if !docs {
        found?;
    }

    if cfg!(feature = "qemu") {
        let found = config
            .atleast_version(LIBVIRT_VERSION)
            .probe("libvirt-qemu");
        if !docs {
            found?;
        }
    }

    let bindgen_in_dir = PathBuf::from("bindgen");
    let bindgen_in_file = bindgen_in_dir.join("bindings.rs");
    let bindgen_out_dir = PathBuf::from(env::var("OUT_DIR")?);
    let bindgen_out_file = bindgen_out_dir.join("bindings.rs");

    bindgen_regenerate(&bindgen_in_file)?;

    fs::copy(bindgen_in_file, bindgen_out_file)?;

    Ok(())
}