Skip to content

Commit ada8041

Browse files
committed
basic-udev: add no-dependency udev alternative
Add an alternative to `libudev-sys` that doesn't require pkgconfig. This allows for cross-compiling to Linux without first obtaining a sysroot for the target. It also allows for compiling to musl, and statically-linking projects that use hidapi. Signed-off-by: Sean Cross <[email protected]>
1 parent 62e9710 commit ada8041

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ linux-static-hidraw = []
4848
linux-shared-libusb = []
4949
linux-shared-hidraw = []
5050
linux-native = ["dep:udev", "dep:nix"]
51+
linux-native-basic-udev = ["dep:basic-udev", "dep:nix"]
5152
illumos-static-libusb = []
5253
illumos-shared-libusb = []
5354
macos-shared-device = []

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,13 @@ let res = device.write(&buf).unwrap();
4242
println!("Wrote: {:?} byte(s)", res);
4343
```
4444

45+
# Cross-compiling
46+
47+
When cross-compiling, you will need a linker for the target platform. There are a number of ways to do this, but the easiest is to use [cargo-zigbuild](https://github.com/rust-cross/cargo-zigbuild).
48+
49+
* **macOS**: [Obtain an SDK](https://github.com/ziglang/zig/issues/1349#issuecomment-1343055209) and set the SDKROOT variable
50+
* **Linux** (musl or gnu): Compile with `--no-default-features --features linux-native-basic-udev` or obtain a sysroot for your target architecture
51+
* **Windows**: No extra requirements
52+
4553
# Documentation
4654
Available at [docs.rs](https://docs.rs/hidapi).

build.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ fn compile_linux() {
4949
// First check the features enabled for the crate.
5050
// Only one linux backend should be enabled at a time.
5151

52-
let avail_backends: [(&'static str, Box<dyn Fn()>); 5] = [
52+
let avail_backends: [(&'static str, Box<dyn Fn()>); 6] = [
5353
(
5454
"LINUX_STATIC_HIDRAW",
5555
Box::new(|| {
@@ -106,6 +106,15 @@ fn compile_linux() {
106106
// The udev crate takes care of finding its library
107107
}),
108108
),
109+
(
110+
"LINUX_NATIVE_BASIC_UDEV",
111+
Box::new(|| {
112+
// Enable `feature="linux-native"` to reuse the existing
113+
// linux-native code. It is considered an error in
114+
// basic-udev if this fails to compile.
115+
println!("cargo:rustc-cfg=feature=\"linux-native\"");
116+
}),
117+
),
109118
];
110119

111120
let mut backends = avail_backends

src/linux_native.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
33
mod ioctl;
44

5+
#[cfg(feature = "linux-native-basic-udev")]
6+
use basic_udev as udev;
7+
58
use std::{
69
cell::{Cell, Ref, RefCell},
710
ffi::{CStr, CString, OsStr, OsString},

0 commit comments

Comments
 (0)