Skip to content

Commit 9a2e7ce

Browse files
authored
Use $WASI_SDK_PATH on WASI targets by default (#1562)
1 parent d740f9b commit 9a2e7ce

File tree

5 files changed

+41
-9
lines changed

5 files changed

+41
-9
lines changed

.github/workflows/main.yml

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ jobs:
152152
CXX: clang-cl
153153
steps:
154154
- uses: actions/checkout@v5
155+
- run: curl -vv https://static.rust-lang.org/dist/channel-rust-1.87.0.toml.sha256
156+
shell: bash
155157
- name: Install Rust (rustup)
156158
run: |
157159
set -euxo pipefail
@@ -277,11 +279,17 @@ jobs:
277279
- name: check clean Git workting tree
278280
uses: ./.github/actions/check-clean-git-working-tree
279281

280-
test-wasm32-wasip1-thread:
281-
name: Test wasm32-wasip1-thread
282+
test-wasi:
283+
name: Test wasm32-wasi* targets
282284
runs-on: ubuntu-latest
285+
strategy:
286+
matrix:
287+
target:
288+
- wasm32-wasip1
289+
- wasm32-wasip2
290+
- wasm32-wasip1-threads
283291
env:
284-
TARGET: wasm32-wasip1-threads
292+
TARGET: ${{ matrix.target }}
285293
steps:
286294
- uses: actions/checkout@v5
287295
- name: Install Rust (rustup)
@@ -308,9 +316,7 @@ jobs:
308316
wget "https://github.com/$REPO/releases/download/${VERSION}/${FILE}"
309317
sudo dpkg -i "${FILE}"
310318
WASI_SDK_PATH="/opt/wasi-sdk"
311-
CC="${WASI_SDK_PATH}/bin/clang"
312319
echo "WASI_SDK_PATH=$WASI_SDK_PATH" >> "$GITHUB_ENV"
313-
echo "CC=$CC" >> "$GITHUB_ENV"
314320
315321
- run: cargo update
316322
- uses: Swatinem/rust-cache@v2
@@ -319,7 +325,7 @@ jobs:
319325
cache-all-crates: "true"
320326

321327
- name: Run tests
322-
run: cargo +nightly build -p $TARGET-test --target $TARGET
328+
run: cargo +nightly build -p wasi-test --target $TARGET
323329

324330
# check that there are no uncommitted changes to prevent bugs like https://github.com/rust-lang/cc-rs/issues/1411
325331
- name: check clean Git workting tree
@@ -417,7 +423,7 @@ jobs:
417423
- test
418424
- check-build-std
419425
- check-wasm
420-
- test-wasm32-wasip1-thread
426+
- test-wasi
421427
- cuda
422428
- msrv
423429
- clippy

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ members = [
4444
"dev-tools/cc-test",
4545
"dev-tools/gen-target-info",
4646
"dev-tools/gen-windows-sys-binding",
47-
"dev-tools/wasm32-wasip1-threads-test",
47+
"dev-tools/wasi-test",
4848
]
4949

5050
[patch.crates-io]

dev-tools/wasm32-wasip1-threads-test/Cargo.toml renamed to dev-tools/wasi-test/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "wasm32-wasip1-threads-test"
2+
name = "wasi-test"
33
version = "0.1.0"
44
edition = "2021"
55
publish = false
File renamed without changes.

src/lib.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2921,6 +2921,8 @@ impl Build {
29212921
"{}-{}-{}-{}",
29222922
target.full_arch, target.vendor, target.os, traditional
29232923
)
2924+
} else if target.os == "wasi" {
2925+
self.autodetect_wasi_compiler(&raw_target, clang)
29242926
} else if target.arch == "wasm32" || target.arch == "wasm64" {
29252927
// Compiling WASM is not currently supported by GCC, so
29262928
// let's default to Clang.
@@ -4166,6 +4168,30 @@ impl Build {
41664168
::find_msvc_tools::find_tool_with_env(target.full_arch, tool, &BuildEnvGetter(self))
41674169
.map(Tool::from_find_msvc_tools)
41684170
}
4171+
4172+
/// Compiling for WASI targets typically uses the [wasi-sdk] project and
4173+
/// installations of wasi-sdk are typically indicated with the
4174+
/// `WASI_SDK_PATH` environment variable. Check to see if that environment
4175+
/// variable exists, and check to see if an appropriate compiler is located
4176+
/// there. If that all passes then use that compiler by default, but
4177+
/// otherwise fall back to whatever the clang default is since gcc doesn't
4178+
/// have support for compiling to wasm.
4179+
///
4180+
/// [wasi-sdk]: https://github.com/WebAssembly/wasi-sdk
4181+
fn autodetect_wasi_compiler(&self, raw_target: &str, clang: &str) -> String {
4182+
if let Some(path) = self.getenv("WASI_SDK_PATH") {
4183+
let target_clang = Path::new(&path)
4184+
.join("bin")
4185+
.join(format!("{raw_target}-clang"));
4186+
if let Some(path) = self.which(&target_clang, None) {
4187+
if let Some(s) = path.to_str() {
4188+
return s.to_string();
4189+
}
4190+
}
4191+
}
4192+
4193+
clang.to_string()
4194+
}
41694195
}
41704196

41714197
impl Default for Build {

0 commit comments

Comments
 (0)