From 3a3a806f6e8ef848ff527862610dd4180f76af96 Mon Sep 17 00:00:00 2001 From: Sage Hane Date: Mon, 14 Oct 2024 01:52:19 +0900 Subject: [PATCH] build.zig: Fix `@src` logic and a few things --- src/build.zig | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/src/build.zig b/src/build.zig index 8944c7087005..40a506fd5db8 100644 --- a/src/build.zig +++ b/src/build.zig @@ -1,8 +1,14 @@ const std = @import("std"); const builtin = @import("builtin"); +/// Minimum supported version of Zig +const min_ver = "0.12.0"; + comptime { - if (builtin.zig_version.minor < 12) @compileError("Raylib requires zig version 0.12.0"); + const order = std.SemanticVersion.order; + const parse = std.SemanticVersion.parse; + if (order(builtin.zig_version, parse(min_ver) catch unreachable) == .lt) + @compileError("Raylib requires zig version " ++ min_ver); } // NOTE(freakmangd): I don't like using a global here, but it prevents having to @@ -28,6 +34,7 @@ pub fn addRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std. .shared = options.shared, .linux_display_backend = options.linux_display_backend, .opengl_version = options.opengl_version, + // TODO: Fix the type mismatch caused here due to unsupported `?[]const u8`. .config = options.config, }); const raylib = raylib_dep.artifact("raylib"); @@ -51,8 +58,19 @@ fn setDesktopPlatform(raylib: *std.Build.Step.Compile, platform: PlatformBackend } } -fn srcDir() []const u8 { - return std.fs.path.dirname(@src().file) orelse "."; +/// TODO: Once the minimum supported version is bumped to 0.14.0, it can be simplified again: +/// https://github.com/raysan5/raylib/pull/4375#issuecomment-2408998315 +/// https://github.com/raysan5/raylib/blob/9b9c72eb0dc705cde194b053a366a40396acfb67/src/build.zig#L54-L56 +fn srcDir(b: *std.Build) []const u8 { + comptime { + const order = std.SemanticVersion.order; + const parse = std.SemanticVersion.parse; + if (order(parse(min_ver) catch unreachable, parse("0.14.0") catch unreachable) != .lt) + @compileError("Please take a look at this function again"); + } + + const src_file = std.fs.path.relative(b.allocator, @src().file, ".") catch @panic("OOM"); + return std.fs.path.dirname(src_file) orelse "."; } fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, options: Options) !*std.Build.Step.Compile { @@ -69,7 +87,7 @@ fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std. "-fno-sanitize=undefined", // https://github.com/raysan5/raylib/issues/3674 }); if (options.config) |config| { - const file = b.pathJoin(&.{ srcDir(), "config.h" }); + const file = b.pathJoin(&.{ srcDir(b), "config.h" }); const content = try std.fs.cwd().readFileAlloc(b.allocator, file, std.math.maxInt(usize)); defer b.allocator.free(content); @@ -118,7 +136,7 @@ fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std. // No GLFW required on PLATFORM_DRM if (options.platform != .drm) { - raylib.addIncludePath(b.path(b.pathJoin(&.{ srcDir(), "external/glfw/include" }))); + raylib.addIncludePath(b.path(b.pathJoin(&.{ srcDir(b), "external/glfw/include" }))); } var c_source_files = try std.ArrayList([]const u8).initCapacity(b.allocator, 2); @@ -231,7 +249,7 @@ fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std. // On macos rglfw.c include Objective-C files. try raylib_flags_arr.append(b.allocator, "-ObjC"); raylib.root_module.addCSourceFile(.{ - .file = b.path(b.pathJoin(&.{ srcDir(), "rglfw.c" })), + .file = b.path(b.pathJoin(&.{ srcDir(b), "rglfw.c" })), .flags = raylib_flags_arr.items, }); _ = raylib_flags_arr.pop(); @@ -265,7 +283,7 @@ fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std. } raylib.root_module.addCSourceFiles(.{ - .root = b.path(srcDir()), + .root = b.path(srcDir(b)), .files = c_source_files.items, .flags = raylib_flags_arr.items, }); @@ -366,14 +384,14 @@ pub fn build(b: *std.Build) !void { .shared = b.option(bool, "shared", "Compile as shared library") orelse defaults.shared, .linux_display_backend = b.option(LinuxDisplayBackend, "linux_display_backend", "Linux display backend to use") orelse defaults.linux_display_backend, .opengl_version = b.option(OpenglVersion, "opengl_version", "OpenGL version to use") orelse defaults.opengl_version, - .config = b.option([]const u8, "config", "Compile with custom define macros overriding config.h") orelse null, + .config = b.option([]const u8, "config", "Compile with custom define macros overriding config.h"), }; const lib = try compileRaylib(b, target, optimize, options); - lib.installHeader(b.path(b.pathJoin(&.{ srcDir(), "raylib.h" })), "raylib.h"); - lib.installHeader(b.path(b.pathJoin(&.{ srcDir(), "raymath.h" })), "raymath.h"); - lib.installHeader(b.path(b.pathJoin(&.{ srcDir(), "rlgl.h" })), "rlgl.h"); + lib.installHeader(b.path(b.pathJoin(&.{ srcDir(b), "raylib.h" })), "raylib.h"); + lib.installHeader(b.path(b.pathJoin(&.{ srcDir(b), "raymath.h" })), "raymath.h"); + lib.installHeader(b.path(b.pathJoin(&.{ srcDir(b), "rlgl.h" })), "rlgl.h"); b.installArtifact(lib); } @@ -384,7 +402,7 @@ fn waylandGenerate( comptime protocol: []const u8, comptime basename: []const u8, ) void { - const waylandDir = b.pathJoin(&.{ srcDir(), "external/glfw/deps/wayland" }); + const waylandDir = b.pathJoin(&.{ srcDir(b), "external/glfw/deps/wayland" }); const protocolDir = b.pathJoin(&.{ waylandDir, protocol }); const clientHeader = basename ++ ".h"; const privateCode = basename ++ "-code.h";