-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.zig
110 lines (92 loc) · 3.57 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 builtin = @import("builtin");
const std = @import("std");
pub fn build(b: *std.Build) !void {
const exe = try create_executable(b);
try compile_shaders(b, exe);
try create_run_command(b, exe);
}
fn create_executable(b: *std.Build) !*std.Build.Step.Compile {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "gardenrift",
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
const ember_dep = b.dependency("ember", .{
.target = target,
.optimize = optimize,
});
exe.addModule("ember", ember_dep.module("ember"));
const tehuti_dep = b.dependency("tehuti", .{
.target = target,
.optimize = optimize,
});
exe.addModule("tehuti", tehuti_dep.module("tehuti"));
try add_glfw(b, exe);
try add_vulkan(b, exe);
try add_openal(b, exe);
exe.linkLibC();
b.installArtifact(exe);
return exe;
}
fn add_glfw(b: *std.Build, exe: *std.Build.Step.Compile) !void {
_ = b;
exe.addIncludePath(.{ .path = "includes/glfw/include" });
exe.addObjectFile(.{ .path = "includes/glfw/lib/glfw3.dll" });
exe.addObjectFile(.{ .path = "includes/glfw/lib/libglfw3.a" });
exe.addObjectFile(.{ .path = "includes/glfw/lib/libglfw3dll.a" });
}
fn add_vulkan(b: *std.Build, exe: *std.Build.Step.Compile) !void {
_ = b;
exe.addIncludePath(.{ .path = "includes/vulkan/include" });
exe.addObjectFile(.{ .path = "includes/vulkan/lib/vulkan-1.lib" });
}
fn add_openal(b: *std.Build, exe: *std.Build.Step.Compile) !void {
_ = b;
exe.addIncludePath(.{ .path = "includes/openal/include" });
exe.addObjectFile(.{ .path = "includes/openal/lib/Win64/OpenAL32.lib" });
}
fn create_run_command(b: *std.Build, exe: *std.Build.Step.Compile) !void {
const run_cmd = b.addRunArtifact(exe);
if (b.args) |args| {
run_cmd.addArgs(args);
}
run_cmd.addPathDir("includes/glfw/lib/");
run_cmd.addPathDir("includes/vulkan/lib/");
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
}
fn compile_shaders(b: *std.Build, exe: *std.Build.Step.Compile) !void {
const shaders_dir_name = "assets/shaders";
var dir = try std.fs.cwd().openIterableDir(shaders_dir_name, .{});
defer dir.close();
errdefer dir.close();
var it = dir.iterate();
while (try it.next()) |file| {
const compiled_ext = ".spv";
// skip .spv files
if (file.name.len > 4 and std.mem.eql(u8, file.name[(file.name.len - 4)..], compiled_ext)) {
continue;
}
const input_file = file.name;
const input_file_path = try std.fs.path.join(b.allocator, &[_][]const u8{ shaders_dir_name, input_file });
var output_file = try std.ArrayList(u8).initCapacity(b.allocator, input_file.len + compiled_ext.len);
defer output_file.deinit();
try output_file.appendSlice(input_file);
try output_file.appendSlice(compiled_ext);
const output_file_path = try std.fs.path.join(b.allocator, &[_][]const u8{ shaders_dir_name, try output_file.toOwnedSlice() });
try compile_shader(b, exe, input_file_path, output_file_path);
}
}
fn compile_shader(b: *std.Build, exe: *std.Build.Step.Compile, input_file: []const u8, output_file: []const u8) !void {
const run_cmd = b.addSystemCommand(&[_][]const u8{
"glslc",
"--target-env=vulkan1.1",
"-o",
output_file,
input_file,
});
exe.step.dependOn(&run_cmd.step);
}