Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FreeBSD support #102

Merged
merged 1 commit into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/v4l2/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ mod detail {
pub unsafe fn close(fd: std::os::raw::c_int) -> std::os::raw::c_int {
libc::close(fd)
}
#[cfg(any(target_os = "linux", target_os = "android"))]
pub unsafe fn ioctl(
fd: std::os::raw::c_int,
request: vidioc::_IOC_TYPE,
Expand All @@ -80,6 +81,14 @@ mod detail {
*/
libc::syscall(libc::SYS_ioctl, fd, request, argp) as std::os::raw::c_int
}
#[cfg(target_os = "freebsd")]
pub unsafe fn ioctl(
fd: std::os::raw::c_int,
request: vidioc::_IOC_TYPE,
argp: *mut std::os::raw::c_void,
) -> std::os::raw::c_int {
libc::ioctl(fd, request, argp)
}
pub unsafe fn mmap(
start: *mut std::os::raw::c_void,
length: usize,
Expand Down
8 changes: 8 additions & 0 deletions src/v4l2/vidioc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,16 @@ const _IOC_SIZESHIFT: u8 = _IOC_TYPESHIFT + _IOC_TYPEBITS;
const _IOC_DIRSHIFT: u8 = _IOC_SIZESHIFT + _IOC_SIZEBITS;

const _IOC_NONE: u8 = 0;

#[cfg(any(target_os = "linux", target_os = "android"))]
const _IOC_WRITE: u8 = 1;
#[cfg(target_os = "freebsd")]
const _IOC_WRITE: u8 = 2;

#[cfg(any(target_os = "linux", target_os = "android"))]
const _IOC_READ: u8 = 2;
#[cfg(target_os = "freebsd")]
const _IOC_READ: u8 = 1;

macro_rules! _IOC_TYPECHECK {
($type:ty) => {
Expand Down
1 change: 1 addition & 0 deletions v4l2-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ build = "build.rs"

[build-dependencies]
bindgen = "0.69.1"
pkg-config = "0.3.30"
10 changes: 10 additions & 0 deletions v4l2-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,18 @@ use std::env;
use std::path::PathBuf;

fn main() {
let pkg_conf = pkg_config::Config::new()
.probe("libv4l2")
.expect("pkg-config has failed to find `libv4l2`");
Comment on lines +7 to +9
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't v4l2-sys to build purely against the kernel bindings? v4l-sys links against libv4l2. Can you clarify why this is needed here (i.e. does FreeBSD provide fallbacks here?).

Copy link
Contributor Author

@vladmovchan vladmovchan Aug 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Location of linux/videodev2.h (included in wrapper.h) is generally speaking indeterminate.
It might reside outside of default include location, i.e. not in /usr/include.

pkg-config crate provides means to query include directories necessary to locate libv4l2 header files in pkgconf database:

$ pkgconf --cflags-only-I libv4l2
-I/usr/local/include
$

I don't think changes from this PR affected/changed linking in any way.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't affect linking, but the question remains why the kernel-only package now relies on pkg-config and lib4l2 being available, just to locate standard Linux headers. That should at the very least require a comment otherwise, to explain that this build.rs is not interested in generating bindings to libv4l2.

There should be a different/better way that doesn't involve these implicit dependencies. @raymanfx what do you think?

Copy link
Contributor Author

@vladmovchan vladmovchan Aug 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I didn't realize the presence of linux/videodev2.h header file doesn't depend on the existence of libv4l2 in pkgconf database on Linux.

I prepared a PR to limit use of pkg-config to FreeBSD only avoid libv4l2 dependency added via pkg-config: #106

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, understood, yeah it should be a Linux UAPI header (https://github.com/torvalds/linux/blob/master/include/uapi/linux/videodev2.h), but maybe FreeBSD provides it as part of the v4l2 package for compatibility? I'm not certain how FreeBSD works, but I think I found it here: https://www.freshports.org/multimedia/v4l_compat


let bindings = bindgen::Builder::default()
.header("wrapper.h")
.clang_args(
pkg_conf
.include_paths
.into_iter()
.map(|path| format!("-I{}", path.to_string_lossy())),
)
.generate()
.expect("Failed to generate bindings");

Expand Down