minifb-zig-port/build.zig

86 lines
2.8 KiB
Zig

const std = @import("std");
const base_cflags: []const []const u8 = &.{
"-Wall",
"-Wextra",
"-pedantic",
"-Wno-switch",
"-Wno-unused-function",
"-Wno-implicit-fallthrough",
};
pub fn build(b: *std.build.Builder) !void {
const mode = b.standardReleaseOptions();
const target = b.standardTargetOptions(.{});
var c_flags = try std.ArrayList([]const u8).initCapacity(b.allocator, base_cflags.len);
defer c_flags.deinit();
try c_flags.appendSlice(base_cflags);
var c_sources = std.ArrayList([]const u8).init(b.allocator);
defer c_sources.deinit();
try c_sources.appendSlice(&.{
"src/MiniFB_common.c",
"src/MiniFB_internal.c",
"src/MiniFB_timer.c",
});
const use_opengl = b.option(bool, "opengl", "Use OpenGL") orelse false;
const lib = b.addStaticLibrary("minifb-zig-port", null);
lib.addIncludeDir("src");
lib.addIncludeDir("include");
lib.linkLibC();
lib.setBuildMode(mode);
lib.install();
switch (target.getOsTag()) {
std.Target.Os.Tag.windows => {
try c_sources.append("src/windows/WinMiniFB.c");
lib.linkSystemLibraryName("gdi32");
if (use_opengl) {
try c_flags.append("-DUSE_OPENGL_API");
try c_sources.append("src/gl/MiniFB_GL.c");
lib.linkSystemLibraryName("opengl32");
}
},
std.Target.Os.Tag.linux => {
try c_sources.append("src/MiniFB_linux.c");
try c_sources.append("src/x11/X11MiniFB.c");
lib.linkSystemLibrary("rt");
lib.linkSystemLibrary("X11");
if (use_opengl) {
try c_flags.append("-DUSE_OPENGL_API");
try c_sources.append("src/gl/MiniFB_GL.c");
lib.linkSystemLibrary("GL");
}
// TODO wayland
},
else => {
return error.PlatformNotSupported;
},
}
lib.addCSourceFiles(c_sources.items, c_flags.items);
addExample(b, "noise", "tests/noise.c", lib, target, mode);
addExample(b, "multiple-windows", "tests/multiple_windows.c", lib, target, mode);
addExample(b, "input-events", "tests/input_events.c", lib, target, mode);
addExample(b, "hidpi", "tests/hidpi.c", lib, target, mode);
}
fn addExample(b: *std.build.Builder, comptime name: []const u8, file: []const u8, lib: *std.build.LibExeObjStep, target: std.zig.CrossTarget, mode: std.builtin.Mode) void {
const exe = b.addExecutable(name, null);
exe.setBuildMode(mode);
exe.setTarget(target);
exe.linkLibrary(lib);
exe.addIncludeDir("include");
exe.addCSourceFile(file, base_cflags);
exe.install();
const step_name = "run-" ++ name;
b.step("run-" ++ name, "Run " ++ name ++ " example").dependOn(&exe.run().step);
}