Skip to content
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

Add build.zig #198

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open

Add build.zig #198

wants to merge 20 commits into from

Conversation

desttinghim
Copy link
Contributor

These commits add a build.zig to libui. This allows for cross-compilation using the zig toolchain (compiling for windows from linux is as easy as zig build -Dtarget=x86_64-windows) and for using zig's built-in package manager (currently only in the nightly build) to directly depend on libui-ng.

The build.zig has not been configured to build for MacOS, though this should be easy to add.

It might be best to hold off on merging this until there is a stable release of zig including the package manager. The current expected release date is May.

I'm happy accepting constructive criticism on this PR; if you don't feel like merging the build.zig, that's fine - I mostly wrote it for a private project, and I am making a PR just in case it would be appreciated :)

@cody271
Copy link
Contributor

cody271 commented Apr 28, 2023

These commits add a build.zig to libui. This allows for cross-compilation using the zig toolchain

Hi @desttinghim, this looks neat!

I'd be particularly interested in using this to cross-compile the Go bindings. Is this possible?

The build.zig has not been configured to build for MacOS, though this should be easy to add.

Yes support for all three platforms would be required to merge this.

build.zig Outdated Show resolved Hide resolved
@desttinghim
Copy link
Contributor Author

These commits add a build.zig to libui. This allows for cross-compilation using the zig toolchain

Hi @desttinghim, this looks neat!

I'd be particularly interested in using this to cross-compile the Go bindings. Is this possible?

Yes, I believe so. I have no experience with using go myself, but there have been some posts on using zig cc for cross-compiling cgo binaries: https://zig.news/kristoff/building-sqlite-with-cgo-for-every-os-4cic

The build.zig has not been configured to build for MacOS, though this should be easy to add.

Yes support for all three platforms would be required to merge this.

This makes sense. I don't have any Apple devices I can use to test, but I'll push a build.zig that should theoretically work, if someone would like to test it.

@cody271
Copy link
Contributor

cody271 commented Apr 30, 2023

This PR produced the following error when I tried testing, same error on both Mac and Linux:

$ zig build
build.zig:249:18: error: expected ')', found ','
    for (examples, example_names) |example, name| {
                 ^

And if I try removing the code section for the examples, I then get this error:

$ zig build
build.zig:3:21: error: root struct of file 'std' has no member named 'Build'
pub fn build(b: *std.Build) void {
                 ~~~^~~~~~

@cody271
Copy link
Contributor

cody271 commented Apr 30, 2023

and for using zig's built-in package manager (currently only in the nightly build) to directly depend on libui-ng.

Okay I'm getting past the above build.zig compile errors by using the nightly build of Zig.
So this build file doesn't appear to work against the latest stable release of Zig at all?

Build still fails though: Now seeing lots of Objective-C runtime related link errors on Mac..

@desttinghim
Copy link
Contributor Author

Yes, this is using zig nightly. There have been a lot of changes since the latest stable release. I plan to stick to using zig 0.11 when it is released to make it easier for others to follow.

Could you post the errors you are seeing?

Copy link
Contributor

@cody271 cody271 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I fixed it:

build.zig Outdated Show resolved Hide resolved
build.zig Outdated Show resolved Hide resolved
@cody271
Copy link
Contributor

cody271 commented May 1, 2023

I mostly wrote it for a private project, and I am making a PR just in case it would be appreciated :)

Curious are you also working on Zig bindings, or using the C API directly from Zig?
Would be great to have Zig bindings to add to the list.. ;)

@desttinghim
Copy link
Contributor Author

I am using the C API directly at the moment. It wouldn't be terribly difficult to wrap into a more ziggish API but wrapping it is not a priority for me at the moment :) that being said, I wouldn't mind working on a set of bindings when I have the time.

@cody271
Copy link
Contributor

cody271 commented May 3, 2023

that being said, I wouldn't mind working on a set of bindings when I have the time.

