| 1 | # Libudev Rust Bindings |
| 2 | |
| 3 | The `libudev-sys` crate provides declarations and linkage for the `libudev` C library. Following the |
| 4 | `*-sys` package conventions, the `libudev-sys` crate does not define higher-level abstractions over |
| 5 | the native `libudev` library functions. |
| 6 | |
| 7 | ## Dependencies |
| 8 | In order to use the `libudev-sys` crate, you must have a Linux system with the `libudev` library |
| 9 | installed where it can be found by `pkg-config`. To install `libudev` on Debian-based Linux |
| 10 | distributions, execute the following command: |
| 11 | |
| 12 | ``` |
| 13 | sudo apt-get install libudev-dev |
| 14 | ``` |
| 15 | |
| 16 | `libudev` is a Linux-specific package. It is not available for Windows, OSX, or other operating |
| 17 | systems. |
| 18 | |
| 19 | ### Cross-Compiling |
| 20 | To link to a cross-compiled version of the native `libudev` library, it's necessary to set several |
| 21 | environment variables to configure `pkg-config` to work with a cross-compiler's sysroot. [Autotools |
| 22 | Mythbuster](https://autotools.io/) has a good explanation of [supporting |
| 23 | cross-compilation](https://autotools.io/pkgconfig/cross-compiling.html) with `pkg-config`. |
| 24 | |
| 25 | However, Rust's [`pkg-config` build helper](https://github.com/alexcrichton/pkg-config-rs) doesn't |
| 26 | support calling a `$CHOST`-prefixed `pkg-config`. It will always call `pkg-config` without a prefix. |
| 27 | To cross-compile `libudev-sys` with the `pkg-config` build helper, one must define the environment |
| 28 | variables `PKG_CONFIG_DIR`, `PKG_CONFIG_LIBDIR`, and `PKG_CONFIG_SYSROOT_DIR` for the *default* |
| 29 | `pkg-config`. It's also necessary to set `PKG_CONFIG_ALLOW_CROSS` to tell Rust's `pkg-config` helper |
| 30 | that it's okay to proceed with a cross-compile. |
| 31 | |
| 32 | To adapt the `pkg-config` wrapper in the Autotools Mythbuster guide so that it works with Rust, one |
| 33 | will end up with a script similar to the following: |
| 34 | |
| 35 | ```sh |
| 36 | #!/bin/sh |
| 37 | |
| 38 | SYSROOT=/build/root |
| 39 | |
| 40 | export PKG_CONFIG_DIR= |
| 41 | export PKG_CONFIG_LIBDIR=${SYSROOT}/usr/lib/pkgconfig:${SYSROOT}/usr/share/pkgconfig |
| 42 | export PKG_CONFIG_SYSROOT_DIR=${SYSROOT} |
| 43 | export PKG_CONFIG_ALLOW_CROSS=1 |
| 44 | |
| 45 | cargo build |
| 46 | ``` |
| 47 | |
| 48 | ## Usage |
| 49 | Add `libudev-sys` as a dependency in `Cargo.toml`: |
| 50 | |
| 51 | ```toml |
| 52 | [dependencies] |
| 53 | libudev-sys = "0.1" |
| 54 | ``` |
| 55 | |
| 56 | Import the `libudev_sys` crate and use the functions as they're defined in the native `libudev` |
| 57 | library. See the [`libudev` API documention](http://www.freedesktop.org/software/systemd/libudev/) |
| 58 | for more usage information. |
| 59 | |
| 60 | ```rust |
| 61 | extern crate libudev_sys as ffi; |
| 62 | ``` |
| 63 | |
| 64 | The `libudev-sys` build script detects the presence of native `libudev` functions and exports the |
| 65 | functions found to exist. `libudev-sys` exports this information from its build script, which Cargo |
| 66 | will provide to the build scripts of dependent packages in the form of environment variables: |
| 67 | |
| 68 | * `DEP_LIBUDEV_HWDB={true,false}`: The native `libudev` library has `udev_hwdb_*` functions. They will be |
| 69 | exported by `libudev-sys`. |
| 70 | |
| 71 | ### Finding Help |
| 72 | Since `libudev-sys` does nothing more than export symbols from the native `libudev` library, the |
| 73 | best source for help is the information already available for the native `libudev`: |
| 74 | |
| 75 | * [API Documentation](http://www.freedesktop.org/software/systemd/libudev/) |
| 76 | |
| 77 | ## License |
| 78 | Copyright © 2015 David Cuddeback |
| 79 | |
| 80 | Distributed under the [MIT License](LICENSE). |