Skip to content

Commit 8bb9d30

Browse files
committed
Update how WASI toolchains are used in CI and bootstrap
This commit updates how the WASI targets are configured with their toolchain. Long ago a `config.toml` option of `wasi-root` was added to enable building with the WASI files produced by wasi-libc. Additionally for CI testing and release building the Rust toolchain has been using a hard-coded commit of wasi-libc which is bundled with the release of the `wasm32-wasip1` target, for example. Nowadays though the wasi-sdk project, the C/C++ toolchain for WASI, is the go-to solution for compiling/linking WASI code and contains the more-or-less official releases of wasi-libc. This commit migrates CI to using wasi-sdk releases and additionally updates `bootstrap` to recognize when this is configured. This means that with `$WASI_SDK_PATH` configured there's no further configuration necessary to get a working build. Notably this also works better for the new targets of WASI as well, such as `wasm32-wasip2` and `wasm32-wasip1-threads` where the wasi-sdk release now has libraries for all targets bundled within it.
1 parent 0230848 commit 8bb9d30

File tree

9 files changed

+94
-102
lines changed

9 files changed

+94
-102
lines changed

src/bootstrap/src/core/build_steps/compile.rs

+9-16
Original file line numberDiff line numberDiff line change
@@ -394,16 +394,13 @@ fn copy_self_contained_objects(
394394
target_deps.push((libunwind_path, DependencyType::TargetSelfContained));
395395
}
396396
} else if target.contains("-wasi") {
397-
let srcdir = builder
398-
.wasi_root(target)
399-
.unwrap_or_else(|| {
400-
panic!(
401-
"Target {:?} does not have a \"wasi-root\" key in Config.toml",
402-
target.triple
403-
)
404-
})
405-
.join("lib")
406-
.join(target.to_string().replace("-preview1", "").replace("p2", "").replace("p1", ""));
397+
let srcdir = builder.wasi_libdir(target).unwrap_or_else(|| {
398+
panic!(
399+
"Target {:?} does not have a \"wasi-root\" key in Config.toml \
400+
or `$WASI_SDK_PATH` set",
401+
target.triple
402+
)
403+
});
407404
for &obj in &["libc.a", "crt1-command.o", "crt1-reactor.o"] {
408405
copy_and_stamp(
409406
builder,
@@ -514,12 +511,8 @@ pub fn std_cargo(builder: &Builder<'_>, target: TargetSelection, stage: u32, car
514511
}
515512

516513
if target.contains("-wasi") {
517-
if let Some(p) = builder.wasi_root(target) {
518-
let root = format!(
519-
"native={}/lib/{}",
520-
p.to_str().unwrap(),
521-
target.to_string().replace("-preview1", "")
522-
);
514+
if let Some(dir) = builder.wasi_libdir(target) {
515+
let root = format!("native={}", dir.to_str().unwrap());
523516
cargo.rustflag("-L").rustflag(&root);
524517
}
525518
}

src/bootstrap/src/lib.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -1351,9 +1351,24 @@ impl Build {
13511351
self.musl_root(target).map(|root| root.join("lib"))
13521352
}
13531353

1354-
/// Returns the sysroot for the wasi target, if defined
1355-
fn wasi_root(&self, target: TargetSelection) -> Option<&Path> {
1356-
self.config.target_config.get(&target).and_then(|t| t.wasi_root.as_ref()).map(|p| &**p)
1354+
/// Returns the `lib` directory for the WASI target specified, if
1355+
/// configured.
1356+
///
1357+
/// This first consults `wasi-root` as configured in per-target
1358+
/// configuration, and failing that it assumes that `$WASI_SDK_PATH` is
1359+
/// set in the environment, and failing that `None` is returned.
1360+
fn wasi_libdir(&self, target: TargetSelection) -> Option<PathBuf> {
1361+
let configured =
1362+
self.config.target_config.get(&target).and_then(|t| t.wasi_root.as_ref()).map(|p| &**p);
1363+
if let Some(path) = configured {
1364+
return Some(path.join("lib").join(target.to_string()));
1365+
}
1366+
let mut env_root = PathBuf::from(std::env::var_os("WASI_SDK_PATH")?);
1367+
env_root.push("share");
1368+
env_root.push("wasi-sysroot");
1369+
env_root.push("lib");
1370+
env_root.push(target.to_string());
1371+
Some(env_root)
13571372
}
13581373

13591374
/// Returns `true` if this is a no-std `target`, if defined

src/bootstrap/src/utils/cc_detect.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ fn cc2ar(cc: &Path, target: TargetSelection) -> Option<PathBuf> {
4747
Some(PathBuf::from("ar"))
4848
} else if target.contains("vxworks") {
4949
Some(PathBuf::from("wr-ar"))
50-
} else if target.contains("android") {
50+
} else if target.contains("android") || target.contains("-wasi") {
5151
Some(cc.parent().unwrap().join(PathBuf::from("llvm-ar")))
5252
} else {
5353
let parent = cc.parent().unwrap();
@@ -223,6 +223,16 @@ fn default_compiler(
223223
}
224224
}
225225

226+
t if t.contains("-wasi") => {
227+
let root = PathBuf::from(std::env::var_os("WASI_SDK_PATH")?);
228+
let compiler = match compiler {
229+
Language::C => format!("{t}-clang"),
230+
Language::CPlusPlus => format!("{t}-clang++"),
231+
};
232+
let compiler = root.join("bin").join(compiler);
233+
Some(compiler)
234+
}
235+
226236
_ => None,
227237
}
228238
}

src/ci/docker/host-x86_64/dist-various-2/Dockerfile

+3-8
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,9 @@ RUN /tmp/build-solaris-toolchain.sh sparcv9 sparcv9 solaris-sparc sun
8585
COPY host-x86_64/dist-various-2/build-x86_64-fortanix-unknown-sgx-toolchain.sh /tmp/
8686
RUN /tmp/build-x86_64-fortanix-unknown-sgx-toolchain.sh
8787

88-
COPY host-x86_64/dist-various-2/build-wasi-toolchain.sh /tmp/
89-
RUN /tmp/build-wasi-toolchain.sh
90-
91-
COPY host-x86_64/dist-various-2/build-wasi-threads-toolchain.sh /tmp/
92-
RUN /tmp/build-wasi-threads-toolchain.sh
88+
RUN curl -L https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-22/wasi-sdk-22.0-linux.tar.gz | \
89+
tar -xz
90+
ENV WASI_SDK_PATH=/tmp/wasi-sdk-22.0
9391

9492
COPY scripts/freebsd-toolchain.sh /tmp/
9593
RUN /tmp/freebsd-toolchain.sh i686
@@ -136,9 +134,6 @@ ENV TARGETS=$TARGETS,x86_64-unknown-uefi
136134
RUN ln -s /usr/include/x86_64-linux-gnu/asm /usr/local/include/asm
137135

138136
ENV RUST_CONFIGURE_ARGS --enable-extended --enable-lld --enable-llvm-bitcode-linker --disable-docs \
139-
--set target.wasm32-wasi.wasi-root=/wasm32-wasip1 \
140-
--set target.wasm32-wasip1.wasi-root=/wasm32-wasip1 \
141-
--set target.wasm32-wasip1-threads.wasi-root=/wasm32-wasip1-threads \
142137
--musl-root-armv7=/musl-armv7
143138

144139
ENV SCRIPT python3 ../x.py dist --host='' --target $TARGETS

src/ci/docker/host-x86_64/dist-various-2/build-wasi-threads-toolchain.sh

-24
This file was deleted.

src/ci/docker/host-x86_64/dist-various-2/build-wasi-toolchain.sh

-23
This file was deleted.

src/ci/docker/host-x86_64/test-various/Dockerfile

+4-7
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ WORKDIR /
3939
COPY scripts/sccache.sh /scripts/
4040
RUN sh /scripts/sccache.sh
4141

42-
COPY host-x86_64/dist-various-2/build-wasi-toolchain.sh /tmp/
43-
RUN /tmp/build-wasi-toolchain.sh
42+
RUN curl -L https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-22/wasi-sdk-22.0-linux.tar.gz | \
43+
tar -xz
44+
ENV WASI_SDK_PATH=/wasi-sdk-22.0
4445

4546
ENV RUST_CONFIGURE_ARGS \
4647
--musl-root-x86_64=/usr/local/x86_64-linux-musl \
4748
--set build.nodejs=/node-v18.12.0-linux-x64/bin/node \
48-
--set rust.lld \
49-
--set target.wasm32-wasip1.wasi-root=/wasm32-wasip1
49+
--set rust.lld
5050

5151
# Some run-make tests have assertions about code size, and enabling debug
5252
# assertions in libstd causes the binary to be much bigger than it would
@@ -68,9 +68,6 @@ ENV WASM_SCRIPT python3 /checkout/x.py --stage 2 test --host='' --target $WASM_T
6868
tests/codegen \
6969
tests/assembly \
7070
library/core
71-
ENV CC_wasm32_wasip1=clang-11 \
72-
CFLAGS_wasm32_wasip1="--sysroot /wasm32-wasip1" \
73-
AR_wasm32_wasip1=llvm-ar-11
7471

7572
ENV NVPTX_TARGETS=nvptx64-nvidia-cuda
7673
ENV NVPTX_SCRIPT python3 /checkout/x.py --stage 2 test --host='' --target $NVPTX_TARGETS \

src/doc/rustc/src/platform-support/wasm32-wasip1.md

+20-16
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,21 @@ be used instead.
7373

7474
## Building the target
7575

76-
To build this target a compiled version of [`wasi-libc`] is required to be
77-
present at build time. This can be installed through
78-
[`wasi-sdk`](https://github.com/WebAssembly/wasi-sdk) as well. This is the
79-
configured with:
80-
81-
```toml
82-
[target.wasm32-wasip1]
83-
wasi-root = ".../wasi-libc/sysroot"
76+
To build this target first acquire a copy of
77+
[`wasi-sdk`](https://github.com/WebAssembly/wasi-sdk/). At this time version 22
78+
is the minimum needed.
79+
80+
Next configure the `WASI_SDK_PATH` environment variable to point to where this
81+
is installed. For example:
82+
83+
```text
84+
export WASI_SDK_PATH=/path/to/wasi-sdk-22.0
8485
```
8586

86-
Additionally users will need to enable LLD when building Rust from source as
87-
LLVM's `wasm-ld` driver for LLD is required when linking WebAssembly code
88-
together.
87+
Next be sure to enable LLD when building Rust from source as LLVM's `wasm-ld`
88+
driver for LLD is required when linking WebAssembly code together. Rust's build
89+
system will automatically pick up any necessary binaries and programs from
90+
`WASI_SDK_PATH`.
8991

9092
## Building Rust programs
9193

@@ -112,8 +114,10 @@ This target can be cross-compiled from any hosts.
112114

113115
## Testing
114116

115-
Currently the WASI target is not tested in rust-lang/rust CI. This means that
116-
tests in the repository are not guaranteed to pass. This is theoretically
117-
possibly by installing a standalone WebAssembly runtime and using it as a
118-
"runner" for all tests, but there are various failures that will need to be
119-
waded through to adjust tests to work on the WASI target.
117+
This target is tested in rust-lang/rust CI on all merges. A subset of tests are
118+
run in the `test-various` builder such as the UI tests and libcore tests. This
119+
can be tested locally, for example, with:
120+
121+
```text
122+
./x.py test --target wasm32-wasip1 tests/ui
123+
```

src/doc/rustc/src/platform-support/wasm32-wasip2.md

+29-4
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,34 @@ This target is cross-compiled. The target supports `std` fully.
2222

2323
## Platform requirements
2424

25-
The WebAssembly runtime should support the wasi preview 2 API set.
25+
The WebAssembly runtime should support the wasi preview 2 API set. Runtimes also
26+
are required to support components since this target outputs a component as
27+
opposed to a core wasm module. As of the time of this writing Wasmtime 17 and
28+
above is able to run this target natively with no extra flags.
2629

27-
This target is not a stable target. This means that there are only a few engines
28-
which implement wasi preview 2, for example:
30+
## Building the target
2931

30-
* Wasmtime - `-W component-model`
32+
To build this target first acquire a copy of
33+
[`wasi-sdk`](https://github.com/WebAssembly/wasi-sdk/). At this time version 22
34+
is the minimum needed.
35+
36+
Next configure the `WASI_SDK_PATH` environment variable to point to where this
37+
is installed. For example:
38+
39+
```text
40+
export WASI_SDK_PATH=/path/to/wasi-sdk-22.0
41+
```
42+
43+
Next be sure to enable LLD when building Rust from source as LLVM's `wasm-ld`
44+
driver for LLD is required when linking WebAssembly code together. Rust's build
45+
system will automatically pick up any necessary binaries and programs from
46+
`WASI_SDK_PATH`.
47+
48+
## Testing
49+
50+
This target is not tested in CI at this time. Locally it can be tested with a
51+
`wasmtime` binary in `PATH` like so:
52+
53+
```text
54+
./x.py test --target wasm32-wasip2 tests/ui
55+
```

0 commit comments

Comments
 (0)