-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.zig
318 lines (272 loc) · 11.4 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
const std = @import ("std");
const zig_version = @import ("builtin").zig_version;
const glfw = @import ("build/glfw.zig");
const vk = @import ("build/vk.zig");
const imgui = @import ("build/imgui.zig");
const shaders = @import ("build/shaders.zig");
const utils = @import ("build/utils.zig");
const Options = utils.Options;
const Package = utils.Package;
const Profile = utils.Profile;
const zon = utils.zon;
pub fn build (builder: *std.Build) !void
{
try requirements ();
const profile = try parse_options (builder);
const shaders_module = try run_shaders_compiler (builder, &profile);
try run_exe (builder, &profile, shaders_module);
try run_test (builder, &profile);
}
fn requirements () !void
{
if (zig_version.order (
try std.SemanticVersion.parse (zon.min_zig_version)) == .lt)
std.debug.panic ("{s} needs at least Zig {s} to be build",
.{ zon.name, zon.min_zig_version, });
}
fn turbo (profile: *Profile) !void
{
// Keep this for debug purpose
// profile.optimize = .Debug;
// profile.variables.addOption (u8, "log_level", 2);
profile.optimize = .ReleaseFast;
profile.variables.addOption ([] const u8, "log_dir", "");
profile.variables.addOption (u8, "log_level", 0);
profile.variables.addOption ([] const [] const [] const u8,
"vk_optional_extensions", &.{});
profile.compile_options = .{
.optimization = .Performance,
.vulkan_env_version = std.meta.stringToEnum (
shaders.Options.VulkanEnvVersion, profile.options.vkminor).?,
};
}
fn dev (builder: *std.Build, profile: *Profile) !void
{
const log_dir = "log";
// Make log directory if not present only in dev mode
builder.build_root.handle.makeDir (log_dir) catch |err|
{
// Do not return error if log directory already exists
if (err != std.fs.File.OpenError.PathAlreadyExists) return err;
};
profile.optimize = .Debug;
profile.variables.addOption ([] const u8, "log_dir",
try builder.build_root.join (builder.allocator, &.{ log_dir, }));
profile.variables.addOption (u8, "log_level", 2);
profile.variables.addOption ([] const [] const [] const u8,
"vk_optional_extensions", &.{
&.{ "EXT", "DEVICE_ADDRESS_BINDING_REPORT", },
&.{ "EXT", "VALIDATION_FEATURES", },
&.{ "KHR", "SHADER_NON_SEMANTIC_INFO", },
});
profile.compile_options = .{
.optimization = .Zero,
.vulkan_env_version = std.meta.stringToEnum (
shaders.Options.VulkanEnvVersion, profile.options.vkminor).?,
};
}
fn default (builder: *std.Build, profile: *Profile) !void
{
profile.optimize = builder.standardOptimizeOption (.{});
profile.variables.addOption ([] const u8, "log_dir", profile.options.logdir);
profile.variables.addOption (u8, "log_level", 1);
profile.variables.addOption ([] const [] const [] const u8,
"vk_optional_extensions", &.{
&.{ "EXT", "DEVICE_ADDRESS_BINDING_REPORT", },
});
profile.compile_options = .{
.optimization = .Zero,
.vulkan_env_version = std.meta.stringToEnum (
shaders.Options.VulkanEnvVersion,
profile.options.vkminor).?,
};
}
fn parse_options (builder: *std.Build) !Profile
{
const options = Options {
.dev = builder.option (bool, std.meta.fieldInfo (Options, .dev).name,
"Build " ++ zon.name ++ " with full logging features. Default: " ++
(if (@field (Options.default, std.meta.fieldInfo (Options, .dev).name))
"enabled" else "disabled") ++ ".")
orelse @field (Options.default, std.meta.fieldInfo (Options, .dev).name),
.turbo = builder.option (bool, std.meta.fieldInfo (Options, .turbo).name,
"Build " ++ zon.name ++ " with optimized features. Default: " ++
(if (@field (Options.default, std.meta.fieldInfo (Options, .turbo).name))
"enabled" else "disabled") ++ ".")
orelse @field (Options.default, std.meta.fieldInfo (Options, .turbo).name),
.logdir = builder.option ([] const u8,
std.meta.fieldInfo (Options, .logdir).name,
"Absolute path to log directory. If not specified, logs are not registered in a file.")
orelse @field (Options.default, std.meta.fieldInfo (Options, .logdir).name),
.vkminor = builder.option ([] const u8,
std.meta.fieldInfo (Options, .vkminor).name,
"Vulkan minor version to use: Possible values: 0, 1, 2 or 3. Default value: " ++
@field (Options.default, std.meta.fieldInfo (Options, .vkminor).name) ++ ".")
orelse @field (Options.default, std.meta.fieldInfo (Options, .vkminor).name),
};
_ = std.fmt.parseInt (u2, options.vkminor, 10) catch
std.debug.panic ("-D{s} value should be 0, 1, 2 or 3",
.{ std.meta.fieldInfo (Options, .vkminor).name, });
if (options.turbo and options.dev)
std.debug.panic ("-D{s} and -D{s} can not be used together",
.{ std.meta.fieldInfo (Options, .turbo).name,
std.meta.fieldInfo (Options, .dev).name, })
else if (options.turbo and options.logdir.len > 0)
std.debug.panic ("-D{s} and -D{s} can not be used together",
.{ std.meta.fieldInfo (Options, .turbo).name,
std.meta.fieldInfo (Options, .logdir).name, });
var profile: Profile = undefined;
profile.target = builder.standardTargetOptions (.{});
profile.options = options;
profile.variables = builder.addOptions ();
profile.variables.addOption ([] const u8,
std.meta.fieldInfo (@TypeOf (zon), .name).name, zon.name);
profile.variables.addOption ([] const u8,
std.meta.fieldInfo (@TypeOf (zon), .version).name, zon.version);
profile.variables.addOption ([] const u8, "vk_minor",
profile.options.vkminor);
if (profile.options.turbo) try turbo (&profile)
else if (profile.options.dev) try dev (builder, &profile)
else try default (builder, &profile);
return profile;
}
fn link (builder: *std.Build, profile: *const Profile) !*Package
{
const cimgui_dep = builder.dependency ("cimgui", .{
.target = profile.target,
.optimize = profile.optimize,
.platform = .GLFW,
.renderer = .Vulkan,
});
const c = try Package.init (builder, profile, "c",
try builder.build_root.join (builder.allocator,
&.{ "src", zon.name, "bindings", "raw.zig", }));
c.link (cimgui_dep.artifact ("cimgui"));
return c;
}
fn manage_deps (glfw_pkg: *Package, vk_pkg: *Package) !void
{
try glfw_pkg.get ("window").put (vk_pkg.get ("instance"), .{});
try glfw_pkg.get ("window").put (vk_pkg.get ("khr").get ("surface"), .{});
try vk_pkg.put (glfw_pkg.get ("vk"), .{ .pkg_name = "glfw", });
}
fn import (builder: *std.Build, exe: *std.Build.Step.Compile,
profile: *const Profile, shaders_module: *std.Build.Module) !void
{
const datetime_dep = builder.dependency ("zig-datetime", .{
.target = profile.target,
.optimize = profile.optimize,
});
const datetime = datetime_dep.module ("zig-datetime");
const jdz_dep = builder.dependency ("jdz_allocator", .{
.target = profile.target,
.optimize = profile.optimize,
});
const jdz = jdz_dep.module ("jdz_allocator");
const c = try link (builder, profile);
const glfw_pkg = try glfw.import (builder, profile, c);
const vk_pkg = try vk.import (builder, profile, c);
const imgui_pkg = try imgui.import (builder, profile, c, glfw_pkg, vk_pkg);
try manage_deps (glfw_pkg, vk_pkg);
const build_options = profile.variables.createModule ();
const logger = builder.createModule (.{
.root_source_file = .{ .cwd_relative = try builder.build_root.join (
builder.allocator, &.{ "src", zon.name, "logger.zig", }), },
.target = profile.target,
.optimize = profile.optimize,
});
logger.addImport ("build", build_options);
logger.addImport ("datetime", datetime);
logger.addImport ("jdz", jdz);
const instance = builder.createModule (.{
.root_source_file = .{ .cwd_relative = try builder.build_root.join (
builder.allocator, &.{ "src", zon.name, "vk", "instance",
if (profile.options.turbo) "turbo.zig" else "default.zig", }), },
.target = profile.target,
.optimize = profile.optimize,
});
instance.addImport ("logger", logger);
instance.addImport ("vk", vk_pkg.module);
for ([_] struct { name: [] const u8, ptr: *std.Build.Module, } {
.{ .name = "datetime", .ptr = datetime, },
.{ .name = "jdz", .ptr = jdz, },
.{ .name = "shader", .ptr = shaders_module, },
.{ .name = "glfw", .ptr = glfw_pkg.module, },
.{ .name = "vk", .ptr = vk_pkg.module, },
.{ .name = "imgui", .ptr = imgui_pkg.module, },
.{ .name = "logger", .ptr = logger, },
.{ .name = "instance", .ptr = instance, },
}) |module| exe.root_module.addImport (module.name, module.ptr);
}
fn run_shaders_compiler (builder: *std.Build,
profile: *const Profile) !*std.Build.Module
{
const shaders_compiler = builder.addExecutable (.{
.name = "shaders_compiler",
.root_source_file = .{ .cwd_relative = try builder.build_root.join (
builder.allocator, &.{ "src", "compiler", "main.zig", }), },
.target = builder.host,
.optimize = .Debug,
});
const shaderc_dep = builder.dependency ("shaderc", .{
.target = profile.target,
.optimize = profile.optimize,
});
const c = builder.createModule (.{
.root_source_file = .{ .cwd_relative = try builder.build_root.join (
builder.allocator, &.{ "src", "compiler", "bindings", "raw.zig", }), },
.target = builder.host,
.optimize = .Debug,
});
c.linkLibrary (shaderc_dep.artifact ("shaderc"));
const shaderc = builder.createModule (.{
.root_source_file = .{ .cwd_relative = try builder.build_root.join (
builder.allocator,
&.{ "src", "compiler", "bindings", "shaderc", "shaderc.zig", }), },
.target = profile.target,
.optimize = profile.optimize,
});
shaderc.addImport ("c", c);
shaders_compiler.root_module.addImport ("shaderc", shaderc);
const install_shaders_compiler =
builder.addInstallArtifact (shaders_compiler, .{});
builder.install_tls.step.dependOn (&install_shaders_compiler.step);
var self_dependency = std.Build.Dependency { .builder = builder, };
const shaders_module = try shaders.Step.compileModule (
&self_dependency, profile.compile_options);
const shaders_compile_step = builder.addRunArtifact (shaders_compiler);
shaders_compile_step.step.dependOn (&install_shaders_compiler.step);
return shaders_module;
}
fn run_exe (builder: *std.Build, profile: *const Profile,
shaders_module: *std.Build.Module) !void
{
const exe = builder.addExecutable (.{
.name = zon.name,
.root_source_file = .{ .cwd_relative = try builder.build_root.join (
builder.allocator, &.{ "src", zon.name, "main.zig", }), },
.target = profile.target,
.optimize = profile.optimize,
});
try import (builder, exe, profile, shaders_module);
builder.installArtifact (exe);
const run_cmd = builder.addRunArtifact (exe);
run_cmd.step.dependOn (builder.getInstallStep ());
const run_step = builder.step ("run", "Run " ++ zon.name);
run_step.dependOn (&run_cmd.step);
}
fn run_test (builder: *std.Build, profile: *const Profile) !void
{
const unit_tests = builder.addTest (.{
.target = profile.target,
.optimize = profile.optimize,
.test_runner = .{ .cwd_relative = try builder.build_root.join (builder.allocator,
&.{ "test", "runner.zig", }), },
.root_source_file = .{ .cwd_relative = try builder.build_root.join (
builder.allocator, &.{ "test", "main.zig", }), },
});
unit_tests.step.dependOn (builder.getInstallStep ());
const run_unit_tests = builder.addRunArtifact (unit_tests);
const test_step = builder.step ("test", "Run tests");
test_step.dependOn (&run_unit_tests.step);
}