Skip to content

Commit 4c7f1cf

Browse files
authored
Move cabi_realloc to a wit-bindgen-rt crate (bytecodealliance#876)
* Move `cabi_realloc` to a `wit-bindgen-rt` crate This commit extracts out a `wit-bindgen-rt` crate which unconditionally provides the `cabi_realloc` function. This'll exist for `cargo component` to add a dependency on and otherwise the `wit-bindgen` crate isn't actually need at runtime at all. * Remove realloc feature gate * Add runtime to publish script * Remove realloc feature guard in build script
1 parent 29c1079 commit 4c7f1cf

File tree

16 files changed

+120
-96
lines changed

16 files changed

+120
-96
lines changed

.github/workflows/main.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ jobs:
137137
- run: cargo build --no-default-features --features csharp
138138
- run: cargo build --no-default-features --features markdown
139139

140+
# Feature combos of the `wit-bindgen` crate
141+
- run: cargo build --target wasm32-wasi -p wit-bindgen --no-default-features
142+
- run: cargo build --target wasm32-wasi -p wit-bindgen --no-default-features --features realloc
143+
- run: cargo build --target wasm32-wasi -p wit-bindgen --no-default-features --features macros
144+
- run: cargo build --target wasm32-wasi -p wit-bindgen --no-default-features --features macros,realloc
145+
140146
# Verity that documentation can be generated for the rust bindings crate.
141147
- run: cargo doc -p wit-bindgen --no-deps
142148
env:

Cargo.lock

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

ci/publish.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const CRATES_TO_PUBLISH: &[&str] = &[
2525
"wit-bindgen-teavm-java",
2626
"wit-bindgen-markdown",
2727
"wit-bindgen-rust-macro",
28+
"wit-bindgen-rt",
2829
"wit-bindgen",
2930
"wit-bindgen-cli",
3031
];

ci/rebuild-libcabi-realloc.sh

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ version=$(grep '^version =' ./Cargo.toml | sed 's/.*"\(.*\)"/\1/' | sed 's/\./_/
4343

4444
sym=cabi_realloc_wit_bindgen_$version
4545

46-
cat >./crates/guest-rust/src/cabi_realloc.rs <<-EOF
46+
cat >./crates/guest-rust/rt/src/cabi_realloc.rs <<-EOF
4747
// This file is generated by $0
4848
4949
#[no_mangle]
@@ -53,11 +53,11 @@ pub unsafe extern "C" fn $sym(
5353
align: usize,
5454
new_len: usize,
5555
) -> *mut u8 {
56-
crate::rt::cabi_realloc(old_ptr, old_len, align, new_len)
56+
crate::cabi_realloc(old_ptr, old_len, align, new_len)
5757
}
5858
EOF
5959

60-
cat >./crates/guest-rust/src/cabi_realloc.c <<-EOF
60+
cat >./crates/guest-rust/rt/src/cabi_realloc.c <<-EOF
6161
// This file is generated by $0
6262
6363
#include <stdint.h>
@@ -70,16 +70,16 @@ void *cabi_realloc(void *ptr, size_t old_size, size_t align, size_t new_size) {
7070
}
7171
EOF
7272

73-
rm -f crates/guest-rust/src/cabi_realloc.o
74-
$WASI_SDK_PATH/bin/clang crates/guest-rust/src/cabi_realloc.c \
75-
-O -c -o crates/guest-rust/src/cabi_realloc.o
73+
rm -f crates/guest-rust/rt/src/cabi_realloc.o
74+
$WASI_SDK_PATH/bin/clang crates/guest-rust/rt/src/cabi_realloc.c \
75+
-O -c -o crates/guest-rust/rt/src/cabi_realloc.o
7676

7777
# Remove the `producers` section. This appears to differ whether the host for
7878
# clang is either macOS or Linux. Not needed here anyway, so discard it to help
7979
# either host produce the same object.
80-
wasm-tools strip -d producers ./crates/guest-rust/src/cabi_realloc.o \
81-
-o ./crates/guest-rust/src/cabi_realloc.o
80+
wasm-tools strip -d producers ./crates/guest-rust/rt/src/cabi_realloc.o \
81+
-o ./crates/guest-rust/rt/src/cabi_realloc.o
8282

83-
rm -f crates/guest-rust/src/libwit_bindgen_cabi_realloc.a
84-
$WASI_SDK_PATH/bin/llvm-ar crus crates/guest-rust/src/libwit_bindgen_cabi_realloc.a \
85-
crates/guest-rust/src/cabi_realloc.o
83+
rm -f crates/guest-rust/rt/src/libwit_bindgen_cabi_realloc.a
84+
$WASI_SDK_PATH/bin/llvm-ar crus crates/guest-rust/rt/src/libwit_bindgen_cabi_realloc.a \
85+
crates/guest-rust/rt/src/cabi_realloc.o

crates/guest-rust/Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ Used when compiling Rust programs to the component model.
1212
"""
1313

1414
[dependencies]
15-
wit-bindgen-rust-macro = { path = "../rust-macro", optional = true, version = "0.20.0" }
15+
wit-bindgen-rust-macro = { path = "./macro", optional = true, version = "0.20.0" }
16+
wit-bindgen-rt = { path = "./rt", optional = true, version = "0.20.0" }
1617
bitflags = { workspace = true }
1718

1819
[features]
1920
default = ["macros", "realloc"]
20-
macros = ["wit-bindgen-rust-macro"]
21-
realloc = []
21+
macros = ["dep:wit-bindgen-rust-macro"]
22+
realloc = ["dep:wit-bindgen-rt"]
File renamed without changes.
File renamed without changes.
File renamed without changes.

crates/guest-rust/rt/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "wit-bindgen-rt"
3+
version.workspace = true
4+
edition.workspace = true
5+
repository = 'https://github.com/bytecodealliance/wit-bindgen'
6+
license = "Apache-2.0 WITH LLVM-exception"
7+
homepage = 'https://github.com/bytecodealliance/wit-bindgen'
8+
description = """
9+
Runtime support for the `wit-bindgen` crate
10+
"""

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
fn main() {
2-
if !cfg!(feature = "realloc") {
3-
return;
4-
}
5-
62
let target_arch = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap_or(String::new());
73
let target_family = std::env::var("CARGO_CFG_TARGET_FAMILY").unwrap_or(String::new());
84

0 commit comments

Comments
 (0)