From 731e2b1287c48d5c1e32cc0a7ee9525f8a895eec Mon Sep 17 00:00:00 2001 From: Keith Chambers Date: Mon, 14 Nov 2022 17:43:53 -0500 Subject: [PATCH] {mach,model3d}: link model3d as static lib instead of adding C files to build step (#613) This allows the library to link better with projects. Otherwise you can end up with a mix of conflicts over that compiler + version to use. Also adds .use_model3d option to mach build system. If set to true, model3d will be linked in statically to target project. --- build.zig | 6 ++++++ libs/model3d/build.zig | 12 +++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/build.zig b/build.zig index d3a8aff0..61cadec8 100644 --- a/build.zig +++ b/build.zig @@ -169,6 +169,7 @@ pub const App = struct { res_dirs: ?[]const []const u8, watch_paths: ?[]const []const u8, use_freetype: ?[]const u8 = null, + use_model3d: bool = false, pub const InitError = error{OutOfMemory} || std.zig.system.NativeTargetInfo.DetectError; pub const LinkError = glfw.LinkError; @@ -199,6 +200,7 @@ pub const App = struct { /// If set, freetype will be linked and can be imported using this name. // TODO(build-system): name is currently not used / always "freetype" use_freetype: ?[]const u8 = null, + use_model3d: bool = false, }, ) InitError!App { const target = (try std.zig.system.NativeTargetInfo.detect(options.target)).target; @@ -254,6 +256,7 @@ pub const App = struct { .res_dirs = options.res_dirs, .watch_paths = options.watch_paths, .use_freetype = options.use_freetype, + .use_model3d = options.use_model3d, }; } @@ -266,6 +269,9 @@ pub const App = struct { } sysaudio.link(app.b, app.step, options.sysaudio_options); if (app.use_freetype) |_| freetype.link(app.b, app.step, options.freetype_options); + if (app.use_model3d) { + model3d.link(app.b, app.step); + } } pub fn install(app: *const App) void { diff --git a/libs/model3d/build.zig b/libs/model3d/build.zig index 4fc794a1..1488f0a5 100644 --- a/libs/model3d/build.zig +++ b/libs/model3d/build.zig @@ -16,15 +16,17 @@ pub fn testStep(b: *std.build.Builder, mode: std.builtin.Mode, target: std.zig.C const main_tests = b.addTestExe("model3d-tests", "src/main.zig"); main_tests.setBuildMode(mode); main_tests.setTarget(target); - link(main_tests); + link(b, main_tests); main_tests.install(); return main_tests.run(); } -pub fn link(step: *std.build.LibExeObjStep) void { - step.addCSourceFile(sdkPath("/src/c/m3d.c"), &.{}); - step.addIncludePath(sdkPath("/src/c")); - step.linkLibC(); +pub fn link(b: *std.build.Builder, step: *std.build.LibExeObjStep) void { + const lib = b.addStaticLibrarySource("model3d", null); + lib.addCSourceFile(sdkPath("/src/c/m3d.c"), &.{ "-std=c89", "-DM3D_ASCII" }); + lib.linkLibC(); + step.addIncludePath(sdkPath("/src/c/")); + step.linkLibrary(lib); } fn sdkPath(comptime suffix: []const u8) []const u8 {