{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.
This commit is contained in:
Keith Chambers 2022-11-14 17:43:53 -05:00 committed by GitHub
parent 005a99a323
commit 731e2b1287
Failed to generate hash of commit
2 changed files with 13 additions and 5 deletions

View file

@ -169,6 +169,7 @@ pub const App = struct {
res_dirs: ?[]const []const u8, res_dirs: ?[]const []const u8,
watch_paths: ?[]const []const u8, watch_paths: ?[]const []const u8,
use_freetype: ?[]const u8 = null, use_freetype: ?[]const u8 = null,
use_model3d: bool = false,
pub const InitError = error{OutOfMemory} || std.zig.system.NativeTargetInfo.DetectError; pub const InitError = error{OutOfMemory} || std.zig.system.NativeTargetInfo.DetectError;
pub const LinkError = glfw.LinkError; 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. /// If set, freetype will be linked and can be imported using this name.
// TODO(build-system): name is currently not used / always "freetype" // TODO(build-system): name is currently not used / always "freetype"
use_freetype: ?[]const u8 = null, use_freetype: ?[]const u8 = null,
use_model3d: bool = false,
}, },
) InitError!App { ) InitError!App {
const target = (try std.zig.system.NativeTargetInfo.detect(options.target)).target; const target = (try std.zig.system.NativeTargetInfo.detect(options.target)).target;
@ -254,6 +256,7 @@ pub const App = struct {
.res_dirs = options.res_dirs, .res_dirs = options.res_dirs,
.watch_paths = options.watch_paths, .watch_paths = options.watch_paths,
.use_freetype = options.use_freetype, .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); 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_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 { pub fn install(app: *const App) void {

View file

@ -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"); const main_tests = b.addTestExe("model3d-tests", "src/main.zig");
main_tests.setBuildMode(mode); main_tests.setBuildMode(mode);
main_tests.setTarget(target); main_tests.setTarget(target);
link(main_tests); link(b, main_tests);
main_tests.install(); main_tests.install();
return main_tests.run(); return main_tests.run();
} }
pub fn link(step: *std.build.LibExeObjStep) void { pub fn link(b: *std.build.Builder, step: *std.build.LibExeObjStep) void {
step.addCSourceFile(sdkPath("/src/c/m3d.c"), &.{}); const lib = b.addStaticLibrarySource("model3d", null);
step.addIncludePath(sdkPath("/src/c")); lib.addCSourceFile(sdkPath("/src/c/m3d.c"), &.{ "-std=c89", "-DM3D_ASCII" });
step.linkLibC(); lib.linkLibC();
step.addIncludePath(sdkPath("/src/c/"));
step.linkLibrary(lib);
} }
fn sdkPath(comptime suffix: []const u8) []const u8 { fn sdkPath(comptime suffix: []const u8) []const u8 {