2021-10-14 14:24:00 +03:00
|
|
|
const std = @import("std");
|
|
|
|
const builtin = @import("builtin");
|
|
|
|
|
2024-01-11 21:48:37 +03:00
|
|
|
// This has been tested to work with zig 0.11.0 (67709b6, Aug 4 2023) and zig 0.12.0-dev.2075+f5978181e (Jan 8 2024)
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
// us to accept both CrossTarget and ResolvedTarget and act accordingly in getOsTagVersioned.
|
|
|
|
fn add_module(comptime module: []const u8, b: *std.Build, target: anytype, optimize: std.builtin.OptimizeMode) !*std.Build.Step {
|
|
|
|
if (comptime builtin.zig_version.minor >= 12 and @TypeOf(target) != std.Build.ResolvedTarget) {
|
|
|
|
@compileError("Expected 'std.Build.ResolvedTarget' for argument 2 'target' in 'add_module', found '" ++ @typeName(@TypeOf(target)) ++ "'");
|
|
|
|
} else if (comptime builtin.zig_version.minor == 11 and @TypeOf(target) != std.zig.CrossTarget) {
|
|
|
|
@compileError("Expected 'std.zig.CrossTarget' for argument 2 'target' in 'add_module', found '" ++ @typeName(@TypeOf(target)) ++ "'");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (getOsTagVersioned(target) == .emscripten) {
|
2023-02-01 13:09:03 +03:00
|
|
|
@panic("Emscripten building via Zig unsupported");
|
|
|
|
}
|
|
|
|
|
2021-10-14 14:24:00 +03:00
|
|
|
const all = b.step(module, "All " ++ module ++ " examples");
|
2024-01-11 21:48:37 +03:00
|
|
|
var dir = try openIterableDirVersioned(std.fs.cwd(), module);
|
|
|
|
defer if (comptime builtin.zig_version.minor >= 12) dir.close();
|
|
|
|
|
2021-10-14 14:24:00 +03:00
|
|
|
var iter = dir.iterate();
|
|
|
|
while (try iter.next()) |entry| {
|
2023-10-13 20:55:52 +03:00
|
|
|
if (entry.kind != .file) continue;
|
2021-10-14 14:24:00 +03:00
|
|
|
const extension_idx = std.mem.lastIndexOf(u8, entry.name, ".c") orelse continue;
|
|
|
|
const name = entry.name[0..extension_idx];
|
|
|
|
const path = try std.fs.path.join(b.allocator, &.{ module, entry.name });
|
|
|
|
|
|
|
|
// zig's mingw headers do not include pthread.h
|
2024-01-11 21:48:37 +03:00
|
|
|
if (std.mem.eql(u8, "core_loading_thread", name) and getOsTagVersioned(target) == .windows) continue;
|
2021-10-14 14:24:00 +03:00
|
|
|
|
2023-02-05 15:03:03 +03:00
|
|
|
const exe = b.addExecutable(.{
|
|
|
|
.name = name,
|
|
|
|
.target = target,
|
|
|
|
.optimize = optimize,
|
|
|
|
});
|
2023-10-13 20:55:52 +03:00
|
|
|
exe.addCSourceFile(.{ .file = .{ .path = path }, .flags = &.{} });
|
2021-10-14 14:24:00 +03:00
|
|
|
exe.linkLibC();
|
2024-01-11 21:48:37 +03:00
|
|
|
exe.addObjectFile(switch (getOsTagVersioned(target)) {
|
2023-10-13 20:55:52 +03:00
|
|
|
.windows => .{ .path = "../zig-out/lib/raylib.lib" },
|
|
|
|
.linux => .{ .path = "../zig-out/lib/libraylib.a" },
|
|
|
|
.macos => .{ .path = "../zig-out/lib/libraylib.a" },
|
|
|
|
.emscripten => .{ .path = "../zig-out/lib/libraylib.a" },
|
2021-10-14 14:24:00 +03:00
|
|
|
else => @panic("Unsupported OS"),
|
|
|
|
});
|
|
|
|
|
2023-10-13 20:55:52 +03:00
|
|
|
exe.addIncludePath(.{ .path = "../src" });
|
|
|
|
exe.addIncludePath(.{ .path = "../src/external" });
|
|
|
|
exe.addIncludePath(.{ .path = "../src/external/glfw/include" });
|
2021-10-14 14:24:00 +03:00
|
|
|
|
2024-01-11 21:48:37 +03:00
|
|
|
switch (getOsTagVersioned(target)) {
|
2021-10-14 14:24:00 +03:00
|
|
|
.windows => {
|
|
|
|
exe.linkSystemLibrary("winmm");
|
|
|
|
exe.linkSystemLibrary("gdi32");
|
|
|
|
exe.linkSystemLibrary("opengl32");
|
2023-02-01 13:09:03 +03:00
|
|
|
|
|
|
|
exe.defineCMacro("PLATFORM_DESKTOP", null);
|
2021-10-14 14:24:00 +03:00
|
|
|
},
|
|
|
|
.linux => {
|
|
|
|
exe.linkSystemLibrary("GL");
|
|
|
|
exe.linkSystemLibrary("rt");
|
|
|
|
exe.linkSystemLibrary("dl");
|
|
|
|
exe.linkSystemLibrary("m");
|
|
|
|
exe.linkSystemLibrary("X11");
|
2023-02-01 13:09:03 +03:00
|
|
|
|
|
|
|
exe.defineCMacro("PLATFORM_DESKTOP", null);
|
2021-10-14 14:24:00 +03:00
|
|
|
},
|
2021-11-27 12:46:18 +03:00
|
|
|
.macos => {
|
|
|
|
exe.linkFramework("Foundation");
|
|
|
|
exe.linkFramework("Cocoa");
|
|
|
|
exe.linkFramework("OpenGL");
|
|
|
|
exe.linkFramework("CoreAudio");
|
|
|
|
exe.linkFramework("CoreVideo");
|
|
|
|
exe.linkFramework("IOKit");
|
2023-02-01 13:09:03 +03:00
|
|
|
|
|
|
|
exe.defineCMacro("PLATFORM_DESKTOP", null);
|
2021-11-27 12:46:18 +03:00
|
|
|
},
|
2021-10-14 14:24:00 +03:00
|
|
|
else => {
|
|
|
|
@panic("Unsupported OS");
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-10-13 20:55:52 +03:00
|
|
|
const install_cmd = b.addInstallArtifact(exe, .{});
|
|
|
|
|
|
|
|
const run_cmd = b.addRunArtifact(exe);
|
|
|
|
run_cmd.step.dependOn(&install_cmd.step);
|
|
|
|
|
|
|
|
const run_step = b.step(name, name);
|
|
|
|
run_step.dependOn(&run_cmd.step);
|
|
|
|
|
|
|
|
all.dependOn(&install_cmd.step);
|
2021-10-14 14:24:00 +03:00
|
|
|
}
|
|
|
|
return all;
|
|
|
|
}
|
|
|
|
|
2023-02-06 12:28:24 +03:00
|
|
|
pub fn build(b: *std.Build) !void {
|
2021-10-14 14:24:00 +03:00
|
|
|
// Standard target options allows the person running `zig build` to choose
|
|
|
|
// what target to build for. Here we do not override the defaults, which
|
|
|
|
// means any target is allowed, and the default is native. Other options
|
|
|
|
// for restricting supported target set are available.
|
|
|
|
const target = b.standardTargetOptions(.{});
|
2023-02-05 15:03:03 +03:00
|
|
|
// Standard optimization options allow the person running `zig build` to select
|
|
|
|
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
|
|
|
|
// set a preferred release mode, allowing the user to decide how to optimize.
|
|
|
|
const optimize = b.standardOptimizeOption(.{});
|
2021-10-14 14:24:00 +03:00
|
|
|
|
|
|
|
const all = b.getInstallStep();
|
|
|
|
|
2023-02-05 15:03:03 +03:00
|
|
|
all.dependOn(try add_module("audio", b, target, optimize));
|
|
|
|
all.dependOn(try add_module("core", b, target, optimize));
|
|
|
|
all.dependOn(try add_module("models", b, target, optimize));
|
|
|
|
all.dependOn(try add_module("others", b, target, optimize));
|
|
|
|
all.dependOn(try add_module("shaders", b, target, optimize));
|
|
|
|
all.dependOn(try add_module("shapes", b, target, optimize));
|
|
|
|
all.dependOn(try add_module("text", b, target, optimize));
|
|
|
|
all.dependOn(try add_module("textures", b, target, optimize));
|
2021-10-14 14:24:00 +03:00
|
|
|
}
|
2024-01-11 21:48:37 +03:00
|
|
|
|
|
|
|
fn getOsTagVersioned(target: anytype) std.Target.Os.Tag {
|
|
|
|
if (comptime builtin.zig_version.minor >= 12) {
|
|
|
|
return target.result.os.tag;
|
|
|
|
} else {
|
|
|
|
return target.getOsTag();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn openIterableDirVersioned(dir: std.fs.Dir, path: []const u8) !(if (builtin.zig_version.minor >= 12) std.fs.Dir else std.fs.IterableDir) {
|
|
|
|
if (comptime builtin.zig_version.minor >= 12) {
|
|
|
|
return dir.openDir(path, .{ .iterate = true });
|
|
|
|
} else {
|
|
|
|
return dir.openIterableDir(path, .{});
|
|
|
|
}
|
|
|
|
}
|