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

Update to latest zig and simplify build.zig #3905

Merged
merged 2 commits into from
Apr 10, 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
107 changes: 49 additions & 58 deletions src/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ const builtin = @import("builtin");

// This has been tested with zig version(s):
// 0.11.0
// 0.12.0-dev.2075+f5978181e
// 0.12.0-dev.2990+31763d28c
// 0.12.0-dev.3580+e204a6edb
//
// Anytype is used here to preserve compatibility, in 0.12.0dev the std.zig.CrossTarget type
// was reworked into std.Target.Query and std.Build.ResolvedTarget. Using anytype allows
Expand Down Expand Up @@ -51,37 +50,37 @@ pub fn addRaylib(b: *std.Build, target: anytype, optimize: std.builtin.OptimizeM

// No GLFW required on PLATFORM_DRM
if (!options.platform_drm) {
raylib.addIncludePath(.{ .cwd_relative = srcdir ++ "/external/glfw/include" });
raylib.addIncludePath(.{ .path = "src/external/glfw/include" });
}

addCSourceFilesVersioned(raylib, &.{
try join2(gpa, srcdir, "rcore.c"),
try join2(gpa, srcdir, "utils.c"),
"src/rcore.c",
"src/utils.c",
}, raylib_flags_arr.items);

if (options.raudio) {
addCSourceFilesVersioned(raylib, &.{
try join2(gpa, srcdir, "raudio.c"),
"src/raudio.c",
}, raylib_flags_arr.items);
}
if (options.rmodels) {
addCSourceFilesVersioned(raylib, &.{
try join2(gpa, srcdir, "rmodels.c"),
"src/rmodels.c",
}, raylib_flags_arr.items);
}
if (options.rshapes) {
addCSourceFilesVersioned(raylib, &.{
try join2(gpa, srcdir, "rshapes.c"),
"src/rshapes.c",
}, raylib_flags_arr.items);
}
if (options.rtext) {
addCSourceFilesVersioned(raylib, &.{
try join2(gpa, srcdir, "rtext.c"),
"src/rtext.c",
}, raylib_flags_arr.items);
}
if (options.rtextures) {
addCSourceFilesVersioned(raylib, &.{
try join2(gpa, srcdir, "rtextures.c"),
"src/rtextures.c",
}, raylib_flags_arr.items);
}

Expand All @@ -91,14 +90,14 @@ pub fn addRaylib(b: *std.Build, target: anytype, optimize: std.builtin.OptimizeM
if (options.raygui) {
const raygui_c_path = gen_step.add("raygui.c", "#define RAYGUI_IMPLEMENTATION\n#include \"raygui.h\"\n");
raylib.addCSourceFile(.{ .file = raygui_c_path, .flags = raylib_flags_arr.items });
raylib.addIncludePath(.{ .cwd_relative = srcdir });
raylib.addIncludePath(.{ .cwd_relative = srcdir ++ "/../../raygui/src" });
raylib.addIncludePath(.{ .path = "src" });
raylib.addIncludePath(.{ .path = "../raygui/src" });
}

switch (getOsTagVersioned(target)) {
.windows => {
addCSourceFilesVersioned(raylib, &.{
try join2(gpa, srcdir, "rglfw.c"),
"src/rglfw.c",
}, raylib_flags_arr.items);
raylib.linkSystemLibrary("winmm");
raylib.linkSystemLibrary("gdi32");
Expand All @@ -109,7 +108,7 @@ pub fn addRaylib(b: *std.Build, target: anytype, optimize: std.builtin.OptimizeM
.linux => {
if (!options.platform_drm) {
addCSourceFilesVersioned(raylib, &.{
try join2(gpa, srcdir, "rglfw.c"),
"src/rglfw.c",
}, raylib_flags_arr.items);
raylib.linkSystemLibrary("GL");
raylib.linkSystemLibrary("rt");
Expand All @@ -130,7 +129,7 @@ pub fn addRaylib(b: *std.Build, target: anytype, optimize: std.builtin.OptimizeM
raylib.linkSystemLibrary("wayland-cursor");
raylib.linkSystemLibrary("wayland-egl");
raylib.linkSystemLibrary("xkbcommon");
raylib.addIncludePath(.{ .path = srcdir });
raylib.addIncludePath(.{ .path = "src" });
try waylandGenerate(gpa, "wayland.xml", "wayland-client-protocol");
try waylandGenerate(gpa, "xdg-shell.xml", "xdg-shell-client-protocol");
try waylandGenerate(gpa, "xdg-decoration-unstable-v1.xml", "xdg-decoration-unstable-v1-client-protocol");
Expand Down Expand Up @@ -163,7 +162,7 @@ pub fn addRaylib(b: *std.Build, target: anytype, optimize: std.builtin.OptimizeM
},
.freebsd, .openbsd, .netbsd, .dragonfly => {
addCSourceFilesVersioned(raylib, &.{
try join2(gpa, srcdir, "rglfw.c"),
"src/rglfw.c",
}, raylib_flags_arr.items);
raylib.linkSystemLibrary("GL");
raylib.linkSystemLibrary("rt");
Expand All @@ -182,7 +181,7 @@ pub fn addRaylib(b: *std.Build, target: anytype, optimize: std.builtin.OptimizeM
// On macos rglfw.c include Objective-C files.
try raylib_flags_arr.append("-ObjC");
addCSourceFilesVersioned(raylib, &.{
try join2(gpa, srcdir, "rglfw.c"),
"src/rglfw.c",
}, raylib_flags_arr.items);
raylib.linkFramework("Foundation");
raylib.linkFramework("CoreServices");
Expand Down Expand Up @@ -259,24 +258,18 @@ pub fn build(b: *std.Build) !void {

const lib = try addRaylib(b, target, optimize, options);

lib.installHeader("src/raylib.h", "raylib.h");
lib.installHeader("src/raymath.h", "raymath.h");
lib.installHeader("src/rlgl.h", "rlgl.h");
installHeaderVersioned(lib, "src/raylib.h", "raylib.h");
installHeaderVersioned(lib, "src/raymath.h", "raymath.h");
installHeaderVersioned(lib, "src/rlgl.h", "rlgl.h");

if (options.raygui) {
lib.installHeader("../raygui/src/raygui.h", "raygui.h");
installHeaderVersioned(lib, "../raygui/src/raygui.h", "raygui.h");
}

b.installArtifact(lib);
}

const srcdir = struct {
fn getSrcDir() []const u8 {
return std.fs.path.dirname(@src().file).?;
}
}.getSrcDir();

const waylandDir = srcdir ++ "/external/glfw/deps/wayland";
const waylandDir = "src/external/glfw/deps/wayland";

fn getOsTagVersioned(target: anytype) std.Target.Os.Tag {
if (comptime builtin.zig_version.minor >= 12) {
Expand All @@ -291,47 +284,45 @@ fn addCSourceFilesVersioned(
files: []const []const u8,
flags: []const []const u8,
) void {
//- HACK(cabarger): I hate this so much!!!
if (comptime builtin.zig_version.minor >= 12) {
for (files) |file| {
exe.addCSourceFile(.{
.file = .{ .path = file },
.flags = flags,
});
}
exe.addCSourceFiles(.{
.files = files,
.flags = flags,
});
} else if (comptime builtin.zig_version.minor == 11) {
exe.addCSourceFiles(files, flags);
} else {
@compileError("Expected zig version 11 or 12");
}
}

fn waylandGenerate(allocator: std.mem.Allocator, comptime protocol: []const u8, comptime basename: []const u8) !void {
const protocolDir = waylandDir ++ "/" ++ protocol;
const clientHeader = srcdir ++ "/" ++ basename ++ ".h";
const privateCode = srcdir ++ "/" ++ basename ++ "-code.h";
fn installHeaderVersioned(
lib: *std.Build.Step.Compile,
source: []const u8,
dest: []const u8,
) void {
if (comptime builtin.zig_version.minor >= 12) {
_ = try std.process.Child.run(.{
.allocator = allocator,
.argv = &[_][]const u8{ "wayland-scanner", "client-header", protocolDir, clientHeader },
});
_ = try std.process.Child.run(.{
.allocator = allocator,
.argv = &[_][]const u8{ "wayland-scanner", "private-code", protocolDir, privateCode },
});
lib.installHeader(.{ .path = source }, dest);
} else {
_ = try std.process.Child.exec(.{
.allocator = allocator,
.argv = &[_][]const u8{ "wayland-scanner", "client-header", protocolDir, clientHeader },
});
_ = try std.process.Child.exec(.{
.allocator = allocator,
.argv = &[_][]const u8{ "wayland-scanner", "private-code", protocolDir, privateCode },
});
lib.installHeader(source, dest);
}
}

fn join2(allocator: std.mem.Allocator, path1: []const u8, path2: []const u8) ![]u8 {
const joinedPath = try std.fs.path.join(allocator, &[_][]const u8{ path1, path2 });
return joinedPath;
const childRunVersioned = if (builtin.zig_version.minor >= 12)
std.process.Child.run
else
std.process.Child.exec;

fn waylandGenerate(allocator: std.mem.Allocator, comptime protocol: []const u8, comptime basename: []const u8) !void {
const protocolDir = waylandDir ++ "/" ++ protocol;
const clientHeader = "src/" ++ basename ++ ".h";
const privateCode = "src/" ++ basename ++ "-code.h";
_ = try childRunVersioned(.{
.allocator = allocator,
.argv = &[_][]const u8{ "wayland-scanner", "client-header", protocolDir, clientHeader },
});
_ = try childRunVersioned(.{
.allocator = allocator,
.argv = &[_][]const u8{ "wayland-scanner", "private-code", protocolDir, privateCode },
});
}
Loading