소스 검색

build.zig: Fix `@src` logic and a few things (#4380)

Sage Hane 11 달 전
부모
커밋
bac3798ad3
1개의 변경된 파일31개의 추가작업 그리고 12개의 파일을 삭제
  1. 31 12
      src/build.zig

+ 31 - 12
src/build.zig

@@ -1,8 +1,14 @@
 const std = @import("std");
 const std = @import("std");
 const builtin = @import("builtin");
 const builtin = @import("builtin");
 
 
+/// Minimum supported version of Zig
+const min_ver = "0.12.0";
+
 comptime {
 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
 // 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,
         .shared = options.shared,
         .linux_display_backend = options.linux_display_backend,
         .linux_display_backend = options.linux_display_backend,
         .opengl_version = options.opengl_version,
         .opengl_version = options.opengl_version,
+        // TODO: Fix the type mismatch caused here due to unsupported `?[]const u8`.
         .config = options.config,
         .config = options.config,
     });
     });
     const raylib = raylib_dep.artifact("raylib");
     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(min_ver, 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 {
 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
         "-fno-sanitize=undefined", // https://github.com/raysan5/raylib/issues/3674
     });
     });
     if (options.config) |config| {
     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));
         const content = try std.fs.cwd().readFileAlloc(b.allocator, file, std.math.maxInt(usize));
         defer b.allocator.free(content);
         defer b.allocator.free(content);
 
 
@@ -118,7 +136,8 @@ fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.
 
 
     // No GLFW required on PLATFORM_DRM
     // No GLFW required on PLATFORM_DRM
     if (options.platform != .drm) {
     if (options.platform != .drm) {
-        raylib.addIncludePath(b.path(b.pathJoin(&.{ srcDir(), "external/glfw/include" })));
+        _ = std.debug.print("\n\n{s}\n\n", .{@src().file});
+        raylib.addIncludePath(b.path(b.pathJoin(&.{ srcDir(b), "external/glfw/include" })));
     }
     }
 
 
     var c_source_files = try std.ArrayList([]const u8).initCapacity(b.allocator, 2);
     var c_source_files = try std.ArrayList([]const u8).initCapacity(b.allocator, 2);
@@ -231,7 +250,7 @@ fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.
             // On macos rglfw.c include Objective-C files.
             // On macos rglfw.c include Objective-C files.
             try raylib_flags_arr.append(b.allocator, "-ObjC");
             try raylib_flags_arr.append(b.allocator, "-ObjC");
             raylib.root_module.addCSourceFile(.{
             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,
                 .flags = raylib_flags_arr.items,
             });
             });
             _ = raylib_flags_arr.pop();
             _ = raylib_flags_arr.pop();
@@ -265,7 +284,7 @@ fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.
     }
     }
 
 
     raylib.root_module.addCSourceFiles(.{
     raylib.root_module.addCSourceFiles(.{
-        .root = b.path(srcDir()),
+        .root = b.path(srcDir(b)),
         .files = c_source_files.items,
         .files = c_source_files.items,
         .flags = raylib_flags_arr.items,
         .flags = raylib_flags_arr.items,
     });
     });
@@ -366,14 +385,14 @@ pub fn build(b: *std.Build) !void {
         .shared = b.option(bool, "shared", "Compile as shared library") orelse defaults.shared,
         .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,
         .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,
         .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);
     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);
     b.installArtifact(lib);
 }
 }
@@ -384,7 +403,7 @@ fn waylandGenerate(
     comptime protocol: []const u8,
     comptime protocol: []const u8,
     comptime basename: []const u8,
     comptime basename: []const u8,
 ) void {
 ) 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 protocolDir = b.pathJoin(&.{ waylandDir, protocol });
     const clientHeader = basename ++ ".h";
     const clientHeader = basename ++ ".h";
     const privateCode = basename ++ "-code.h";
     const privateCode = basename ++ "-code.h";