Skip to content

Update for zig 0.17 #1122

Closed
Closed

Description

@Avokadoen
Update zig to 0.17

Not much changed in 0.17 and the current state of zig integration should
work without any changes. However, zig 0.17 deprecates the built in
translate c functionality[0] in favor of an external package[1] (also
maintained by the zig team).

[0]: https://ziglang.org/download/0.17.0/release-notes.html#toc-C-Translation-Moving-to-External-Package
[1]: https://codeberg.org/ziglang/translate-c

Diff

diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 512def3..7075a60 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -271,7 +271,7 @@ jobs:
     - name: Setup zig
       uses: mlugg/setup-zig@v2
       with:
-        version: 0.16.0
+        version: 0.17.0
 
     - name: Build
       run: zig build -Dunit_tests=true -Doptimize=ReleaseFast
diff --git a/build.zig b/build.zig
index 7acd932..ce5f354 100644
--- a/build.zig
+++ b/build.zig
@@ -5,13 +5,7 @@ const ResolvedTarget = Build.ResolvedTarget;
 const OptimizeMode = std.builtin.OptimizeMode;
 const builtin = @import("builtin");
 
-const min_supported_ver = "0.15.1";
-comptime {
-    const order = std.SemanticVersion.order;
-    const parse = std.SemanticVersion.parse;
-    if (order(builtin.zig_version, parse(min_supported_ver) catch unreachable) == .lt)
-        @compileError("Box2d requires zig version " ++ min_supported_ver);
-}
+const Translator = @import("translate_c").Translator;
 
 pub const Options = struct {
     shared: bool,
@@ -53,14 +47,20 @@ pub fn build(b: *Build) !void {
     const lib = try compileBox2d(b, target, optimize, options);
 
     // Translate the box2d headers and export them as a module
-    const translate_c = b.addTranslateC(.{
-        .root_source_file = b.path("include/box2d/box2d.h"),
+    const translate_c = b.dependency("translate_c", .{});
+    const translator: Translator = .init(translate_c, .{
+        .name = "translate_box2d",
+        .c_source_file = b.path("include/box2d/box2d.h"),
         .target = target,
         .optimize = optimize,
     });
-    translate_c.addIncludePath(b.path("include"));
-    const translate_module = translate_c.addModule("box2d");
-    translate_module.linkLibrary(lib);
+    translator.addIncludePath(b.path("include"));
+
+    const module = b.addModule("box2d", .{
+        .root_source_file = translator.output_file,
+    });
+
+    module.linkLibrary(lib);
 
     b.installArtifact(lib);
 
diff --git a/build.zig.zon b/build.zig.zon
index b7244da..6a3a65f 100644
--- a/build.zig.zon
+++ b/build.zig.zon
@@ -1,7 +1,13 @@
 .{
     .name = .box2d,
     .version = "3.2.0",
-    .minimum_zig_version = "0.16.0",
+    .dependencies = .{
+        .translate_c = .{
+            .url = "git+https://codeberg.org/ziglang/translate-c#875969d3493e245e01bf5d7860f792d8f3eb9ef5",
+            .hash = "translate_c-0.0.0-Q_BUWoFOBwAhz77Zd15HCVuhTKzdUKc94kezmCeJ7IC_",
+        },
+    },
+    .minimum_zig_version = "0.17.0",
     .fingerprint = 0xbbd8fcbd18ea5e0b,
     .paths = .{
         "build.zig",

Activity

  1. erincatto commented on Oct 8, 2026

    @erincatto
    Owner

    Please make a Box2D fork that demonstrates this working in CI.

  2. Avokadoen commented on Oct 12, 2026

    @Avokadoen
    Author
  3. erincatto commented on Oct 12, 2026

    @erincatto
    Owner

    Thanks, I'll take a look.

  4. erincatto commented on Oct 18, 2026

    @erincatto
    Owner

    I don't know much about zig so I had an LLM review. This is what it says:

    1. The translate-c pin is from main and requires zig 0.18-dev. For 0.17, use the 2.0.2 tag on the zig-0.17.x branch.
    2. CI doesn't run the translation because nothing imports the box2d module. Please add a small zig test that imports it.
    3. addTranslateC is only deprecated in 0.17. If you'd rather switch now, please make the dependency lazy and use translator.mod. Otherwise just bumping the CI version is enough for now.
  5. Avokadoen commented on Oct 18, 2026

    @Avokadoen
    Author

    I don't have access to my PC the coming days, but I'll fix it during the week

    1. Seem valid to pin it
    2. True, I test it manually but a test is a good idea
    3. Lazy dependency is valid.

    Lazy dependency seems non trivial for translate_c. It is not very well documented API atm either so it is hard to figure out how to use it in this context. Given that zig users will ALWAYS need translate_c and CI will always need it as well not that there is a zig test I thought it makes sense to not go this route anyways. The build script also got a bit more convoluted as we must handle not having translate_c when compiling zig tests (by emitting an error). It could be beneficial to c users that want to use zig build (there are dusins of us!), but if someone complains I might reconsider

  6. Avokadoen commented on Oct 20, 2026

    @Avokadoen
    Author

    Passing CI: https://github.com/Avokadoen/box2d/actions/runs/39489993890

    Commit message:

        Update zig to 0.17
        
        Not much changed in 0.17 and the current state of zig integration should
        work without changes. However, zig 0.17 deprecates the built in
        translate c functionality[0] in favor of a external package[1] (also
        maintained by the zig team).
        
        There is also a new unit test that is written in zig to ensure that
        translate_c is working as intended.
        
        [0]: https://ziglang.org/download/0.17.0/release-notes.html#toc-C-Translation-Moving-to-External-Package
        [1]: https://codeberg.org/ziglang/translate-c
    diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
    index 512def3..effdad0 100644
    --- a/.github/workflows/build.yml
    +++ b/.github/workflows/build.yml
    @@ -271,13 +271,16 @@ jobs:
         - name: Setup zig
           uses: mlugg/setup-zig@v2
           with:
    -        version: 0.16.0
    +        version: 0.17.0
     
         - name: Build
    -      run: zig build -Dunit_tests=true -Doptimize=ReleaseFast
    +      run: zig build -Dc_unit_tests=true -Dzig_unit_tests=true -Doptimize=ReleaseFast
     
    -    - name: Test
    -      run: ${{github.workspace}}/zig-out/bin/test
    +    - name: C Test
    +      run: ${{github.workspace}}/zig-out/bin/c_test
    +
    +    - name: Zig Test
    +      run: ${{github.workspace}}/zig-out/bin/zig_test
     
       swift-package-manager:
         name: swift-spm
    diff --git a/build.zig b/build.zig
    index 7acd932..4cb5311 100644
    --- a/build.zig
    +++ b/build.zig
    @@ -2,26 +2,23 @@ const std = @import("std");
     const Build = std.Build;
     const Compile = Build.Step.Compile;
     const ResolvedTarget = Build.ResolvedTarget;
    +const Module = Build.Module;
     const OptimizeMode = std.builtin.OptimizeMode;
     const builtin = @import("builtin");
     
    -const min_supported_ver = "0.15.1";
    -comptime {
    -    const order = std.SemanticVersion.order;
    -    const parse = std.SemanticVersion.parse;
    -    if (order(builtin.zig_version, parse(min_supported_ver) catch unreachable) == .lt)
    -        @compileError("Box2d requires zig version " ++ min_supported_ver);
    -}
    +const Translator = @import("translate_c").Translator;
     
     pub const Options = struct {
         shared: bool,
    -    unit_tests: bool,
    +    c_unit_tests: bool,
    +    zig_unit_tests: bool,
         disable_simd: bool,
         avx2: bool,
     
         const defaults = Options{
             .shared = false,
    -        .unit_tests = false,
    +        .c_unit_tests = false,
    +        .zig_unit_tests = false,
             .disable_simd = false,
             .avx2 = true,
         };
    @@ -39,7 +36,8 @@ pub const Options = struct {
     
             return .{
                 .shared = b.option(bool, "shared", "Compile as shared library") orelse defaults.shared,
    -            .unit_tests = b.option(bool, "unit_tests", "Compile unit tests") orelse defaults.unit_tests,
    +            .c_unit_tests = b.option(bool, "c_unit_tests", "Compile the c unit tests") orelse defaults.c_unit_tests,
    +            .zig_unit_tests = b.option(bool, "zig_unit_tests", "Compile the zig unit tests, will always be false if 'disable_zig_target' is set") orelse defaults.zig_unit_tests,
                 .disable_simd = disable_simd,
                 .avx2 = avx2,
             };
    @@ -52,20 +50,31 @@ pub fn build(b: *Build) !void {
         const options = Options.getOptions(b, target);
         const lib = try compileBox2d(b, target, optimize, options);
     
    +    b.installArtifact(lib);
    +
    +    if (options.c_unit_tests) {
    +        buildCTests(b, target, optimize, lib);
    +    }
    +
         // Translate the box2d headers and export them as a module
    -    const translate_c = b.addTranslateC(.{
    -        .root_source_file = b.path("include/box2d/box2d.h"),
    +    const translate_c = try b.dependencyLazy("translate_c", .{});
    +    const translator: Translator = .init(translate_c, .{
    +        .name = "translate_box2d",
    +        .c_source_file = b.path("include/box2d/box2d.h"),
             .target = target,
             .optimize = optimize,
         });
    -    translate_c.addIncludePath(b.path("include"));
    -    const translate_module = translate_c.addModule("box2d");
    -    translate_module.linkLibrary(lib);
    +    translator.addIncludePath(b.path("include"));
     
    -    b.installArtifact(lib);
    +    const translate_c_mod = b.addModule("box2d", .{
    +        .root_source_file = translator.output_file,
    +    });
    +
    +    translate_c_mod.linkLibrary(lib);
     
    -    if (options.unit_tests) {
    -        buildTests(b, target, optimize, lib);
    +    if (options.zig_unit_tests) {
    +        // translate_c_mod is already asserted to always be present during the option stage of build
    +        buildZigTest(b, target, optimize, translate_c_mod);
         }
     }
     
    @@ -173,7 +182,7 @@ fn compileBox2d(b: *Build, target: ResolvedTarget, optimize: OptimizeMode, optio
         return box2d;
     }
     
    -fn buildTests(b: *Build, target: ResolvedTarget, optimize: OptimizeMode, box2d_lib: *Compile) void {
    +fn buildCTests(b: *Build, target: ResolvedTarget, optimize: OptimizeMode, box2d_lib: *Compile) void {
         const test_module = b.createModule(.{
             .target = target,
             .optimize = optimize,
    @@ -243,7 +252,7 @@ fn buildTests(b: *Build, target: ResolvedTarget, optimize: OptimizeMode, box2d_l
         shared_module.linkLibrary(box2d_lib);
     
         const test_exe = b.addExecutable(.{
    -        .name = "test",
    +        .name = "c_test",
             .root_module = test_module,
         });
     
    @@ -252,3 +261,24 @@ fn buildTests(b: *Build, target: ResolvedTarget, optimize: OptimizeMode, box2d_l
     
         b.installArtifact(test_exe);
     }
    +
    +// Build a simple zig test to validate that translate_c works as expected
    +fn buildZigTest(b: *Build, target: ResolvedTarget, optimize: OptimizeMode, box2d_lib: *Module) void {
    +    const mod = b.addModule(
    +        "zig_test_mod",
    +        .{
    +            .root_source_file = b.path("test/zig/test_world.zig"),
    +            .target = target,
    +            .optimize = optimize,
    +        },
    +    );
    +
    +    mod.addImport("box2d", box2d_lib);
    +
    +    const zig_test = b.addTest(.{
    +        .name = "zig_test",
    +        .root_module = mod,
    +    });
    +
    +    b.installArtifact(zig_test);
    +}
    diff --git a/build.zig.zon b/build.zig.zon
    index b7244da..c0a60a0 100644
    --- a/build.zig.zon
    +++ b/build.zig.zon
    @@ -1,7 +1,13 @@
     .{
         .name = .box2d,
         .version = "3.2.0",
    -    .minimum_zig_version = "0.16.0",
    +    .dependencies = .{
    +        .translate_c = .{
    +            .url = "git+https://codeberg.org/ziglang/translate-c?ref=zig-0.17.x#cfac21f3199b2b02ce3fd7db652aa456421fe4ab",
    +            .hash = "translate_c-2.0.2-Q_BUWvBNBwDnyehSiuAJmdFcMYHYGA-Jc3KiYdrV2Dqx",
    +        },
    +    },
    +    .minimum_zig_version = "0.17.0",
         .fingerprint = 0xbbd8fcbd18ea5e0b,
         .paths = .{
             "build.zig",
    diff --git a/test/zig/test_world.zig b/test/zig/test_world.zig
    new file mode 100644
    index 0000000..926fa84
    --- /dev/null
    +++ b/test/zig/test_world.zig
    @@ -0,0 +1,36 @@
    +const std = @import("std");
    +
    +const box2d = @import("box2d");
    +
    +// Simple world test to ensure translate_c creates the expected API for us
    +test "simple world" {
    +    const world_def = box2d.b2DefaultWorldDef();
    +    const world_id = box2d.b2CreateWorld(&world_def);
    +    try std.testing.expect(box2d.b2World_IsValid(world_id));
    +
    +    var ground_def = box2d.b2DefaultBodyDef();
    +    ground_def.position = box2d.b2Pos{ .x = 0.0, .y = -10.0 };
    +
    +    const ground_id = box2d.b2CreateBody(world_id, &ground_def);
    +    var ground_box = box2d.b2MakeBox(100.0, 1.0);
    +    var shape_def = box2d.b2DefaultShapeDef();
    +    _ = box2d.b2CreatePolygonShape(ground_id, &shape_def, &ground_box);
    +
    +    var body_def = box2d.b2DefaultBodyDef();
    +    body_def.type = box2d.b2_dynamicBody;
    +    body_def.position = box2d.b2Pos{ .x = 0.0, .y = 10.0 };
    +    const body_id = box2d.b2CreateBody(world_id, &body_def);
    +    var body_box = box2d.b2MakeBox(0.5, 0.5);
    +    _ = box2d.b2CreatePolygonShape(body_id, &shape_def, &body_box);
    +
    +    for (0..120) |_| {
    +        box2d.b2World_Step(world_id, 1.0 / 60.0, 4);
    +    }
    +
    +    const y = box2d.b2Body_GetPosition(body_id).y;
    +    try std.testing.expect(y < 10);
    +    try std.testing.expect(y > -10);
    +
    +    box2d.b2DestroyWorld(world_id);
    +    try std.testing.expect(!box2d.b2World_IsValid(world_id));
    +}
  7. added a commit that references this issue on Oct 20, 2026
  8. erincatto commented on Oct 20, 2026

    @erincatto
    Owner

    Thanks for the updates. Implemented in #1125

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions