Skip to content

Commit

Permalink
Use cfg aliases throught the code base
Browse files Browse the repository at this point in the history
Co-authored-by: Mads Marquart <[email protected]>
  • Loading branch information
amrbashir and madsmtm authored Dec 25, 2022
1 parent 58ec458 commit 5e77d70
Show file tree
Hide file tree
Showing 27 changed files with 186 additions and 228 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ wayland-csd-adwaita-notitle = ["sctk-adwaita"]
android-native-activity = [ "android-activity/native-activity" ]
android-game-activity = [ "android-activity/game-activity" ]

[build-dependencies]
cfg_aliases = "0.1.1"

[dependencies]
instant = { version = "0.1", features = ["wasm-bindgen"] }
once_cell = "1.12"
Expand Down
21 changes: 21 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use cfg_aliases::cfg_aliases;

fn main() {
// The script doesn't depend on our code
println!("cargo:rerun-if-changed=build.rs");
// Setup cfg aliases
cfg_aliases! {
// Systems.
android_platform: { target_os = "android" },
wasm_platform: { target_arch = "wasm32" },
macos_platform: { target_os = "macos" },
ios_platform: { target_os = "ios" },
windows_platform: { target_os = "windows" },
apple: { any(target_os = "ios", target_os = "macos") },
free_unix: { all(unix, not(apple), not(android_platform)) },

// Native displays.
x11_platform: { all(feature = "x11", free_unix, not(wasm)) },
wayland_platform: { all(feature = "wayland", free_unix, not(wasm)) },
}
}
12 changes: 2 additions & 10 deletions examples/child_window.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
#[cfg(any(
all(target_os = "linux", feature = "x11"),
target_os = "macos",
target_os = "windows"
))]
#[cfg(any(x11_platform, macos_platform, windows_platform))]
fn main() {
use std::collections::HashMap;

Expand Down Expand Up @@ -78,11 +74,7 @@ fn main() {
})
}

#[cfg(not(any(
all(target_os = "linux", feature = "x11"),
target_os = "macos",
target_os = "windows"
)))]
#[cfg(not(any(x11_platform, macos_platform, windows_platform)))]
fn main() {
panic!("This example is supported only on x11, macOS, and Windows.");
}
4 changes: 2 additions & 2 deletions examples/custom_events.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(clippy::single_match)]

#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(wasm_platform))]
fn main() {
use simple_logger::SimpleLogger;
use winit::{
Expand Down Expand Up @@ -49,7 +49,7 @@ fn main() {
});
}

#[cfg(target_arch = "wasm32")]
#[cfg(wasm_platform)]
fn main() {
panic!("This example is not supported on web.");
}
4 changes: 2 additions & 2 deletions examples/multithreaded.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(clippy::single_match)]

#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(wasm_platform))]
fn main() {
use std::{collections::HashMap, sync::mpsc, thread, time::Duration};

Expand Down Expand Up @@ -193,7 +193,7 @@ fn main() {
})
}

#[cfg(target_arch = "wasm32")]
#[cfg(wasm_platform)]
fn main() {
panic!("Example not supported on Wasm");
}
4 changes: 2 additions & 2 deletions examples/request_redraw_threaded.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(clippy::single_match)]

#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(wasm_platform))]
fn main() {
use std::{thread, time};

Expand Down Expand Up @@ -42,7 +42,7 @@ fn main() {
});
}

#[cfg(target_arch = "wasm32")]
#[cfg(wasm_platform)]
fn main() {
unimplemented!() // `Window` can't be sent between threads
}
6 changes: 3 additions & 3 deletions examples/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ pub fn main() {
.build(&event_loop)
.unwrap();

#[cfg(target_arch = "wasm32")]
#[cfg(wasm_platform)]
let log_list = wasm::insert_canvas_and_create_log_list(&window);

event_loop.run(move |event, _, control_flow| {
control_flow.set_wait();

#[cfg(target_arch = "wasm32")]
#[cfg(wasm_platform)]
wasm::log_event(&log_list, &event);

match event {
Expand All @@ -36,7 +36,7 @@ pub fn main() {
});
}

#[cfg(target_arch = "wasm32")]
#[cfg(wasm_platform)]
mod wasm {
use wasm_bindgen::prelude::*;
use winit::{event::Event, window::Window};
Expand Down
2 changes: 1 addition & 1 deletion examples/web_aspect_ratio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub fn main() {
println!("This example must be run with cargo run-wasm --example web_aspect_ratio")
}

#[cfg(target_arch = "wasm32")]
#[cfg(wasm_platform)]
mod wasm {
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
Expand Down
15 changes: 6 additions & 9 deletions examples/window_run_return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

// Limit this example to only compatible platforms.
#[cfg(any(
target_os = "windows",
target_os = "macos",
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
target_os = "android",
windows_platform,
macos_platform,
x11_platform,
wayland_platform,
android_platform
))]
fn main() {
use std::{thread::sleep, time::Duration};
Expand Down Expand Up @@ -60,7 +57,7 @@ fn main() {
}
}

#[cfg(any(target_os = "ios", target_arch = "wasm32"))]
#[cfg(any(ios_platform, wasm_platform))]
fn main() {
println!("This platform doesn't support run_return.");
}
13 changes: 3 additions & 10 deletions src/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ impl<T> EventLoopBuilder<T> {
///
/// [`platform`]: crate::platform
#[cfg_attr(
target_os = "android",
android,
doc = "[`.with_android_app(app)`]: crate::platform::android::EventLoopBuilderExtAndroid::with_android_app"
)]
#[cfg_attr(
not(target_os = "android"),
not(android),
doc = "[`.with_android_app(app)`]: #only-available-on-android"
)]
#[inline]
Expand Down Expand Up @@ -339,14 +339,7 @@ impl<T> EventLoopWindowTarget<T> {
///
/// [`DeviceEvent`]: crate::event::DeviceEvent
pub fn set_device_event_filter(&self, _filter: DeviceEventFilter) {
#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
target_os = "windows"
))]
#[cfg(any(x11_platform, wayland_platform, windows))]
self.p.set_device_event_filter(_filter);
}
}
Expand Down
45 changes: 12 additions & 33 deletions src/platform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,26 @@
//!
//! However only the module corresponding to the platform you're compiling to will be available.
#[cfg(target_os = "android")]
#[cfg(android_platform)]
pub mod android;
#[cfg(target_os = "ios")]
#[cfg(ios_platform)]
pub mod ios;
#[cfg(target_os = "macos")]
#[cfg(macos_platform)]
pub mod macos;
#[cfg(all(
feature = "wayland",
any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
)
))]
#[cfg(wayland_platform)]
pub mod wayland;
#[cfg(target_arch = "wasm32")]
#[cfg(wasm_platform)]
pub mod web;
#[cfg(target_os = "windows")]
#[cfg(windows_platform)]
pub mod windows;
#[cfg(all(
feature = "x11",
any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd",
)
))]
#[cfg(x11_platform)]
pub mod x11;

