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
This commit is contained in:
parent
c4be013294
commit
d3f86eb957
2 changed files with 26 additions and 18 deletions
|
@ -9,3 +9,6 @@ pub fn build(b: *std.Build) !void {
|
||||||
// expose helper functions to user's build.zig
|
// expose helper functions to user's build.zig
|
||||||
pub const addRaylib = raylib.addRaylib;
|
pub const addRaylib = raylib.addRaylib;
|
||||||
pub const addRaygui = raylib.addRaygui;
|
pub const addRaygui = raylib.addRaygui;
|
||||||
|
|
||||||
|
// expose options for compiling
|
||||||
|
pub const RaylibOptions = raylib.Options;
|
||||||
|
|
|
@ -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 {
|
fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, options: Options) !*std.Build.Step.Compile {
|
||||||
raylib_flags_arr.clearRetainingCapacity();
|
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
|
"-fno-sanitize=undefined", // https://github.com/raysan5/raylib/issues/3674
|
||||||
});
|
});
|
||||||
if (options.config) |config| {
|
if (options.config) |config| {
|
||||||
const file = try std.fs.path.join(b.allocator, &.{ std.fs.path.dirname(@src().file) orelse ".", "config.h" });
|
const file = b.pathJoin(&.{ srcDir(), "config.h" });
|
||||||
defer b.allocator.free(file);
|
|
||||||
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);
|
||||||
|
|
||||||
|
@ -115,7 +118,7 @@ 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("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);
|
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) {
|
if (options.linux_display_backend == .Wayland or options.linux_display_backend == .Both) {
|
||||||
_ = b.findProgram(&.{"wayland-scanner"}, &.{}) catch {
|
_ = b.findProgram(&.{"wayland-scanner"}, &.{}) catch {
|
||||||
std.log.err(
|
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`
|
\\ 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.defineCMacro("_GLFW_WAYLAND", null);
|
||||||
raylib.linkSystemLibrary("wayland-client");
|
raylib.linkSystemLibrary("wayland-client");
|
||||||
raylib.linkSystemLibrary("wayland-cursor");
|
raylib.linkSystemLibrary("wayland-cursor");
|
||||||
raylib.linkSystemLibrary("wayland-egl");
|
raylib.linkSystemLibrary("wayland-egl");
|
||||||
raylib.linkSystemLibrary("xkbcommon");
|
raylib.linkSystemLibrary("xkbcommon");
|
||||||
raylib.addIncludePath(b.path("src"));
|
|
||||||
waylandGenerate(b, raylib, "wayland.xml", "wayland-client-protocol");
|
waylandGenerate(b, raylib, "wayland.xml", "wayland-client-protocol");
|
||||||
waylandGenerate(b, raylib, "xdg-shell.xml", "xdg-shell-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");
|
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.
|
// 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("src/rglfw.c"),
|
.file = b.path(b.pathJoin(&.{ srcDir(), "rglfw.c" })),
|
||||||
.flags = raylib_flags_arr.items,
|
.flags = raylib_flags_arr.items,
|
||||||
});
|
});
|
||||||
_ = raylib_flags_arr.pop();
|
_ = 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\"'");
|
@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");
|
const cache_include = b.pathJoin(&.{ b.sysroot.?, "cache", "sysroot", "include" });
|
||||||
defer b.allocator.free(cache_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!");
|
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();
|
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(.{
|
raylib.root_module.addCSourceFiles(.{
|
||||||
.root = b.path("src"),
|
.root = b.path(srcDir()),
|
||||||
.files = c_source_files.items,
|
.files = c_source_files.items,
|
||||||
.flags = raylib_flags_arr.items,
|
.flags = raylib_flags_arr.items,
|
||||||
});
|
});
|
||||||
|
@ -358,6 +358,7 @@ pub fn build(b: *std.Build) !void {
|
||||||
const options = Options{
|
const options = Options{
|
||||||
.platform = b.option(PlatformBackend, "platform", "Choose the platform backedn for desktop target") orelse defaults.platform,
|
.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,
|
.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,
|
.rmodels = b.option(bool, "rmodels", "Compile with models support") orelse defaults.rmodels,
|
||||||
.rtext = b.option(bool, "rtext", "Compile with text support") orelse defaults.rtext,
|
.rtext = b.option(bool, "rtext", "Compile with text support") orelse defaults.rtext,
|
||||||
.rtextures = b.option(bool, "rtextures", "Compile with textures support") orelse defaults.rtextures,
|
.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);
|
const lib = try compileRaylib(b, target, optimize, options);
|
||||||
|
|
||||||
lib.installHeader(b.path("src/raylib.h"), "raylib.h");
|
lib.installHeader(b.path(b.pathJoin(&.{ srcDir(), "raylib.h" })), "raylib.h");
|
||||||
lib.installHeader(b.path("src/raymath.h"), "raymath.h");
|
lib.installHeader(b.path(b.pathJoin(&.{ srcDir(), "raymath.h" })), "raymath.h");
|
||||||
lib.installHeader(b.path("src/rlgl.h"), "rlgl.h");
|
lib.installHeader(b.path(b.pathJoin(&.{ srcDir(), "rlgl.h" })), "rlgl.h");
|
||||||
|
|
||||||
b.installArtifact(lib);
|
b.installArtifact(lib);
|
||||||
}
|
}
|
||||||
|
|
||||||
const waylandDir = "src/external/glfw/deps/wayland";
|
fn waylandGenerate(
|
||||||
|
b: *std.Build,
|
||||||
fn waylandGenerate(b: *std.Build, raylib: *std.Build.Step.Compile, comptime protocol: []const u8, comptime basename: []const u8) void {
|
raylib: *std.Build.Step.Compile,
|
||||||
const protocolDir = waylandDir ++ "/" ++ protocol;
|
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 clientHeader = basename ++ ".h";
|
||||||
const privateCode = basename ++ "-code.h";
|
const privateCode = basename ++ "-code.h";
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue