Files
voxel-game/build.zig

109 lines
3.1 KiB
Zig

const std = @import("std");
const zon = @import("build.zig.zon");
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const llvm = b.option(bool, "llvm", "Use LLVM and LLD") orelse false;
const media_dep = b.dependency("media", .{
.target = target,
});
const vecmath_dep = b.dependency("vecmath", .{
.target = target,
});
const vulkan_dep = b.dependency("vulkan_zig", .{
.target = target,
.optimize = optimize,
.registry = b.path("vendor/vk.xml"),
});
const zglfw_dep = b.dependency("zglfw", .{
.target = target,
.optimize = optimize,
.import_vulkan = true,
});
const media_mod = media_dep.module("media");
const vecmath_mod = vecmath_dep.module("vecmath");
const vulkan_mod = vulkan_dep.module("vulkan-zig");
const zglfw_mod = zglfw_dep.module("root");
zglfw_mod.addImport("vulkan", vulkan_mod);
const zglfw_lib = zglfw_dep.artifact("glfw");
const exe_mod = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
exe_mod.addImport("media", media_mod);
exe_mod.addImport("vecmath", vecmath_mod);
exe_mod.addImport("vulkan", vulkan_mod);
exe_mod.addImport("zglfw", zglfw_mod);
exe_mod.linkLibrary(zglfw_lib);
try addShaders(b, exe_mod);
const options = b.addOptions();
options.addOption([]const u8, "version", zon.version);
exe_mod.addOptions("config", options);
const exe = b.addExecutable(.{
.name = "voxel-game",
.root_module = exe_mod,
.use_llvm = llvm,
.use_lld = llvm,
});
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the game");
run_step.dependOn(&run_cmd.step);
}
fn addShaders(b: *std.Build, exe_mod: *std.Build.Module) !void {
var dir = try b.build_root.handle.openDir(b.graph.io, "assets/shaders", .{ .iterate = true });
defer dir.close(b.graph.io);
var it = dir.iterateAssumeFirstIteration();
while (try it.next(b.graph.io)) |entry| {
if (entry.kind != .file) continue;
const stem = std.fs.path.stem(entry.name);
const extension = std.fs.path.extension(entry.name);
if (!std.mem.eql(u8, extension, ".slang")) continue;
const slangc = b.addSystemCommand(&.{
"slangc",
"-g",
"-target",
"spirv",
"-fvk-use-scalar-layout",
"-matrix-layout-column-major",
});
slangc.addArg("-o");
const spv = slangc.addOutputFileArg(b.fmt("{s}.spv", .{stem}));
slangc.addArg("-depfile");
_ = slangc.addDepFileOutputArg(b.fmt("{s}.d", .{stem}));
slangc.addFileArg(b.path(b.pathJoin(&.{ "assets/shaders", entry.name })));
exe_mod.addAnonymousImport(b.fmt("shaders/{s}", .{stem}), .{ .root_source_file = spv });
}
}