-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.zig
110 lines (91 loc) · 3.48 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const std = @import("std");
const vkz = @import("vulkan_zig");
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "cycle",
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
const known_folders_dep = b.dependency("known_folders", .{});
exe.root_module.addImport("known_folders", known_folders_dep.module("known-folders"));
const glfw_dep = b.dependency("mach_glfw", .{});
exe.root_module.addImport("glfw", glfw_dep.module("mach-glfw"));
exe.root_module.addImport("vulkan", try vulkanModule(b));
exe.root_module.addImport("shaders", shadersModule(b));
exe.linkLibC();
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
const main_tests = b.addTest(.{
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
const run_main_tests = b.addRunArtifact(main_tests);
const main_tests_step = b.step("test", "Run main tests");
main_tests_step.dependOn(&run_main_tests.step);
}
fn vulkanModule(b: *std.Build) !*std.Build.Module {
const vk_hash = "3dae5d7fbf332970ae0a97d5ab05ae5db93e62f0";
const vk_file_name = vk_hash ++ "-vk.xml";
const vk_file_url = "https://raw.githubusercontent.com/KhronosGroup/Vulkan-Docs/" ++
vk_hash ++ "/xml/vk.xml";
const vk_file_path = try ensureCachedFile(b.allocator, b.cache_root.path.?, vk_file_name, vk_file_url);
const vkzig_dep = b.dependency("vulkan_zig", .{
.registry = vk_file_path,
});
return vkzig_dep.module("vulkan-zig");
}
fn shadersModule(b: *std.Build) *std.Build.Module {
const shader_comp = vkz.ShaderCompileStep.create(
b,
&[_][]const u8{"glslc"},
"-o",
);
shader_comp.add("vertex", "src/composite/shaders/vert.glsl", .{
.args = &[_][]const u8{"-fshader-stage=vertex"},
});
shader_comp.add("fragment", "src/composite/shaders/frag.glsl", .{
.args = &[_][]const u8{"-fshader-stage=fragment"},
});
return shader_comp.getModule();
}
fn ensureCachedFile(allocator: std.mem.Allocator, cache_root: []const u8, name: []const u8, url: []const u8) ![]const u8 {
const path = try std.fs.path.join(allocator, &.{ cache_root, name });
const file = std.fs.openFileAbsolute(path, .{}) catch |e| {
switch (e) {
error.FileNotFound => {
const result = try std.ChildProcess.run(.{
.allocator = allocator,
.argv = &.{ "curl", url, "-o", path },
});
allocator.free(result.stdout);
allocator.free(result.stderr);
switch (result.term) {
.Exited => |code| {
if (code != 0) {
return error.ExitCodeFailure;
}
},
.Signal, .Stopped, .Unknown => {
return error.ProcessTerminated;
},
}
return path;
},
else => {
return e;
},
}
};
file.close();
return path;
}