forked from Snektron/vulkan-zig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.zig
88 lines (72 loc) · 3.55 KB
/
build.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const std = @import("std");
const vkgen = @import("generator/index.zig");
pub const ShaderCompileStep = vkgen.ShaderCompileStep;
pub const VkGenerateStep = vkgen.VkGenerateStep;
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const vk_xml_path: ?[]const u8 = b.option([]const u8, "registry", "Override the path to the Vulkan registry");
// using the package manager, this artifact can be obtained by the user
// through `b.dependency(<name in build.zig.zon>, .{}).artifact("generator")`.
// with that, the user need only `.addArg("path/to/vk.xml")`, and then obtain
// a file source to the generated code with `.addOutputArg("vk.zig")`
const generator_exe = b.addExecutable(.{
.name = "generator",
.root_source_file = .{ .path = "generator/main.zig" },
.target = target,
.optimize = optimize,
});
b.installArtifact(generator_exe);
// or they can skip all that, and just make sure to pass `.registry = "path/to/vk.xml"` to `b.dependency`,
// and then obtain the module directly via `.module("vulkan-zig")`.
if (vk_xml_path) |path| {
const generate_cmd = b.addRunArtifact(generator_exe);
if (!std.fs.path.isAbsolute(path)) @panic("Make sure to assign an absolute path to the `registry` option (see: std.Build.pathFromRoot).\n");
generate_cmd.addArg(path);
_ = b.addModule("vulkan-zig", .{
.source_file = generate_cmd.addOutputFileArg("vk.zig"),
});
}
// remainder of the script is for local testing
const triangle_exe = b.addExecutable(.{
.name = "triangle",
.root_source_file = .{ .path = "examples/triangle.zig" },
.target = target,
.optimize = optimize,
});
b.installArtifact(triangle_exe);
triangle_exe.linkLibC();
triangle_exe.linkSystemLibrary("glfw");
const example_registry = b.option([]const u8, "example-registry", "Override the path to the Vulkan registry used for the examples") orelse "examples/vk.xml";
const gen = VkGenerateStep.create(b, example_registry);
triangle_exe.addModule("vulkan", gen.getModule());
const vk_zig_install_step = b.addInstallFile(gen.getSource(), "src/vk.zig");
b.getInstallStep().dependOn(&vk_zig_install_step.step);
const shaders = ShaderCompileStep.create(
b,
&[_][]const u8{ "glslc", "--target-env=vulkan1.2" },
"-o",
);
shaders.add("triangle_vert", "examples/shaders/triangle.vert", .{});
shaders.add("triangle_frag", "examples/shaders/triangle.frag", .{});
triangle_exe.addModule("shaders", shaders.getModule());
const triangle_run_cmd = b.addRunArtifact(triangle_exe);
triangle_run_cmd.step.dependOn(b.getInstallStep());
const triangle_run_step = b.step("run-triangle", "Run the triangle example");
triangle_run_step.dependOn(&triangle_run_cmd.step);
var test_target = b.addTest(.{
.root_source_file = .{ .path = "generator/index.zig" },
});
var test_step = b.step("test", "Run all the tests");
test_step.dependOn(&test_target.step);
// This test needs to be an object so that vulkan-zig can import types from the root.
// It does not need to run anyway.
const ref_all_decls_test = b.addObject(.{
.name = "ref-all-decls-test",
.root_source_file = .{ .path = "test/ref_all_decls.zig" },
.target = target,
.optimize = optimize,
});
ref_all_decls_test.addModule("vulkan", gen.getModule());
test_step.dependOn(&ref_all_decls_test.step);
}