mirror of https://github.com/smol-rs/polling
Add build constants to reduce cfg expansion
This commit is contained in:
parent
d443196f64
commit
02daef8ac6
79
build.rs
79
build.rs
|
@ -1,3 +1,5 @@
|
|||
use std::env;
|
||||
|
||||
fn main() {
|
||||
let cfg = match autocfg::AutoCfg::new() {
|
||||
Ok(cfg) => cfg,
|
||||
|
@ -22,4 +24,81 @@ fn main() {
|
|||
if !cfg.probe_rustc_version(1, 53) {
|
||||
autocfg::emit("polling_no_unsupported_error_kind");
|
||||
}
|
||||
|
||||
// Probe for the target_os.
|
||||
let target_os = TargetOs::new();
|
||||
|
||||
// kqueue is supported for most BSD-derives OSes.
|
||||
let bsdlike = &[
|
||||
"macos",
|
||||
"ios",
|
||||
"tvos",
|
||||
"watchos",
|
||||
"freebsd",
|
||||
"netbsd",
|
||||
"dragonfly",
|
||||
"openbsd"
|
||||
];
|
||||
|
||||
// We fall back to poll() on these platforms.
|
||||
let poll_fallback = &[
|
||||
"vxworks",
|
||||
"fuchsia",
|
||||
"horizon"
|
||||
];
|
||||
|
||||
// If we're forced to use the polling fallback, use that and return.
|
||||
if has_cfg("polling_test_poll_backend") {
|
||||
return;
|
||||
}
|
||||
|
||||
// epoll is supported on Linux and Android
|
||||
if target_os.is("linux") || target_os.is("android") {
|
||||
autocfg::emit("polling_epoll");
|
||||
}
|
||||
|
||||
// Event ports are supported on illumos and Solaris
|
||||
else if target_os.is("illumos") || target_os.is("solaris") {
|
||||
autocfg::emit("polling_event_port");
|
||||
}
|
||||
|
||||
// kqueue is supported on most BSD-derives OSes.
|
||||
else if bsdlike.iter().any(|os| target_os.is(os)) {
|
||||
autocfg::emit("polling_kqueue");
|
||||
}
|
||||
|
||||
// If we're not on a supported platform, fall back to poll().
|
||||
else if poll_fallback.iter().any(|os| target_os.is(os)) || has_cfg("unix") {
|
||||
autocfg::emit("polling_poll");
|
||||
}
|
||||
|
||||
// Otherwise, check for windows.
|
||||
else if target_os.is("windows") {
|
||||
autocfg::emit("polling_iocp");
|
||||
}
|
||||
}
|
||||
|
||||
/// Tell whether the `cfg` directive is set.
|
||||
fn has_cfg(name: &str) -> bool {
|
||||
let env_check = format!("CARGO_FEATURE_{}", cfg_name(name));
|
||||
env::var_os(env_check).is_some()
|
||||
}
|
||||
|
||||
struct TargetOs(String);
|
||||
|
||||
impl TargetOs {
|
||||
fn new() -> Self {
|
||||
let target_os = env::var_os("CARGO_CFG_TARGET_OS")
|
||||
.map(|s| s.to_string_lossy().into_owned())
|
||||
.unwrap_or_else(|| "".to_string());
|
||||
Self(target_os)
|
||||
}
|
||||
|
||||
fn is(&self, os: &str) -> bool {
|
||||
self.0.split(',').any(|s| s == os)
|
||||
}
|
||||
}
|
||||
|
||||
fn cfg_name(name: &str) -> String {
|
||||
name.to_uppercase().replace('-', "_").replace('"', "")
|
||||
}
|
||||
|
|
44
src/lib.rs
44
src/lib.rs
|
@ -83,36 +83,19 @@ cfg_if! {
|
|||
if #[cfg(polling_test_poll_backend)] {
|
||||
mod poll;
|
||||
use poll as sys;
|
||||
} else if #[cfg(any(target_os = "linux", target_os = "android"))] {
|
||||
} else if #[cfg(polling_epoll)] {
|
||||
mod epoll;
|
||||
use epoll as sys;
|
||||
} else if #[cfg(any(
|
||||
target_os = "illumos",
|
||||
target_os = "solaris",
|
||||
))] {
|
||||
} else if #[cfg(polling_event_port)] {
|
||||
mod port;
|
||||
use port as sys;
|
||||
} else if #[cfg(any(
|
||||
target_os = "macos",
|
||||
target_os = "ios",
|
||||
target_os = "tvos",
|
||||
target_os = "watchos",
|
||||
target_os = "freebsd",
|
||||
target_os = "netbsd",
|
||||
target_os = "openbsd",
|
||||
target_os = "dragonfly",
|
||||
))] {
|
||||
} else if #[cfg(polling_kqueue)] {
|
||||
mod kqueue;
|
||||
use kqueue as sys;
|
||||
} else if #[cfg(any(
|
||||
target_os = "vxworks",
|
||||
target_os = "fuchsia",
|
||||
target_os = "horizon",
|
||||
unix,
|
||||
))] {
|
||||
} else if #[cfg(polling_poll)] {
|
||||
mod poll;
|
||||
use poll as sys;
|
||||
} else if #[cfg(target_os = "windows")] {
|
||||
} else if #[cfg(polling_iocp)] {
|
||||
mod wepoll;
|
||||
use wepoll as sys;
|
||||
} else {
|
||||
|
@ -542,18 +525,9 @@ impl Poller {
|
|||
|
||||
#[cfg(all(
|
||||
any(
|
||||
target_os = "linux",
|
||||
target_os = "android",
|
||||
target_os = "illumos",
|
||||
target_os = "solaris",
|
||||
target_os = "macos",
|
||||
target_os = "ios",
|
||||
target_os = "tvos",
|
||||
target_os = "watchos",
|
||||
target_os = "freebsd",
|
||||
target_os = "netbsd",
|
||||
target_os = "openbsd",
|
||||
target_os = "dragonfly",
|
||||
polling_epoll,
|
||||
polling_kqueue,
|
||||
polling_event_port
|
||||
),
|
||||
not(polling_test_poll_backend),
|
||||
))]
|
||||
|
@ -595,7 +569,7 @@ mod raw_fd_impl {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
#[cfg(polling_iocp)]
|
||||
#[cfg_attr(docsrs, doc(cfg(windows)))]
|
||||
mod raw_handle_impl {
|
||||
use crate::Poller;
|
||||
|
|
Loading…
Reference in New Issue