system to the correct locations. During native builds, the configure script
can auto-detect the location of the development files.
-Rust bindings require cargo support.
+Rust bindings require cargo support. When building the Rust bindings along the
+C library using make, they will be automatically configured to build against the
+build results of the C library.
TESTING
-------
Automatically generated Rust FFI bindings via
[bindgen](https://github.com/rust-lang/rust-bindgen).
+## Build requirements
+
+A compatible variant of the C library needs to detectable using pkg-config.
+Alternatively, one can inform the build system about the location of the
+libs and headers by setting environment variables. The mechanism for that is
+documented in the
+[system_deps crate documentation](https://docs.rs/system-deps/6.1.0/system_deps/#overriding-build-flags).
+
## License
This project is licensed under either of
// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause
-// SPDX-FileCopyrightText: 2022 Linaro Ltd.
+// SPDX-FileCopyrightText: 2022-2023 Linaro Ltd.
// SPDX-FileCopyrightText: 2022 Viresh Kumar <viresh.kumar@linaro.org>
+// SPDX-FileCopyrightText: 2023 Erik Schilling <erik.schilling@linaro.org>
use std::env;
use std::path::PathBuf;
-fn generate_bindings() {
+fn main() {
+ // Probe dependency info based on the metadata from Cargo.toml
+ // (and potentially other sources like environment, pkg-config, ...)
+ // https://docs.rs/system-deps/latest/system_deps/#overriding-build-flags
+ let libs = system_deps::Config::new().probe().unwrap();
+
// Tell cargo to invalidate the built crate whenever following files change
- println!("cargo:rerun-if-changed=../../../include/gpiod.h");
+ println!("cargo:rerun-if-changed=wrapper.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()
+ let mut builder = bindgen::Builder::default()
// The input header we would like to generate
// bindings for.
- .header("../../../include/gpiod.h")
+ .header("wrapper.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.
+ .parse_callbacks(Box::new(bindgen::CargoCallbacks));
+
+ // Inform bindgen about the include paths identified by system_deps.
+ for (_name, lib) in libs {
+ for include_path in lib.include_paths {
+ builder = builder.clang_arg("-I").clang_arg(
+ include_path
+ .to_str()
+ .expect("Failed to convert include_path to &str!"),
+ );
+ }
+ }
+
+ // Finish the builder and generate the bindings.
+ let bindings = builder
.generate()
// Unwrap the Result and panic on failure.
.expect("Unable to generate bindings");
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}
-
-fn main() {
- generate_bindings();
-
- println!("cargo:rustc-link-search=./../../lib/.libs/");
- println!("cargo:rustc-link-lib=gpiod");
-}
# SPDX-FileCopyrightText: 2022 Linaro Ltd.
# SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
-command = cargo build --release --lib
+# We do not want to build against the system libs when building with make. So we
+# specify the paths to the build directory of the C lib.
+command = SYSTEM_DEPS_LIBGPIOD_NO_PKG_CONFIG=1 \
+ SYSTEM_DEPS_LIBGPIOD_SEARCH_NATIVE="${PWD}/../../../lib/.libs/" \
+ SYSTEM_DEPS_LIBGPIOD_LIB=gpiod \
+ SYSTEM_DEPS_LIBGPIOD_INCLUDE="${PWD}/../../../include/" \
+ cargo build --release --lib
if WITH_TESTS
command += --tests