Skip to content

Commit 13853fe

Browse files
authored
Merge pull request #54 from ids1024/bsd
Add support and CI tests for BSDs
2 parents c374c9e + a90c7bc commit 13853fe

File tree

5 files changed

+77
-16
lines changed

5 files changed

+77
-16
lines changed

.github/workflows/ci.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ jobs:
3333
- { target: x86_64-unknown-linux-gnu, os: ubuntu-latest, options: --no-default-features, features: x11 }
3434
- { target: x86_64-unknown-linux-gnu, os: ubuntu-latest, options: --no-default-features, features: "wayland,wayland-dlopen" }
3535
- { target: x86_64-unknown-redox, os: ubuntu-latest, }
36+
- { target: x86_64-unknown-freebsd, os: ubuntu-latest, }
37+
- { target: x86_64-unknown-netbsd, os: ubuntu-latest, }
3638
- { target: x86_64-apple-darwin, os: macos-latest, }
3739
# We're using Windows rather than Ubuntu to run the wasm tests because caching cargo-web
3840
# doesn't currently work on Linux.
@@ -77,6 +79,8 @@ jobs:
7779
if: >
7880
!((matrix.platform.os == 'ubuntu-latest') && contains(matrix.platform.target, 'i686')) &&
7981
!contains(matrix.platform.target, 'redox') &&
82+
!contains(matrix.platform.target, 'freebsd') &&
83+
!contains(matrix.platform.target, 'netbsd') &&
8084
matrix.rust_version != '1.60.0'
8185
run: cargo $CMD test --no-run --verbose --target ${{ matrix.platform.target }} $OPTIONS --features $FEATURES
8286

@@ -85,7 +89,9 @@ jobs:
8589
if: >
8690
!((matrix.platform.os == 'ubuntu-latest') && contains(matrix.platform.target, 'i686')) &&
8791
!contains(matrix.platform.target, 'wasm32') &&
88-
!contains(matrix.platform.target, 'redox')
92+
!contains(matrix.platform.target, 'redox') &&
93+
!contains(matrix.platform.target, 'freebsd') &&
94+
!contains(matrix.platform.target, 'netbsd')
8995
run: cargo $CMD test --verbose --target ${{ matrix.platform.target }} $OPTIONS --features $FEATURES
9096

9197
- name: Lint with clippy
@@ -94,5 +100,7 @@ jobs:
94100
(matrix.rust_version == 'stable') &&
95101
!contains(matrix.platform.options, '--no-default-features') &&
96102
!((matrix.platform.os == 'ubuntu-latest') && contains(matrix.platform.target, 'i686')) &&
97-
!contains(matrix.platform.target, 'redox')
103+
!contains(matrix.platform.target, 'redox') &&
104+
!contains(matrix.platform.target, 'freebsd') &&
105+
!contains(matrix.platform.target, 'netbsd')
98106
run: cargo clippy --all-targets --target ${{ matrix.platform.target }} $OPTIONS --features $FEATURES -- -Dwarnings

Cargo.toml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ rust-version = "1.60.0"
1414

1515
[features]
1616
default = ["x11", "wayland", "wayland-dlopen"]
17-
wayland = ["wayland-backend", "wayland-client", "nix"]
17+
wayland = ["wayland-backend", "wayland-client", "nix", "fastrand"]
1818
wayland-dlopen = ["wayland-sys/dlopen"]
1919
x11 = ["bytemuck", "x11rb", "x11-dl"]
2020

@@ -23,7 +23,7 @@ thiserror = "1.0.30"
2323
raw-window-handle = "0.5.0"
2424
log = "0.4.17"
2525

26-
[target.'cfg(any(target_os = "linux", target_os = "freebsd"))'.dependencies]
26+
[target.'cfg(all(unix, not(any(target_vendor = "apple", target_os = "android", target_os = "redox"))))'.dependencies]
2727
nix = { version = "0.26.1", optional = true }
2828
wayland-backend = { version = "0.1.0", features = ["client_system"], optional = true }
2929
wayland-client = { version = "0.30.0", optional = true }
@@ -32,6 +32,9 @@ bytemuck = { version = "1.12.3", optional = true }
3232
x11-dl = { version = "2.19.1", optional = true }
3333
x11rb = { version = "0.11.0", features = ["allow-unsafe-code", "dl-libxcb"], optional = true }
3434

