Skip to content

Commit

Permalink
wasm: use build script to detect wasm (#4865)
Browse files Browse the repository at this point in the history
Co-authored-by: Taiki Endo <te316e89@gmail.com>
  • Loading branch information
Darksonn and taiki-e authored Jul 26, 2022
1 parent 0dc62da commit b2ada60
Show file tree
Hide file tree
Showing 91 changed files with 222 additions and 195 deletions.
8 changes: 4 additions & 4 deletions tokio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ mio = { version = "0.8.4", optional = true }
num_cpus = { version = "1.8.0", optional = true }
parking_lot = { version = "0.12.0", optional = true }

[target.'cfg(not(target_family = "wasm"))'.dependencies]
[target.'cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))'.dependencies]
socket2 = { version = "0.4.4", features = [ "all" ] }

# Currently unstable. The API exposed by these features may be broken at any time.
Expand Down Expand Up @@ -150,14 +150,14 @@ mockall = "0.11.1"
tempfile = "3.1.0"
async-stream = "0.3"

[target.'cfg(not(target_family = "wasm"))'.dev-dependencies]
[target.'cfg(not(any(target_arch = "wasm32", target_arch = "wasm64")))'.dev-dependencies]
proptest = "1"
socket2 = "0.4"

[target.'cfg(not(all(target_family = "wasm", target_os = "unknown")))'.dev-dependencies]
[target.'cfg(not(all(any(target_arch = "wasm32", target_arch = "wasm64"), target_os = "unknown")))'.dev-dependencies]
rand = "0.8.0"

[target.'cfg(all(target_family = "wasm", not(target_os = "wasi")))'.dev-dependencies]
[target.'cfg(all(any(target_arch = "wasm32", target_arch = "wasm64"), not(target_os = "wasi")))'.dev-dependencies]
wasm-bindgen-test = "0.3.0"

[target.'cfg(target_os = "freebsd")'.dev-dependencies]
Expand Down
13 changes: 13 additions & 0 deletions tokio/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,17 @@ fn main() {
// RUSTFLAGS="--cfg tokio_no_addr_of"
autocfg::emit("tokio_no_addr_of")
}

let target = ::std::env::var("TARGET").unwrap_or_default();

// We emit cfgs instead of using `target_family = "wasm"` that requires Rust 1.54.
// Note that these cfgs are unavailable in `Cargo.toml`.
if target.starts_with("wasm") {
autocfg::emit("tokio_wasm");
if target.contains("wasi") {
autocfg::emit("tokio_wasi");
} else {
autocfg::emit("tokio_wasm_not_wasi");
}
}
}
2 changes: 1 addition & 1 deletion tokio/src/coop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ cfg_coop! {
mod test {
use super::*;

#[cfg(all(target_family = "wasm", not(target_os = "wasi")))]
#[cfg(tokio_wasm_not_wasi)]
use wasm_bindgen_test::wasm_bindgen_test as test;

fn get() -> Budget {
Expand Down
10 changes: 5 additions & 5 deletions tokio/src/io/driver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub(super) struct Inner {

/// Used to wake up the reactor from a call to `turn`.
/// Not supported on Wasi due to lack of threading support.
#[cfg(not(target_os = "wasi"))]
#[cfg(not(tokio_wasi))]
waker: mio::Waker,

metrics: IoDriverMetrics,
Expand Down Expand Up @@ -117,7 +117,7 @@ impl Driver {
/// creation.
pub(crate) fn new() -> io::Result<Driver> {
let poll = mio::Poll::new()?;
#[cfg(not(target_os = "wasi"))]
#[cfg(not(tokio_wasi))]
let waker = mio::Waker::new(poll.registry(), TOKEN_WAKEUP)?;
let registry = poll.registry().try_clone()?;

Expand All @@ -132,7 +132,7 @@ impl Driver {
inner: Arc::new(Inner {
registry,
io_dispatch: RwLock::new(IoDispatcher::new(allocator)),
#[cfg(not(target_os = "wasi"))]
#[cfg(not(tokio_wasi))]
waker,
metrics: IoDriverMetrics::default(),
}),
Expand Down Expand Up @@ -168,7 +168,7 @@ impl Driver {
match self.poll.poll(&mut events, max_wait) {
Ok(_) => {}
Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}
#[cfg(target_os = "wasi")]
#[cfg(tokio_wasi)]
Err(e) if e.kind() == io::ErrorKind::InvalidInput => {
// In case of wasm32_wasi this error happens, when trying to poll without subscriptions
// just return from the park, as there would be nothing, which wakes us up.
Expand Down Expand Up @@ -310,7 +310,7 @@ impl Handle {
/// blocked in `turn`, then the next call to `turn` will not block and
/// return immediately.
fn wakeup(&self) {
#[cfg(not(target_os = "wasi"))]
#[cfg(not(tokio_wasi))]
self.inner.waker.wake().expect("failed to wake I/O driver");
}
}
Expand Down
2 changes: 1 addition & 1 deletion tokio/src/io/driver/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl Registration {

// Uses the poll path, requiring the caller to ensure mutual exclusion for
// correctness. Only the last task to call this function is notified.
#[cfg(not(all(target_family = "wasm", target_os = "wasi")))]
#[cfg(not(tokio_wasi))]
pub(crate) fn poll_read_io<R>(
&self,
cx: &mut Context<'_>,
Expand Down
4 changes: 2 additions & 2 deletions tokio/src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,11 @@ cfg_io_driver_impl! {
pub use driver::{Interest, Ready};
}

#[cfg_attr(target_os = "wasi", allow(unused_imports))]
#[cfg_attr(tokio_wasi, allow(unused_imports))]
mod poll_evented;

#[cfg(not(loom))]
#[cfg_attr(target_os = "wasi", allow(unused_imports))]
#[cfg_attr(tokio_wasi, allow(unused_imports))]
pub(crate) use poll_evented::PollEvented;
}

Expand Down
19 changes: 18 additions & 1 deletion tokio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,9 +393,26 @@ compile_error! {
"Tokio requires the platform pointer width to be 32, 64, or 128 bits"
}

// Ensure that our build script has correctly set cfg flags for wasm.
//
// Each condition is written all(a, not(b)). This should be read as
// "if a, then we must also have b".
#[cfg(any(
all(target_arch = "wasm32", not(tokio_wasm)),
all(target_arch = "wasm64", not(tokio_wasm)),
all(target_family = "wasm", not(tokio_wasm)),
all(target_os = "wasi", not(tokio_wasm)),
all(target_os = "wasi", not(tokio_wasi)),
all(target_os = "wasi", tokio_wasm_not_wasi),
all(tokio_wasm, not(any(target_arch = "wasm32", target_arch = "wasm64"))),
all(tokio_wasm_not_wasi, not(tokio_wasm)),
all(tokio_wasi, not(tokio_wasm))
))]
compile_error!("Tokio's build script has incorrectly detected wasm.");

#[cfg(all(
not(tokio_unstable),
target_family = "wasm",
tokio_wasm,
any(
feature = "fs",
feature = "io-std",
Expand Down
16 changes: 8 additions & 8 deletions tokio/src/macros/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ macro_rules! cfg_fs {
($($item:item)*) => {
$(
#[cfg(feature = "fs")]
#[cfg(not(target_os = "wasi"))]
#[cfg(not(tokio_wasi))]
#[cfg_attr(docsrs, doc(cfg(feature = "fs")))]
$item
)*
Expand Down Expand Up @@ -252,7 +252,7 @@ macro_rules! cfg_process {
#[cfg(feature = "process")]
#[cfg_attr(docsrs, doc(cfg(feature = "process")))]
#[cfg(not(loom))]
#[cfg(not(target_os = "wasi"))]
#[cfg(not(tokio_wasi))]
$item
)*
}
Expand Down Expand Up @@ -281,7 +281,7 @@ macro_rules! cfg_signal {
#[cfg(feature = "signal")]
#[cfg_attr(docsrs, doc(cfg(feature = "signal")))]
#[cfg(not(loom))]
#[cfg(not(target_os = "wasi"))]
#[cfg(not(tokio_wasi))]
$item
)*
}
Expand Down Expand Up @@ -341,7 +341,7 @@ macro_rules! cfg_not_rt {
macro_rules! cfg_rt_multi_thread {
($($item:item)*) => {
$(
#[cfg(all(feature = "rt-multi-thread", not(target_os = "wasi")))]
#[cfg(all(feature = "rt-multi-thread", not(tokio_wasi)))]
#[cfg_attr(docsrs, doc(cfg(feature = "rt-multi-thread")))]
$item
)*
Expand Down Expand Up @@ -459,7 +459,7 @@ macro_rules! cfg_has_atomic_u64 {
target_arch = "mips",
target_arch = "powerpc",
target_arch = "riscv32",
target_family = "wasm"
tokio_wasm
)))]
$item
)*
Expand All @@ -474,7 +474,7 @@ macro_rules! cfg_not_has_atomic_u64 {
target_arch = "mips",
target_arch = "powerpc",
target_arch = "riscv32",
target_family = "wasm"
tokio_wasm
))]
$item
)*
Expand All @@ -484,7 +484,7 @@ macro_rules! cfg_not_has_atomic_u64 {
macro_rules! cfg_not_wasi {
($($item:item)*) => {
$(
#[cfg(not(target_os = "wasi"))]
#[cfg(not(tokio_wasi))]
$item
)*
}
Expand All @@ -493,7 +493,7 @@ macro_rules! cfg_not_wasi {
macro_rules! cfg_is_wasm_not_wasi {
($($item:item)*) => {
$(
#[cfg(all(target_family = "wasm", not(target_os = "wasi")))]
#[cfg(tokio_wasm_not_wasi)]
$item
)*
}
Expand Down
4 changes: 2 additions & 2 deletions tokio/src/net/tcp/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ impl TcpListener {
.map(|raw_socket| unsafe { std::net::TcpListener::from_raw_socket(raw_socket) })
}

#[cfg(target_os = "wasi")]
#[cfg(tokio_wasi)]
{
use std::os::wasi::io::{FromRawFd, IntoRawFd};
self.io
Expand Down Expand Up @@ -403,7 +403,7 @@ mod sys {
}

cfg_unstable! {
#[cfg(target_os = "wasi")]
#[cfg(tokio_wasi)]
mod sys {
use super::TcpListener;
use std::os::wasi::prelude::*;
Expand Down
4 changes: 2 additions & 2 deletions tokio/src/net/tcp/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ impl TcpStream {
.map(|raw_socket| unsafe { std::net::TcpStream::from_raw_socket(raw_socket) })
}

#[cfg(target_os = "wasi")]
#[cfg(tokio_wasi)]
{
use std::os::wasi::io::{FromRawFd, IntoRawFd};
self.io
Expand Down Expand Up @@ -1334,7 +1334,7 @@ mod sys {
}
}

#[cfg(all(tokio_unstable, target_os = "wasi"))]
#[cfg(all(tokio_unstable, tokio_wasi))]
mod sys {
use super::TcpStream;
use std::os::wasi::prelude::*;
Expand Down
4 changes: 2 additions & 2 deletions tokio/src/park/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ impl Park for ParkThread {

fn park_timeout(&mut self, duration: Duration) -> Result<(), Self::Error> {
// Wasi doesn't have threads, so just sleep.
#[cfg(not(target_os = "wasi"))]
#[cfg(not(tokio_wasi))]
self.inner.park_timeout(duration);
#[cfg(target_os = "wasi")]
#[cfg(tokio_wasi)]
std::thread::sleep(duration);
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion tokio/src/runtime/blocking/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ const KEEP_ALIVE: Duration = Duration::from_secs(10);
/// Tasks will be scheduled as non-mandatory, meaning they may not get executed
/// in case of runtime shutdown.
#[track_caller]
#[cfg_attr(target_os = "wasi", allow(dead_code))]
#[cfg_attr(tokio_wasi, allow(dead_code))]
pub(crate) fn spawn_blocking<F, R>(func: F) -> JoinHandle<R>
where
F: FnOnce() -> R + Send + 'static,
Expand Down
6 changes: 3 additions & 3 deletions tokio/src/runtime/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ pub(crate) type ThreadNameFn = std::sync::Arc<dyn Fn() -> String + Send + Sync +

pub(crate) enum Kind {
CurrentThread,
#[cfg(all(feature = "rt-multi-thread", not(target_os = "wasi")))]
#[cfg(all(feature = "rt-multi-thread", not(tokio_wasi)))]
MultiThread,
}

Expand Down Expand Up @@ -619,7 +619,7 @@ impl Builder {
pub fn build(&mut self) -> io::Result<Runtime> {
match &self.kind {
Kind::CurrentThread => self.build_basic_runtime(),
#[cfg(all(feature = "rt-multi-thread", not(target_os = "wasi")))]
#[cfg(all(feature = "rt-multi-thread", not(tokio_wasi)))]
Kind::MultiThread => self.build_threaded_runtime(),
}
}
Expand All @@ -628,7 +628,7 @@ impl Builder {
driver::Cfg {
enable_pause_time: match self.kind {
Kind::CurrentThread => true,
#[cfg(all(feature = "rt-multi-thread", not(target_os = "wasi")))]
#[cfg(all(feature = "rt-multi-thread", not(tokio_wasi)))]
Kind::MultiThread => false,
},
enable_io: self.enable_io,
Expand Down
10 changes: 5 additions & 5 deletions tokio/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@
// At the top due to macros
#[cfg(test)]
#[cfg(not(target_family = "wasm"))]
#[cfg(not(tokio_wasm))]
#[macro_use]
mod tests;

Expand Down Expand Up @@ -204,7 +204,7 @@ cfg_rt! {

mod blocking;
use blocking::BlockingPool;
#[cfg_attr(target_os = "wasi", allow(unused_imports))]
#[cfg_attr(tokio_wasi, allow(unused_imports))]
pub(crate) use blocking::spawn_blocking;

cfg_trace! {
Expand Down Expand Up @@ -304,7 +304,7 @@ cfg_rt! {
CurrentThread(BasicScheduler),

/// Execute tasks across multiple threads.
#[cfg(all(feature = "rt-multi-thread", not(target_os = "wasi")))]
#[cfg(all(feature = "rt-multi-thread", not(tokio_wasi)))]
ThreadPool(ThreadPool),
}

Expand Down Expand Up @@ -484,7 +484,7 @@ cfg_rt! {

match &self.kind {
Kind::CurrentThread(exec) => exec.block_on(future),
#[cfg(all(feature = "rt-multi-thread", not(target_os = "wasi")))]
#[cfg(all(feature = "rt-multi-thread", not(tokio_wasi)))]
Kind::ThreadPool(exec) => exec.block_on(future),
}
}
Expand Down Expand Up @@ -614,7 +614,7 @@ cfg_rt! {
},
}
},
#[cfg(all(feature = "rt-multi-thread", not(target_os = "wasi")))]
#[cfg(all(feature = "rt-multi-thread", not(tokio_wasi)))]
Kind::ThreadPool(_) => {
// The threaded scheduler drops its tasks on its worker threads, which is
// already in the runtime's context.
Expand Down
Loading

0 comments on commit b2ada60

Please sign in to comment.