Skip to content

Commit f61634f

Browse files
authored
Refactor the wit-bindgen crate for being suitable again to include with the Rust standard library (#1369)
* Remove old wit-bindgen compat module This has long since become unnecessary, so delete it. * Merge `wit-bindgen-rt` back into `wit-bindgen` This was originally done a long time ago at this point to split out the "abi stable bits" from `wit-bindgen` for the purpose of `cargo component` where one version of the macro was used in the binary itself and a different version might be linked at runtime. Over time though this has proven effectively intractable and isn't really serving much purpose any more. This commit instead merges everything into the `wit-bindgen` crate. This should make it easier to integrate elsewhere and additionally integrate into Rust's standard library. * Get `wit-bindgen` working without `bitflags` dep. Generate a bare-bones `bitflags!` macro. * Update rebuild script * Fix CI issues * More CI fixes
1 parent 05249c3 commit f61634f

33 files changed

+136
-397
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,13 @@ jobs:
152152
- run: cargo test
153153
- run: cargo test -p wit-bindgen-core
154154
- run: cargo test -p wit-bindgen
155+
- run: cargo test -p wit-bindgen --all-features
155156
- run: cargo test -p wit-bindgen-rust
156157
- run: cargo test --workspace --exclude 'wit-bindgen*'
157-
- run: cargo test -p wit-bindgen-rt --all-features
158158
- run: rustup update nightly --no-self-update
159159
- run: rustup component add miri --toolchain nightly
160160
- run: rustup component add rust-src --toolchain nightly
161-
- run: cargo +nightly miri test -p wit-bindgen-rt --all-features
161+
- run: cargo +nightly miri test -p wit-bindgen --all-features
162162

163163
check:
164164
name: Check

Cargo.lock

Lines changed: 17 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ci/rebuild-libwit-bindgen-cabi.sh

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ version=$(./ci/print-current-version.sh | sed 's/\./_/g')
4343

4444
realloc=cabi_realloc_wit_bindgen_$version
4545

46-
rm -f crates/guest-rust/rt/src/wit_bindgen_*.{rs,o,c}
47-
rm -f crates/guest-rust/rt/src/libwit_bindgen_cabi.a
46+
rm -f crates/guest-rust/src/rt/wit_bindgen_*.{rs,o,c}
47+
rm -f crates/guest-rust/src/rt/libwit_bindgen_cabi.a
4848

49-
cat >./crates/guest-rust/rt/src/wit_bindgen_cabi_realloc.rs <<-EOF
49+
cat >./crates/guest-rust/src/rt/wit_bindgen_cabi_realloc.rs <<-EOF
5050
// This file is generated by $0
5151
5252
#[unsafe(no_mangle)]
@@ -56,11 +56,11 @@ pub unsafe extern "C" fn $realloc(
5656
align: usize,
5757
new_len: usize,
5858
) -> *mut u8 {
59-
crate::cabi_realloc(old_ptr, old_len, align, new_len)
59+
crate::rt::cabi_realloc(old_ptr, old_len, align, new_len)
6060
}
6161
EOF
6262

63-
cat >./crates/guest-rust/rt/src/wit_bindgen_cabi_realloc.c <<-EOF
63+
cat >./crates/guest-rust/src/rt/wit_bindgen_cabi_realloc.c <<-EOF
6464
// This file is generated by $0
6565
6666
#include <stdint.h>
@@ -73,7 +73,7 @@ void *cabi_realloc(void *ptr, size_t old_size, size_t align, size_t new_size) {
7373
}
7474
EOF
7575

76-
cat >./crates/guest-rust/rt/src/wit_bindgen_cabi_wasip3.c <<-EOF
76+
cat >./crates/guest-rust/src/rt/wit_bindgen_cabi_wasip3.c <<-EOF
7777
// This file is generated by $0
7878
7979
#include <stdlib.h>
@@ -90,18 +90,18 @@ EOF
9090

9191
build() {
9292
file=$1
93-
$WASI_SDK_PATH/bin/clang crates/guest-rust/rt/src/$1.c \
94-
-O -c -o crates/guest-rust/rt/src/$1.o -mcpu=mvp
93+
$WASI_SDK_PATH/bin/clang crates/guest-rust/src/rt/$1.c \
94+
-O -c -o crates/guest-rust/src/rt/$1.o -mcpu=mvp
9595
# Remove the `producers` section. This appears to differ whether the host for
9696
# clang is either macOS or Linux. Not needed here anyway, so discard it to help
9797
# either host produce the same object.
98-
wasm-tools strip -d producers ./crates/guest-rust/rt/src/$1.o \
99-
-o ./crates/guest-rust/rt/src/$1.o
98+
wasm-tools strip -d producers ./crates/guest-rust/src/rt/$1.o \
99+
-o ./crates/guest-rust/src/rt/$1.o
100100
}
101101

102102
build wit_bindgen_cabi_realloc
103103
build wit_bindgen_cabi_wasip3
104104

105-
$WASI_SDK_PATH/bin/llvm-ar crus crates/guest-rust/rt/src/libwit_bindgen_cabi.a \
106-
crates/guest-rust/rt/src/wit_bindgen_cabi_realloc.o \
107-
crates/guest-rust/rt/src/wit_bindgen_cabi_wasip3.o
105+
$WASI_SDK_PATH/bin/llvm-ar crus crates/guest-rust/src/rt/libwit_bindgen_cabi.a \
106+
crates/guest-rust/src/rt/wit_bindgen_cabi_realloc.o \
107+
crates/guest-rust/src/rt/wit_bindgen_cabi_wasip3.o

crates/guest-rust/Cargo.toml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,21 @@ all-features = true
1717

1818
[dependencies]
1919
wit-bindgen-rust-macro = { path = "./macro", optional = true, version = "0.44.0" }
20-
wit-bindgen-rt = { path = "./rt", version = "0.44.0", features = ["bitflags"] }
20+
bitflags = { workspace = true, optional = true }
21+
futures = { version = "0.3.30", optional = true }
22+
once_cell = { version = "1.19.0", optional = true }
23+
24+
# When built as part of libstd
25+
core = { version = "1.0", optional = true, package = "rustc-std-workspace-core" }
26+
alloc = { version = "1.0", optional = true, package = "rustc-std-workspace-alloc" }
2127

2228
[features]
23-
default = ["macros", "realloc", "async"]
29+
default = ["macros", "realloc", "async", "std", "bitflags"]
2430
macros = ["dep:wit-bindgen-rust-macro"]
2531
realloc = []
26-
async = ["macros", "wit-bindgen-rt/async", "wit-bindgen-rust-macro/async"]
32+
std = []
33+
async = ["macros", "dep:futures", "dep:once_cell", "wit-bindgen-rust-macro/async"]
34+
bitflags = ["dep:bitflags"]
35+
36+
# Unstable feature to support being a libstd dependency
37+
rustc-dep-of-std = ["dep:core", "dep:alloc"]

crates/guest-rust/rt/build.rs renamed to crates/guest-rust/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ fn main() {
1616

1717
let mut src = env::current_dir().unwrap();
1818
src.push("src");
19+
src.push("rt");
1920
src.push("libwit_bindgen_cabi.a");
2021

2122
let dst_name = format!(

crates/guest-rust/rt/Cargo.toml

Lines changed: 0 additions & 19 deletions
This file was deleted.

crates/guest-rust/rt/LICENSE-APACHE

Lines changed: 0 additions & 1 deletion
This file was deleted.

crates/guest-rust/rt/LICENSE-Apache-2.0_WITH_LLVM-exception

Lines changed: 0 additions & 1 deletion
This file was deleted.

crates/guest-rust/rt/LICENSE-MIT

Lines changed: 0 additions & 1 deletion
This file was deleted.

crates/guest-rust/rt/README.md

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)