Even just our hello-world example in Zig (with the C API directly and an example build file) would be quite helpful to those trying to get started with a new app in Zig.

build.zig Show resolved Hide resolved
build.zig Outdated Show resolved Hide resolved
@desttinghim
Copy link
Contributor Author

@cody271now that zig 0.11.0 has been released, I am interested in getting this into a mergeable state. The 0.11.0 release is the first stable version of zig to include a package manager. Using the nightlies would be difficult due to the frequent changes - pinning to a stable release makes it much easier to provide support.

windows/winutil.cpp Outdated Show resolved Hide resolved
Copy link
Contributor

@cody271 cody271 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few more nits / suggestions

CHANGELOG.md Outdated Show resolved Hide resolved
.gitignore Outdated Show resolved Hide resolved
.gitignore Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
@cody271
Copy link
Contributor

cody271 commented Oct 26, 2023

Is this still intended to work with the recent Zig stable release, or does it need dev nightly?

Packages for 0.11.0 have now been released for most systems.

@desttinghim
Copy link
Contributor Author

The new commits I pushed are not for 0.11.0, unfortunately. I updated to a nightly version of zig while troubleshooting a libui project that was failing to run on windows. The nightly version integrates a windows resource compiler into zig which simplifies getting a toolchain set up for windows significantly. I can move the most recent commit to a new branch and roll back to 0.11.0 here if you'd like.

@cody271

This comment was marked as resolved.

@kojix2 kojix2 mentioned this pull request Nov 1, 2023
@cody271
Copy link
Contributor

cody271 commented Nov 5, 2023

I updated to a nightly version of zig while troubleshooting a libui project that was failing to run on windows.
The nightly version integrates a windows resource compiler into zig which simplifies getting a toolchain set up for windows significantly.

After having actually tried running this on a new machine, changed my mind: you had it right the first time. 😛
Requiring a third party resource compiler is very painful.. using the builtin Zig resource compiler is a much better idea.
As long as the docs are updated to require 0.12.0, I think its okay.

@desttinghim
Copy link
Contributor Author

Ok, this is back to the build.zig for 0.12.0. Hopefully there aren't too many breaking changes before the release 😅 If we want to guarantee support for the latest nightly at all times, we will probably want to set up CI to download the latest version and run zig build. I know other zig projects have something like that set up, though I haven't personally done it. Otherwise we can just pin to a known working version and update when a stable version is released.

@cody271
Copy link
Contributor

cody271 commented Dec 1, 2023

Ok, this is back to the build.zig for 0.12.0.

Otherwise we can just pin to a known working version and update when a stable version is released.

Yes the hope is that the upcoming 0.12.0 stable release from packages will just work, without depending on nightly download..

@desttinghim
Copy link
Contributor Author

Another useful update has been merged into zig: ziglang/zig#18160

The short version is that zig modules with a c dependency will now forward that dependency. Previously all the c dependencies had to be added for each module you wanted to depend on it.

I will update this branch to make use of the new build feature update the documentation on which zig version to use.

On a related note, should I try and add the zig build step to the CI?

desttinghim and others added 13 commits March 20, 2024 15:30
This commit squashes several other commits into one. It introduces a
build.zig script for compiling libui. This allows easy cross-compilation
(at least between Linux and Windows). Additionally, a small change is
made to some of the Windows code to detect the
WIDL_EXPLICIT_AGGREGATE_RETURNS macro and work around an ABI bug between
clang and msvc.
Co-authored-by: cody271 <cody271@hey.com>
Co-authored-by: cody271 <cody271@hey.com>
Adds `zig-linux` step to CI to test build.zig on linux.

Changes build.zig to accept `libtype={static,shared}` option instead of
using `shared={true,false}` so it is more consistent with the other
github actions.
Adds the tests to the CI using zig as well.
This reverts commit f56ae5f. The
zig-setup action does not confirm the signature of the downloaded
zig files.
cmocka is not packaged for the zig build system
Running the build with zig in ci will be added in a later pull request.
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.

None yet

3 participants