-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.zig
299 lines (270 loc) · 12.2 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
const std = @import("std");
fn addGenProfile(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.Mode) void {
const gen_profile = b.addExecutable(.{
.name = "gen_profile",
.root_source_file = .{ .path = "src/gen_profile.zig" },
.target = target,
.optimize = optimize,
});
const install_gen_profile = b.addInstallArtifact(gen_profile, .{});
const install_gen_profile_step = b.step("gen_profile", "Install gen_profile");
install_gen_profile_step.dependOn(&install_gen_profile.step);
const run_gen_profile = b.addRunArtifact(gen_profile);
run_gen_profile.step.dependOn(install_gen_profile_step);
if (b.args) |args| {
run_gen_profile.addArgs(args);
}
const run_gen_profile_step = b.step("run_gen_profile", "Run gen_profile");
run_gen_profile_step.dependOn(&run_gen_profile.step);
}
fn addBenchParser(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.Mode) void {
const bench_parser = b.addExecutable(.{
.name = "bench_parser",
.root_source_file = .{ .path = "src/bench_parser.zig" },
.target = target,
.optimize = optimize,
});
const install_bench_parser = b.addInstallArtifact(bench_parser, .{});
const install_bench_parser_step = b.step("bench_parser", "Install bench_parser");
install_bench_parser_step.dependOn(&install_bench_parser.step);
const run_bench_parser = b.addRunArtifact(bench_parser);
run_bench_parser.step.dependOn(install_bench_parser_step);
if (b.args) |args| {
run_bench_parser.addArgs(args);
}
const run_bench_parser_step = b.step("run_bench_parser", "Run bench_parser");
run_bench_parser_step.dependOn(&run_bench_parser.step);
}
fn add_imgui(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.Mode) *std.Build.Step.Compile {
const imgui = b.addStaticLibrary(.{
.name = "imgui",
.target = target,
.optimize = if (optimize == .Debug) .ReleaseSafe else optimize,
});
imgui.addIncludePath(.{ .path = "." });
imgui.addIncludePath(.{ .path = "third_party/cimgui/imgui" });
if (target.result.isWasm()) {
imgui.defineCMacro("ZTRACING_WASM", "1");
} else {
imgui.addIncludePath(.{ .path = "third_party/SDL/build/install/include/SDL2" });
}
imgui.addCSourceFiles(.{
.files = &.{
"src/imgui_wrapper.cpp",
},
});
imgui.linkLibC();
// msvc doesn't support -lc++ yet. https://github.com/ziglang/zig/issues/5312
if (target.result.abi != .msvc) {
imgui.linkLibCpp();
}
return imgui;
}
fn add_zlib(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.Mode) *std.Build.Step.Compile {
const zlib = b.addStaticLibrary(.{
.name = "zlib",
.target = target,
.optimize = if (optimize == .Debug) .ReleaseSafe else optimize,
});
zlib.addIncludePath(.{ .path = "third_party/zlib" });
zlib.defineCMacro("Z_SOLO", "1");
zlib.addCSourceFiles(.{
.files = &.{
"third_party/zlib/adler32.c",
"third_party/zlib/compress.c",
"third_party/zlib/crc32.c",
"third_party/zlib/deflate.c",
"third_party/zlib/inflate.c",
"third_party/zlib/infback.c",
"third_party/zlib/inftrees.c",
"third_party/zlib/inffast.c",
"third_party/zlib/trees.c",
"third_party/zlib/uncompr.c",
"third_party/zlib/zutil.c",
},
});
zlib.linkLibC();
return zlib;
}
fn add_tracy(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.Mode) *std.Build.Step.Compile {
const tracy = b.addStaticLibrary(.{
.name = "tracy",
.target = target,
.optimize = if (optimize == .Debug) .ReleaseSafe else optimize,
});
tracy.addIncludePath(.{ .path = "third_party/tracy/public/tracy" });
tracy.defineCMacro("TRACY_ENABLE", "1");
tracy.addCSourceFiles(.{
.files = &.{
"third_party/tracy/public/TracyClient.cpp",
},
});
tracy.linkLibC();
// https://github.com/wolfpld/tracy/issues/602
tracy.root_module.sanitize_c = false;
if (target.result.os.tag != .windows) {
tracy.linkLibCpp();
}
return tracy;
}
fn add_ztraing(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.Mode) void {
const build_options = b.addOptions();
const imgui = add_imgui(b, target, optimize);
const ztracing = blk: {
if (target.result.isWasm()) {
build_options.addOption(bool, "enable_tracy", false);
var query = target.query;
query.cpu_features_add.addFeature(@intFromEnum(std.Target.wasm.Feature.atomics));
query.cpu_features_add.addFeature(@intFromEnum(std.Target.wasm.Feature.bulk_memory));
const target2 = b.resolveTargetQuery(query);
const ztracing = b.addExecutable(.{
.name = "ztracing",
.root_source_file = .{ .path = "src/main_wasm.zig" },
.target = target2,
.optimize = optimize,
});
ztracing.import_memory = true;
ztracing.initial_memory = 260 * 65536;
ztracing.max_memory = 65536 * 65536;
ztracing.shared_memory = true;
ztracing.root_module.single_threaded = false;
ztracing.root_module.export_symbol_names = &.{
"wasi_thread_start",
"alloc",
"init",
"update",
"on_resize",
"onMousePos",
"onMouseButton",
"onMouseWheel",
"onKey",
"onFocus",
"shouldLoadFile",
"onLoadFileStart",
"onLoadFileChunk",
"onLoadFileDone",
};
ztracing.defineCMacro("ZTRACING_WASM", "1");
break :blk ztracing;
} else {
const zlib = add_zlib(b, target, optimize);
const tracy = add_tracy(b, target, optimize);
const ztracing = b.addExecutable(.{
.name = "ztracing",
.root_source_file = .{ .path = "src/main_native.zig" },
.target = target,
.optimize = optimize,
});
ztracing.root_module.addAnonymousImport("assets", .{
.root_source_file = .{ .path = "assets/assets.zig" },
});
ztracing.addIncludePath(.{ .path = "third_party/zlib" });
ztracing.linkLibrary(zlib);
ztracing.addIncludePath(.{ .path = "third_party/SDL/build/install/include" });
switch (target.result.os.tag) {
.windows => {
ztracing.addObjectFile(.{ .path = "third_party/SDL/build/install/lib/SDL2-static.lib" });
ztracing.linkSystemLibrary("setupapi");
ztracing.linkSystemLibrary("user32");
ztracing.linkSystemLibrary("winmm");
ztracing.linkSystemLibrary("gdi32");
ztracing.linkSystemLibrary("imm32");
ztracing.linkSystemLibrary("version");
ztracing.linkSystemLibrary("oleaut32");
ztracing.linkSystemLibrary("ole32");
ztracing.linkSystemLibrary("shell32");
ztracing.linkSystemLibrary("advapi32");
},
.macos => {
ztracing.addObjectFile(.{ .path = "third_party/SDL/build/install/lib/libSDL2.a" });
ztracing.linkFramework("CoreVideo");
ztracing.linkFramework("Cocoa");
ztracing.linkFramework("IOKit");
ztracing.linkFramework("ForceFeedback");
ztracing.linkFramework("Carbon");
ztracing.linkFramework("CoreAudio");
ztracing.linkFramework("AudioToolbox");
ztracing.linkFramework("AVFoundation");
ztracing.linkFramework("Foundation");
ztracing.linkFramework("GameController");
ztracing.linkFramework("Metal");
ztracing.linkFramework("QuartzCore");
ztracing.linkFramework("CoreHaptics");
ztracing.linkSystemLibrary("iconv");
},
.linux => {
ztracing.addObjectFile(.{ .path = "third_party/SDL/build/install/lib/libSDL2.a" });
},
else => @panic("Unsupported os"),
}
ztracing.linkLibC();
ztracing.root_module.single_threaded = false;
const enable_tracy = b.option(bool, "tracy", "Enable Tracy integration") orelse false;
build_options.addOption(bool, "enable_tracy", enable_tracy);
if (enable_tracy) {
ztracing.addIncludePath(.{ .path = "third_party/tracy/public/tracy" });
build_options.addOption(bool, "enable_tracy_allocation", true);
build_options.addOption(bool, "enable_tracy_callstack", true);
ztracing.linkLibrary(tracy);
}
break :blk ztracing;
}
};
ztracing.linkLibrary(imgui);
ztracing.addIncludePath(.{ .path = "." });
ztracing.addIncludePath(.{ .path = "third_party/cimgui" });
ztracing.root_module.addOptions("build_options", build_options);
b.installArtifact(ztracing);
}
// 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 {
// 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.
var target = b.standardTargetOptions(.{});
if (target.result.os.tag == .windows and target.result.abi != .msvc) {
target = b.resolveTargetQuery(.{ .os_tag = .windows, .abi = .msvc });
}
// 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(.{});
add_ztraing(b, target, optimize);
addGenProfile(b, target, optimize);
addBenchParser(b, target, optimize);
// This *creates* a Run step in the build graph, to be executed when another
// step is evaluated that depends on it. The next line below will establish
// such a dependency.
// const run_cmd = b.addRunArtifact(exe);
// By making the run step depend on the install step, it will be run from the
// installation directory rather than directly from within the cache directory.
// This is not necessary, however, if the application depends on other installed
// files, this ensures they will be present and in the expected location.
// run_cmd.step.dependOn(b.getInstallStep());
// This allows the user to pass arguments to the application in the build
// command itself, like this: `zig build run -- arg1 arg2 etc`
// if (b.args) |args| {
// run_cmd.addArgs(args);
// }
// This creates a build step. It will be visible in the `zig build --help` menu,
// and can be selected like this: `zig build run`
// This will evaluate the `run` step rather than the default, which is "install".
// const run_step = b.step("run", "Run the app");
// run_step.dependOn(&run_cmd.step);
// Creates a step for unit testing. This only builds the test executable
// but does not run it.
const unit_tests = b.addTest(.{
.root_source_file = .{ .path = "src/test.zig" },
.target = target,
.optimize = optimize,
});
const run_unit_tests = b.addRunArtifact(unit_tests);
// Similar to creating the run step earlier, this exposes a `test` step to
// the `zig build --help` menu, providing a way for the user to request
// running the unit tests.
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_unit_tests.step);
}