Skip to content

Commit ce3b121

Browse files
authored
build.zig: Remove addRaylib and fix raygui builds when using raylib as dep (#4475)
- addRaylib just duplicates what adding raylib as dependency already does so it do not needs to exist - move raygui build to standard build process when flag is enabled. this works correctly when using raylib as dependency and having raygui as dependency as well. problem with previous approach was that raygui was in options but it was doing nothing because you had to also use addRaygui for it to be effective Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
1 parent d8feef5 commit ce3b121

File tree

1 file changed

+25
-60
lines changed

1 file changed

+25
-60
lines changed

build.zig

Lines changed: 25 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,6 @@ comptime {
1111
@compileError("Raylib requires zig version " ++ min_ver);
1212
}
1313

14-
// NOTE(freakmangd): I don't like using a global here, but it prevents having to
15-
// get the flags a second time when adding raygui
16-
var raylib_flags_arr: std.ArrayListUnmanaged([]const u8) = .{};
17-
18-
// This has been tested with zig version 0.13.0
19-
pub fn addRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, options: Options) !*std.Build.Step.Compile {
20-
const raylib_dep = b.dependencyFromBuildZig(@This(), .{
21-
.target = target,
22-
.optimize = optimize,
23-
.raudio = options.raudio,
24-
.rmodels = options.rmodels,
25-
.rshapes = options.rshapes,
26-
.rtext = options.rtext,
27-
.rtextures = options.rtextures,
28-
.platform = options.platform,
29-
.shared = options.shared,
30-
.linux_display_backend = options.linux_display_backend,
31-
.opengl_version = options.opengl_version,
32-
.config = options.config,
33-
});
34-
const raylib = raylib_dep.artifact("raylib");
35-
36-
if (options.raygui) {
37-
const raygui_dep = b.dependency(options.raygui_dependency_name, .{});
38-
addRaygui(b, raylib, raygui_dep);
39-
}
40-
41-
return raylib;
42-
}
43-
4414
fn setDesktopPlatform(raylib: *std.Build.Step.Compile, platform: PlatformBackend) void {
4515
raylib.defineCMacro("PLATFORM_DESKTOP", null);
4616

@@ -107,21 +77,26 @@ const config_h_flags = outer: {
10777
};
10878

10979
fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, options: Options) !*std.Build.Step.Compile {
110-
raylib_flags_arr.clearRetainingCapacity();
80+
var raylib_flags_arr = std.ArrayList([]const u8).init(b.allocator);
81+
defer raylib_flags_arr.deinit();
11182

112-
const shared_flags = &[_][]const u8{
113-
"-fPIC",
114-
"-DBUILD_LIBTYPE_SHARED",
115-
};
116-
try raylib_flags_arr.appendSlice(b.allocator, &[_][]const u8{
83+
try raylib_flags_arr.appendSlice(&[_][]const u8{
11784
"-std=gnu99",
11885
"-D_GNU_SOURCE",
11986
"-DGL_SILENCE_DEPRECATION=199309L",
12087
"-fno-sanitize=undefined", // https://github.com/raysan5/raylib/issues/3674
12188
});
89+
90+
if (options.shared) {
91+
try raylib_flags_arr.appendSlice(&[_][]const u8{
92+
"-fPIC",
93+
"-DBUILD_LIBTYPE_SHARED",
94+
});
95+
}
96+
12297
if (options.config.len > 0) {
12398
// Sets a flag indiciating the use of a custom `config.h`
124-
try raylib_flags_arr.append(b.allocator, "-DEXTERNAL_CONFIG_FLAGS");
99+
try raylib_flags_arr.append("-DEXTERNAL_CONFIG_FLAGS");
125100

126101
// Splits a space-separated list of config flags into multiple flags
127102
//
@@ -131,7 +106,7 @@ fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.
131106

132107
// Apply config flags supplied by the user
133108
while (config_iter.next()) |config_flag|
134-
try raylib_flags_arr.append(b.allocator, config_flag);
109+
try raylib_flags_arr.append(config_flag);
135110

136111
// Apply all relevant configs from `src/config.h` *except* the user-specified ones
137112
//
@@ -149,14 +124,10 @@ fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.
149124
}
150125

151126
// Otherwise, append default value from config.h to compile flags
152-
try raylib_flags_arr.append(b.allocator, flag);
127+
try raylib_flags_arr.append(flag);
153128
}
154129
}
155130

156-
if (options.shared) {
157-
try raylib_flags_arr.appendSlice(b.allocator, shared_flags);
158-
}
159-
160131
const raylib = if (options.shared)
161132
b.addSharedLibrary(.{
162133
.name = "raylib",
@@ -288,7 +259,7 @@ fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.
288259
}
289260

290261
// On macos rglfw.c include Objective-C files.
291-
try raylib_flags_arr.append(b.allocator, "-ObjC");
262+
try raylib_flags_arr.append("-ObjC");
292263
raylib.root_module.addCSourceFile(.{
293264
.file = b.path("src/rglfw.c"),
294265
.flags = raylib_flags_arr.items,
@@ -327,25 +298,19 @@ fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.
327298
.flags = raylib_flags_arr.items,
328299
});
329300

330-
return raylib;
331-
}
332-
333-
/// This function does not need to be called if you passed .raygui = true to addRaylib
334-
pub fn addRaygui(b: *std.Build, raylib: *std.Build.Step.Compile, raygui_dep: *std.Build.Dependency) void {
335-
if (raylib_flags_arr.items.len == 0) {
336-
@panic(
337-
\\argument 2 `raylib` in `addRaygui` must come from b.dependency("raylib", ...).artifact("raylib")
338-
);
339-
}
301+
if (options.raygui) {
302+
const raygui_dep = b.dependency(options.raygui_dependency_name, .{});
340303

341-
var gen_step = b.addWriteFiles();
342-
raylib.step.dependOn(&gen_step.step);
304+
var gen_step = b.addWriteFiles();
305+
raylib.step.dependOn(&gen_step.step);
343306

344-
const raygui_c_path = gen_step.add("raygui.c", "#define RAYGUI_IMPLEMENTATION\n#include \"raygui.h\"\n");
345-
raylib.addCSourceFile(.{ .file = raygui_c_path, .flags = raylib_flags_arr.items });
346-
raylib.addIncludePath(raygui_dep.path("src"));
307+
const raygui_c_path = gen_step.add("raygui.c", "#define RAYGUI_IMPLEMENTATION\n#include \"raygui.h\"\n");
308+
raylib.addCSourceFile(.{ .file = raygui_c_path, .flags = raylib_flags_arr.items });
309+
raylib.addIncludePath(raygui_dep.path("src"));
310+
raylib.installHeader(raygui_dep.path("src/raygui.h"), "raygui.h");
311+
}
347312

348-
raylib.installHeader(raygui_dep.path("src/raygui.h"), "raygui.h");
313+
return raylib;
349314
}
350315

351316
pub const Options = struct {

0 commit comments

Comments
 (0)