Skip to content

Commit e1bf9e4

Browse files
joshtriplettalexcrichton
authored andcommitted
Add a wasi-ephemeral crate to build with ephemeral WASI interfaces (WebAssembly#31)
* Add a wasi-ephemeral crate to build with ephemeral WASI interfaces Developers working on WASI interfaces can use the wasi-ephemeral crate as a path dependency or git dependency, to build and run with the ephemeral WASI interface rather than the latest snapshot. (wasi-ephemeral uses publish=false and will not be uploaded to crates.io.) wasi-ephemeral builds with a build.rs script, so that it automatically rebuilds on changes to the witx file. wasi-ephemeral also supports building with an arbitrary witx file, using the WASI_EPHEMERAL_WITX environment variable. * Build-test wasi-ephemeral in CI
1 parent 8fd6f68 commit e1bf9e4

File tree

7 files changed

+95
-1
lines changed

7 files changed

+95
-1
lines changed

.github/workflows/main.yml

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ jobs:
1919
- run: cargo build --no-default-features
2020
- run: cargo build --target wasm32-wasi
2121
- run: cargo build --target wasm32-wasi --no-default-features
22+
- run: cargo build -p wasi-ephemeral
2223

2324
rustfmt:
2425
name: Rustfmt

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ readme = "README.md"
1212
documentation = "https://docs.rs/wasi"
1313

1414
[workspace]
15-
members = ['crates/generate-raw']
15+
members = ['crates/generate-raw', 'crates/wasi-ephemeral']
1616

1717
[dependencies]
1818
# When built as part of libstd

crates/wasi-ephemeral/Cargo.toml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "wasi-ephemeral"
3+
version = "0.0.0"
4+
publish = false
5+
authors = ["Bytecode Alliance"]
6+
license = "Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT"
7+
description = "Experimental WASI API bindings for Rust"
8+
edition = "2018"
9+
categories = ["no-std", "wasm"]
10+
keywords = ["webassembly", "wasm"]
11+
repository = "https://github.com/bytecodealliance/wasi"
12+
13+
[build-dependencies]
14+
generate-raw = { path = "../generate-raw" }
15+
16+
[features]
17+
default = ["std"]
18+
std = []
19+
20+
[badges]
21+
maintenance = { status = "experimental" }

crates/wasi-ephemeral/README.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
This crate contains API bindings for the [ephemeral
2+
phase](https://github.com/WebAssembly/WASI/blob/master/phases/README.md) of
3+
[WASI](https://github.com/WebAssembly/WASI). The ephemeral phase exists for
4+
experimentation prior to snapshotting.
5+
6+
This crate will never be released, and should only be used by path dependency
7+
or git dependency. By default, this crate provides bindings for the ephemeral
8+
phase from the included git submodule of the [WASI
9+
repository](https://github.com/WebAssembly/WASI), but you can set the
10+
`WASI_EPHEMERAL_WITX` environment variable to the full path to a witx file to
11+
build bindings for that witx file instead.
12+
13+
This crate makes it easier to test bindings to arbitrary witx interfaces. Note
14+
that WebAssembly runtimes typically do not implement the ephemeral phase of
15+
WASI, so a WebAssembly program built against the default wasi-ephemeral may not
16+
load. Instead, for development, you may want to build with
17+
`WASI_EPHEMERAL_WITX` pointing to an edited version of the latest snapshot, and
18+
a correspondingly modified version of a WebAssembly runtime.

crates/wasi-ephemeral/build.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use std::{env, fs::File, io::Write, path::PathBuf};
2+
3+
fn main() {
4+
let out_path: PathBuf = env::var_os("OUT_DIR").unwrap().into();
5+
let mut f = File::create(out_path.join("lib_generated.rs")).unwrap();
6+
const WASI_EPHEMERAL_WITX: &str =
7+
"../generate-raw/WASI/phases/ephemeral/witx/wasi_ephemeral_preview.witx";
8+
let witx_path: PathBuf = env::var_os("WASI_EPHEMERAL_WITX")
9+
.unwrap_or_else(|| WASI_EPHEMERAL_WITX.into())
10+
.into();
11+
let out = generate_raw::generate(&witx_path);
12+
write!(f, "{}", out).unwrap();
13+
println!("cargo:rerun-if-env-changed=WASI_EPHEMERAL_WITX");
14+
println!("cargo:rerun-if-changed={}", witx_path.display());
15+
println!(
16+
"cargo:rerun-if-changed={}",
17+
witx_path.with_file_name("typenames.witx").display(),
18+
);
19+
// TODO: Account for changes in use directives.
20+
}

crates/wasi-ephemeral/src/error.rs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../src/error.rs

crates/wasi-ephemeral/src/lib.rs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//! Raw API bindings to the WebAssembly System Interface (WASI)
2+
//!
3+
//! This crate provides Rust API bindings to WASI APIs. All WASI APIs are
4+
//! exported from this crate and provided with the appropriate type signatures.
5+
//! This crate is entirely procedurally generated from the `*.witx` files that
6+
//! describe the WASI API.
7+
//!
8+
//! # WASI API Version
9+
//!
10+
//! The WASI API is evolving over time. It is both gaining new features as well
11+
//! as tweaking the ABI of existing features. As a result it's important to
12+
//! understand what version of this crate you're using and how it relates to
13+
//! the WASI version of the spec.
14+
//!
15+
//! The WASI specification is organized into phases where there is a snapshot
16+
//! at any one point in time describing the current state of the specification.
17+
//! This crate implements the latest "ephemeral" version, not yet snapshotted.
18+
//!
19+
//! This crate will never be released, and should only be used by path
20+
//! dependency or git dependency.
21+
//!
22+
//! # Crate Features
23+
//!
24+
//! This crate supports one feature, `std`, which implements the standard
25+
//! `Error` trait for the exported [`Error`] type in this crate. This is
26+
//! enabled by default but can be disabled to make the library `no_std`
27+
//! compatible.
28+
29+
#![no_std]
30+
31+
mod error;
32+
33+
include!(concat!(env!("OUT_DIR"), "/lib_generated.rs"));

0 commit comments

Comments
 (0)