orbbec-sdk-sys 0.1.2+2.5.5

Low-level Rust bindings for https://github.com/orbbec/OrbbecSDK_v2
Documentation
use std::env;
use std::path::PathBuf;

use cmake::Config;

fn main() {
    let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());

    println!("cargo:rerun-if-changed=vendor/*");
    // Copy the vendor directory to OUT_DIR for building
    // This is necessary because cmake will generate some version info in the source tree
    fs_extra::dir::copy(
        "vendor/",
        &out_dir,
        &fs_extra::dir::CopyOptions::new().overwrite(true),
    )
    .expect("Failed to copy vendor directory");

    let libdir_path = out_dir
        .join("vendor")
        // Canonicalize the path as `rustc-link-search` requires an absolute
        // path.
        .canonicalize()
        .expect("cannot canonicalize path");

    let headers_path = libdir_path.join("include/libobsensor/ObSensor.h");

    let build_destination = Config::new(&libdir_path)
        // The main CMakeLists already requires 3.5, but some children do not, this helps with compatibility with newer CMakes
        .define("CMAKE_POLICY_VERSION_MINIMUM", "3.5")
        // Disable building unnecessary components
        .define("OB_INSTALL_EXAMPLES_SOURCE", "OFF")
        .define("OB_BUILD_EXAMPLES", "OFF")
        .define("OB_BUILD_TESTS", "OFF")
        .define("OB_BUILD_DOCS", "OFF")
        .define("OB_BUILD_TOOLS", "OFF")
        .build();

    println!(
        "cargo:rustc-link-search=native={}",
        build_destination.join("lib").display()
    );
    println!("cargo:rustc-link-lib=dylib=OrbbecSDK");

    // The bindgen::Builder is the main entry point
    // to bindgen, and lets you build up options for
    // the resulting bindings.
    let bindings = bindgen::Builder::default()
        // The input header we would like to generate
        // bindings for.
        .header(headers_path.to_str().unwrap())
        .clang_arg(format!("-I{}", libdir_path.join("include/").display()))
        // Finish the builder and generate the bindings.
        .generate()
        // Unwrap the Result and panic on failure.
        .expect("Unable to generate bindings");

    // Clean up copied vendor directory
    let _ = fs_extra::dir::remove(&out_dir.join("vendor"));

    // Write the bindings to the $OUT_DIR/bindings.rs file.
    let out_path = out_dir.join("bindings.rs");
    bindings
        .write_to_file(out_path)
        .expect("Couldn't write bindings!");
}