Skip to content

Commit

Permalink
build.zig: Improve logic (#4375)
Browse files Browse the repository at this point in the history
* build.zig: Fix `@src` logic

* build.zig: Clarify build error

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

* build.zig: Expose `Options` type
  • Loading branch information
sagehane authored Oct 10, 2024
1 parent c4be013 commit d3f86eb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
3 changes: 3 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
41 changes: 23 additions & 18 deletions src/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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,
});
Expand Down Expand Up @@ -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,
Expand All @@ -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";

Expand Down

0 comments on commit d3f86eb

Please sign in to comment.