cargo: compile C for wasm32-wasip2 with a WASI clang
ci / check (push) Successful in 18s

The Nix package, the dev shell, and the test check set CC_wasm32_wasip2
and AR_wasm32_wasip2 to the clang of pkgsCross.wasi32 (wasi-libc), so
build scripts with the cc crate work, for example rusqlite with bundled
SQLite. The runtime-toolchain check links C into a probe with the
packaged host and an empty PATH.
This commit is contained in:
2026-10-22 20:16:38 +01:00
parent 34e2f35272
commit a536d873e6
8 changed files with 112 additions and 6 deletions
+3
View File
@@ -8,6 +8,9 @@
//! The broker knows nothing about Rust. This actor is one way to get a
//! `.wasm` file. Other actors can add other languages.
//!
//! Build scripts compile C for WASI with the clang in `CC_wasm32_wasip2`
//! and `AR_wasm32_wasip2`. The Nix package and the dev shell set both.
//!
//! Actors depend on the SDK with `sausage-actor = "0.1"`. The SDK is not
//! published: this actor copies the SDK of this host to `public/sdk/` (so
//! that every actor can read it), and points the dependency there in the
+12
View File
@@ -0,0 +1,12 @@
[package]
name = "cargo-c"
version = "0.1.0"
edition = "2024"
[dependencies]
sausage-actor = "0.1"
[build-dependencies]
cc = "1"
[workspace]
+5
View File
@@ -0,0 +1,5 @@
// Compile C with the `cc` crate: natively for the unit test, and with the
// WASI clang (`CC_wasm32_wasip2`) for the actor.
fn main() {
cc::Build::new().file("src/add.c").compile("add");
}
+1
View File
@@ -0,0 +1 @@
int c_add(int a, int b) { return a + b; }
+25
View File
@@ -0,0 +1,25 @@
//! Adds two numbers with a C function: `{"type": "add", "a", "b"}` -> `{"type": "sum", "value"}`.
use sausage_actor::{json, messages, reply};
unsafe extern "C" {
fn c_add(a: i32, b: i32) -> i32;
}
fn add(a: i32, b: i32) -> i32 {
unsafe { c_add(a, b) }
}
fn main() {
for message in messages() {
let (a, b) = (message.body["a"].as_i64().unwrap_or(0), message.body["b"].as_i64().unwrap_or(0));
let _ = reply(&message, json!({"type": "sum", "value": add(a as i32, b as i32)}));
}
}
#[cfg(test)]
mod tests {
#[test]
fn c_code_runs_on_the_host_target() {
assert_eq!(super::add(2, 3), 5);
}
}
+16
View File
@@ -769,6 +769,22 @@ async fn cargo_actor_runs_unit_tests_by_default_and_can_skip_them() {
host.stop().await;
}
#[tokio::test]
async fn cargo_actor_compiles_c_code_for_wasi() {
let world = create_world("cargo-c");
let (host, mut mailbox) = start(&world).await;
let source = cargo_source("cargo-c");
let id = host.broker.send("console", Some("cargo"), None, None, json!({"type": "build", "source": source.display().to_string()}));
let built = receive_reply(&mut mailbox, &id, Duration::from_secs(300)).await;
assert_eq!(built.body["type"], "built", "{built:?}");
assert_eq!(built.body["tests"]["passed"], 1, "{built:?}");
let wasm = host.world.resolve("console", built.body["wasm"].as_str().unwrap()).unwrap();
spawn_type(&host, &mut mailbox, "c-add", &wasm, &source).await;
let sum = ask_type(&host, &mut mailbox, "c-add", json!({"type": "add", "a": 2, "b": 3}), "sum").await;
assert_eq!(sum.body["value"], 5);
host.stop().await;
}
#[tokio::test]
async fn rejects_invalid_actor_ids() {
let world = create_world("invalid-id");