Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Oct 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 31 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(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 {
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,8 @@ 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" })));
_ = 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);
Expand Down Expand Up @@ -231,7 +250,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 +284,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 +385,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 +403,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
Loading