Build FFI bindings for the libgpiod helpers.
Signed-off-by: Viresh Kumar viresh.kumar@linaro.org --- .gitignore | 7 ++++++ Cargo.toml | 12 ++++++++++ build.rs | 60 +++++++++++++++++++++++++++++++++++++++++++++++++ src/bindings.h | 3 +++ src/bindings.rs | 16 +++++++++++++ src/lib.rs | 0 6 files changed, 98 insertions(+) create mode 100644 Cargo.toml create mode 100644 build.rs create mode 100644 src/bindings.h create mode 100644 src/bindings.rs create mode 100644 src/lib.rs
diff --git a/.gitignore b/.gitignore index 2d7cc7fc0758..82a9d1ea2879 100644 --- a/.gitignore +++ b/.gitignore @@ -29,3 +29,10 @@ libtool *-libtool m4/ stamp-h1 + + +# Added by cargo + +/target +Cargo.lock +bindings_gen.rs diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 000000000000..1a43063a9d64 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "libgpiod" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] + +[build-dependencies] +bindgen = "0.59.1" +cc = "1.0.46" diff --git a/build.rs b/build.rs new file mode 100644 index 000000000000..ee29798312d1 --- /dev/null +++ b/build.rs @@ -0,0 +1,60 @@ +extern crate bindgen; + +use std::path::PathBuf; + +fn generate_bindings() { + // Tell cargo to invalidate the built crate whenever the wrapper changes + println!("cargo:rerun-if-changed=include/gpiod.h"); + println!("cargo:rerun-if-changed=src/bindings.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("src/bindings.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("src/bindings_gen.rs"); + bindings + .write_to_file(out_path) + .expect("Couldn't write bindings!"); +} + +fn build_gpiod() { + // Tell Cargo that if the given file changes, to rerun this build script. + println!("cargo:rerun-if-changed=lib/"); + + let files = vec![ + "lib/chip.c", + "lib/edge-event.c", + "lib/info-event.c", + "lib/internal.c", + "lib/line-config.c", + "lib/line-info.c", + "lib/line-request.c", + "lib/misc.c", + "lib/request-config.c", + ]; + + // Use the `cc` crate to build a C file and statically link it. + cc::Build::new() + .files(files) + .define("_GNU_SOURCE", None) + .define("GPIOD_VERSION_STR", ""libgpio-rust"") + .include("include") + .compile("gpiod"); +} + +fn main() { + generate_bindings(); + build_gpiod(); +} diff --git a/src/bindings.h b/src/bindings.h new file mode 100644 index 000000000000..734943a920e2 --- /dev/null +++ b/src/bindings.h @@ -0,0 +1,3 @@ +#include <string.h> +#include "../include/gpiod.h" + diff --git a/src/bindings.rs b/src/bindings.rs new file mode 100644 index 000000000000..3da778daa796 --- /dev/null +++ b/src/bindings.rs @@ -0,0 +1,16 @@ +// SPDX-License-Identifier: GPL-2.0 + +#[allow( + clippy::all, + deref_nullptr, + dead_code, + non_camel_case_types, + non_upper_case_globals, + non_snake_case, + improper_ctypes +)] + +mod bindings_raw { + include!("bindings_gen.rs"); +} +pub use bindings_raw::*; diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 000000000000..e69de29bb2d1