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

Commit 209b120

Browse files
committed
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.
1 parent 2c96f4d commit 209b120

File tree

5 files changed

+90
-24
lines changed

5 files changed

+90
-24
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: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,11 @@ 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 --release --target wasm32-unknown-unknown
36+
- run: cargo run -p verify -- ./target/wasm32-unknown-unknown/release/wasi_snapshot_preview1.wasm
4137
- run: cargo test -p host
4238

4339
build:
@@ -50,13 +46,11 @@ jobs:
5046
- uses: actions/checkout@v3
5147
- run: rustup update stable && rustup default stable
5248
- 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
49+
- run: cargo build --target wasm32-unknown-unknown --release
5650
- uses: actions/upload-artifact@v3
5751
with:
5852
name: wasi_snapshot_preview1.wasm
59-
path: target/wasm32-wasi/release/wasi_snapshot_preview1.wasm
53+
path: target/wasm32-unknown-unknown/release/wasi_snapshot_preview1.wasm
6054

6155
- uses: marvinpinto/action-automatic-releases@latest
6256
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
@@ -65,4 +59,4 @@ jobs:
6559
automatic_release_tag: latest
6660
prerelease: true
6761
title: "Latest Build"
68-
files: target/wasm32-wasi/release/wasi_snapshot_preview1.wasm
62+
files: target/wasm32-unknown-unknown/release/wasi_snapshot_preview1.wasm

Cargo.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@ 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'
34+
debug = 1
3135
# Omit integer overflow checks, which include failure messages which require
3236
# string initializers.
3337
overflow-checks = false

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.

test-programs/macros/build.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,13 @@ fn main() {
1414
cmd.arg("build")
1515
.arg("--release")
1616
.current_dir("../../")
17-
.arg("--target=wasm32-wasi")
17+
.arg("--target=wasm32-unknown-unknown")
1818
.env("CARGO_TARGET_DIR", &out_dir)
19-
.env(
20-
"RUSTFLAGS",
21-
"-Clink-args=--import-memory -Clink-args=-zstack-size=0",
22-
)
2319
.env_remove("CARGO_ENCODED_RUSTFLAGS");
2420
let status = cmd.status().unwrap();
2521
assert!(status.success());
2622

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

0 commit comments

Comments
 (0)