Skip to content

Commit

Permalink
Improve Platform and Feature Detection
Browse files Browse the repository at this point in the history
- Add cfg aliases to simplify feature checks and make sure that
  platform specific features will not break when enabled on
  unsupported platforms.
- Disable X11 on MacOS. It appears it was meant to be supported, but
  never actually was. Disabling it so that MacOS builds can run with
  the X11 feature ( when desired for Linux ) without breaking.
- Make the platform::unix module Linux specific ( for now? ) as Android
  and MacOS have their own modules and all unix backends aren't
  supported for Android or MacOS yet.
  • Loading branch information
zicklag committed Apr 10, 2020
1 parent f661a3c commit e951740
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 22 deletions.
5 changes: 1 addition & 4 deletions surfman/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ build = "build.rs"

[build-dependencies]
gl_generator = "0.13"
cfg_aliases = "0.1.0"

[features]
default = ["sm-winit"]
Expand Down Expand Up @@ -73,11 +74,7 @@ features = ["client", "dlopen", "egl"]
[target.'cfg(all(unix, not(any(target_os = "macos", target_os = "android"))))'.dependencies.x11]
version = "2.3.0"
features = ["xlib"]

[target.'cfg(any(not(unix), target_os = "macos", target_os = "android"))'.dependencies.x11]
optional = true
version = "2.3.0"
features = ["xlib"]

# Ensure that we have a static libEGL.lib present for linking with EGL bindings.
[target.'cfg(target_os = "windows")'.dependencies.mozangle]
Expand Down
21 changes: 21 additions & 0 deletions surfman/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,32 @@
//! The `surfman` build script.
use gl_generator::{Api, Fallbacks, Profile, Registry, StructGenerator};
use cfg_aliases::cfg_aliases;
use std::env;
use std::fs::File;
use std::path::PathBuf;

fn main() {
// Setup aliases for #[cfg] checks
cfg_aliases! {
// Platforms
windows: { target_os = "windows" },
macos: { target_os = "macos" },
android: { target_os = "android" },
// TODO: is `target_os = "linux"` the same as the following check?
linux: { all(unix, not(any(macos, android))) },

// Features:
// Here we collect the features that are only valid on certain platforms and
// we add aliases that include checks for the correct platform.
angle: { all(windows, feature = "sm-angle") },
angle_builtin: { all(windows, feature = "sm-angle-builtin") },
angle_default: { all(windows, feature = "sm-angle-default") },
no_wgl: { all(windows, feature = "sm-no-wgl") },
wayland_default: { all(linux, feature = "sm-wayland-default") },
x11: { all(linux, feature = "sm-x11") },
}

let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
let target_family = env::var("CARGO_CFG_TARGET_FAMILY").unwrap();
let dest = PathBuf::from(&env::var("OUT_DIR").unwrap());
Expand Down
4 changes: 1 addition & 3 deletions surfman/src/platform/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
//
//! Backends that are not specific to any operating system.
#[cfg(any(target_os = "android",
all(target_os = "windows", feature = "sm-angle"),
all(unix, not(target_os = "macos"))))]
#[cfg(any(android, angle, linux))]
pub(crate) mod egl;

pub mod multi;
20 changes: 10 additions & 10 deletions surfman/src/platform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@
pub mod generic;

#[cfg(target_os = "android")]
#[cfg(android)]
pub mod android;
#[cfg(target_os = "android")]
#[cfg(android)]
pub use android as default;

#[cfg(target_os = "macos")]
#[cfg(macos)]
pub mod macos;
#[cfg(all(target_os = "macos", not(feature = "sm-x11")))]
#[cfg(macos)]
pub use macos::cgl as default;
#[cfg(target_os = "macos")]
#[cfg(macos)]
pub use macos::system;

#[cfg(unix)]
#[cfg(linux)]
pub mod unix;
#[cfg(all(unix, not(target_os = "macos"), not(target_os = "android")))]
#[cfg(linux)]
pub use unix::default;

#[cfg(target_os = "windows")]
#[cfg(windows)]
pub mod windows;
#[cfg(all(target_os = "windows", feature = "sm-angle-default"))]
#[cfg(angle_default)]
pub use windows::angle as default;
#[cfg(all(target_os = "windows", not(feature = "sm-angle-default")))]
#[cfg(all(windows, not(angle_default)))]
pub use windows::wgl as default;
16 changes: 11 additions & 5 deletions surfman/src/platform/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@
//
//! Backends specific to Unix-like systems, particularly Linux.
#[cfg(all(unix, not(any(target_os = "macos", target_os = "android"))))]
// The default when x11 is enabled
#[cfg(x11)]
pub mod default;
#[cfg(all(unix, not(any(target_os = "macos", target_os = "android"))))]

// The default when x11 is not enabled
#[cfg(not(x11))]
pub use wayland as default;

#[cfg(linux)]
pub mod generic;
#[cfg(all(unix, not(any(target_os = "macos", target_os = "android"))))]

#[cfg(linux)]
pub mod wayland;
#[cfg(all(any(feature = "sm-x11",
all(unix, not(any(target_os = "macos", target_os = "android"))))))]
#[cfg(x11)]
pub mod x11;

0 comments on commit e951740

Please sign in to comment.