-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathbuild.zig
More file actions
383 lines (322 loc) · 15.4 KB
/
build.zig
File metadata and controls
383 lines (322 loc) · 15.4 KB
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
const std = @import("std");
fn link_windows_system_libraries(comptime T: type, mod: *T, is_gnu: bool) void {
const linkSystemLibrary = switch (T) {
std.Build.Module => std.Build.Module.linkSystemLibrary,
std.Build.Step.Compile => std.Build.Step.Compile.linkSystemLibrary2,
else => @compileError("Provided type must either be std.Build.Module or std.Build.Step.Compile"),
};
if (is_gnu) {
// For gnu, the linker needs the d3dcompiler dll since it can't find a suitable static lib
// (I'd guess it tries to search for something like "libd3dcompiler.a" instead of "d3dcompiler.lib").
linkSystemLibrary(mod, "d3dcompiler_47", .{});
// This seems to have something to do with the windows-result crate in wgpu-native's dependencies
linkSystemLibrary(mod, "api-ms-win-core-winrt-error-l1-1-0", .{});
} else {
linkSystemLibrary(mod, "d3dcompiler", .{});
// GetClientRect is unresolved unless we link this for msvc
linkSystemLibrary(mod, "user32", .{});
linkSystemLibrary(mod, "RuntimeObject", .{});
}
linkSystemLibrary(mod, "opengl32", .{});
linkSystemLibrary(mod, "gdi32", .{});
// COM-related
linkSystemLibrary(mod, "OleAut32", .{});
linkSystemLibrary(mod, "Ole32", .{});
// Apparently these are needed because of rust stdlib
linkSystemLibrary(mod, "ws2_32", .{});
linkSystemLibrary(mod, "userenv", .{});
// Needed by windows-rs (wgpu-native dependency)
linkSystemLibrary(mod, "propsys", .{});
}
fn link_mac_frameworks(mod: *std.Build.Step.Compile) void {
mod.linkFramework("Foundation");
mod.linkFramework("QuartzCore");
mod.linkFramework("Metal");
}
const WGPUBuildContext = struct {
link_mode: std.builtin.LinkMode,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
is_windows: bool,
is_mac: bool,
wgpu_dep: *std.Build.Dependency,
libwgpu_path: ?std.Build.LazyPath,
install_lib_dir: []const u8,
wgpu_mod: *std.Build.Module,
wgpu_c_mod: *std.Build.Module,
fn init(b: *std.Build) ?WGPUBuildContext {
const link_mode = b.option(std.builtin.LinkMode, "link_mode", "Use static linking instead of dynamic linking.") orelse .static;
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});
// Standard optimization options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
// set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption(.{});
const target_res = target.result;
const os_str = @tagName(target_res.os.tag);
const arch_str = @tagName(target_res.cpu.arch);
const mode_str = switch (optimize) {
.Debug => "debug",
else => "release",
};
const abi_str = switch (target_res.os.tag) {
.ios => switch (target_res.abi) {
.simulator => "_simulator",
else => "",
},
.windows => switch (target_res.abi) {
.msvc => "_msvc",
else => "_gnu",
},
else => "",
};
const target_name_slices = [_][:0]const u8{ "wgpu_", os_str, "_", arch_str, abi_str, "_", mode_str };
const maybe_target_name = std.mem.concatWithSentinel(b.allocator, u8, &target_name_slices, 0);
const target_name = maybe_target_name catch |err| {
std.debug.panic("Failed to format target name: {s}", .{@errorName(err)});
};
// Check if we have a dependency matching our selected target.
for (b.available_deps) |dep| {
const name, _ = dep;
if (std.mem.eql(u8, name, target_name)) {
break;
}
} else {
std.debug.panic("Could not find dependency matching target {s}", .{target_name});
}
const wgpu_mod = b.addModule("wgpu", .{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
.link_libcpp = true,
});
const wgpu_dep = b.lazyDependency(target_name, .{}) orelse return null;
const translate_step = b.addTranslateC(.{
// wgpu.h imports webgpu.h, so we get the contents of both files, as well as a bunch of libc garbage.
.root_source_file = wgpu_dep.path("include/webgpu/wgpu.h"),
.target = target,
.optimize = optimize,
});
const wgpu_c_mod = translate_step.addModule("wgpu-c");
wgpu_c_mod.resolved_target = target;
wgpu_c_mod.link_libcpp = true;
var libwgpu_path: ?std.Build.LazyPath = null;
var is_windows: bool = false;
var is_mac: bool = target_res.os.tag == .macos or target_res.os.tag == .ios;
// TODO: This seems like it could be made smaller, lots of repetitive code here.
switch (target_res.os.tag) {
.windows => {
is_windows = true;
if (target_res.abi == .msvc) {
// I feel like libcpp should work, but it definitely does not on msvc. Fortunately libc does.
wgpu_mod.link_libcpp = false;
wgpu_c_mod.link_libcpp = false;
wgpu_mod.link_libc = true;
wgpu_c_mod.link_libc = true;
if (link_mode == .static) {
libwgpu_path = wgpu_dep.path("lib/wgpu_native.lib");
link_windows_system_libraries(std.Build.Module, wgpu_mod, false);
link_windows_system_libraries(std.Build.Module, wgpu_c_mod, false);
} else {
libwgpu_path = wgpu_dep.path("lib/wgpu_native.dll.lib");
// Unfortunately, it seems only the local tests can access the dll this way.
// For dependees, it copies to the zig cache, which you can use for testing if you do some weird stuff with the install steps,
// but it never copies to the output folder. So not helpful if you need to distribute a binary with the dll alongside it.
const dll_install_file = b.addInstallLibFile(wgpu_dep.path("lib/wgpu_native.dll"), "wgpu_native.dll");
b.getInstallStep().dependOn(&dll_install_file.step);
// For dependees that need the dll file, this seems to be the only reliable way to propagate it through.
// In Zig 0.14 there seems to be some method for exposing LazyPaths to dependees, which might be a bit cleaner.
const writeFiles = b.addNamedWriteFiles("lib");
_ = writeFiles.addCopyFile(wgpu_dep.path("lib/wgpu_native.dll"), "wgpu_native.dll");
}
} else {
if (link_mode == .static) {
libwgpu_path = wgpu_dep.path("lib/libwgpu_native.a");
link_windows_system_libraries(std.Build.Module, wgpu_mod, true);
link_windows_system_libraries(std.Build.Module, wgpu_c_mod, true);
} else {
libwgpu_path = wgpu_dep.path("lib/libwgpu_native.dll.a");
const dll_install_file = b.addInstallLibFile(wgpu_dep.path("lib/wgpu_native.dll"), "wgpu_native.dll");
b.getInstallStep().dependOn(&dll_install_file.step);
const writeFiles = b.addNamedWriteFiles("lib");
_ = writeFiles.addCopyFile(wgpu_dep.path("lib/wgpu_native.dll"), "wgpu_native.dll");
}
}
},
// This only tries to account for linux/macos since we're using pre-compiled wgpu-native;
// need to think harder about this if I get custom builds working.
else => if (link_mode == .static) {
libwgpu_path = wgpu_dep.path("lib/libwgpu_native.a");
} else if (target_res.os.tag == .macos or target_res.os.tag == .ios) { // TODO: This is just guesswork, need to test it somehow, but I don't have a mac.
is_mac = true;
const dylib_install_file = b.addInstallLibFile(wgpu_dep.path("lib/libwgpu_native.dylib"), "libwgpu_native.dylib");
b.getInstallStep().dependOn(&dylib_install_file.step);
const writeFiles = b.addNamedWriteFiles("lib");
_ = writeFiles.addCopyFile(wgpu_dep.path("lib/libwgpu_native.dylib"), "libwgpu_native.dylib");
} else {
const so_install_file = b.addInstallLibFile(wgpu_dep.path("lib/libwgpu_native.so"), "libwgpu_native.so");
b.getInstallStep().dependOn(&so_install_file.step);
const writeFiles = b.addNamedWriteFiles("lib");
_ = writeFiles.addCopyFile(wgpu_dep.path("lib/libwgpu_native.so"), "libwgpu_native.so");
},
}
if (libwgpu_path != null) {
wgpu_mod.addObjectFile(libwgpu_path.?);
wgpu_c_mod.addObjectFile(libwgpu_path.?);
}
return WGPUBuildContext{
.link_mode = link_mode,
.target = target,
.optimize = optimize,
.is_windows = is_windows,
.is_mac = is_mac,
.wgpu_dep = wgpu_dep,
.libwgpu_path = libwgpu_path,
.install_lib_dir = b.getInstallPath(.lib, ""),
.wgpu_mod = wgpu_mod,
.wgpu_c_mod = wgpu_c_mod,
};
}
};
fn dynamic_link(context: *const WGPUBuildContext, c: *std.Build.Step.Compile, cmd: *std.Build.Step.Run) void {
if (!context.is_windows) {
c.addLibraryPath(context.wgpu_dep.path("lib"));
c.linkSystemLibrary2("wgpu_native", .{});
}
cmd.addPathDir(context.install_lib_dir);
}
fn handle_rt(context: *const WGPUBuildContext, exe: *std.Build.Step.Compile) void {
if (context.is_windows and context.target.result.abi == .msvc) {
// We get duplicate symbol errors at link-time if we don't disable these;
exe.bundle_compiler_rt = false;
exe.bundle_ubsan_rt = false;
}
}
fn triangle_example(b: *std.Build, context: *const WGPUBuildContext) void {
const bmp_mod = b.createModule(.{
.root_source_file = b.path("examples/bmp.zig"),
});
const triangle_example_exe_mod = b.createModule(.{
.root_source_file = b.path("examples/triangle/triangle.zig"),
.target = context.target,
.optimize = context.optimize,
});
triangle_example_exe_mod.addImport("wgpu", context.wgpu_mod);
triangle_example_exe_mod.addImport("bmp", bmp_mod);
const triangle_example_exe = b.addExecutable(.{
.name = "triangle-example",
.root_module = triangle_example_exe_mod,
});
handle_rt(context, triangle_example_exe);
const run_triangle_cmd = b.addRunArtifact(triangle_example_exe);
const run_triangle_step = b.step("run-triangle-example", "Run the triangle example");
run_triangle_step.dependOn(&run_triangle_cmd.step);
if (context.link_mode == .dynamic) {
dynamic_link(context, triangle_example_exe, run_triangle_cmd);
run_triangle_cmd.step.dependOn(b.getInstallStep());
}
}
fn unit_tests(b: *std.Build, context: *const WGPUBuildContext) void {
const unit_test_step = b.step("test", "Run unit tests");
if (context.is_windows) {
unit_test_step.dependOn(b.getInstallStep());
}
const test_files = [_][:0]const u8{
"src/instance.zig",
"src/adapter.zig",
"src/pipeline.zig",
};
comptime var test_names: [test_files.len][:0]const u8 = test_files;
comptime for (test_files, 0..) |test_file, idx| {
const test_name = test_file[4..(test_file.len - 4)] ++ "-test";
test_names[idx] = test_name;
};
for (test_files, test_names) |test_file, test_name| {
// TODO: Seems weird to have a mod for each unit test, should probably revisit this.
const test_mod = b.createModule(.{
.root_source_file = b.path(test_file),
.target = context.target,
.optimize = context.optimize,
});
const t = b.addTest(.{
.name = test_name,
.root_module = test_mod,
});
handle_rt(context, t);
if (context.libwgpu_path != null) {
t.addObjectFile(context.libwgpu_path.?);
}
if (context.is_windows) {
t.linkLibC();
} else {
t.linkLibCpp();
}
const run_test = b.addRunArtifact(t);
if (context.is_mac) {
link_mac_frameworks(t);
}
if (context.link_mode == .dynamic) {
dynamic_link(context, t, run_test);
} else if (context.is_windows) {
if (context.target.result.abi == .gnu) {
link_windows_system_libraries(std.Build.Step.Compile, t, true);
// TODO: Find out why this is only required here; seems suspicious
t.linkSystemLibrary2("unwind", .{});
} else {
link_windows_system_libraries(std.Build.Step.Compile, t, false);
}
}
unit_test_step.dependOn(&run_test.step);
}
}
fn compute_tests(b: *std.Build, context: *const WGPUBuildContext) void {
const compute_test_mod = b.createModule(.{
.root_source_file = b.path("tests/compute.zig"),
.target = context.target,
.optimize = context.optimize,
});
compute_test_mod.addImport("wgpu", context.wgpu_mod);
const compute_test = b.addTest(.{
.name = "compute-test",
.root_module = compute_test_mod,
});
handle_rt(context, compute_test);
const run_compute_test = b.addRunArtifact(compute_test);
const compute_test_c_mod = b.createModule(.{
.root_source_file = b.path("tests/compute_c.zig"),
.target = context.target,
.optimize = context.optimize,
});
compute_test_c_mod.addImport("wgpu-c", context.wgpu_c_mod);
const compute_test_c = b.addTest(.{
.name = "compute-test-c",
.root_module = compute_test_c_mod,
});
handle_rt(context, compute_test_c);
const run_compute_test_c = b.addRunArtifact(compute_test_c);
const compute_test_step = b.step("compute-tests", "Run compute shader tests");
if (context.link_mode == .dynamic) {
dynamic_link(context, compute_test, run_compute_test);
dynamic_link(context, compute_test_c, run_compute_test_c);
run_compute_test.step.dependOn(b.getInstallStep());
run_compute_test_c.step.dependOn(b.getInstallStep());
}
if (context.is_mac) {
link_mac_frameworks(compute_test);
link_mac_frameworks(compute_test_c);
}
compute_test_step.dependOn(&run_compute_test.step);
compute_test_step.dependOn(&run_compute_test_c.step);
}
// Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external
// runner.
pub fn build(b: *std.Build) void {
const context = WGPUBuildContext.init(b) orelse return;
compute_tests(b, &context);
unit_tests(b, &context);
triangle_example(b, &context);
}