build.zig: Fix @src logic

This commit is contained in:
Sage Hane 2024-10-10 15:33:35 +09:00
parent 3fb1ba25ac
commit 9b9c72eb0d
No known key found for this signature in database

View File

@ -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);
@ -178,7 +181,6 @@ fn compileRaylib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.
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,
}); });
@ -370,17 +370,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";