const builtin = @import("builtin"); const std = @import("std"); pub fn build(b: *std.Build) !void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); const coral_module = b.createModule(.{ .root_source_file = b.path("src/coral/coral.zig"), }); const ona_module = add: { const sokol_dependency = b.dependency("sokol", .{ .target = target, .optimize = optimize, }); const module = b.addModule("ona", .{ .root_source_file = b.path("src/ona/ona.zig"), .imports = &.{ .{ .name = "sokol", .module = sokol_dependency.module("sokol"), }, .{ .name = "coral", .module = coral_module, }, }, }); break: add module; }; b.step("test", "Run unit tests").dependOn(create: { const tests = b.addTest(.{ .root_source_file = b.path("src/test.zig"), .target = target, .optimize = optimize, }); break: create &tests.step; }); 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); } } }