Skip to content

Commit 57444d2

Browse files
authored
Add wasmtime-c-api-impl to the list of crates to publish (#7837) (#7872)
* Add wasmtime-c-api-impl to the list of crates to publish * Enable rustdoc and publishing for c-api crate * Provide paths to c-api headers as cargo links metadata * Add a README section about using wasm-c-api in a rust crate * In C API doc comment, mention use case for crates w/ C bindings * Enable publishing for wasmtime-c-api-macros (prtest:full) * Move c-api crates later in the publishing sequence (prtest:full)
1 parent c7a7b8c commit 57444d2

File tree

9 files changed

+59
-9
lines changed

9 files changed

+59
-9
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ all = 'allow'
154154
arbitrary = { version = "1.3.1" }
155155
wasmtime-wmemcheck = { path = "crates/wmemcheck", version = "=18.0.0" }
156156
wasmtime = { path = "crates/wasmtime", version = "18.0.0", default-features = false }
157+
wasmtime-c-api-macros = { path = "crates/c-api-macros", version = "=18.0.0" }
157158
wasmtime-cache = { path = "crates/cache", version = "=18.0.0" }
158159
wasmtime-cli-flags = { path = "crates/cli-flags", version = "=18.0.0" }
159160
wasmtime-cranelift = { path = "crates/cranelift", version = "=18.0.0" }

crates/c-api/macros/Cargo.toml renamed to crates/c-api-macros/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
[package]
22
name = "wasmtime-c-api-macros"
3-
version = "0.0.0"
3+
version.workspace = true
44
authors = ["The Wasmtime Project Developers"]
55
license = "Apache-2.0 WITH LLVM-exception"
66
edition.workspace = true
7-
publish = false
87

98
[lints]
109
workspace = true
File renamed without changes.

crates/c-api/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ license = "Apache-2.0 WITH LLVM-exception"
77
repository = "https://github.com/bytecodealliance/wasmtime"
88
readme = "README.md"
99
edition.workspace = true
10-
publish = false
10+
links = "wasmtime-c-api"
11+
include = ["include", "src", "wasm-c-api/include", "build.rs"]
1112

1213
[lints]
1314
workspace = true
1415

1516
[lib]
1617
name = "wasmtime_c_api"
17-
doc = false
1818
test = false
1919
doctest = false
2020

@@ -23,7 +23,7 @@ env_logger = { workspace = true, optional = true }
2323
anyhow = { workspace = true }
2424
once_cell = { workspace = true }
2525
wasmtime = { workspace = true, features = ['cranelift', 'runtime'] }
26-
wasmtime-c-api-macros = { path = "macros" }
26+
wasmtime-c-api-macros = { workspace = true }
2727
log = { workspace = true }
2828
tracing = { workspace = true }
2929

crates/c-api/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,43 @@
22

33
For more information you can find the documentation for this library
44
[online](https://bytecodealliance.github.io/wasmtime/c-api/).
5+
6+
## Using in a C Project
7+
8+
To use Wasmtime from a C or C++ project, you can use Cargo to build the Wasmtime C bindings. From the root of the Wasmtime repository, run the following command:
9+
10+
```
11+
cargo build --release wasmtime-c-api
12+
```
13+
14+
This will create static and dynamic libraries called `libwasmtime` in the `target/release` directory.
15+
16+
## Using in a Rust Project
17+
18+
If you have a Rust crate that contains bindings to a C or C++ library that uses Wasmtime, you can link the Wasmtime C API using Cargo.
19+
20+
1. Add a dependency on the `wasmtime-c-api-impl` crate to your `Cargo.toml`. Note that package name differs from the library name.
21+
22+
```toml
23+
[dependencies]
24+
wasmtime-c-api = { version = "16.0.0", package = "wasmtime-c-api-impl" }
25+
```
26+
27+
2. In your `build.rs` file, when compiling your C/C++ source code, add the C `wasmtime-c-api` headers to the include path:
28+
29+
```rust
30+
fn main() {
31+
let mut cfg = cc::Build::new();
32+
33+
// Add to the include path the wasmtime headers and the standard
34+
// Wasm C API headers.
35+
cfg
36+
.include(std::env::var("DEP_WASMTIME_C_API_INCLUDE").unwrap());
37+
.include(std::env::var("DEP_WASMTIME_C_API_WASM_INCLUDE").unwrap());
38+
39+
// Compile your C code.
40+
cfg
41+
.file("src/your_c_code.c")
42+
.compile("your_library");
43+
}
44+
```

crates/c-api/build.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() {
2+
let dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
3+
println!("cargo:include={dir}/include");
4+
println!("cargo:wasm_include={dir}/wasm-c-api/include");
5+
}

crates/c-api/src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
//! This crate is the implementation of Wasmtime's C API.
22
//!
3-
//! This crate is not intended to be used from Rust itself, for that see the
4-
//! `wasmtime` crate. Otherwise this is typically compiled as a
5-
//! cdylib/staticlib. Documentation for this crate largely lives in the header
3+
//! This crate is normally not intended to be used from Rust itself. For that,
4+
//! see the `wasmtime` crate. It is possible to use this crate via Cargo, for
5+
//! Rust crates that wrap C libraries that use wasmtime. Most often, this crate
6+
//! is compiled as a cdylib or staticlib, via the `wasmtime-c-api` crate.
7+
//!
8+
//! Documentation for this crate largely lives in the header
69
//! files of the `include` directory for this crate.
710
//!
811
//! At a high level this crate implements the `wasm.h` API with some gymnastics,

scripts/publish.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ const CRATES_TO_PUBLISH: &[&str] = &[
7171
"wasmtime-wasi-nn",
7272
"wasmtime-wasi-threads",
7373
"wasmtime-wast",
74+
"wasmtime-c-api-macros",
75+
"wasmtime-c-api-impl",
7476
"wasmtime-cli-flags",
7577
"wasmtime-explorer",
7678
"wasmtime-cli",

0 commit comments

Comments
 (0)