Skip to content

Add Zig bindings generator - #7

Merged
ykmnkmi merged 7 commits into
ykmnkmi:mainfrom
kingwill101:add-zig-bindings-generator
Aug 13, 2026
Merged

ykmnkmi merged 7 commits into
ykmnkmi:mainfrom
kingwill101:add-zig-bindings-generator

Conversation

@kingwill101

Copy link
Copy Markdown
Contributor

Hi, I actually built the same library and it wasn't until I tried publishing that I realized. I've opted to port the useful bits from my package here since you've already published.

This pr introduces a source-first generator that extracts exported Zig declarations and emits Dart FFI bindings.

Add a source-first generator that extracts exported Zig declarations and emits Dart FFI bindings. Include the CLI entrypoint, Zig metadata dumper, generator tests, public exports, and documentation for the workflow.
@kingwill101
kingwill101 force-pushed the add-zig-bindings-generator branch from a369ee5 to f8c7b3f Compare April 23, 2026 14:30
@ykmnkmi

ykmnkmi commented May 20, 2026

Copy link
Copy Markdown
Owner

Hi, thanks for your contribution and sorry for a delay. I'll look into it.

@ykmnkmi

ykmnkmi commented Jun 10, 2026

Copy link
Copy Markdown
Owner

@kingwill101 resolve merge conflicts.

@kingwill101

Copy link
Copy Markdown
Contributor Author

@ykmnkmi done. could you approve the workflow so we can verify the build works in ci?

@ykmnkmi

ykmnkmi commented Jun 25, 2026

Copy link
Copy Markdown
Owner

@kingwill101 just checking in. Do you need any help? Did you try compiling the Zig source with the package itself?

PS: Accidentally deleted the root README.md, it's restored now.

- Fix stale CI working-directory path: example/zig_dart_api/zig → example/dart_api/zig
- Resolve README.md merge conflict: keep generator workflow in step 6
@kingwill101

Copy link
Copy Markdown
Contributor Author

@ykmnkmi resolved. There is a known issue with the generator, it currently does not support exporting c types

➜  dart_api git:(add-zig-bindings-generator) ✗  dart run native_toolchain_zig:zig bindings \
  --zig-dir zig \
  --root-source-file src/lib.zig \
  --output lib/my_package.dart
Building package executable...
Built native_toolchain_zig:zig.
Bad state: Unsupported Zig named type: c.Dart_Port_DL

which i think is fine for now, zig types work well

allow the ci to run once more and we can wrap up.

@kingwill101

Copy link
Copy Markdown
Contributor Author

@ykmnkmi resolved. There is a known issue with the generator, it currently does not support exporting c types

➜  dart_api git:(add-zig-bindings-generator) ✗  dart run native_toolchain_zig:zig bindings \
  --zig-dir zig \
  --root-source-file src/lib.zig \
  --output lib/my_package.dart
Building package executable...
Built native_toolchain_zig:zig.
Bad state: Unsupported Zig named type: c.Dart_Port_DL

which i think is fine for now, zig types work well

allow the ci to run once more and we can wrap up.

I have a working fix but I'm not confident in it's current state. I'd prefer to land that as a separate pr

@ykmnkmi

ykmnkmi commented Jun 29, 2026

Copy link
Copy Markdown
Owner

All passed. I will take a final look and merge it.

Did you try to compile the Zig code using the package itself? I tried it before, but I got some errors.

Demonstrates generating Dart FFI bindings from Zig source code using
the native_toolchain_zig bindings generator.

- zig/src/counter.zig: example Zig library with exported functions and
  an extern struct
- hook/build.dart: build hook using ZigBuilder
- lib/ffi.g.dart: generated FFI bindings (7 functions, 1 struct)
- lib/bindings.dart: Dart-friendly wrapper around raw FFI
- bin/main.dart: demo of raw and wrapped bindings
- README with usage instructions for generating, running, and watch mode
@kingwill101
kingwill101 force-pushed the add-zig-bindings-generator branch from b84ff60 to 8443535 Compare July 23, 2026 14:27
@kingwill101

Copy link
Copy Markdown
Contributor Author

Tested again and everything runs locally. Also added a full bindings example.

generator doesn't work well on zig 0.16.0 though, will include a fix in the follow up work i have queued

should be able to merge now

@ykmnkmi
ykmnkmi merged commit f044341 into ykmnkmi:main Aug 13, 2026
2 checks passed
@ykmnkmi

ykmnkmi commented Aug 18, 2026

Copy link
Copy Markdown
Owner

Merged. I'll bump SDK to 3.13.0 and publish later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants