Skip to content
This repository was archived by the owner on Oct 13, 2023. It is now read-only.

Commit 345edcf

Browse files
authored
Update a few aspects of the adapter build (#17)
* Update a few aspects of the adapter build * Use the `wasm32-unknown-unknown` target for the adapter and specify flags in `.cargo/config.toml` to avoid having to pass the same flags everywhere. This allows using `wasm32-wasi` for tests to ensure the flags only apply to the adapter. * Use `opt-level=s` since speed is not of the utmost concern for this wasm but since it's likely to be included in many places size is likely more important. * Use `strip = 'debuginfo'` for the release build to remove the standard library's debugging information which isn't necessary. * Remove `debug = 0` from the `dev` profile to have debugging information for development. * Add a small `README.md` describing what's here for now. * Move `command` support behind a `command` feature This commit adds a `command` feature to main crate to avoid importing the `_start` function when the `command` feature is disabled, making this adapter useful for non-command WASI programs as well. For now this still emits the `command` export in the final component but with `use` in `*.wit` files it should be easier to avoid that export.
1 parent 6f0b2e8 commit 345edcf

File tree

6 files changed

+115
-28
lines changed

6 files changed

+115
-28
lines changed

.cargo/config.toml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,18 @@
1-
[target.wasm32-wasi]
2-
#rustflags = ['-Clink-arg=--import-memory']
1+
# The adapter module created in this repository can technically be either
2+
# compiled for wasm32-wasi or the unknown-unknown target but the unknown version
3+
# is used to be able to specify custom flags here. That way the wasi tests don't
4+
# use these custom flags but the adapter does.
5+
[target.wasm32-unknown-unknown]
6+
rustflags = [
7+
# The adapter must import its memory from the main module so pass this for LLD
8+
# to generate the right memory import.
9+
'-Clink-arg=--import-memory',
10+
# The adapter will allocate its own stack and doesn't use the --stack-first
11+
# layout that LLD has by default. Set the stack size from LLD to zero here to
12+
# ensure that the memory imported into the module has a minimum size of 0 as
13+
# opposed to 1MB which might not be compatible with some WASI-using modules.
14+
'-Clink-arg=-zstack-size=0',
15+
# Currently all locations that will run this adapter have this feature enabled
16+
# and this avoid generating a `memcpy` function in the adapter itself.
17+
'-Ctarget-feature=+bulk-memory',
18+
]

.github/workflows/main.yml

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ jobs:
2929
steps:
3030
- uses: actions/checkout@v3
3131
- run: rustup update stable && rustup default stable
32-
- run: rustup target add wasm32-wasi
33-
- run: cargo build --target wasm32-wasi
34-
env:
35-
RUSTFLAGS: -Clink-args=--import-memory -Ctarget-feature=+bulk-memory -Clink-args=-zstack-size=0
36-
- run: cargo run -p verify -- ./target/wasm32-wasi/debug/wasi_snapshot_preview1.wasm
37-
- run: cargo build --release --target wasm32-wasi
38-
env:
39-
RUSTFLAGS: -Clink-args=--import-memory -Ctarget-feature=+bulk-memory -Clink-args=-zstack-size=0
40-
- run: cargo run -p verify -- ./target/wasm32-wasi/release/wasi_snapshot_preview1.wasm
32+
- run: rustup target add wasm32-wasi wasm32-unknown-unknown
33+
- run: cargo build --target wasm32-unknown-unknown
34+
- run: cargo run -p verify -- ./target/wasm32-unknown-unknown/debug/wasi_snapshot_preview1.wasm
35+
- run: cargo build --target wasm32-unknown-unknown --features command
36+
- run: cargo run -p verify -- ./target/wasm32-unknown-unknown/debug/wasi_snapshot_preview1.wasm
37+
- run: cargo build --release --target wasm32-unknown-unknown
38+
- run: cargo run -p verify -- ./target/wasm32-unknown-unknown/release/wasi_snapshot_preview1.wasm
39+
- run: cargo build --release --target wasm32-unknown-unknown --features command
40+
- run: cargo run -p verify -- ./target/wasm32-unknown-unknown/release/wasi_snapshot_preview1.wasm
4141
- run: cargo test -p host
4242

4343
build:
@@ -49,14 +49,18 @@ jobs:
4949
steps:
5050
- uses: actions/checkout@v3
5151
- run: rustup update stable && rustup default stable
52-
- run: rustup target add wasm32-wasi
53-
- run: cargo build --target wasm32-wasi --release
54-
env:
55-
RUSTFLAGS: -Clink-args=--import-memory -Ctarget-feature=+bulk-memory -Clink-args=-zstack-size=0
52+
- run: rustup target add wasm32-wasi wasm32-unknown-unknown
53+
- run: cargo build --target wasm32-unknown-unknown --release --features command
54+
- run: mv target/wasm32-unknown-unknown/release/wasi_snapshot_preview1.wasm wasi_snapshot_preview1.command.wasm
55+
- uses: actions/upload-artifact@v3
56+
with:
57+
name: wasi_snapshot_preview1.command.wasm
58+
path: wasi_snapshot_preview1.command.wasm
59+
- run: cargo build --target wasm32-unknown-unknown --release
5660
- uses: actions/upload-artifact@v3
5761
with:
5862
name: wasi_snapshot_preview1.wasm
59-
path: target/wasm32-wasi/release/wasi_snapshot_preview1.wasm
63+
path: target/wasm32-unknown-unknown/release/wasi_snapshot_preview1.wasm
6064

6165
- uses: marvinpinto/action-automatic-releases@latest
6266
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
@@ -65,4 +69,6 @@ jobs:
6569
automatic_release_tag: latest
6670
prerelease: true
6771
title: "Latest Build"
68-
files: target/wasm32-wasi/release/wasi_snapshot_preview1.wasm
72+
files: |
73+
target/wasm32-unknown-unknown/release/wasi_snapshot_preview1.wasm
74+
wasi_snapshot_preview1.command.wasm

Cargo.toml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,18 @@ crate-type = ["cdylib"]
2222
# that could theoretically change in the future.
2323
panic = 'abort'
2424

25+
[profile.release.package.wasi_snapshot_preview1]
26+
opt-level = 's'
27+
strip = 'debuginfo'
28+
2529
# Make dev look like a release build since this adapter module won't work with
2630
# a debug build that uses data segments and such.
2731
[profile.dev.package.wasi_snapshot_preview1]
2832
incremental = false
29-
opt-level = 3
30-
debug = 0
33+
opt-level = 's'
3134
# Omit integer overflow checks, which include failure messages which require
3235
# string initializers.
3336
overflow-checks = false
37+
38+
[features]
39+
command = []

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# `wasi_snapshot_preview1.wasm`
2+
3+
> **Note**: This repository is a work in progress. This is intended to be an
4+
> internal tool which not everyone has to look at but many might rely on. You
5+
> may need to reach out via issues or
6+
> [Zulip](https://bytecodealliance.zulipchat.com/) to learn more about this
7+
> repository.
8+
9+
This repository currently contains an implementation of a WebAssembly module:
10+
`wasi_snapshot_preview1.wasm`. This module bridges the `wasi_snapshot_preview1`
11+
ABI to the preview2 ABI of the component model. At this time the preview2 APIs
12+
themselves are not done being specified so a local copy of `wit/*.wit` is used
13+
instead.
14+
15+
## Building
16+
17+
This adapter can be built with:
18+
19+
```sh
20+
$ cargo build --target wasm32-unknown-unknown --release
21+
```
22+
23+
And the artifact will be located at
24+
`target/wasm32-unknown-unknown/release/wasi_snapshot_preview1.wasm`.
25+
Alternatively the latest copy of this can be [downloaded from the releases
26+
page][latest-release]
27+
28+
[latest-release]: https://github.com/bytecodealliance/preview2-prototyping/releases/tag/latest
29+
30+
## Using
31+
32+
With a `wasi_snapshot_preview1.wasm` file on-hand you can create a component
33+
from a module that imports WASI functions using the [`wasm-tools`
34+
CLI](https://github.com/bytecodealliance/wasm-tools)
35+
36+
```sh
37+
$ cat foo.rs
38+
fn main() {
39+
println!("Hello, world!");
40+
}
41+
$ rustc foo.rs --target wasm32-wasi
42+
$ wasm-tools print foo.wasm | grep '(import'
43+
(import "wasi_snapshot_preview1" "fd_write" (func ...
44+
(import "wasi_snapshot_preview1" "environ_get" (func ...
45+
(import "wasi_snapshot_preview1" "environ_sizes_get" ...
46+
(import "wasi_snapshot_preview1" "proc_exit" (func ...
47+
$ wasm-tools component new foo.wasm --adapt wasi_snapshot_preview1.wasm -o component.wasm
48+
49+
# Inspect the generated `component.wasm`
50+
$ wasm-tools validate component.wasm --features component-model
51+
$ wasm-tools component wit component.wasm
52+
```
53+
54+
Here the `component.wasm` that's generated is a ready-to-run component which
55+
imports wasi preview2 functions and is compatible with the wasi-preview1-using
56+
module internally.

src/lib.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,19 @@ mod bindings {
2525
});
2626
}
2727

28-
#[export_name = "command"]
29-
unsafe extern "C" fn command_entrypoint(
28+
#[no_mangle]
29+
pub unsafe extern "C" fn command(
3030
stdin: u32,
3131
stdout: u32,
3232
args_ptr: *const WasmStr,
3333
args_len: usize,
3434
) {
35+
// TODO: ideally turning off `command` would remove this import and the
36+
// `*.wit` metadata entirely but doing that ergonomically will likely
37+
// require some form of `use` to avoid duplicating lots of `*.wit` bits.
38+
if !cfg!(feature = "command") {
39+
unreachable();
40+
}
3541
State::with_mut(|state| {
3642
state.push_desc(Descriptor::File(File {
3743
fd: stdin,
@@ -1430,7 +1436,7 @@ struct State {
14301436
args: Option<&'static [WasmStr]>,
14311437
}
14321438

1433-
struct WasmStr {
1439+
pub struct WasmStr {
14341440
ptr: *const u8,
14351441
len: usize,
14361442
}

test-programs/macros/build.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,14 @@ fn main() {
1414
cmd.arg("build")
1515
.arg("--release")
1616
.current_dir("../../")
17-
.arg("--target=wasm32-wasi")
17+
.arg("--target=wasm32-unknown-unknown")
18+
.arg("--features=command")
1819
.env("CARGO_TARGET_DIR", &out_dir)
19-
.env(
20-
"RUSTFLAGS",
21-
"-Clink-args=--import-memory -Clink-args=-zstack-size=0",
22-
)
2320
.env_remove("CARGO_ENCODED_RUSTFLAGS");
2421
let status = cmd.status().unwrap();
2522
assert!(status.success());
2623

27-
let wasi_adapter = out_dir.join("wasm32-wasi/release/wasi_snapshot_preview1.wasm");
24+
let wasi_adapter = out_dir.join("wasm32-unknown-unknown/release/wasi_snapshot_preview1.wasm");
2825
println!("wasi adapter: {:?}", &wasi_adapter);
2926
let wasi_adapter = fs::read(&wasi_adapter).unwrap();
3027

0 commit comments

Comments
 (0)