Skip to content

Commit 7b229bb

Browse files
committed
support combining SDKs with multiple WIT worlds
This adds two new features: - Multiple `--world` options are accepted, in which case the worlds will be merged into a single world for building, testing, or bindings generation. - In addition to any worlds and/or WIT paths specified explicitly via CLI options, `componentize-go` will use `go list` to scan the current module and its dependencies, looking for files named `componentize-go.toml` in their source trees. For each such file found, any worlds and/or WIT paths referenced therein will be added to the respective lists. This behavior can be disabled using the `--ignore-toml-files` option if desired. In combination, these options make it easier to use multiple SDKs or other libraries which contain `wit-bindgen-go`-generated code. Note that I've also removed the `wat` dependency because `go build` always generates a binary Wasm file, so no need to check for or parse WAT. Fixes #28
1 parent f365a7a commit 7b229bb

5 files changed

Lines changed: 199 additions & 54 deletions

File tree

Cargo.lock

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

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ allow_attributes_without_reason = 'warn'
3737
anyhow = { workspace = true}
3838
clap = { version = "4.5.60", features = ["derive"] }
3939
regex = "1.12.3"
40-
wat = { version = "1.245.1"}
40+
serde = { version = "1.0.228", features = ["derive"] }
41+
toml = "1.1.0"
4142
wit-bindgen-go = { git = "https://github.com/bytecodealliance/wit-bindgen", rev = "3ee9fe20a5bce398360d5d291e81a4224a6d7c76" }
4243
wit-component = "0.245.1"
4344
wit-parser = "0.245.1"

src/cmd_bindings.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@ use std::path::{Path, PathBuf};
44

55
#[allow(clippy::too_many_arguments)]
66
pub fn generate_bindings(
7-
wit_path: &[impl AsRef<Path>],
8-
world: Option<&str>,
7+
paths: &[impl AsRef<Path>],
8+
worlds: &[String],
9+
ignore_toml_files: bool,
910
features: &[String],
1011
all_features: bool,
1112
generate_stubs: bool,
1213
should_format: bool,
1314
output: Option<&Path>,
1415
pkg_name: Option<String>,
1516
) -> Result<()> {
16-
let (mut resolve, world) = parse_wit(wit_path, world, features, all_features)?;
17+
let (mut resolve, world) = parse_wit(paths, worlds, ignore_toml_files, features, all_features)?;
1718
let mut files = Default::default();
1819

1920
let format = if should_format {

src/command.rs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,29 @@ pub struct WitOpts {
2828
///
2929
/// These paths can be either directories containing `*.wit` files, `*.wit`
3030
/// files themselves, or `*.wasm` files which are wasm-encoded WIT packages.
31+
///
32+
/// Note that, unless `--ignore-toml-files` is specified, `componentize-go`
33+
/// will also use `go list` to scan the current Go module and its
34+
/// dependencies to find any `componentize-go.toml` files. The WIT
35+
/// documents referenced by any such files will be added to this list
36+
/// automatically.
3137
#[arg(long, short = 'd')]
3238
pub wit_path: Vec<PathBuf>,
3339

34-
/// Name of world to target (or default world if `None`).
40+
/// Name of world to target (or default world if not specified).
41+
///
42+
/// This may be specified more than once, in which case the worlds will be
43+
/// merged.
44+
///
45+
/// Note that, unless `--ignore-toml-files` is specified, `componentize-go`
46+
/// will also use `go list` to scan the current Go module and its
47+
/// dependencies to find any `componentize-go.toml` files. The WIT worlds
48+
/// referenced by any such files will be added to this list automatically.
3549
#[arg(long, short = 'w')]
36-
pub world: Option<String>,
50+
pub world: Vec<String>,
51+
52+
#[arg(long)]
53+
pub ignore_toml_files: bool,
3754

3855
/// Whether or not to activate all WIT features when processing WIT files.
3956
///
@@ -164,13 +181,14 @@ fn build(wit_opts: WitOpts, build: Build) -> Result<()> {
164181
embed_wit(
165182
&module,
166183
&wit_opts.wit_path,
167-
wit_opts.world.as_deref(),
184+
&wit_opts.world,
185+
wit_opts.ignore_toml_files,
168186
&wit_opts.features,
169187
wit_opts.all_features,
170188
)?;
171189

172190
// Update the wasm module to use the current component model ABI.
173-
module_to_component(&module, &build.adapt)?;
191+
module_to_component(&module, build.adapt.as_deref())?;
174192
}
175193

176194
Ok(())
@@ -190,13 +208,14 @@ fn test(wit_opts: WitOpts, test: Test) -> Result<()> {
190208
embed_wit(
191209
&module,
192210
&wit_opts.wit_path,
193-
wit_opts.world.as_deref(),
211+
&wit_opts.world,
212+
wit_opts.ignore_toml_files,
194213
&wit_opts.features,
195214
wit_opts.all_features,
196215
)?;
197216

198217
// Update the wasm module to use the current component model ABI.
199-
module_to_component(&module, &test.adapt)?;
218+
module_to_component(&module, test.adapt.as_deref())?;
200219
}
201220
}
202221

@@ -205,8 +224,9 @@ fn test(wit_opts: WitOpts, test: Test) -> Result<()> {
205224

206225
fn bindings(wit_opts: WitOpts, bindings: Bindings) -> Result<()> {
207226
generate_bindings(
208-
wit_opts.wit_path.as_ref(),
209-
wit_opts.world.as_deref(),
227+
&wit_opts.wit_path,
228+
&wit_opts.world,
229+
wit_opts.ignore_toml_files,
210230
&wit_opts.features,
211231
wit_opts.all_features,
212232
bindings.generate_stubs,

0 commit comments

Comments
 (0)