35+
[target.'cfg(all(unix, not(any(target_vendor = "apple", target_os = "android", target_os = "redox", target_os = "linux", target_os = "freebsd"))))'.dependencies]
36+
fastrand = { version = "1.8.0", optional = true }
37+
3538
[target.'cfg(target_os = "windows")'.dependencies.windows-sys]
3639
version = "0.42.0"
3740
features = ["Win32_Graphics_Gdi", "Win32_UI_WindowsAndMessaging", "Win32_Foundation"]
@@ -52,6 +55,9 @@ features = ["CanvasRenderingContext2d", "Document", "Element", "HtmlCanvasElemen
5255
[target.'cfg(target_os = "redox")'.dependencies]
5356
redox_syscall = "0.3"
5457

58+
[build-dependencies]
59+
cfg_aliases = "0.1.1"
60+
5561
[dev-dependencies]
5662
instant = "0.1.12"
5763
winit = "0.27.2"

build.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn main() {
2+
cfg_aliases::cfg_aliases! {
3+
free_unix: { all(unix, not(any(target_vendor = "apple", target_os = "android", target_os = "redox"))) },
4+
x11_platform: { all(feature = "x11", free_unix, not(target_arch = "wasm32")) },
5+
wayland_platform: { all(feature = "wayland", free_unix, not(target_arch = "wasm32")) },
6+
}
7+
}

src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ extern crate core;
1010
mod cg;
1111
#[cfg(target_os = "redox")]
1212
mod orbital;
13-
#[cfg(all(feature = "wayland", any(target_os = "linux", target_os = "freebsd")))]
13+
#[cfg(wayland_platform)]
1414
mod wayland;
1515
#[cfg(target_arch = "wasm32")]
1616
mod web;
1717
#[cfg(target_os = "windows")]
1818
mod win32;
19-
#[cfg(all(feature = "x11", any(target_os = "linux", target_os = "freebsd")))]
19+
#[cfg(x11_platform)]
2020
mod x11;
2121

2222
mod error;
@@ -66,9 +66,9 @@ macro_rules! make_dispatch {
6666
}
6767

6868
make_dispatch! {
69-
#[cfg(all(feature = "x11", any(target_os = "linux", target_os = "freebsd")))]
69+
#[cfg(x11_platform)]
7070
X11(x11::X11Impl),
71-
#[cfg(all(feature = "wayland", any(target_os = "linux", target_os = "freebsd")))]
71+
#[cfg(wayland_platform)]
7272
Wayland(wayland::WaylandImpl),
7373
#[cfg(target_os = "windows")]
7474
Win32(win32::Win32Impl),
@@ -105,21 +105,21 @@ impl GraphicsContext {
105105
raw_display_handle: RawDisplayHandle,
106106
) -> Result<Self, SoftBufferError> {
107107
let imple: Dispatch = match (raw_window_handle, raw_display_handle) {
108-
#[cfg(all(feature = "x11", any(target_os = "linux", target_os = "freebsd")))]
108+
#[cfg(x11_platform)]
109109
(
110110
RawWindowHandle::Xlib(xlib_window_handle),
111111
RawDisplayHandle::Xlib(xlib_display_handle),
112112
) => Dispatch::X11(unsafe {
113113
x11::X11Impl::from_xlib(xlib_window_handle, xlib_display_handle)?
114114
}),
115-
#[cfg(all(feature = "x11", any(target_os = "linux", target_os = "freebsd")))]
115+
#[cfg(x11_platform)]
116116
(
117117
RawWindowHandle::Xcb(xcb_window_handle),
118118
RawDisplayHandle::Xcb(xcb_display_handle),
119119
) => Dispatch::X11(unsafe {
120120
x11::X11Impl::from_xcb(xcb_window_handle, xcb_display_handle)?
121121
}),
122-
#[cfg(all(feature = "wayland", any(target_os = "linux", target_os = "freebsd")))]
122+
#[cfg(wayland_platform)]
123123
(
124124
RawWindowHandle::Wayland(wayland_window_handle),
125125
RawDisplayHandle::Wayland(wayland_display_handle),

src/wayland/buffer.rs

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use nix::sys::memfd::{memfd_create, MemFdCreateFlag};
21
use std::{
32
ffi::CStr,
43
fs::File,
@@ -15,6 +14,50 @@ use wayland_client::{
1514

1615
use super::State;
1716

17+
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
18+
fn create_memfile() -> File {
19+
use nix::sys::memfd::{memfd_create, MemFdCreateFlag};
20+
21+
let name = unsafe { CStr::from_bytes_with_nul_unchecked("softbuffer\0".as_bytes()) };
22+
let fd = memfd_create(name, MemFdCreateFlag::MFD_CLOEXEC)
23+
.expect("Failed to create memfd to store buffer.");
24+
unsafe { File::from_raw_fd(fd) }
25+
}
26+
27+
#[cfg(not(any(target_os = "linux", target_os = "freebsd")))]
28+
fn create_memfile() -> File {
29+
use nix::{
30+
errno::Errno,
31+
fcntl::OFlag,
32+
sys::{
33+
mman::{shm_open, shm_unlink},
34+
stat::Mode,
35+
},
36+
};
37+
use std::iter;
38+
39+
for _ in 0..=4 {
40+
let mut name = String::from("softbuffer-");
41+
name.extend(iter::repeat_with(fastrand::alphanumeric).take(7));
42+
name.push('\0');
43+
44+
let name = unsafe { CStr::from_bytes_with_nul_unchecked(name.as_bytes()) };
45+
// `CLOEXEC` is implied with `shm_open`
46+
let fd = shm_open(
47+
name,
48+
OFlag::O_RDWR | OFlag::O_CREAT | OFlag::O_EXCL,
49+
Mode::S_IRWXU,
50+
);
51+
if fd != Err(Errno::EEXIST) {
52+
let fd = fd.expect("Failed to create POSIX shm to store buffer.");
53+
let _ = shm_unlink(name);
54+
return unsafe { File::from_raw_fd(fd) };
55+
}
56+
}
57+
58+
panic!("Failed to generate non-existant shm name")
59+
}
60+
1861
pub(super) struct WaylandBuffer {
1962
qh: QueueHandle<State>,
2063
tempfile: File,
@@ -28,10 +71,7 @@ pub(super) struct WaylandBuffer {
2871

2972
impl WaylandBuffer {
3073
pub fn new(shm: &wl_shm::WlShm, width: i32, height: i32, qh: &QueueHandle<State>) -> Self {
31-
let name = unsafe { CStr::from_bytes_with_nul_unchecked("softbuffer\0".as_bytes()) };
32-
let tempfile_fd = memfd_create(name, MemFdCreateFlag::MFD_CLOEXEC)
33-
.expect("Failed to create memfd to store buffer.");
34-
let tempfile = unsafe { File::from_raw_fd(tempfile_fd) };
74+
let tempfile = create_memfile();
3575
let pool_size = width * height * 4;
3676
let pool = shm.create_pool(tempfile.as_raw_fd(), pool_size, qh, ());
3777
let released = Arc::new(AtomicBool::new(true));

0 commit comments

Comments
 (0)