Skip to content

Commit 70838a1

Browse files
committed
v4l2-sys: Replace FreeBSD **host-only** include path override with docs
With `cfg!()` on `target_os` this include path is unconditionally used if the _host_ OS is FreeBSD, even if the target OS is different (and its cross-compilation headers are installed elsewhere on the system). The accurate target OS, regardless of what the build script is _running on_ is stored in `CARGO_CFG_TARGET_OS`. Since it is unlikely that the FreeBSD headers reside in `/usr/ local/include` when the *target* is FreeBSD while the host may be something completely different, remove the workaround and document how the user can set up arbitrary include directories for their target using `BINDGEN_EXTRA_CLANG_ARGS` (or the triple-specific variant) by documenting this environment variable in the main `README`. It is common for developers to maintain such a configuration in their home directory's `~/.cargo/ config.toml` for the various architectures that they cross-compile to (together with related variables for the linker and `cc-rs`).
1 parent 677b02d commit 70838a1

2 files changed

Lines changed: 19 additions & 14 deletions

File tree

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,21 @@ fn main() {
9494
```
9595

9696
Have a look at the provided `examples` for more sample applications.
97+
98+
### Building and cross-compiling
99+
100+
When building on targets like FreeBSD, or cross-compiling for different targets entirely (as identified by their _targe triple_), bindgen may not know where to find the headers if they are located in a nonstandard directory like `/usr/local/include`, resulting in an error similar to `wrapper.h:1:10: fatal error: 'linux/videodev2.h' file not found`. In this case, provide the system include directory with the `-I` flag using the [target-specific environment variable][bindgen-env] (note that `-` is typically subtituted with `_` to help shells like `bash` parse it successfully):
101+
102+
```console
103+
$ BINDGEN_EXTRA_CLANG_ARGS_x86_64_unknown_freebsd="-I/usr/local/include" cargo build --target x86_64-unknown-freebsd
104+
```
105+
106+
It is also possible to set this environment variable for Rust inside [`.cargo/config.toml`][cargo-config] in your project directory or user home directory:
107+
108+
```toml
109+
[env]
110+
BINDGEN_EXTRA_CLANG_ARGS_x86_64-unknown-freebsd = "-I/usr/local/include"
111+
```
112+
113+
[bindgen-env]: https://github.com/rust-lang/rust-bindgen/blob/main/README.md#environment-variables
114+
[cargo-config]: https://doc.rust-lang.org/cargo/reference/config.html

v4l2-sys/build.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,11 @@
11
extern crate bindgen;
22

33
use std::env;
4-
use std::path::{Path, PathBuf};
4+
use std::path::PathBuf;
55

66
fn main() {
7-
let extra_include_paths = if cfg!(target_os = "freebsd") {
8-
assert!(
9-
Path::new("/usr/local/include/linux/videodev2.h").exists(),
10-
"Video4Linux `videodev2.h` UAPI header is required to generate bindings \
11-
against `libv4l2` and the header file is missing.\n\
12-
Consider installing `multimedia/v4l_compat` FreeBSD package."
13-
);
14-
vec!["-I/usr/local/include"]
15-
} else {
16-
vec![]
17-
};
18-
197
let bindings = bindgen::Builder::default()
208
.header("wrapper.h")
21-
.clang_args(extra_include_paths)
229
.generate()
2310
.expect("Failed to generate bindings");
2411

0 commit comments

Comments
 (0)