Use cfg_aliases crate to make Wayland/X #[cfg(..)] less redundant

This commit is contained in:
Ian Douglas Scott 2022-12-27 15:17:42 -08:00
parent 4f6542ceaa
commit a90c7bca04
3 changed files with 19 additions and 54 deletions

View file

@ -23,7 +23,7 @@ thiserror = "1.0.30"
raw-window-handle = "0.5.0" raw-window-handle = "0.5.0"
log = "0.4.17" log = "0.4.17"
[target.'cfg(any(target_os = "linux", target_os = "freebsd", target_os = "dragonfly", target_os = "netbsd", target_os = "openbsd"))'.dependencies] [target.'cfg(all(unix, not(any(target_vendor = "apple", target_os = "android", target_os = "redox"))))'.dependencies]
nix = { version = "0.26.1", optional = true } nix = { version = "0.26.1", optional = true }
wayland-backend = { version = "0.1.0", features = ["client_system"], optional = true } wayland-backend = { version = "0.1.0", features = ["client_system"], optional = true }
wayland-client = { version = "0.30.0", optional = true } wayland-client = { version = "0.30.0", optional = true }
@ -32,7 +32,7 @@ bytemuck = { version = "1.12.3", optional = true }
x11-dl = { version = "2.19.1", optional = true } x11-dl = { version = "2.19.1", optional = true }
x11rb = { version = "0.11.0", features = ["allow-unsafe-code", "dl-libxcb"], optional = true } x11rb = { version = "0.11.0", features = ["allow-unsafe-code", "dl-libxcb"], optional = true }
[target.'cfg(any(target_os = "dragonfly", target_os = "netbsd", target_os = "openbsd"))'.dependencies] [target.'cfg(all(unix, not(any(target_vendor = "apple", target_os = "android", target_os = "redox", target_os = "linux", target_os = "freebsd"))))'.dependencies]
fastrand = { version = "1.8.0", optional = true } fastrand = { version = "1.8.0", optional = true }
[target.'cfg(target_os = "windows")'.dependencies.windows-sys] [target.'cfg(target_os = "windows")'.dependencies.windows-sys]
@ -55,6 +55,9 @@ features = ["CanvasRenderingContext2d", "Document", "Element", "HtmlCanvasElemen
[target.'cfg(target_os = "redox")'.dependencies] [target.'cfg(target_os = "redox")'.dependencies]
redox_syscall = "0.3" redox_syscall = "0.3"
[build-dependencies]
cfg_aliases = "0.1.1"
[dev-dependencies] [dev-dependencies]
instant = "0.1.12" instant = "0.1.12"
winit = "0.27.2" winit = "0.27.2"

7
build.rs Normal file
View file

@ -0,0 +1,7 @@
fn main() {
cfg_aliases::cfg_aliases! {
free_unix: { all(unix, not(any(target_vendor = "apple", target_os = "android", target_os = "redox"))) },
x11_platform: { all(feature = "x11", free_unix, not(target_arch = "wasm32")) },
wayland_platform: { all(feature = "wayland", free_unix, not(target_arch = "wasm32")) },
}
}

View file

@ -10,31 +10,13 @@ extern crate core;
mod cg; mod cg;
#[cfg(target_os = "redox")] #[cfg(target_os = "redox")]
mod orbital; mod orbital;
#[cfg(all( #[cfg(wayland_platform)]
feature = "wayland",
any(
target_os = "linux",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "netbsd",
target_os = "openbsd"
)
))]
mod wayland; mod wayland;
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
mod web; mod web;
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
mod win32; mod win32;
#[cfg(all( #[cfg(x11_platform)]
feature = "x11",
any(
target_os = "linux",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "netbsd",
target_os = "openbsd"
)
))]
mod x11; mod x11;
mod error; mod error;
@ -84,9 +66,9 @@ macro_rules! make_dispatch {
} }
make_dispatch! { make_dispatch! {
#[cfg(all(feature = "x11", any(target_os = "linux", target_os = "freebsd", target_os = "dragonfly", target_os = "netbsd", target_os = "openbsd")))] #[cfg(x11_platform)]
X11(x11::X11Impl), X11(x11::X11Impl),
#[cfg(all(feature = "wayland", any(target_os = "linux", target_os = "freebsd", target_os = "dragonfly", target_os = "netbsd", target_os = "openbsd")))] #[cfg(wayland_platform)]
Wayland(wayland::WaylandImpl), Wayland(wayland::WaylandImpl),
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
Win32(win32::Win32Impl), Win32(win32::Win32Impl),
@ -123,48 +105,21 @@ impl GraphicsContext {
raw_display_handle: RawDisplayHandle, raw_display_handle: RawDisplayHandle,
) -> Result<Self, SoftBufferError> { ) -> Result<Self, SoftBufferError> {
let imple: Dispatch = match (raw_window_handle, raw_display_handle) { let imple: Dispatch = match (raw_window_handle, raw_display_handle) {
#[cfg(all( #[cfg(x11_platform)]
feature = "x11",
any(
target_os = "linux",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "netbsd",
target_os = "openbsd"
)
))]
( (
RawWindowHandle::Xlib(xlib_window_handle), RawWindowHandle::Xlib(xlib_window_handle),
RawDisplayHandle::Xlib(xlib_display_handle), RawDisplayHandle::Xlib(xlib_display_handle),
) => Dispatch::X11(unsafe { ) => Dispatch::X11(unsafe {
x11::X11Impl::from_xlib(xlib_window_handle, xlib_display_handle)? x11::X11Impl::from_xlib(xlib_window_handle, xlib_display_handle)?
}), }),
#[cfg(all( #[cfg(x11_platform)]
feature = "x11",
any(
target_os = "linux",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "netbsd",
target_os = "openbsd"
)
))]
( (
RawWindowHandle::Xcb(xcb_window_handle), RawWindowHandle::Xcb(xcb_window_handle),
RawDisplayHandle::Xcb(xcb_display_handle), RawDisplayHandle::Xcb(xcb_display_handle),
) => Dispatch::X11(unsafe { ) => Dispatch::X11(unsafe {
x11::X11Impl::from_xcb(xcb_window_handle, xcb_display_handle)? x11::X11Impl::from_xcb(xcb_window_handle, xcb_display_handle)?
}), }),
#[cfg(all( #[cfg(wayland_platform)]
feature = "wayland",
any(
target_os = "linux",
target_os = "freebsd",
target_os = "dragonfly",
target_os = "netbsd",
target_os = "openbsd"
)
))]
( (
RawWindowHandle::Wayland(wayland_window_handle), RawWindowHandle::Wayland(wayland_window_handle),
RawDisplayHandle::Wayland(wayland_display_handle), RawDisplayHandle::Wayland(wayland_display_handle),