Skip to content

Commit b5be8c9

Browse files
committed
Add wasm32-unknown-unknown support
1 parent 4697476 commit b5be8c9

5 files changed

Lines changed: 111 additions & 21 deletions

File tree

‎.github/actions/setup-deps/action.yml‎

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,27 @@ inputs:
44
target:
55
description: 'rust target being set up'
66
required: true
7+
toolchain:
8+
description: 'Rust toolchain to install'
9+
default: stable
710
emscripten_sdk:
811
description: 'emsdk version to install'
912
default: latest
1013
wasi_sdk:
1114
description: 'wasi-sdk version to install'
12-
default: 33
15+
default: '34'
1316
wasmtime:
1417
description: 'wasmtime version to install'
15-
default: v47.0.3
18+
default: v50.0.0
1619

1720
runs:
1821
using: composite
1922
steps:
2023
- uses: dtolnay/rust-toolchain@stable
2124
with:
25+
toolchain: ${{ inputs.toolchain }}
2226
target: ${{ inputs.target }}
23-
- run: echo "CARGO_BUILD_TARGET=${{ matrix.target }}" >> $GITHUB_ENV
27+
- run: echo "CARGO_BUILD_TARGET=${{ inputs.target }}" >> $GITHUB_ENV
2428
shell: bash
2529

2630
- name: Install GCC (x86_64-unknown-linux-musl)
@@ -75,19 +79,36 @@ runs:
7579
echo "CARGO_TARGET_WASM32_UNKNOWN_EMSCRIPTEN_RUNNER=$EMSDK_NODE" >> $GITHUB_ENV
7680
shell: bash
7781

78-
- name: Install wasi-sdk/Wasmtime (wasm32-wasip2)
79-
if: ${{ inputs.target == 'wasm32-wasip2' }}
82+
- name: Install wasi-sdk/Wasmtime
83+
if: ${{ inputs.target == 'wasm32-unknown-unknown' || inputs.target == 'wasm32-wasip2' || inputs.target == 'wasm32-wasip3' }}
8084
working-directory: ${{ runner.tool_cache }}
85+
env:
86+
WASM_TARGET: ${{ inputs.target }}
8187
run: |
82-
curl -LO https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-${{ inputs.wasi_sdk }}/wasi-sdk-${{ inputs.wasi_sdk }}.0-x86_64-linux.tar.gz
88+
curl -fLO https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-${{ inputs.wasi_sdk }}/wasi-sdk-${{ inputs.wasi_sdk }}.0-x86_64-linux.tar.gz
8389
tar xf wasi-sdk-${{ inputs.wasi_sdk }}.0-x86_64-linux.tar.gz
84-
WASI_SDK_PATH=`pwd`/wasi-sdk-${{ inputs.wasi_sdk }}.0-x86_64-linux
85-
echo "WASI_SDK_PATH=$WASI_SDK_PATH" >> $GITHUB_ENV
86-
echo "CC_wasm32_wasip2=$WASI_SDK_PATH/bin/clang" >> $GITHUB_ENV
87-
echo "CARGO_TARGET_WASM32_WASIP2_LINKER=$WASI_SDK_PATH/bin/clang" >> $GITHUB_ENV
88-
echo "CARGO_TARGET_WASM32_WASIP2_RUSTFLAGS=-Clink-arg=-Wl,--export=cabi_realloc" >> $GITHUB_ENV
90+
WASI_SDK_PATH="$PWD/wasi-sdk-${{ inputs.wasi_sdk }}.0-x86_64-linux"
91+
echo "WASI_SDK_PATH=$WASI_SDK_PATH" >> "$GITHUB_ENV"
92+
cc_target="${WASM_TARGET//-/_}"
93+
cargo_target="CARGO_TARGET_${cc_target^^}"
94+
echo "CC_$cc_target=$WASI_SDK_PATH/bin/clang" >> "$GITHUB_ENV"
95+
rustflags="-Clink-arg=-Wl,--export=cabi_realloc"
96+
runner_flags="-W exceptions"
97+
if [[ "$WASM_TARGET" == wasm32-unknown-unknown ]]; then
98+
echo "AR_$cc_target=$WASI_SDK_PATH/bin/llvm-ar" >> "$GITHUB_ENV"
99+
echo "CFLAGS_$cc_target=--target=wasm32-wasip1" >> "$GITHUB_ENV"
100+
rustflags="-Lnative=$WASI_SDK_PATH/share/wasi-sysroot/lib/wasm32-wasip1 -lstatic=c"
101+
else
102+
echo "${cargo_target}_LINKER=$WASI_SDK_PATH/bin/clang" >> "$GITHUB_ENV"
103+
fi
104+
if [[ "$WASM_TARGET" == wasm32-wasip3 ]]; then
105+
rustflags+=" -Clink-self-contained=no"
106+
runner_flags+=" -S p3"
107+
fi
108+
echo "${cargo_target}_RUSTFLAGS=$rustflags" >> "$GITHUB_ENV"
89109
90-
curl -LO https://github.com/bytecodealliance/wasmtime/releases/download/${{ inputs.wasmtime }}/wasmtime-${{ inputs.wasmtime }}-x86_64-linux.tar.xz
110+
curl -fLO https://github.com/bytecodealliance/wasmtime/releases/download/${{ inputs.wasmtime }}/wasmtime-${{ inputs.wasmtime }}-x86_64-linux.tar.xz
91111
tar xf wasmtime-${{ inputs.wasmtime }}-x86_64-linux.tar.xz
92-
echo "CARGO_TARGET_WASM32_WASIP2_RUNNER=`pwd`/wasmtime-${{ inputs.wasmtime }}-x86_64-linux/wasmtime -W exceptions" >> $GITHUB_ENV
112+
echo "$PWD/wasmtime-${{ inputs.wasmtime }}-x86_64-linux" >> "$GITHUB_PATH"
113+
echo "${cargo_target}_RUNNER=$PWD/wasmtime-${{ inputs.wasmtime }}-x86_64-linux/wasmtime $runner_flags" >> "$GITHUB_ENV"
93114
shell: bash

