Backlog/lib/sdl3/build.zig

94 lines
2.9 KiB
Zig

const std = @import("std");
pub fn addShaderDefinition(
b: *std.Build,
comptime sdl3Path: []const u8,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
shaderName: []const u8,
jsonPath: std.Build.LazyPath,
) *std.Build.Module {
const reflectedZig = b.addSystemCommand(&[_][]const u8{"python"});
reflectedZig.addArg(sdl3Path ++ "/spvreflect/spvreflect.py");
reflectedZig.addFileArg(jsonPath);
reflectedZig.addArg("--output");
const reflectedZigOut = reflectedZig.addOutputFileArg(b.fmt("reflected/{s}.zig", .{shaderName}));
const module = b.createModule(.{ .root_source_file = reflectedZigOut, .optimize = optimize });
const dep = b.dependency("shaderTypes", .{
.target = target,
.optimize = optimize,
});
module.addImport("shaderTypes", dep.module("shaderTypes"));
return module;
}
pub fn shaderDefintion(
b: *std.Build,
module: *std.Build.Module,
comptime sdl3Path: []const u8,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
shaderName: []const u8,
jsonPath: std.Build.LazyPath,
) void {
const mod = addShaderDefinition(b, sdl3Path, target, optimize, shaderName, jsonPath);
module.addImport(shaderName, mod);
}
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const sdl_dep = b.dependency("sdl", .{
.target = target,
.optimize = optimize,
});
const sdl3_lib = sdl_dep.artifact("SDL3");
const sdl3_fwd = b.addModule("sdl3_lib", .{
.target = target,
.optimize = optimize,
.root_source_file = b.path("src/sdl3_lib_fwd.zig"),
});
sdl3_fwd.linkLibrary(sdl3_lib);
const mod = b.addModule("sdl3", .{
.target = target,
.optimize = optimize,
.root_source_file = b.path("src/sdl3.zig"),
});
const shaderTypes = b.dependency("shaderTypes", .{
.target = target,
.optimize = optimize,
});
mod.addImport("shaderTypes", shaderTypes.module("shaderTypes"));
mod.addIncludePath(b.path("SDL/include"));
mod.linkLibrary(sdl3_lib);
const test_step = b.step("test", "run unit tests for sdl3");
const tests = b.addExecutable(.{
.name = "hello-triangle",
.target = target,
.optimize = optimize,
.root_source_file = b.path("src/samples/hello-triangle.zig"),
.link_libc = true,
});
tests.root_module.addImport("sdl3", mod);
const vertexDefinitions = addShaderDefinition(b, ".", target, optimize, "hello-triangle.vert", b.path("content/hello-triangle.vert.json"));
tests.root_module.addImport("hello-triangle.vert", vertexDefinitions);
const runArtifact = b.addRunArtifact(tests);
test_step.dependOn(&runArtifact.step);
b.installArtifact(sdl3_lib);
b.installArtifact(tests);
}