-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.zig
424 lines (348 loc) · 13 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
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
const std = @import("std");
const Build = std.Build;
const Step = Build.Step;
const Manifest = Build.Cache.Manifest;
const fs = std.fs;
const mem = std.mem;
const compress = std.compress;
const io = std.io;
const math = std.math;
const LazyPath = std.Build.LazyPath;
const json = std.json;
const ArrayList = std.ArrayList;
const process = std.process;
const cpp_source_files = .{
"./src/main.cpp",
"./src/utils.cpp",
"./src/download.cpp",
};
const compiler_flags = .{
"-std=c++17",
"-Wall",
"-Wextra",
"-Wshadow",
// "-g",
};
const JsonLanguage = struct {
lang: []const u8,
formalname: []const u8,
phrases: json.ArrayHashMap([]const u8),
};
pub fn build(b: *Build) !void {
// It barely works, so we're just waiting for zig to fix it
const doGenerateCompileCommands = b.option(bool, "generate-ccjson", "Generate compile_commands.json (experimental)") orelse false;
const target_query = std.Target.Query.parse(.{ .arch_os_abi = "x86_64-windows-gnu" }) catch unreachable;
const target = b.resolveTargetQuery(target_query);
const optimize = b.standardOptimizeOption(.{});
const glfw_dep = b.dependency("glfw", .{});
const glfw = b.addStaticLibrary(.{
.name = "glfw",
.target = target,
.optimize = optimize,
});
glfw.defineCMacro("_GLFW_WIN32", null);
glfw.linkLibC();
glfw.linkSystemLibrary("gdi32");
for (glfw_source_files) |file| {
glfw.addCSourceFile(.{
.file = glfw_dep.path(file),
.flags = &.{},
});
}
const imgui_dep = b.dependency("imgui", .{});
const imgui = b.addStaticLibrary(.{
.name = "imgui",
.target = target,
.optimize = optimize,
});
for (imgui_source_files) |file| {
imgui.addCSourceFile(.{
.file = imgui_dep.path(file),
.flags = &.{
"-std=c++11",
},
});
}
imgui.addIncludePath(imgui_dep.path(""));
imgui.addIncludePath(glfw_dep.path("include"));
imgui.linkLibC();
imgui.linkLibCpp();
imgui.linkSystemLibrary("opengl32");
var compilation_args = std.ArrayList([]const u8).init(b.allocator);
try compilation_args.appendSlice(&compiler_flags);
const i18n = b.addStaticLibrary(.{
.name = "i18n",
.target = target,
.optimize = optimize,
.root_source_file = b.path("src/i18n.zig"),
});
i18n.linkLibC();
i18n.addIncludePath(b.path("src"));
const i18n_build = b.addOptions();
// i18n.addOptions("i18n_build", i18n_build);
i18n.root_module.addOptions("i18n_build", i18n_build);
const translation_texts = try getTranslationTexts(b);
defer {
for (translation_texts.items) |i| {
b.allocator.free(i);
}
translation_texts.deinit();
}
try fillTranslationOptions(i18n_build, translation_texts.items);
const cpp_json_dep = b.dependency("cpp-json", .{});
const exe = b.addExecutable(.{
.name = "YtDownloader",
.target = target,
.optimize = optimize,
});
exe.addIncludePath(imgui_dep.path(""));
exe.addIncludePath(imgui_dep.path("backends"));
exe.addIncludePath(imgui_dep.path("misc/cpp"));
exe.addIncludePath(glfw_dep.path("include"));
exe.addSystemIncludePath(cpp_json_dep.path("single_include"));
exe.linkLibC();
exe.linkLibCpp();
exe.linkSystemLibrary("ole32");
exe.linkSystemLibrary("winhttp");
exe.linkSystemLibrary("shlwapi");
exe.defineCMacro("INITGUID", null);
if (optimize == .Debug) {
exe.defineCMacro("JSON_DIAGNOSTICS", null);
}
exe.addIncludePath(b.path("src"));
exe.linkLibrary(imgui);
exe.linkLibrary(glfw);
exe.linkLibrary(i18n);
exe.subsystem = std.Target.SubSystem.Windows;
if (doGenerateCompileCommands) {
var genstep = try CompilationDatabaseStep.initAlloc(b, exe, &cpp_source_files, &compiler_flags);
// FIXME This doesn't consistently check if the files exist and creates them, so deleting
// the build file can result in this not creating a new one until something changes the manifest
if (!genstep.cached) {
try compilation_args.appendSlice(&.{ "-gen-cdb-fragment-path", genstep.cdb_dir_path });
genstep.step.dependOn(&exe.step);
b.default_step.dependOn(&genstep.step);
}
}
exe.addCSourceFiles(.{
.files = &cpp_source_files,
.flags = compilation_args.items,
});
const install_artifact = b.installArtifact(exe);
_ = install_artifact;
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 CompilationDatabaseStep = struct {
step: Step,
cached: bool,
hash: [Build.Cache.hex_digest_len]u8,
cdb_dir_path: []u8,
manifest: Manifest,
const Self = @This();
pub fn initAlloc(b: *Build, exe: *Step.Compile, source_files: []const []const u8, comp_flags: []const []const u8) !*Self {
var manifest = b.graph.cache.obtain();
// FIXME: This isn't nearly enough to consistently cache builds, we need
// to also cache the dependencies and their flags and includes
// for this to be completely consistent, or atleast consistent enough
manifest.hash.add(@as(u32, 0xad92deab));
try manifest.addListOfFiles(source_files);
manifest.hash.addListOfBytes(comp_flags);
try addCompileStepToManifest(&manifest, exe, b);
const cached = try manifest.hit();
const hash = manifest.final();
const cache_path = b.cache_root.path orelse try fs.cwd().realpathAlloc(b.allocator, ".");
const cdb_dir_path = b.pathFromRoot(b.pathJoin(&.{ cache_path, "cdb", &hash }));
try b.cache_root.handle.makePath("cdb");
const allocated = try b.allocator.create(Self);
allocated.* = .{
.step = Step.init(.{
.id = .custom,
.owner = b,
.name = "Stitch together compile_commands.json",
.makeFn = make,
}),
.cached = cached,
.hash = hash,
.cdb_dir_path = cdb_dir_path,
.manifest = manifest,
};
return allocated;
}
fn addCompileStepToManifest(manifest: *Manifest, exe: *Step.Compile, b: *Build) !void {
// FIXME This isn't nearly enough to consistently cache files,
// for example, compiler flags aren't cached.
for (exe.root_module.c_macros.items) |macro| {
manifest.hash.add(@as(u32, 0x92197e58));
manifest.hash.addBytes(macro);
}
for (exe.root_module.include_dirs.items) |incl_dir| {
switch (incl_dir) {
.path => |p| {
manifest.hash.add(@as(u32, 0x4dfe76cb));
manifest.hash.addBytes(p.getPath(b));
},
.path_system => |p| {
manifest.hash.add(@as(u32, 0xe31c9fb0));
manifest.hash.addBytes(p.getPath(b));
},
.framework_path => |p| {
manifest.hash.add(@as(u32, 0x06ec1b92));
manifest.hash.addBytes(p.getPath(b));
},
.framework_path_system => |p| {
manifest.hash.add(@as(u32, 0x9f63b83f));
manifest.hash.addBytes(p.getPath(b));
},
.other_step => |s| {
manifest.hash.add(@as(u32, 0x343f7121));
try addCompileStepToManifest(manifest, s, b);
manifest.hash.add(@as(u32, 0x7e56bbd1));
},
.config_header_step => |_| {
@panic("Unimplemented");
},
.path_after => |p| {
manifest.hash.add(@as(u32, 0x097c4d6b));
manifest.hash.addBytes(p.getPath(b));
},
}
}
}
fn make(step: *Step, make_opts: Build.Step.MakeOptions) !void {
if (make_opts.watch) {
// TODO: Add warning or error that watch is unsupported
}
const self: *Self = @alignCast(@fieldParentPtr("step", step));
defer self.manifest.deinit();
const cfjson_path = step.owner.pathFromRoot("build/compile_commands.json");
const exists = exists: {
fs.accessAbsolute(cfjson_path, .{}) catch |err| {
if (err == error.FileNotFound) {
break :exists false;
}
return err;
};
break :exists true;
};
step.result_cached = self.cached and exists;
if (step.result_cached) {
return;
}
fs.makeDirAbsolute(step.owner.pathFromRoot("build")) catch |err| {
if (err != error.PathAlreadyExists) {
return err;
}
};
const cfjson_file = try fs.createFileAbsolute(cfjson_path, .{});
defer cfjson_file.close();
const json_writer = cfjson_file.writer();
try json_writer.writeByte('[');
var cdb_dir = fs.openDirAbsolute(self.cdb_dir_path, .{ .iterate = true }) catch |err| {
return step.fail("Cannot open CDB directory '{s}': {s}", .{ self.cdb_dir_path, @errorName(err) });
};
defer cdb_dir.close();
var cdb_iter = cdb_dir.iterate();
var first = true;
while (try cdb_iter.next()) |entry| {
if (entry.kind != .file) {
return step.fail("Expected a file, found {s}", .{@tagName(entry.kind)});
}
if (first) {
first = false;
} else {
try json_writer.writeByte(',');
}
const file = try cdb_dir.openFile(entry.name, .{});
const file_content = try file.readToEndAlloc(step.owner.allocator, math.maxInt(usize));
defer step.owner.allocator.free(file_content);
try json_writer.writeAll(file_content[0 .. file_content.len - 2]);
}
try json_writer.writeByte(']');
try self.manifest.writeManifest();
}
};
fn getTranslationTexts(b: *Build) !std.ArrayList([]const u8) {
var translation = try b.build_root.handle.openDir("translation", .{ .iterate = true });
var walker = try translation.walk(b.allocator);
defer walker.deinit();
var texts = std.ArrayList([]const u8).init(b.allocator);
errdefer {
for (texts.items) |i| {
b.allocator.free(i);
}
texts.deinit();
}
while (try walker.next()) |entry| {
if (entry.kind != .file) {
continue;
}
if (!mem.endsWith(u8, entry.path, ".json")) {
continue;
}
// std.debug.print("Parsing {s}\n", .{entry.basename});
var file = try entry.dir.openFile(entry.basename, .{});
defer file.close();
const jsontext = try file.readToEndAlloc(b.allocator, math.pow(usize, 2, 16));
try texts.append(jsontext);
}
return texts;
}
fn fillTranslationOptions(options: *Build.Step.Options, translation_texts: []const []const u8) !void {
const b = options.step.owner;
const allocator = b.allocator;
var langobjects = ArrayList(json.Parsed(JsonLanguage)).init(allocator);
defer {
for (langobjects.items) |obj| {
obj.deinit();
}
langobjects.deinit();
}
var langs = ArrayList([]const u8).init(allocator);
defer langs.deinit();
var formalnames = ArrayList([]const u8).init(allocator);
defer formalnames.deinit();
for (translation_texts) |text| {
// TODO Log syntax errors with filename and/or path
const lang = try json.parseFromSlice(JsonLanguage, allocator, text, .{});
try langobjects.append(lang);
try langs.append(lang.value.lang);
try formalnames.append(lang.value.formalname);
options.addOption([]const []const u8, b.fmt("phrases_{s}_keys", .{lang.value.lang}), lang.value.phrases.map.keys());
options.addOption([]const []const u8, b.fmt("phrases_{s}_vals", .{lang.value.lang}), lang.value.phrases.map.values());
}
options.addOption([]const []const u8, "langs", langs.items);
options.addOption([]const []const u8, "formalnames", formalnames.items);
}
const imgui_source_files = [_][]const u8{
"imgui.cpp",
"imgui_draw.cpp",
"imgui_tables.cpp",
"imgui_widgets.cpp",
"imgui_demo.cpp",
"misc/cpp/imgui_stdlib.cpp",
"backends/imgui_impl_glfw.cpp",
"backends/imgui_impl_opengl3.cpp",
};
const glfw_source_files = [_][]const u8{
"src/context.c",
"src/init.c",
"src/input.c",
"src/monitor.c",
"src/vulkan.c",
"src/window.c",
"src/win32_init.c",
"src/win32_joystick.c",
"src/win32_monitor.c",
"src/win32_time.c",
"src/win32_thread.c",
"src/win32_window.c",
"src/wgl_context.c",
"src/egl_context.c",
"src/osmesa_context.c",
};