Skip to content

Inital zig build support and integrating it with the CI #982

Open
Avokadoen wants to merge 5 commits intoerincatto:mainfrom
Avokadoen:main
Open

Inital zig build support and integrating it with the CI #982
Avokadoen wants to merge 5 commits intoerincatto:mainfrom
Avokadoen:main

Conversation

@Avokadoen
Copy link
Copy Markdown

@Avokadoen Avokadoen commented Sep 14, 2025

This PR adds a build.zig + build.zig.zon. It also add jobs in the CI to build with zig for windows and linux

This means there is now support for building box2d as a lib and enabling any zig build users (including other C/C++ projects) to trivially call zig fetch --save git+https://github.com/erincatto/box2d and start using it!

TODOs:

  • Building as lib for zig fetch
  • Building unit tests
  • option BOX2D_DISABLE_SIMD
  • option BOX2D_AVX2
  • option BOX2D_DOCS
  • option BOX2D_PROFILE

Out of PR scope?
option BOX2D_SAMPLES
option BOX2D_BENCHMARKS

@Avokadoen Avokadoen marked this pull request as draft September 14, 2025 13:19
@Avokadoen
Copy link
Copy Markdown
Author

Avokadoen commented Sep 14, 2025

Before I go ahead and do additional changes I would like some input from you @erincatto:

  1. Are you still interested in hosting a build.zig for box2d?
  2. If yes, would it require supporting building samples and tests, or simply serve as a utility for zig users to use box2d as a library?

@Avokadoen Avokadoen changed the title DRAFT/WIP: zig build DRAFT/WIP: zig build and CI Sep 14, 2025
@erincatto
Copy link
Copy Markdown
Owner

erincatto commented Sep 15, 2025

Update: given the problems with enkiTS and my lack of knowledge of zig, it is probably best if the zig script does the minimal thing that helps users while still compiling the core library in CI.

@Avokadoen
Copy link
Copy Markdown
Author

Ok. I'll make this PR do the following when I have time:

  1. Allow zig users fetch and link with box2d
  2. Test building on windows and Linux

I have the unit tests compiling and passing with the exception of deterministic one. I'll raise an issue in enkiTS to add support for zig build as well. Fixing the remaining test is trivial at that point.

@Avokadoen Avokadoen force-pushed the main branch 2 times, most recently from d8d5f09 to cea29db Compare September 15, 2025 05:56
martindrlik and others added 2 commits September 15, 2025 08:07
This adds support for building box2d as a library
for zig 0.14.1
@Avokadoen Avokadoen changed the title DRAFT/WIP: zig build and CI Inital zig build support and integrating it with the CI Sep 15, 2025
@Avokadoen Avokadoen marked this pull request as ready for review September 15, 2025 06:12
@Avokadoen
Copy link
Copy Markdown
Author

Avokadoen commented Sep 15, 2025

Created enkiTS#142

@Avokadoen
Copy link
Copy Markdown
Author

dougbinks/enkiTS#143

@Avokadoen Avokadoen marked this pull request as draft September 16, 2025 20:17
@Avokadoen
Copy link
Copy Markdown
Author

Converting to draft until enkiTS PR is merged

@Avokadoen
Copy link
Copy Markdown
Author

Avokadoen commented Sep 16, 2025

Seems like the built determinism tests fails on both:

ENSURE( data.sleepStep == EXPECTED_SLEEP_STEP );
ENSURE( data.hash == EXPECTED_HASH );

@Avokadoen
Copy link
Copy Markdown
Author

@erincatto are you suuuuure the test is deterministic? 😅

Not quite sure how a build script can cause a race condition in these tests. Am I maybe missing some flags or macros?

@Avokadoen Avokadoen force-pushed the main branch 2 times, most recently from fe97e23 to 653e3ac Compare September 16, 2025 21:47
@erincatto
Copy link
Copy Markdown
Owner

Does zig have some flags for fast math? If the compiler introduces contractions (e.g. fused multiply-add) then determinism will fail. This needs to pass single-threaded before considering multi-threaded determinism.

@Avokadoen
Copy link
Copy Markdown
Author

Avokadoen commented Sep 17, 2025

Issue was that I was missing -ffp-contract=off for the shared lib! 🤦

.shared = false,
.unit_tests = false,
.disable_simd = false,
.avx2 = true,
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Not sure this is desired, or avx2 should always be explicitly enabled.
Current logic will always enabled avx2 if features is supported by compile target, it will always be false if not supported.

@Avokadoen Avokadoen marked this pull request as ready for review October 5, 2025 15:49
@Avokadoen
Copy link
Copy Markdown
Author

Avokadoen commented Oct 5, 2025

@erincatto I think this is ready, before you take a look. These are the following options I did not implement:

  • BOX2D_DOCS
  • BOX2D_PROFILE
  • BOX2D_SAMPLES
  • BOX2D_BENCHMARKS

It seems like users can lean on cmake for these. I doesnt make sense to support everything for both build infrastructures in my mind:

I also changed the CI to utilize a matrix strategy to remove some of the boilerplate in the .github folder

@Avokadoen Avokadoen force-pushed the main branch 5 times, most recently from fab38c8 to 30a70a8 Compare October 5, 2025 16:03
Add
 - disable_simd (BOX2D_DISABLE_SIMD)
 - avx2 (BOX2D_AVX2)

Additionally update the zon enkiTS reference to point upstream!
@Avokadoen
Copy link
Copy Markdown
Author

Avokadoen commented Oct 5, 2025

Sorry for the spam 😮‍💨 (was wrestling github ci config)

@Avokadoen Avokadoen force-pushed the main branch 2 times, most recently from a3be7ed to 1b17d27 Compare October 6, 2025 05:57
@erincatto
Copy link
Copy Markdown
Owner

Can the number of CI steps be reduced and still have reasonable confirmation of compatibility?

@Avokadoen
Copy link
Copy Markdown
Author

Avokadoen commented Oct 19, 2025

Yes, each permutation gets its own run in the UI.
I have a test run on my fork which should be viewable to anyone: https://github.com/Avokadoen/box2d/actions/runs/18271466024

@erincatto
Copy link
Copy Markdown
Owner

Can we reduce the number of stages to 1 or 2?

Imagine 10 years from now I have Odin, Swift, MinGW, Zig, Jai, etc. If I have 100 stages this is going to be quite unpleasant.

@Avokadoen
Copy link
Copy Markdown
Author

Avokadoen commented Oct 20, 2025

Sure I'll get to it later today. I'll reduce it to 1 windows + 1 Linux target

Imagine 10 years from now I have Odin, Swift, MinGW, Zig, Jai, etc. If I have 100 stages this is going to be quite unpleasant.

You forgot to mention rust!

@Avokadoen
Copy link
Copy Markdown
Author

Avokadoen commented Oct 20, 2025

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.

3 participants