#[cfg(any(
target_os = "windows",
target_os = "macos",
target_os = "android",
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
windows_platform,
macos_platform,
android_platform,
x11_platform,
wayland_platform
))]
pub mod run_return;
6 changes: 3 additions & 3 deletions src/platform/wayland.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl<T> EventLoopWindowTargetExtWayland for EventLoopWindowTarget<T> {
LinuxEventLoopWindowTarget::Wayland(ref p) => {
Some(p.display().get_display_ptr() as *mut _)
}
#[cfg(feature = "x11")]
#[cfg(x11_platform)]
_ => None,
}
}
Expand Down Expand Up @@ -95,7 +95,7 @@ impl WindowExtWayland for Window {
fn wayland_surface(&self) -> Option<*mut raw::c_void> {
match self.window {
LinuxWindow::Wayland(ref w) => Some(w.surface().as_ref().c_ptr() as *mut _),
#[cfg(feature = "x11")]
#[cfg(x11_platform)]
_ => None,
}
}
Expand All @@ -104,7 +104,7 @@ impl WindowExtWayland for Window {
fn wayland_display(&self) -> Option<*mut raw::c_void> {
match self.window {
LinuxWindow::Wayland(ref w) => Some(w.display().get_display_ptr() as *mut _),
#[cfg(feature = "x11")]
#[cfg(x11_platform)]
_ => None,
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/platform/x11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl<T> EventLoopWindowTargetExtX11 for EventLoopWindowTarget<T> {
fn xlib_xconnection(&self) -> Option<Arc<XConnection>> {
match self.p {
LinuxEventLoopWindowTarget::X(ref e) => Some(e.x_connection().clone()),
#[cfg(feature = "wayland")]
#[cfg(wayland_platform)]
_ => None,
}
}
Expand Down Expand Up @@ -124,7 +124,7 @@ impl WindowExtX11 for Window {
fn xlib_window(&self) -> Option<raw::c_ulong> {
match self.window {
LinuxWindow::X(ref w) => Some(w.xlib_window()),
#[cfg(feature = "wayland")]
#[cfg(wayland_platform)]
_ => None,
}
}
Expand All @@ -133,7 +133,7 @@ impl WindowExtX11 for Window {
fn xlib_display(&self) -> Option<*mut raw::c_void> {
match self.window {
LinuxWindow::X(ref w) => Some(w.xlib_display()),
#[cfg(feature = "wayland")]
#[cfg(wayland_platform)]
_ => None,
}
}
Expand All @@ -142,7 +142,7 @@ impl WindowExtX11 for Window {
fn xlib_screen_id(&self) -> Option<raw::c_int> {
match self.window {
LinuxWindow::X(ref w) => Some(w.xlib_screen_id()),
#[cfg(feature = "wayland")]
#[cfg(wayland_platform)]
_ => None,
}
}
Expand All @@ -151,7 +151,7 @@ impl WindowExtX11 for Window {
fn xlib_xconnection(&self) -> Option<Arc<XConnection>> {
match self.window {
LinuxWindow::X(ref w) => Some(w.xlib_xconnection()),
#[cfg(feature = "wayland")]
#[cfg(wayland_platform)]
_ => None,
}
}
Expand All @@ -160,7 +160,7 @@ impl WindowExtX11 for Window {
fn xcb_connection(&self) -> Option<*mut raw::c_void> {
match self.window {
LinuxWindow::X(ref w) => Some(w.xcb_connection()),
#[cfg(feature = "wayland")]
#[cfg(wayland_platform)]
_ => None,
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/platform_impl/android/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg(target_os = "android")]
#![cfg(android_platform)]

use std::{
collections::VecDeque,
Expand Down
2 changes: 1 addition & 1 deletion src/platform_impl/ios/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
//!
//! Also note that app may not receive the LoopDestroyed event if suspended; it might be SIGKILL'ed.
#![cfg(target_os = "ios")]
#![cfg(ios_platform)]
#![allow(clippy::let_unit_value)]

// TODO: (mtak-) UIKit requires main thread for virtually all function/method calls. This could be
Expand Down
Loading

0 comments on commit 5e77d70

Please sign in to comment.