168 lines
5.3 KiB
Zig
168 lines
5.3 KiB
Zig
const std = @import("std");
|
|
|
|
const BuildConfig = struct {
|
|
module_target: std.Build.ResolvedTarget,
|
|
spirv_target: std.Build.ResolvedTarget,
|
|
optimize: std.builtin.OptimizeMode,
|
|
|
|
fn scan_demos(self: BuildConfig, ona_module: *std.Build.Module) void {
|
|
const b = ona_module.owner;
|
|
const build_demos_step = b.step("demos", "Build demos");
|
|
|
|
b.default_step.dependOn(build_demos_step);
|
|
|
|
const cwd = std.fs.cwd();
|
|
|
|
var demos_dir = cwd.openDir("src/demos/", .{ .iterate = true }) catch {
|
|
return build_demos_step.dependOn(&b.addFail("failed to open demo files directory").step);
|
|
};
|
|
|
|
defer {
|
|
demos_dir.close();
|
|
}
|
|
|
|
var demos_iterator = demos_dir.iterate();
|
|
|
|
while (demos_iterator.next() catch {
|
|
return build_demos_step.dependOn(&b.addFail("failed to iterate over next entry in demos directory").step);
|
|
}) |entry| {
|
|
if (entry.kind != .file) {
|
|
continue;
|
|
}
|
|
|
|
if (!std.mem.eql(u8, std.fs.path.extension(entry.name), ".zig")) {
|
|
continue;
|
|
}
|
|
|
|
const demo_executable = b.addExecutable(.{
|
|
.name = std.fmt.allocPrint(b.allocator, "{s}_demo", .{std.fs.path.stem(entry.name)}) catch {
|
|
return build_demos_step.dependOn(&b.addFail("failed to allocate demo name buffer").step);
|
|
},
|
|
|
|
.root_module = b.createModule(.{
|
|
.root_source_file = b.path(b.pathJoin(&.{ "src/demos/", entry.name })),
|
|
.target = self.module_target,
|
|
.optimize = self.optimize,
|
|
|
|
.imports = &.{
|
|
.{
|
|
.name = "ona",
|
|
.module = ona_module,
|
|
},
|
|
},
|
|
}),
|
|
});
|
|
|
|
demo_executable.linkSystemLibrary2("SDL3", .{
|
|
.needed = true,
|
|
.preferred_link_mode = .dynamic,
|
|
});
|
|
|
|
const demo_installation = b.addInstallArtifact(demo_executable, .{});
|
|
|
|
build_demos_step.dependOn(&demo_installation.step);
|
|
}
|
|
}
|
|
|
|
// TODO: Revisit Zig shaders once the SPIRV target becomes more mature.
|
|
// pub fn addShaders(self: BuildConfig, module: *std.Build.Module, shader_paths: []const []const u8) void {
|
|
// const b = module.owner;
|
|
|
|
// for (shader_paths) |shader_path| {
|
|
// const shader_path_stem = std.fs.path.stem(shader_path);
|
|
|
|
// const module_identifier = std.mem.join(b.allocator, ".", &.{ shader_path_stem, "spirv" }) catch {
|
|
// @panic("OOM");
|
|
// };
|
|
|
|
// const spirv_object = b.addObject(.{
|
|
// .name = module_identifier,
|
|
// .root_source_file = b.path(shader_path),
|
|
// .target = self.spirv_target,
|
|
// .use_llvm = false,
|
|
// });
|
|
|
|
// module.addAnonymousImport(module_identifier, .{ .root_source_file = spirv_object.getEmittedBin() });
|
|
// }
|
|
// }
|
|
};
|
|
|
|
const CommonArgs = struct {
|
|
target: std.Build.ResolvedTarget,
|
|
optimize: std.builtin.OptimizeMode,
|
|
ona_module: *std.Build.Module,
|
|
};
|
|
|
|
pub fn build(b: *std.Build) void {
|
|
const config = BuildConfig{
|
|
.optimize = b.standardOptimizeOption(.{}),
|
|
.module_target = b.standardTargetOptions(.{}),
|
|
|
|
.spirv_target = b.resolveTargetQuery(.{
|
|
.cpu_arch = .spirv64,
|
|
.os_tag = .vulkan,
|
|
.cpu_model = .{ .explicit = &std.Target.spirv.cpu.vulkan_v1_2 },
|
|
.cpu_features_add = std.Target.spirv.featureSet(&.{.int64}),
|
|
.ofmt = .spirv,
|
|
}),
|
|
};
|
|
|
|
const shaderc_dependency = b.dependency("shaderc_zig", .{});
|
|
|
|
const sdl_dependency = b.dependency("sdl", .{
|
|
.target = config.module_target,
|
|
.optimize = config.optimize,
|
|
.preferred_linkage = .static,
|
|
});
|
|
|
|
const coral_module = b.addModule("coral", .{
|
|
.root_source_file = b.path("src/coral/coral.zig"),
|
|
.target = config.module_target,
|
|
.optimize = config.optimize,
|
|
});
|
|
|
|
const ona_module = b.addModule("ona", .{
|
|
.root_source_file = b.path("src/ona/ona.zig"),
|
|
.target = config.module_target,
|
|
.optimize = config.optimize,
|
|
.link_libc = true,
|
|
|
|
.imports = &.{
|
|
.{
|
|
.name = "coral",
|
|
.module = coral_module,
|
|
},
|
|
},
|
|
});
|
|
|
|
ona_module.linkLibrary(shaderc_dependency.artifact("shaderc"));
|
|
ona_module.linkLibrary(sdl_dependency.artifact("SDL3"));
|
|
|
|
// config.addShaders(ona_module, &.{
|
|
// "./src/ona/gfx/effect_shader.zig",
|
|
// "./src/ona/gfx/effect_fragment.zig",
|
|
// });
|
|
|
|
config.scan_demos(ona_module);
|
|
|
|
const test_tests = b.addTest(.{
|
|
.root_module = b.createModule(.{
|
|
.root_source_file = b.path("src/tests.zig"),
|
|
.target = config.module_target,
|
|
.optimize = config.optimize,
|
|
|
|
.imports = &.{
|
|
.{
|
|
.name = "coral",
|
|
.module = coral_module,
|
|
},
|
|
},
|
|
}),
|
|
});
|
|
|
|
const test_step = b.step("test", "Run unit tests");
|
|
|
|
test_step.dependOn(&b.addRunArtifact(test_tests).step);
|
|
test_step.dependOn(&b.addInstallArtifact(test_tests, .{}).step);
|
|
}
|