2024-05-29 20:27:02 +02:00
|
|
|
const builtin = @import("builtin");
|
|
|
|
|
2023-04-19 01:25:35 +02:00
|
|
|
const std = @import("std");
|
|
|
|
|
2024-05-29 20:27:02 +02:00
|
|
|
pub fn build(b: *std.Build) !void {
|
2023-07-10 02:10:56 +02:00
|
|
|
const target = b.standardTargetOptions(.{});
|
|
|
|
const optimize = b.standardOptimizeOption(.{});
|
|
|
|
|
2024-05-29 20:27:02 +02:00
|
|
|
const coral_module = b.createModule(.{
|
|
|
|
.root_source_file = b.path("src/coral/coral.zig"),
|
2023-04-19 01:25:35 +02:00
|
|
|
});
|
|
|
|
|
2024-05-29 20:27:02 +02:00
|
|
|
const ona_module = add: {
|
|
|
|
const sokol_dependency = b.dependency("sokol", .{
|
2023-07-10 02:10:56 +02:00
|
|
|
.target = target,
|
|
|
|
.optimize = optimize,
|
2023-04-19 01:25:35 +02:00
|
|
|
});
|
|
|
|
|
2024-05-29 20:27:02 +02:00
|
|
|
const module = b.addModule("ona", .{
|
|
|
|
.root_source_file = b.path("src/ona/ona.zig"),
|
2023-04-19 01:25:35 +02:00
|
|
|
|
2024-05-29 20:27:02 +02:00
|
|
|
.imports = &.{
|
|
|
|
.{
|
|
|
|
.name = "sokol",
|
|
|
|
.module = sokol_dependency.module("sokol"),
|
|
|
|
},
|
|
|
|
|
|
|
|
.{
|
|
|
|
.name = "coral",
|
|
|
|
.module = coral_module,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
break: add module;
|
|
|
|
};
|
2023-07-11 00:44:27 +02:00
|
|
|
|
|
|
|
b.step("test", "Run unit tests").dependOn(create: {
|
|
|
|
const tests = b.addTest(.{
|
2024-05-29 20:52:46 +02:00
|
|
|
.root_source_file = b.path("src/main.zig"),
|
2023-07-11 00:44:27 +02:00
|
|
|
.target = target,
|
|
|
|
.optimize = optimize,
|
|
|
|
});
|
|
|
|
|
|
|
|
break: create &tests.step;
|
2023-07-10 02:10:56 +02:00
|
|
|
});
|
2024-05-29 20:27:02 +02:00
|
|
|
|
|
|
|
b.installArtifact(add: {
|
|
|
|
const compile_step = b.addExecutable(.{
|
|
|
|
.name = "main",
|
|
|
|
.root_source_file = b.path("src/main.zig"),
|
|
|
|
.target = target,
|
|
|
|
.optimize = optimize,
|
|
|
|
});
|
|
|
|
|
|
|
|
compile_step.root_module.addImport("ona", ona_module);
|
|
|
|
compile_step.root_module.addImport("coral", coral_module);
|
|
|
|
compile_step.linkLibC();
|
|
|
|
compile_step.linkSystemLibrary("SDL2");
|
|
|
|
|
|
|
|
try depend_on_shaders(b, target, "src/ona/gfx/shaders/", &compile_step.step);
|
|
|
|
|
|
|
|
break: add compile_step;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn depend_on_shaders(
|
|
|
|
b: *std.Build,
|
|
|
|
target: std.Build.ResolvedTarget,
|
|
|
|
shader_dir_path: []const u8,
|
|
|
|
step: *std.Build.Step,
|
|
|
|
) !void {
|
|
|
|
var dir = try std.fs.cwd().openDir(shader_dir_path, .{ .iterate = true });
|
|
|
|
|
|
|
|
defer dir.close();
|
|
|
|
|
|
|
|
var walker = try dir.walk(b.allocator);
|
|
|
|
|
|
|
|
defer walker.deinit();
|
|
|
|
|
|
|
|
const shdc_path = switch (builtin.os.tag) {
|
|
|
|
.windows => "./tools/sokol-shdc.exe",
|
|
|
|
.linux => "./tools/sokol-shdc",
|
|
|
|
else => @compileError("cannot compile sokol shaders on this platform"),
|
|
|
|
};
|
|
|
|
|
|
|
|
const path_buffer_max = 255;
|
|
|
|
var input_path_buffer = [_]u8{undefined} ** path_buffer_max;
|
|
|
|
var output_path_buffer = [_]u8{undefined} ** path_buffer_max;
|
|
|
|
|
|
|
|
const glsl = if (target.result.isDarwin()) "glsl410" else "glsl430";
|
|
|
|
const slang = glsl ++ ":metal_macos:hlsl5:glsl300es:wgsl";
|
|
|
|
|
|
|
|
while (try walker.next()) |entry| {
|
|
|
|
if (entry.kind != .file or !std.mem.endsWith(u8, entry.path, ".glsl")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const input_path = try std.fmt.bufPrint(&input_path_buffer, "{s}{s}", .{shader_dir_path, entry.path});
|
|
|
|
const output_path = try std.fmt.bufPrint(&output_path_buffer, "{s}.zig", .{input_path});
|
|
|
|
const output = std.fs.path.basename(output_path);
|
|
|
|
|
|
|
|
dir.access(output, .{.mode = .read_only}) catch {
|
|
|
|
const cmd = b.addSystemCommand(&.{
|
|
|
|
shdc_path,
|
|
|
|
"-i",
|
|
|
|
input_path,
|
|
|
|
"-o",
|
|
|
|
output_path,
|
|
|
|
"-l",
|
|
|
|
slang,
|
|
|
|
"-f",
|
|
|
|
"sokol_zig",
|
|
|
|
});
|
|
|
|
|
|
|
|
step.dependOn(&cmd.step);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
|
|
|
|
if ((try dir.statFile(entry.path)).mtime > (try dir.statFile(output)).mtime) {
|
|
|
|
const cmd = b.addSystemCommand(&.{
|
|
|
|
shdc_path,
|
|
|
|
"-i",
|
|
|
|
input_path,
|
|
|
|
"-o",
|
|
|
|
output_path,
|
|
|
|
"-l",
|
|
|
|
slang,
|
|
|
|
"-f",
|
|
|
|
"sokol_zig",
|
|
|
|
});
|
|
|
|
|
|
|
|
step.dependOn(&cmd.step);
|
|
|
|
}
|
|
|
|
}
|
2023-04-19 01:25:35 +02:00
|
|
|
}
|