Skip to content

Commit

Permalink
Add basic bindgen of libvfio-user as -sys crate
Browse files Browse the repository at this point in the history
Based on https://rust-lang.github.io/rust-bindgen/tutorial-0.html

Still needs bindgen adjustments in order for it to build
  • Loading branch information
FlorianFreudiger committed Jun 21, 2023
1 parent 71f8f84 commit a7e0250
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@ Cargo.lock

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb


# Editor files
.idea/
*.iml
.vs/
.vscode/
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "libvfio-user-sys/libvfio-user"]
path = libvfio-user-sys/libvfio-user
url = https://github.com/nutanix/libvfio-user.git
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[workspace]
members=[
"libvfio-user-sys",
]
12 changes: 12 additions & 0 deletions libvfio-user-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "libvfio-user-sys"
version = "0.0.1"
edition = "2021"
license = "BSD-3-Clause"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[build-dependencies]
bindgen = "0.66.1"

[dependencies]
35 changes: 35 additions & 0 deletions libvfio-user-sys/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use std::env;
use std::path::PathBuf;

fn main() {
// Tell cargo to look for shared libraries in the specified directory
println!("cargo:rustc-link-search=libvfio-user/build/lib");

// Tell cargo to tell rustc to link our `vfio-user` library. Cargo will
// automatically know it must look for a `libvfio-user.so` file.
println!("cargo:rustc-link-lib=vfio-user");

// Tell cargo to invalidate the built crate whenever the wrapper changes
println!("cargo:rerun-if-changed=libvfio-user/include/libvfio-user.h");

// 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("libvfio-user/include/libvfio-user.h")
// Tell cargo to invalidate the built crate whenever any of the
// included header files changed.
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
// Finish the builder and generate the bindings.
.generate()
// Unwrap the Result and panic on failure.
.expect("Unable to generate bindings");

// Write the bindings to the $OUT_DIR/bindings.rs file.
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}
1 change: 1 addition & 0 deletions libvfio-user-sys/libvfio-user
Submodule libvfio-user added at 728da2
5 changes: 5 additions & 0 deletions libvfio-user-sys/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]

include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

0 comments on commit a7e0250

Please sign in to comment.