From 508ca5c80ff476ed1ef0f34a7d14af8756a09e20 Mon Sep 17 00:00:00 2001 From: Jonathan Marler Date: Wed, 26 Mar 2025 21:11:35 -0600 Subject: [PATCH] [build] only include rglfw.c for glfw platform It looks like raylib doesn't require rglfw.c if you're using the RGFW backend. I'm guessing the same is true for SDL but I haven't tested. Excluding this one file brings the raylib library down from 6.9 MB to 6.3 MB for RGFW. However, one of the examples requires the symbols from rglfw.c, to accomodate this I added a function that will check whether raylib has already included rglfw and if not include it for that one example. --- build.zig | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/build.zig b/build.zig index ea1a6da09..b67ff49d9 100644 --- a/build.zig +++ b/build.zig @@ -179,7 +179,11 @@ fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std. raylib.addIncludePath(b.path("src/platforms")); switch (target.result.os.tag) { .windows => { - try c_source_files.append("src/rglfw.c"); + switch (options.platform) { + .glfw => try c_source_files.append("src/rglfw.c"), + .rgfw, .sdl, .drm => {}, + } + raylib.linkSystemLibrary("winmm"); raylib.linkSystemLibrary("gdi32"); raylib.linkSystemLibrary("opengl32"); @@ -480,6 +484,9 @@ fn addExamples( if (std.mem.eql(u8, name, "rlgl_standalone")) { exe.addIncludePath(b.path("src")); exe.addIncludePath(b.path("src/external/glfw/include")); + if (!hasCSource(raylib.root_module, "rglfw.c")) { + exe.addCSourceFile(.{ .file = b.path("src/rglfw.c"), .flags = &.{} }); + } } if (std.mem.eql(u8, name, "raylib_opengl_interop")) { exe.addIncludePath(b.path("src/external")); @@ -532,3 +539,15 @@ fn addExamples( } return all; } + +fn hasCSource(module: *std.Build.Module, name: []const u8) bool { + for (module.link_objects.items) |o| switch (o) { + .c_source_file => |c| if (switch (c.file) { + .src_path => |s| std.ascii.endsWithIgnoreCase(s.sub_path, name), + .generated, .cwd_relative, .dependency => false, + }) return true, + .c_source_files => |s| for (s.files) |c| if (std.ascii.endsWithIgnoreCase(c, name)) return true, + else => {}, + }; + return false; +}