model3d: update to latest Zig build API

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2023-02-08 00:24:56 -07:00 committed by Stephen Gutekanst
parent ca062e08fe
commit ae06ca541f
2 changed files with 34 additions and 18 deletions

View file

@ -1,29 +1,37 @@
const std = @import("std"); const std = @import("std");
pub const pkg = std.build.Pkg{ pub fn module(b: *std.Build) *std.build.Module {
.name = "model3d", return b.createModule(.{
.source = .{ .path = sdkPath("/src/main.zig") }, .source_file = .{ .path = sdkPath("/src/main.zig") },
}; });
pub fn build(b: *std.build.Builder) void {
const mode = b.standardReleaseOptions();
const target = b.standardTargetOptions(.{});
const test_step = b.step("test", "Run library tests");
test_step.dependOn(&testStep(b, mode, target).step);
} }
pub fn testStep(b: *std.build.Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget) *std.build.RunStep { pub fn build(b: *std.Build) void {
const main_tests = b.addTestExe("model3d-tests", "src/main.zig"); const optimize = b.standardOptimizeOption(.{});
main_tests.setBuildMode(mode); const target = b.standardTargetOptions(.{});
main_tests.setTarget(target); const test_step = b.step("test", "Run library tests");
test_step.dependOn(&testStep(b, optimize, target).step);
}
pub fn testStep(b: *std.Build, optimize: std.builtin.OptimizeMode, target: std.zig.CrossTarget) *std.build.RunStep {
const main_tests = b.addTest(.{
.name = "model3d-tests",
.kind = .test_exe,
.root_source_file = .{ .path = sdkPath("/src/main.zig") },
.target = target,
.optimize = optimize,
});
link(b, main_tests, target); link(b, main_tests, target);
main_tests.install(); main_tests.install();
return main_tests.run(); return main_tests.run();
} }
pub fn link(b: *std.build.Builder, step: *std.build.LibExeObjStep, target: std.zig.CrossTarget) void { pub fn link(b: *std.Build, step: *std.build.CompileStep, target: std.zig.CrossTarget) void {
const lib = b.addStaticLibrarySource("model3d", null); const lib = b.addStaticLibrary(.{
lib.setTarget(target); .name = "model3d",
.target = target,
.optimize = step.optimize,
});
// Note: model3d needs unaligned accesses, which are safe on all modern architectures. // Note: model3d needs unaligned accesses, which are safe on all modern architectures.
// See https://gitlab.com/bztsrc/model3d/-/issues/19 // See https://gitlab.com/bztsrc/model3d/-/issues/19
lib.addCSourceFile(sdkPath("/src/c/m3d.c"), &.{ "-std=c89", "-fno-sanitize=alignment" }); lib.addCSourceFile(sdkPath("/src/c/m3d.c"), &.{ "-std=c89", "-fno-sanitize=alignment" });

View file

@ -216,7 +216,7 @@ fn intToError(int: c_int) Error {
test { test {
testing.refAllDeclsRecursive(@This()); testing.refAllDeclsRecursive(@This());
var model_file = try std.fs.cwd().openFile("assets/cube.m3d", .{}); var model_file = try std.fs.cwd().openFile( thisDir("/../assets/cube.m3d"), .{});
defer model_file.close(); defer model_file.close();
var model_data = try model_file.readToEndAllocOptions(testing.allocator, 1024, 119, @alignOf(u8), 0); var model_data = try model_file.readToEndAllocOptions(testing.allocator, 1024, 119, @alignOf(u8), 0);
defer testing.allocator.free(model_data); defer testing.allocator.free(model_data);
@ -229,3 +229,11 @@ test {
defer std.heap.c_allocator.free(out); defer std.heap.c_allocator.free(out);
try testing.expect(out.len >= 119); try testing.expect(out.len >= 119);
} }
fn thisDir(comptime suffix: []const u8) []const u8 {
if (suffix[0] != '/') @compileError("suffix must be an absolute path");
return comptime blk: {
const root_dir = std.fs.path.dirname(@src().file) orelse ".";
break :blk root_dir ++ suffix;
};
}