Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,17 @@ jobs:
- name: ubuntu-gcc
os: ubuntu-latest
test: ./bin/test
# Clang jobs pin the image because each Ubuntu image ships a different set of clang versions
- name: ubuntu-clang
os: ubuntu-latest
args: '-DCMAKE_CXX_COMPILER=clang++-18 -DCMAKE_C_COMPILER=clang-18 -DCMAKE_C_FLAGS="-fsanitize=thread -fno-omit-frame-pointer" -DCMAKE_CXX_FLAGS="-fsanitize=thread -fno-omit-frame-pointer" -DCMAKE_EXE_LINKER_FLAGS="-fsanitize=thread"'
os: ubuntu-26.04
args: '-DCMAKE_CXX_COMPILER=clang++-20 -DCMAKE_C_COMPILER=clang-20 -DCMAKE_C_FLAGS="-fsanitize=thread -fno-omit-frame-pointer" -DCMAKE_CXX_FLAGS="-fsanitize=thread -fno-omit-frame-pointer" -DCMAKE_EXE_LINKER_FLAGS="-fsanitize=thread"'
test: ./bin/test
# Runner speed varies about 3x by region and MSan at -O0 can exceed the timeout on the slow ones.
# -O1 is the level MSan recommends and Debug keeps asserts. Line buffering shows per test timing.
- name: ubuntu-clang-msan
os: ubuntu-latest
os: ubuntu-26.04
timeout: 10
args: '-DCMAKE_CXX_COMPILER=clang++-18 -DCMAKE_C_COMPILER=clang-18 -DCMAKE_C_FLAGS="-O1 -fsanitize=memory -fsanitize-memory-track-origins=1 -fno-omit-frame-pointer" -DCMAKE_CXX_FLAGS="-O1 -fsanitize=memory -fsanitize-memory-track-origins=1 -fno-omit-frame-pointer" -DCMAKE_EXE_LINKER_FLAGS="-fsanitize=memory"'
args: '-DCMAKE_CXX_COMPILER=clang++-20 -DCMAKE_C_COMPILER=clang-20 -DCMAKE_C_FLAGS="-O1 -fsanitize=memory -fsanitize-memory-track-origins=1 -fno-omit-frame-pointer" -DCMAKE_CXX_FLAGS="-O1 -fsanitize=memory -fsanitize-memory-track-origins=1 -fno-omit-frame-pointer" -DCMAKE_EXE_LINKER_FLAGS="-fsanitize=memory"'
test: lscpu | grep 'Model name'; nproc; stdbuf -oL ./bin/test
msan-options: abort_on_error=1:halt_on_error=1
- name: macos
Expand Down Expand Up @@ -271,13 +272,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
Expand Down
11 changes: 7 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ if (EMSCRIPTEN AND PROJECT_IS_TOP_LEVEL)
string(APPEND CMAKE_EXE_LINKER_FLAGS " ${EMSCRIPTEN_PTHREADS_LINKER_FLAGS}")
endif()

# Needed for samples.exe to find box2d.dll. Must precede add_subdirectory(src) because
# a target reads these when it is created.
if(PROJECT_IS_TOP_LEVEL)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin")
endif()

add_subdirectory(src)

# This hides samples, test, and doxygen from apps that use box2d via FetchContent
Expand All @@ -112,10 +119,6 @@ if(PROJECT_IS_TOP_LEVEL)
option(BOX2D_BENCHMARKS "Build the Box2D benchmarks" OFF)
option(BOX2D_DOCS "Build the Box2D documentation" OFF)
option(BOX2D_UNIT_TESTS "Build the Box2D unit tests" ON)

# Needed for samples.exe to find box2d.dll
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin")

if(BOX2D_UNIT_TESTS OR BOX2D_SAMPLES OR BOX2D_BENCHMARKS)
add_subdirectory(shared)
Expand Down
68 changes: 46 additions & 22 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,29 @@ 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,

const defaults = Options{
.shared = false,
.unit_tests = false,
.c_unit_tests = false,
.zig_unit_tests = false,
.disable_simd = false,
};

pub fn getOptions(b: *Build) Options {
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") orelse defaults.zig_unit_tests,
.disable_simd = b.option(bool, "disable_simd", "Disable SIMD math (slower)") orelse defaults.disable_simd,
};
}
Expand All @@ -39,20 +36,29 @@ pub fn build(b: *Build) !void {
const options = Options.getOptions(b);
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 = 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"));

b.installArtifact(lib);
const box2d_mod = b.addModule("box2d", .{
.root_source_file = translator.output_file,
});
box2d_mod.linkLibrary(lib);

if (options.unit_tests) {
buildTests(b, target, optimize, lib);
if (options.zig_unit_tests) {
buildZigTest(b, target, optimize, box2d_mod);
}
}

Expand Down Expand Up @@ -156,7 +162,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,
Expand Down Expand Up @@ -227,7 +233,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,
});

Expand All @@ -236,3 +242,21 @@ 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_mod: *Module) void {
const mod = b.createModule(.{
.root_source_file = b.path("test/zig/test_world.zig"),
.target = target,
.optimize = optimize,
});

mod.addImport("box2d", box2d_mod);

const zig_test = b.addTest(.{
.name = "zig_test",
.root_module = mod,
});

b.installArtifact(zig_test);
}
8 changes: 7 additions & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
36 changes: 36 additions & 0 deletions test/zig/test_world.zig
Original file line number Diff line number Diff line change
@@ -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));
}
Loading