From 35c01159c24b5b1f9f0ac5de0fa37f206aa8a1ed Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Sun, 12 Dec 2021 16:04:35 -0700 Subject: [PATCH] Support Wayland in build.zig --- build.zig | 74 ++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 59 insertions(+), 15 deletions(-) diff --git a/build.zig b/build.zig index 19910d7..2e65f5c 100644 --- a/build.zig +++ b/build.zig @@ -9,6 +9,32 @@ const base_cflags: []const []const u8 = &.{ "-Wno-implicit-fallthrough", }; +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; +} + pub fn build(b: *std.build.Builder) !void { const mode = b.standardReleaseOptions(); const target = b.standardTargetOptions(.{}); @@ -25,7 +51,7 @@ pub fn build(b: *std.build.Builder) !void { "src/MiniFB_timer.c", }); - const use_opengl = b.option(bool, "opengl", "Use OpenGL") orelse false; + const use_backend = b.option(Backend, "backend", "Which rendering back-end to use (" ++ enumNames(Backend) ++ ")") orelse .default; const lib = b.addStaticLibrary("minifb-zig-port", null); lib.addIncludeDir("src"); @@ -37,29 +63,47 @@ pub fn build(b: *std.build.Builder) !void { 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"); + 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; + } } }, 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"); + 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"); - if (use_opengl) { - try c_flags.append("-DUSE_OPENGL_API"); - try c_sources.append("src/gl/MiniFB_GL.c"); - lib.linkSystemLibrary("GL"); + 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; + } } - - // TODO wayland }, else => { - return error.PlatformNotSupported; + return ConfigError.PlatformNotSupported; }, }