‎.github/workflows/main.yml‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ jobs:
1818
- x86_64-pc-windows-gnu
1919
- x86_64-pc-windows-msvc
2020
- wasm32-unknown-emscripten
21+
- wasm32-unknown-unknown
2122
- wasm32-wasip2
23+
- wasm32-wasip3
2224
include:
2325
- target: x86_64-unknown-linux-gnu
2426
os: ubuntu-latest
@@ -38,13 +40,19 @@ jobs:
3840
os: windows-latest
3941
- target: wasm32-unknown-emscripten
4042
os: ubuntu-latest
43+
- target: wasm32-unknown-unknown
44+
os: ubuntu-latest
4145
- target: wasm32-wasip2
4246
os: ubuntu-latest
47+
- target: wasm32-wasip3
48+
os: ubuntu-latest
49+
toolchain: nightly
4350
steps:
4451
- uses: actions/checkout@main
4552
- uses: ./.github/actions/setup-deps
4653
with:
4754
target: ${{ matrix.target }}
55+
toolchain: ${{ matrix.toolchain || 'stable' }}
4856
- name: Build ${{ matrix.lua }}
4957
run: |
5058
cargo build --manifest-path testcrate/Cargo.toml --release --features ${{ matrix.lua }} --target ${{ matrix.target }}
@@ -63,7 +71,9 @@ jobs:
6371
- aarch64-apple-darwin
6472
- x86_64-pc-windows-msvc
6573
- wasm32-unknown-emscripten
74+
- wasm32-unknown-unknown
6675
- wasm32-wasip2
76+
- wasm32-wasip3
6777
include:
6878
- target: x86_64-unknown-linux-gnu
6979
os: ubuntu-latest
@@ -75,17 +85,30 @@ jobs:
7585
os: windows-latest
7686
- target: wasm32-unknown-emscripten
7787
os: ubuntu-latest
88+
- target: wasm32-unknown-unknown
89+
os: ubuntu-latest
7890
- target: wasm32-wasip2
7991
os: ubuntu-latest
92+
- target: wasm32-wasip3
93+
os: ubuntu-latest
94+
toolchain: nightly
8095
steps:
8196
- uses: actions/checkout@main
8297
- uses: ./.github/actions/setup-deps
8398
with:
8499
target: ${{ matrix.target }}
100+
toolchain: ${{ matrix.toolchain || 'stable' }}
85101
- name: Run ${{ matrix.lua }} tests
102+
if: ${{ matrix.target != 'wasm32-unknown-unknown' }}
86103
run: |
87104
cargo test --manifest-path testcrate/Cargo.toml --release --features ${{ matrix.lua }} --target ${{ matrix.target }}
88105
shell: bash
106+
- name: Run ${{ matrix.lua }} tests on wasm32-unknown-unknown
107+
if: ${{ matrix.target == 'wasm32-unknown-unknown' }}
108+
run: |
109+
cargo rustc --manifest-path testcrate/Cargo.toml --release --features ${{ matrix.lua }} --target ${{ matrix.target }} --lib --crate-type cdylib
110+
wasmtime run -W exceptions --invoke run_tests target/wasm32-unknown-unknown/release/testcrate.wasm
111+
shell: bash
89112

