Repository navigation
Add Zig bindings generator #1788
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Snowiiii
wants to merge
4
commits into
bytecodealliance:main
Choose a base branch
from
Snowiiii:feat/zig-bindings
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
397e040
Add Zig bindings generator
Snowiiii c38d434
Address review feedback: add runtime tests, CI matrix, and docs
Snowiiii d6614b7
Merge remote-tracking branch 'upstream/main' into feat/zig-bindings
Snowiiii ff55200
Download Zig directly in CI and update codegen test filter
Snowiiii File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| use crate::{Compile, LanguageMethods, Runner, Verify}; | ||
| use anyhow::{Context, Result, bail}; | ||
| use std::fs; | ||
| use std::process::Command; | ||
|
|
||
| pub struct Zig; | ||
|
|
||
| impl LanguageMethods for Zig { | ||
| fn display(&self) -> &str { | ||
| "zig" | ||
| } | ||
|
|
||
| fn comment_prefix_for_test_config(&self) -> Option<&str> { | ||
| Some("//@") | ||
| } | ||
|
|
||
| fn should_fail_verify( | ||
| &self, | ||
| _runner: &Runner, | ||
| name: &str, | ||
| config: &crate::config::WitConfig, | ||
| _args: &[String], | ||
| ) -> bool { | ||
| config.async_ | ||
| || config.error_context | ||
| || name.contains("future") | ||
| || name.contains("stream") | ||
| || name.contains("map") | ||
| || name.contains("resource-borrow-in-record") | ||
| // Guest-exported resources and borrowed handles inside aggregates | ||
| // are not yet supported by `wit-bindgen-zig`. | ||
| || matches!( | ||
| name, | ||
| "import-and-export-resource.wit" | ||
| | "import-and-export-resource-alias.wit" | ||
| | "issue929.wit" | ||
| | "issue929-no-import.wit" | ||
| | "issue929-only-methods.wit" | ||
| | "issue1466.wit" | ||
| | "issue1658.wit" | ||
| | "issue-1658.wit" | ||
| | "issue1703.wit" | ||
| | "issue1731.wit" | ||
| | "resource-alias.wit" | ||
| | "resource-fallible-constructor.wit" | ||
| | "resource-local-alias.wit" | ||
| | "resource-local-alias-borrow.wit" | ||
| | "resources.wit" | ||
| | "resources-in-aggregates.wit" | ||
| | "resources-with-lists.wit" | ||
| ) | ||
| } | ||
|
|
||
| fn prepare(&self, runner: &mut Runner) -> Result<()> { | ||
| println!("Testing if `zig` toolchain exists..."); | ||
| if runner | ||
| .run_command(Command::new("zig").arg("version")) | ||
| .is_err() | ||
| { | ||
| bail!("`zig` toolchain not found"); | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn compile(&self, runner: &Runner, compile: &Compile<'_>) -> Result<()> { | ||
| let output = compile.output.with_extension("core.wasm"); | ||
| let wit_root = compile.bindings_dir.join("wit.zig"); | ||
|
|
||
| let mut cmd = Command::new("zig"); | ||
| cmd.arg("build-exe") | ||
| .arg("-target") | ||
| .arg("wasm32-wasi") | ||
| .arg("-O") | ||
| .arg("ReleaseSafe") | ||
| .arg("-fno-entry") | ||
| .arg("-rdynamic") | ||
| .arg("-mexec-model=reactor") | ||
| .arg("--dep") | ||
| .arg("wit") | ||
| .arg(format!("-Mroot={}", compile.component.path.display())) | ||
| .arg(format!("-Mwit={}", wit_root.display())) | ||
| .arg(format!("-femit-bin={}", output.display())); | ||
|
|
||
| runner.run_command(&mut cmd)?; | ||
|
|
||
| runner | ||
| .convert_core_module_to_component(&output, compile) | ||
| .with_context(|| format!("failed to convert {output:?}"))?; | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| fn verify(&self, runner: &Runner, verify: &Verify<'_>) -> Result<()> { | ||
| let check_zig = verify.artifacts_dir.join("check.zig"); | ||
| let wit_root = verify.bindings_dir.join("wit.zig"); | ||
| let tmp_obj = verify.artifacts_dir.join("check.o"); | ||
|
|
||
| let check_src = r#"const std = @import("std"); | ||
| const wit = @import("wit"); | ||
|
|
||
| fn collect(comptime T: type) []const *const anyopaque { | ||
| var out: []const *const anyopaque = &.{}; | ||
| for (std.meta.declarations(T)) |decl| { | ||
| const Decl = @TypeOf(@field(T, decl.name)); | ||
| if (Decl == type) { | ||
| const Inner = @field(T, decl.name); | ||
| switch (@typeInfo(Inner)) { | ||
| .@"struct", .@"enum", .@"union" => out = out ++ collect(Inner), | ||
| else => {}, | ||
| } | ||
| } else if (@typeInfo(Decl) == .@"fn" and !@typeInfo(Decl).@"fn".is_generic) { | ||
| out = out ++ [1]*const anyopaque{@ptrCast(&@field(T, decl.name))}; | ||
| } | ||
| } | ||
| return out; | ||
| } | ||
|
|
||
| const all = blk: { | ||
| @setEvalBranchQuota(10_000_000); | ||
| const list = collect(wit); | ||
| break :blk list[0..list.len].*; | ||
| }; | ||
|
|
||
| export const table: [all.len]*const anyopaque = all; | ||
| "#; | ||
| fs::write(&check_zig, check_src)?; | ||
|
|
||
| let mut cmd = Command::new("zig"); | ||
| cmd.arg("build-obj") | ||
| .arg("-target") | ||
| .arg("wasm32-wasi") | ||
| .arg("-O") | ||
| .arg("ReleaseSmall") | ||
| .arg("--dep") | ||
| .arg("wit") | ||
| .arg(format!("-Mroot={}", check_zig.display())) | ||
| .arg(format!("-Mwit={}", wit_root.display())) | ||
| .arg(format!("-femit-bin={}", tmp_obj.display())); | ||
|
|
||
| runner.run_command(&mut cmd)?; | ||
| Ok(()) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| [package] | ||
| name = "wit-bindgen-zig" | ||
| version = { workspace = true } | ||
| edition = { workspace = true } | ||
| repository = { workspace = true } | ||
| license = { workspace = true } | ||
| rust-version = { workspace = true } | ||
| description = """ | ||
| Zig bindings generator for WIT and the component model, typically used through | ||
| the `wit-bindgen-cli` crate. | ||
| """ | ||
|
|
||
| [lints] | ||
| workspace = true | ||
|
|
||
| [lib] | ||
| doctest = false | ||
| test = false | ||
|
|
||
| [dependencies] | ||
| anyhow = { workspace = true } | ||
| clap = { workspace = true, optional = true } | ||
| indexmap = { workspace = true } | ||
| wit-bindgen-core = { workspace = true } | ||
|
|
||
| [features] | ||
| clap = ['dep:clap', 'wit-bindgen-core/clap'] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
async_should cover future and stream already.And that I believe that
resource-borrow-in-recorderror should be fixed, not ignored.