2021-12-11 00:02:10 +00:00
|
|
|
const std = @import("std");
|
|
|
|
|
2021-12-11 00:45:53 +00:00
|
|
|
const base_cflags: []const []const u8 = &.{
|
2021-12-11 00:02:10 +00:00
|
|
|
"-Wall",
|
|
|
|
"-Wextra",
|
|
|
|
"-pedantic",
|
|
|
|
"-Wno-switch",
|
|
|
|
"-Wno-unused-function",
|
|
|
|
"-Wno-implicit-fallthrough",
|
|
|
|
};
|
|
|
|
|
2021-12-12 23:04:35 +00:00
|
|
|
const Backend = enum {
|
|
|
|
default,
|
|
|
|
x11,
|
|
|
|
wayland,
|
|
|
|
opengl,
|
|
|
|
gdi,
|
|
|
|
};
|
|
|
|
|
|
|
|
const ConfigError = error {
|
|
|
|
PlatformNotSupported,
|
|
|
|
BackendNotSupported,
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Returns a comma-separated list of the names of an enum's values
|
|
|
|
/// in declaration order.
|
|
|
|
fn enumNames(comptime E: type) []const u8 {
|
|
|
|
var s: []const u8 = &[_]u8{};
|
|
|
|
for (std.enums.values(E)) |value| {
|
|
|
|
if (s.len > 0) {
|
|
|
|
s = s ++ ", ";
|
|
|
|
}
|
|
|
|
s = s ++ @tagName(value);
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2021-12-11 00:45:53 +00:00
|
|
|
pub fn build(b: *std.build.Builder) !void {
|
2021-12-11 00:02:10 +00:00
|
|
|
const mode = b.standardReleaseOptions();
|
|
|
|
const target = b.standardTargetOptions(.{});
|
|
|
|
|
2021-12-11 00:45:53 +00:00
|
|
|
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(&.{
|
2021-12-11 00:02:10 +00:00
|
|
|
"src/MiniFB_common.c",
|
|
|
|
"src/MiniFB_internal.c",
|
|
|
|
"src/MiniFB_timer.c",
|
2021-12-11 00:45:53 +00:00
|
|
|
});
|
|
|
|
|
2021-12-12 23:04:35 +00:00
|
|
|
const use_backend = b.option(Backend, "backend", "Which rendering back-end to use (" ++ enumNames(Backend) ++ ")") orelse .default;
|
2021-12-11 00:45:53 +00:00
|
|
|
|
|
|
|
const lib = b.addStaticLibrary("minifb-zig-port", null);
|
2021-12-11 00:02:10 +00:00
|
|
|
lib.addIncludeDir("src");
|
|
|
|
lib.addIncludeDir("include");
|
|
|
|
lib.linkLibC();
|
|
|
|
lib.setBuildMode(mode);
|
|
|
|
lib.install();
|
2021-12-12 20:30:32 +00:00
|
|
|
|
|
|
|
switch (target.getOsTag()) {
|
|
|
|
std.Target.Os.Tag.windows => {
|
|
|
|
try c_sources.append("src/windows/WinMiniFB.c");
|
2021-12-12 23:04:35 +00:00
|
|
|
lib.linkSystemLibrary("gdi32");
|
|
|
|
switch (use_backend) {
|
|
|
|
.gdi, .default => {},
|
|
|
|
.opengl => {
|
|
|
|
try c_flags.append("-DUSE_OPENGL_API");
|
|
|
|
try c_sources.append("src/gl/MiniFB_GL.c");
|
|
|
|
lib.linkSystemLibraryName("opengl32");
|
|
|
|
},
|
|
|
|
else => {
|
|
|
|
return ConfigError.BackendNotSupported;
|
|
|
|
}
|
2021-12-12 20:30:32 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
std.Target.Os.Tag.linux => {
|
|
|
|
try c_sources.append("src/MiniFB_linux.c");
|
|
|
|
lib.linkSystemLibrary("rt");
|
2021-12-12 23:04:35 +00:00
|
|
|
switch (use_backend) {
|
|
|
|
.x11, .default => {
|
|
|
|
try c_sources.append("src/x11/X11MiniFB.c");
|
|
|
|
lib.linkSystemLibrary("X11");
|
|
|
|
},
|
|
|
|
.opengl => {
|
|
|
|
try c_sources.append("src/x11/X11MiniFB.c");
|
|
|
|
lib.linkSystemLibrary("X11");
|
2021-12-12 20:30:32 +00:00
|
|
|
|
2021-12-12 23:04:35 +00:00
|
|
|
try c_flags.append("-DUSE_OPENGL_API");
|
|
|
|
try c_sources.append("src/gl/MiniFB_GL.c");
|
|
|
|
lib.linkSystemLibrary("GL");
|
|
|
|
},
|
|
|
|
.wayland => {
|
|
|
|
try c_sources.append("src/wayland/WaylandMiniFB.c");
|
|
|
|
lib.linkSystemLibrary("wayland-client");
|
|
|
|
lib.linkSystemLibrary("wayland-cursor");
|
|
|
|
},
|
|
|
|
else => {
|
|
|
|
return ConfigError.BackendNotSupported;
|
|
|
|
}
|
2021-12-12 20:30:32 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
else => {
|
2021-12-12 23:04:35 +00:00
|
|
|
return ConfigError.PlatformNotSupported;
|
2021-12-12 20:30:32 +00:00
|
|
|
},
|
2021-12-11 00:45:53 +00:00
|
|
|
}
|
2021-12-12 20:30:32 +00:00
|
|
|
|
2021-12-11 00:45:53 +00:00
|
|
|
lib.addCSourceFiles(c_sources.items, c_flags.items);
|
2021-12-11 00:02:10 +00:00
|
|
|
|
|
|
|
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");
|
2021-12-11 00:45:53 +00:00
|
|
|
exe.addCSourceFile(file, base_cflags);
|
2021-12-11 00:02:10 +00:00
|
|
|
exe.install();
|
|
|
|
|
|
|
|
const step_name = "run-" ++ name;
|
|
|
|
b.step("run-" ++ name, "Run " ++ name ++ " example").dependOn(&exe.run().step);
|
|
|
|
}
|