Skip to content

Commit

Permalink
build.zig: Fix @src logic and a few things
Browse files Browse the repository at this point in the history
  • Loading branch information
sagehane committed Oct 13, 2024
1 parent f5328a9 commit 3a3a806
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions src/build.zig
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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");
Expand All @@ -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 {
Expand All @@ -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);

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

0 comments on commit 3a3a806

Please sign in to comment.