From 1f8c4c76796ad6faa0d8e65921108426f5d938a6 Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Fri, 10 Dec 2021 17:45:53 -0700 Subject: [PATCH] Add OpenGL support to build.zig --- build.zig | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/build.zig b/build.zig index 0225a18..f2c631e 100644 --- a/build.zig +++ b/build.zig @@ -1,6 +1,6 @@ const std = @import("std"); -const cflags: []const []const u8 = &.{ +const base_cflags: []const []const u8 = &.{ "-Wall", "-Wextra", "-pedantic", @@ -10,25 +10,38 @@ const cflags: []const []const u8 = &.{ "-std=c11", }; -pub fn build(b: *std.build.Builder) void { - // Standard release options allow the person running `zig build` to select - // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. +pub fn build(b: *std.build.Builder) !void { const mode = b.standardReleaseOptions(); const target = b.standardTargetOptions(.{}); - const lib = b.addStaticLibrary("minifb-zig-port", null); - lib.addCSourceFiles(&.{ + 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", "src/windows/WinMiniFB.c", - }, cflags); + }); + + 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.linkSystemLibraryName("gdi32"); lib.linkLibC(); lib.setBuildMode(mode); lib.install(); + if (use_opengl) { + try c_flags.append("-DUSE_OPENGL_API"); + try c_sources.append("src/gl/MiniFB_GL.c"); + lib.linkSystemLibraryName("Opengl32"); + } + 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); @@ -42,7 +55,7 @@ fn addExample(b: *std.build.Builder, comptime name: []const u8, file: []const u8 exe.setTarget(target); exe.linkLibrary(lib); exe.addIncludeDir("include"); - exe.addCSourceFile(file, cflags); + exe.addCSourceFile(file, base_cflags); exe.install(); const step_name = "run-" ++ name;