Kaynağa Gözat

build.zig: Improve logic (#4375)

* build.zig: Fix `@src` logic

* build.zig: Clarify build error

* build.zig: Add option for enabling `raygui`

* build.zig: Expose `Options` type
Sage Hane 1 yıl önce
ebeveyn
işleme
d3f86eb957
2 değiştirilmiş dosya ile 26 ekleme ve 18 silme
  1. 3 0
      build.zig
  2. 23 18
      src/build.zig

+ 3 - 0
build.zig

@@ -9,3 +9,6 @@ pub fn build(b: *std.Build) !void {
 // expose helper functions to user's build.zig
 pub const addRaylib = raylib.addRaylib;
 pub const addRaygui = raylib.addRaygui;
+
+// expose options for compiling
+pub const RaylibOptions = raylib.Options;

+ 23 - 18
src/build.zig

@@ -51,6 +51,10 @@ fn setDesktopPlatform(raylib: *std.Build.Step.Compile, platform: PlatformBackend
     }
 }
 
+fn srcDir() []const u8 {
+    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 {
     raylib_flags_arr.clearRetainingCapacity();
 
@@ -65,8 +69,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 = try std.fs.path.join(b.allocator, &.{ std.fs.path.dirname(@src().file) orelse ".", "config.h" });
-        defer b.allocator.free(file);
+        const file = b.pathJoin(&.{ srcDir(), "config.h" });
         const content = try std.fs.cwd().readFileAlloc(b.allocator, file, std.math.maxInt(usize));
         defer b.allocator.free(content);
 
@@ -115,7 +118,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("src/external/glfw/include"));
+        raylib.addIncludePath(b.path(b.pathJoin(&.{ srcDir(), "external/glfw/include" })));
     }
 
     var c_source_files = try std.ArrayList([]const u8).initCapacity(b.allocator, 2);
@@ -168,17 +171,16 @@ fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.
                 if (options.linux_display_backend == .Wayland or options.linux_display_backend == .Both) {
                     _ = b.findProgram(&.{"wayland-scanner"}, &.{}) catch {
                         std.log.err(
-                            \\ Wayland may not be installed on the system.
+                            \\ `wayland-scanner` may not be installed on the system.
                             \\ You can switch to X11 in your `build.zig` by changing `Options.linux_display_backend`
                         , .{});
-                        @panic("No Wayland");
+                        @panic("`wayland-scanner` not found");
                     };
                     raylib.defineCMacro("_GLFW_WAYLAND", null);
                     raylib.linkSystemLibrary("wayland-client");
                     raylib.linkSystemLibrary("wayland-cursor");
                     raylib.linkSystemLibrary("wayland-egl");
                     raylib.linkSystemLibrary("xkbcommon");
-                    raylib.addIncludePath(b.path("src"));
                     waylandGenerate(b, raylib, "wayland.xml", "wayland-client-protocol");
                     waylandGenerate(b, raylib, "xdg-shell.xml", "xdg-shell-client-protocol");
                     waylandGenerate(b, raylib, "xdg-decoration-unstable-v1.xml", "xdg-decoration-unstable-v1-client-protocol");
@@ -229,7 +231,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("src/rglfw.c"),
+                .file = b.path(b.pathJoin(&.{ srcDir(), "rglfw.c" })),
                 .flags = raylib_flags_arr.items,
             });
             _ = raylib_flags_arr.pop();
@@ -251,8 +253,7 @@ fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.
                 @panic("Pass '--sysroot \"$EMSDK/upstream/emscripten\"'");
             }
 
-            const cache_include = std.fs.path.join(b.allocator, &.{ b.sysroot.?, "cache", "sysroot", "include" }) catch @panic("Out of memory");
-            defer b.allocator.free(cache_include);
+            const cache_include = b.pathJoin(&.{ b.sysroot.?, "cache", "sysroot", "include" });
 
             var dir = std.fs.openDirAbsolute(cache_include, std.fs.Dir.OpenDirOptions{ .access_sub_paths = true, .no_follow = true }) catch @panic("No emscripten cache. Generate it!");
             dir.close();
@@ -263,9 +264,8 @@ fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.
         },
     }
 
-    raylib.addIncludePath(b.path("src"));
     raylib.root_module.addCSourceFiles(.{
-        .root = b.path("src"),
+        .root = b.path(srcDir()),
         .files = c_source_files.items,
         .flags = raylib_flags_arr.items,
     });
@@ -358,6 +358,7 @@ pub fn build(b: *std.Build) !void {
     const options = Options{
         .platform = b.option(PlatformBackend, "platform", "Choose the platform backedn for desktop target") orelse defaults.platform,
         .raudio = b.option(bool, "raudio", "Compile with audio support") orelse defaults.raudio,
+        .raygui = b.option(bool, "raygui", "Compile with raygui support") orelse defaults.raygui,
         .rmodels = b.option(bool, "rmodels", "Compile with models support") orelse defaults.rmodels,
         .rtext = b.option(bool, "rtext", "Compile with text support") orelse defaults.rtext,
         .rtextures = b.option(bool, "rtextures", "Compile with textures support") orelse defaults.rtextures,
@@ -370,17 +371,21 @@ pub fn build(b: *std.Build) !void {
 
     const lib = try compileRaylib(b, target, optimize, options);
 
-    lib.installHeader(b.path("src/raylib.h"), "raylib.h");
-    lib.installHeader(b.path("src/raymath.h"), "raymath.h");
-    lib.installHeader(b.path("src/rlgl.h"), "rlgl.h");
+    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");
 
     b.installArtifact(lib);
 }
 
-const waylandDir = "src/external/glfw/deps/wayland";
-
-fn waylandGenerate(b: *std.Build, raylib: *std.Build.Step.Compile, comptime protocol: []const u8, comptime basename: []const u8) void {
-    const protocolDir = waylandDir ++ "/" ++ protocol;
+fn waylandGenerate(
+    b: *std.Build,
+    raylib: *std.Build.Step.Compile,
+    comptime protocol: []const u8,
+    comptime basename: []const u8,
+) void {
+    const waylandDir = b.pathJoin(&.{ srcDir(), "external/glfw/deps/wayland" });
+    const protocolDir = b.pathJoin(&.{ waylandDir, protocol });
     const clientHeader = basename ++ ".h";
     const privateCode = basename ++ "-code.h";