Skip to content

Commit b3dfa2d

Browse files
authored
Update to latest zig and simplify build.zig (#3905)
* update to latest zig, and don't use srcdir * preserve compatibility, simplify Child.run compatibility
1 parent a4819f9 commit b3dfa2d

File tree

1 file changed

+49
-58
lines changed

1 file changed

+49
-58
lines changed

src/build.zig

+49-58
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ const builtin = @import("builtin");
33

44
// This has been tested with zig version(s):
55
// 0.11.0
6-
// 0.12.0-dev.2075+f5978181e
7-
// 0.12.0-dev.2990+31763d28c
6+
// 0.12.0-dev.3580+e204a6edb
87
//
98
// Anytype is used here to preserve compatibility, in 0.12.0dev the std.zig.CrossTarget type
109
// was reworked into std.Target.Query and std.Build.ResolvedTarget. Using anytype allows
@@ -51,37 +50,37 @@ pub fn addRaylib(b: *std.Build, target: anytype, optimize: std.builtin.OptimizeM
5150

5251
// No GLFW required on PLATFORM_DRM
5352
if (!options.platform_drm) {
54-
raylib.addIncludePath(.{ .cwd_relative = srcdir ++ "/external/glfw/include" });
53+
raylib.addIncludePath(.{ .path = "src/external/glfw/include" });
5554
}
5655

5756
addCSourceFilesVersioned(raylib, &.{
58-
try join2(gpa, srcdir, "rcore.c"),
59-
try join2(gpa, srcdir, "utils.c"),
57+
"src/rcore.c",
58+
"src/utils.c",
6059
}, raylib_flags_arr.items);
6160

6261
if (options.raudio) {
6362
addCSourceFilesVersioned(raylib, &.{
64-
try join2(gpa, srcdir, "raudio.c"),
63+
"src/raudio.c",
6564
}, raylib_flags_arr.items);
6665
}
6766
if (options.rmodels) {
6867
addCSourceFilesVersioned(raylib, &.{
69-
try join2(gpa, srcdir, "rmodels.c"),
68+
"src/rmodels.c",
7069
}, raylib_flags_arr.items);
7170
}
7271
if (options.rshapes) {
7372
addCSourceFilesVersioned(raylib, &.{
74-
try join2(gpa, srcdir, "rshapes.c"),
73+
"src/rshapes.c",
7574
}, raylib_flags_arr.items);
7675
}
7776
if (options.rtext) {
7877
addCSourceFilesVersioned(raylib, &.{
79-
try join2(gpa, srcdir, "rtext.c"),
78+
"src/rtext.c",
8079
}, raylib_flags_arr.items);
8180
}
8281
if (options.rtextures) {
8382
addCSourceFilesVersioned(raylib, &.{
84-
try join2(gpa, srcdir, "rtextures.c"),
83+
"src/rtextures.c",
8584
}, raylib_flags_arr.items);
8685
}
8786

@@ -91,14 +90,14 @@ pub fn addRaylib(b: *std.Build, target: anytype, optimize: std.builtin.OptimizeM
9190
if (options.raygui) {
9291
const raygui_c_path = gen_step.add("raygui.c", "#define RAYGUI_IMPLEMENTATION\n#include \"raygui.h\"\n");
9392
raylib.addCSourceFile(.{ .file = raygui_c_path, .flags = raylib_flags_arr.items });
94-
raylib.addIncludePath(.{ .cwd_relative = srcdir });
95-
raylib.addIncludePath(.{ .cwd_relative = srcdir ++ "/../../raygui/src" });
93+
raylib.addIncludePath(.{ .path = "src" });
94+
raylib.addIncludePath(.{ .path = "../raygui/src" });
9695
}
9796

9897
switch (getOsTagVersioned(target)) {
9998
.windows => {
10099
addCSourceFilesVersioned(raylib, &.{
101-
try join2(gpa, srcdir, "rglfw.c"),
100+
"src/rglfw.c",
102101
}, raylib_flags_arr.items);
103102
raylib.linkSystemLibrary("winmm");
104103
raylib.linkSystemLibrary("gdi32");
@@ -109,7 +108,7 @@ pub fn addRaylib(b: *std.Build, target: anytype, optimize: std.builtin.OptimizeM
109108
.linux => {
110109
if (!options.platform_drm) {
111110
addCSourceFilesVersioned(raylib, &.{
112-
try join2(gpa, srcdir, "rglfw.c"),
111+
"src/rglfw.c",
113112
}, raylib_flags_arr.items);
114113
raylib.linkSystemLibrary("GL");
115114
raylib.linkSystemLibrary("rt");
@@ -130,7 +129,7 @@ pub fn addRaylib(b: *std.Build, target: anytype, optimize: std.builtin.OptimizeM
130129
raylib.linkSystemLibrary("wayland-cursor");
131130
raylib.linkSystemLibrary("wayland-egl");
132131
raylib.linkSystemLibrary("xkbcommon");
133-
raylib.addIncludePath(.{ .path = srcdir });
132+
raylib.addIncludePath(.{ .path = "src" });
134133
try waylandGenerate(gpa, "wayland.xml", "wayland-client-protocol");
135134
try waylandGenerate(gpa, "xdg-shell.xml", "xdg-shell-client-protocol");
136135
try waylandGenerate(gpa, "xdg-decoration-unstable-v1.xml", "xdg-decoration-unstable-v1-client-protocol");
@@ -163,7 +162,7 @@ pub fn addRaylib(b: *std.Build, target: anytype, optimize: std.builtin.OptimizeM
163162
},
164163
.freebsd, .openbsd, .netbsd, .dragonfly => {
165164
addCSourceFilesVersioned(raylib, &.{
166-
try join2(gpa, srcdir, "rglfw.c"),
165+
"src/rglfw.c",
167166
}, raylib_flags_arr.items);
168167
raylib.linkSystemLibrary("GL");
169168
raylib.linkSystemLibrary("rt");
@@ -182,7 +181,7 @@ pub fn addRaylib(b: *std.Build, target: anytype, optimize: std.builtin.OptimizeM
182181
// On macos rglfw.c include Objective-C files.
183182
try raylib_flags_arr.append("-ObjC");
184183
addCSourceFilesVersioned(raylib, &.{
185-
try join2(gpa, srcdir, "rglfw.c"),
184+
"src/rglfw.c",
186185
}, raylib_flags_arr.items);
187186
raylib.linkFramework("Foundation");
188187
raylib.linkFramework("CoreServices");
@@ -259,24 +258,18 @@ pub fn build(b: *std.Build) !void {
259258

260259
const lib = try addRaylib(b, target, optimize, options);
261260

262-
lib.installHeader("src/raylib.h", "raylib.h");
263-
lib.installHeader("src/raymath.h", "raymath.h");
264-
lib.installHeader("src/rlgl.h", "rlgl.h");
261+
installHeaderVersioned(lib, "src/raylib.h", "raylib.h");
262+
installHeaderVersioned(lib, "src/raymath.h", "raymath.h");
263+
installHeaderVersioned(lib, "src/rlgl.h", "rlgl.h");
265264

266265
if (options.raygui) {
267-
lib.installHeader("../raygui/src/raygui.h", "raygui.h");
266+
installHeaderVersioned(lib, "../raygui/src/raygui.h", "raygui.h");
268267
}
269268

270269
b.installArtifact(lib);
271270
}
272271

273-
const srcdir = struct {
274-
fn getSrcDir() []const u8 {
275-
return std.fs.path.dirname(@src().file).?;
276-
}
277-
}.getSrcDir();
278-
279-
const waylandDir = srcdir ++ "/external/glfw/deps/wayland";
272+
const waylandDir = "src/external/glfw/deps/wayland";
280273

281274
fn getOsTagVersioned(target: anytype) std.Target.Os.Tag {
282275
if (comptime builtin.zig_version.minor >= 12) {
@@ -291,47 +284,45 @@ fn addCSourceFilesVersioned(
291284
files: []const []const u8,
292285
flags: []const []const u8,
293286
) void {
294-
//- HACK(cabarger): I hate this so much!!!
295287
if (comptime builtin.zig_version.minor >= 12) {
296-
for (files) |file| {
297-
exe.addCSourceFile(.{
298-
.file = .{ .path = file },
299-
.flags = flags,
300-
});
301-
}
288+
exe.addCSourceFiles(.{
289+
.files = files,
290+
.flags = flags,
291+
});
302292
} else if (comptime builtin.zig_version.minor == 11) {
303293
exe.addCSourceFiles(files, flags);
304294
} else {
305295
@compileError("Expected zig version 11 or 12");
306296
}
307297
}
308298

309-
fn waylandGenerate(allocator: std.mem.Allocator, comptime protocol: []const u8, comptime basename: []const u8) !void {
310-
const protocolDir = waylandDir ++ "/" ++ protocol;
311-
const clientHeader = srcdir ++ "/" ++ basename ++ ".h";
312-
const privateCode = srcdir ++ "/" ++ basename ++ "-code.h";
299+
fn installHeaderVersioned(
300+
lib: *std.Build.Step.Compile,
301+
source: []const u8,
302+
dest: []const u8,
303+
) void {
313304
if (comptime builtin.zig_version.minor >= 12) {
314-
_ = try std.process.Child.run(.{
315-
.allocator = allocator,
316-
.argv = &[_][]const u8{ "wayland-scanner", "client-header", protocolDir, clientHeader },
317-
});
318-
_ = try std.process.Child.run(.{
319-
.allocator = allocator,
320-
.argv = &[_][]const u8{ "wayland-scanner", "private-code", protocolDir, privateCode },
321-
});
305+
lib.installHeader(.{ .path = source }, dest);
322306
} else {
323-
_ = try std.process.Child.exec(.{
324-
.allocator = allocator,
325-
.argv = &[_][]const u8{ "wayland-scanner", "client-header", protocolDir, clientHeader },
326-
});
327-
_ = try std.process.Child.exec(.{
328-
.allocator = allocator,
329-
.argv = &[_][]const u8{ "wayland-scanner", "private-code", protocolDir, privateCode },
330-
});
307+
lib.installHeader(source, dest);
331308
}
332309
}
333310

334-
fn join2(allocator: std.mem.Allocator, path1: []const u8, path2: []const u8) ![]u8 {
335-
const joinedPath = try std.fs.path.join(allocator, &[_][]const u8{ path1, path2 });
336-
return joinedPath;
311+
const childRunVersioned = if (builtin.zig_version.minor >= 12)
312+
std.process.Child.run
313+
else
314+
std.process.Child.exec;
315+
316+
fn waylandGenerate(allocator: std.mem.Allocator, comptime protocol: []const u8, comptime basename: []const u8) !void {
317+
const protocolDir = waylandDir ++ "/" ++ protocol;
318+
const clientHeader = "src/" ++ basename ++ ".h";
319+
const privateCode = "src/" ++ basename ++ "-code.h";
320+
_ = try childRunVersioned(.{
321+
.allocator = allocator,
322+
.argv = &[_][]const u8{ "wayland-scanner", "client-header", protocolDir, clientHeader },
323+
});
324+
_ = try childRunVersioned(.{
325+
.allocator = allocator,
326+
.argv = &[_][]const u8{ "wayland-scanner", "private-code", protocolDir, privateCode },
327+
});
337328
}

0 commit comments

Comments
 (0)