90113
test_standalone:
91114
name: Test Standalone
@@ -105,6 +128,7 @@ jobs:
105128
- uses: ./.github/actions/setup-deps
106129
with:
107130
target: ${{ matrix.target }}
131+
toolchain: ${{ matrix.toolchain || 'stable' }}
108132
- name: Run standalone tests
109133
run: |
110134
cargo test

‎src/lib.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ impl Build {
173173
.define("LUA_USE_POSIX", None)
174174
.flag("-sSUPPORT_LONGJMP=wasm"); // Enable setjmp/longjmp support (WASM-specific)
175175
}
176-
_ if target.contains("wasi") => {
176+
_ if target.contains("wasi") || target == "wasm32-unknown-unknown" => {
177177
// WASI is posix-like, but further patches are needed to the Lua
178178
// source to get it compiling.
179179
config.define("LUA_USE_POSIX", None);

‎testcrate/src/allocator.rs‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use std::alloc::{GlobalAlloc, Layout};
2+
use std::ffi::c_void;
3+
4+
// Rust and WASI libc must share an allocator
5+
struct WasiAllocator;
6+
7+
unsafe extern "C" {
8+
fn posix_memalign(ptr: *mut *mut c_void, align: usize, size: usize) -> i32;
9+
fn free(ptr: *mut c_void);
10+
}
11+
12+
unsafe impl GlobalAlloc for WasiAllocator {
13+
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
14+
let mut ptr = std::ptr::null_mut();
15+
if unsafe {
16+
posix_memalign(
17+
&mut ptr,
18+
layout.align().max(size_of::<usize>()),
19+
layout.size(),
20+
)
21+
} != 0
22+
{
23+
return std::ptr::null_mut();
24+
}
25+
ptr.cast()
26+
}
27+
28+
unsafe fn dealloc(&self, ptr: *mut u8, _: Layout) {
29+
unsafe { free(ptr.cast()) };
30+
}
31+
}
32+
33+
#[global_allocator]
34+
static ALLOCATOR: WasiAllocator = WasiAllocator;

‎testcrate/src/lib.rs‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,23 @@ pub unsafe fn to_string<'a>(state: *mut c_void, index: c_int) -> &'a str {
7575
std::str::from_utf8(bytes).unwrap()
7676
}
7777

78-
#[cfg(test)]
78+
#[cfg(target_os = "unknown")]
79+
mod allocator;
80+
81+
#[cfg(target_os = "unknown")]
82+
#[unsafe(no_mangle)]
83+
pub extern "C" fn run_tests() {
84+
tests::test_lua_version();
85+
tests::test_unicode_identifiers();
86+
tests::test_exceptions();
87+
}
88+
89+
#[cfg(any(test, target_os = "unknown"))]
7990
mod tests {
8091
use super::*;
8192

82-
#[test]
83-
fn test_lua_version() {
93+
#[cfg_attr(not(target_os = "unknown"), test)]
94+
pub fn test_lua_version() {
8495
unsafe {
8596
let state = luaL_newstate();
8697
assert!(!state.is_null());
@@ -103,8 +114,8 @@ mod tests {
103114
}
104115
}
105116

106-
#[test]
107-
fn test_unicode_identifiers() {
117+
#[cfg_attr(not(target_os = "unknown"), test)]
118+
pub fn test_unicode_identifiers() {
108119
unsafe {
109120
let state = luaL_newstate();
110121
let ret = luaL_loadstring(state, c"😀 = '🌚︎'".as_ptr());
@@ -122,8 +133,8 @@ mod tests {
122133
}
123134
}
124135

125-
#[test]
126-
fn test_exceptions() {
136+
#[cfg_attr(not(target_os = "unknown"), test)]
137+
pub fn test_exceptions() {
127138
unsafe {
128139
let state = luaL_newstate();
129140
assert!(!state.is_null());

0 commit comments

Comments
 (0)