Skip to content

zig api: (WIP) add a Zig API/bindings for libremidi, also add the posibility to build using the Zig Build System #158

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

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from

Conversation

lelahx
Copy link

@lelahx lelahx commented May 30, 2025

Overview

This PR provides two things:

  • a complete mapping of the C API into Zig, along with a translated example. This should match the functionality of the C API, but is still untested and awaiting feedback
  • a WIP implementation of the CMake build logic into the Zig Build System. This still has a lot of broken parts, so needs work, testing and feedback

Zig API

The bindings make use of Zig's language features, such as namespacing, slices, methods, optionals and more in order to provide an idiomatic Zig API. As such, most C functions are thinly wrapped to provide a better experience, some are provided as-is. Some safety is also gained by means of better type information for pointer arguments.
This means that the wrapper has a non-zero runtime cost, but it should be on the same order of magnitude than what the C wrapper already does. Also, the C API example has been directly translated to Zig using the new API, and as such becomes much more readable in the process.

Zig Build System

There now are build.zig & build.zig.zon files at the root of the tree (necessary for the library to be usable from the Zig user side). This is currently able to build the C API on Linux with :

All the other options are either broken or unimplemented for now. One big blocker is that I haven't been able to make Zig build the CPP library standalone yet. Also that CMake is really hard to grok :).
Once this is solved, adding compilation of all examples and tests should be smooth sailing, in theory (famous last words).

TL;DR

I would appreciate any feedback and especially some help on the build system. Once all this is done I can add CI tests and I think it'd be ready for merging :D

Cheers!

Update 1

  • Big blocker lifted: figured out some bugs in ZBS, got libremidi to compile without header-only, hence the build is now properly distributed across cpu cores and artifacts are reused wherever possible
  • implemented winmm backend logic, windows builds seem to work now
  • I think ni-midi2 is now properly built and linked? However I can't confirm because I still can't build the corresponding example

…sibility to build using the Zig Build System

This commit adds Zig bindings for libremidi and implements a subset
of the build logic in Zig's build system.

At this point, I'm satisfied with the API I landed on for the bindings,
now awaiting feedback!

Work still needed on build logic.
@lelahx lelahx force-pushed the master branch 2 times, most recently from f01bc61 to 202dedf Compare May 30, 2025 17:37
@jcelerier jcelerier self-assigned this Jun 3, 2025
@jcelerier
Copy link
Member

thanks, that's amazing work! :)

@lelahx
Copy link
Author

lelahx commented Jun 3, 2025

Just realized I can probably improve the API a little bit by getting rid of the midi.in.port.Handle & midi.out.port.Handle structs, instead refactoring the port namespace/struct (same thing in Zig) into a Port opaque struct which would effectively take the place of Handle and host its methods.

Let me know what you think!

@lelahx
Copy link
Author

lelahx commented Jun 3, 2025

Also, is there a way I/you can disable auto CI runs on every commit? Until I actually add CI scripts for the Zig Build System this is just going to be a waste of compute...

@jcelerier
Copy link
Member

You can add [ci skip] at the beginning of your commit messages

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