box3d-zig/build.zig

182 lines
5.4 KiB
Zig

const std = @import("std");
const box3d_sources = &.{
"aabb.c",
"arena_allocator.c",
"bitset.c",
"block_allocator.c",
"body.c",
"broad_phase.c",
"capsule.c",
"compound.c",
"constraint_graph.c",
"contact.c",
"contact_solver.c",
"convex_manifold.c",
"core.c",
"distance.c",
"distance_joint.c",
"dynamic_tree.c",
"height_field.c",
"hull.c",
"id_pool.c",
"island.c",
"joint.c",
"manifold.c",
"math_functions.c",
"mesh.c",
"mesh_contact.c",
"motor_joint.c",
"mover.c",
"name_cache.c",
"parallel_for.c",
"parallel_joint.c",
"physics_world.c",
"prismatic_joint.c",
"recording.c",
"recording_replay.c",
"revolute_joint.c",
"scheduler.c",
"sensor.c",
"shape.c",
"simd.c",
"solver.c",
"solver_set.c",
"sphere.c",
"spherical_joint.c",
"table.c",
"timer.c",
"triangle_manifold.c",
"types.c",
"weld_joint.c",
"wheel_joint.c",
"world_snapshot.c",
};
const public_headers = &.{
"base.h",
"box3d.h",
"collision.h",
"constants.h",
"id.h",
"math_functions.h",
"types.h",
};
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const disable_simd = b.option(bool, "disable-simd", "Disable SIMD math (slower)") orelse false;
const double_precision = b.option(bool, "double-precision", "Use double-precision world positions (changes the ABI)") orelse false;
const validate = b.option(bool, "box3d-validate", "Enable heavy internal validation") orelse (optimize == .debug);
const sanitize_c = b.option(std.zig.SanitizeC, "sanitize-c", "C undefined-behavior checking: off, trap, or full") orelse .off;
const config_header = b.addConfigHeader(.{
.style = .blank,
.include_path = "box3d/config.h",
.include_guard = "BOX3D_CONFIG_H",
}, .{});
// This is the only public option: it changes types in the installed headers and the ABI.
config_header.addValue("BOX3D_DOUBLE_PRECISION", ?void, if (double_precision) {} else null);
const box3d_module = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
// Keep the archive suitable for linking into either executables or shared libraries.
.pic = true,
// CMake does not enable UBSan by default. Keeping it opt-in also avoids
// making plain C consumers link Zig's compiler runtime.
.sanitize_c = sanitize_c,
});
box3d_module.addConfigHeader(config_header);
configureBox3dModule(b, box3d_module, target, optimize, disable_simd, double_precision, validate);
const box3d = b.addLibrary(.{
.name = "box3d",
.linkage = .static,
.root_module = box3d_module,
});
const translate_c = b.addTranslateC(.{
.root_source_file = b.path("box3d/include/box3d/box3d.h"),
.target = target,
.optimize = optimize,
});
translate_c.addConfigHeader(config_header);
translate_c.addIncludePath(b.path("box3d/include"));
addTranslateOptions(translate_c, optimize, disable_simd, double_precision, validate);
const bindings = translate_c.addModule("box3d");
bindings.linkLibrary(box3d);
box3d.installConfigHeader(config_header);
inline for (public_headers) |header| {
box3d.installHeader(
b.path(b.fmt("box3d/include/box3d/{s}", .{header})),
b.fmt("box3d/{s}", .{header}),
);
}
b.installArtifact(box3d);
}
const c_flags = &.{
"-std=gnu17",
// Box3D relies on this for cross-platform floating-point determinism.
"-ffp-contract=off",
};
fn configureBox3dModule(
b: *std.Build,
module: *std.Build.Module,
target: std.Build.ResolvedTarget,
optimize: std.builtin.Optimize,
disable_simd: bool,
double_precision: bool,
validate: bool,
) void {
module.addCSourceFiles(.{
.root = b.path("box3d/src"),
.files = box3d_sources,
.flags = c_flags,
});
module.addIncludePath(b.path("box3d/include"));
module.addIncludePath(b.path("box3d/src"));
addBuildOptions(module, optimize, disable_simd, double_precision, validate);
// These dependencies are carried through when a Zig consumer links the artifact.
switch (target.result.os.tag) {
.linux => {
module.linkSystemLibrary("m", .{});
module.linkSystemLibrary("pthread", .{});
},
else => {},
}
}
fn addBuildOptions(
module: *std.Build.Module,
optimize: std.builtin.Optimize,
disable_simd: bool,
double_precision: bool,
validate: bool,
) void {
if (optimize != .debug and !validate) module.addCMacro("NDEBUG", "1");
if (disable_simd) module.addCMacro("BOX3D_DISABLE_SIMD", "1");
if (double_precision) module.addCMacro("BOX3D_DOUBLE_PRECISION", "1");
if (validate) module.addCMacro("BOX3D_VALIDATE", "1");
}
fn addTranslateOptions(
translate_c: *std.Build.Step.TranslateC,
optimize: std.builtin.Optimize,
disable_simd: bool,
double_precision: bool,
validate: bool,
) void {
if (optimize != .debug and !validate) translate_c.defineCMacro("NDEBUG", null);
if (disable_simd) translate_c.defineCMacro("BOX3D_DISABLE_SIMD", null);
if (double_precision) translate_c.defineCMacro("BOX3D_DOUBLE_PRECISION", null);
if (validate) translate_c.defineCMacro("BOX3D_VALIDATE", null);
}