-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathbuild.zig
645 lines (573 loc) · 23.8 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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
const std = @import("std");
const builtin = @import("builtin");
const optimize_deps = .ReleaseFast;
pub fn build(b: *std.Build) void {
const release = b.option(bool, "package_release", "Build all release targets") orelse false;
const tracy_enabled = b.option(bool, "enable_tracy", "Enable tracy client library (default: no)") orelse false;
const use_tree_sitter = b.option(bool, "use_tree_sitter", "Enable tree-sitter (default: yes)") orelse true;
const strip = b.option(bool, "strip", "Disable debug information (default: no)");
const use_llvm = b.option(bool, "use_llvm", "Enable llvm backend (default: none)");
const pie = b.option(bool, "pie", "Produce an executable with position independent code (default: none)");
const gui = b.option(bool, "gui", "Standalone GUI mode") orelse false;
const run_step = b.step("run", "Run the app");
const check_step = b.step("check", "Check the app");
const test_step = b.step("test", "Run unit tests");
const lint_step = b.step("lint", "Run lints");
return (if (release) &build_release else &build_development)(
b,
run_step,
check_step,
test_step,
lint_step,
tracy_enabled,
use_tree_sitter,
strip,
use_llvm,
pie,
gui,
);
}
fn build_development(
b: *std.Build,
run_step: *std.Build.Step,
check_step: *std.Build.Step,
test_step: *std.Build.Step,
lint_step: *std.Build.Step,
tracy_enabled: bool,
use_tree_sitter: bool,
strip: ?bool,
use_llvm: ?bool,
pie: ?bool,
gui: bool,
) void {
const target = b.standardTargetOptions(.{ .default_target = .{ .abi = if (builtin.os.tag == .linux and !tracy_enabled) .musl else null } });
const optimize = b.standardOptimizeOption(.{});
return build_exe(
b,
run_step,
check_step,
test_step,
lint_step,
target,
optimize,
.{},
tracy_enabled,
use_tree_sitter,
strip orelse false,
use_llvm,
pie,
gui,
);
}
fn build_release(
b: *std.Build,
run_step: *std.Build.Step,
check_step: *std.Build.Step,
test_step: *std.Build.Step,
lint_step: *std.Build.Step,
tracy_enabled: bool,
use_tree_sitter: bool,
strip: ?bool,
use_llvm: ?bool,
pie: ?bool,
_: bool, //gui
) void {
const targets: []const std.Target.Query = &.{
.{ .cpu_arch = .x86_64, .os_tag = .linux, .abi = .musl },
.{ .cpu_arch = .x86, .os_tag = .linux, .abi = .musl },
.{ .cpu_arch = .aarch64, .os_tag = .linux, .abi = .musl },
.{ .cpu_arch = .arm, .os_tag = .linux, .abi = .musleabihf },
.{ .cpu_arch = .x86_64, .os_tag = .macos },
.{ .cpu_arch = .aarch64, .os_tag = .macos },
.{ .cpu_arch = .x86_64, .os_tag = .windows },
.{ .cpu_arch = .aarch64, .os_tag = .windows },
};
const optimize = .ReleaseSafe;
var version = std.ArrayList(u8).init(b.allocator);
defer version.deinit();
gen_version(b, version.writer()) catch unreachable;
const write_file_step = b.addWriteFiles();
const version_file = write_file_step.add("version", version.items);
b.getInstallStep().dependOn(&b.addInstallFile(version_file, "version").step);
for (targets) |t| {
const target = b.resolveTargetQuery(t);
var triple = std.mem.splitScalar(u8, t.zigTriple(b.allocator) catch unreachable, '-');
const arch = triple.next() orelse unreachable;
const os = triple.next() orelse unreachable;
const target_path = std.mem.join(b.allocator, "-", &[_][]const u8{ os, arch }) catch unreachable;
build_exe(
b,
run_step,
check_step,
test_step,
lint_step,
target,
optimize,
.{ .dest_dir = .{ .override = .{ .custom = target_path } } },
tracy_enabled,
use_tree_sitter,
strip orelse true,
use_llvm,
pie,
false, //gui
);
if (t.os_tag == .windows)
build_exe(
b,
run_step,
check_step,
test_step,
lint_step,
target,
optimize,
.{ .dest_dir = .{ .override = .{ .custom = target_path } } },
tracy_enabled,
use_tree_sitter,
strip orelse true,
use_llvm,
pie,
true, //gui
);
}
}
pub fn build_exe(
b: *std.Build,
run_step: *std.Build.Step,
check_step: *std.Build.Step,
test_step: *std.Build.Step,
lint_step: *std.Build.Step,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
exe_install_options: std.Build.Step.InstallArtifact.Options,
tracy_enabled: bool,
use_tree_sitter: bool,
strip: bool,
use_llvm: ?bool,
pie: ?bool,
gui: bool,
) void {
const options = b.addOptions();
options.addOption(bool, "enable_tracy", tracy_enabled);
options.addOption(bool, "use_tree_sitter", use_tree_sitter);
options.addOption(bool, "strip", strip);
options.addOption(bool, "gui", gui);
const options_mod = options.createModule();
std.fs.cwd().makeDir(".cache") catch |e| switch (e) {
error.PathAlreadyExists => {},
else => std.debug.panic("makeDir(\".cache\") failed: {any}", .{e}),
};
std.fs.cwd().makeDir(".cache/cdb") catch |e| switch (e) {
error.PathAlreadyExists => {},
else => std.debug.panic("makeDir(\".cache/cdb\") failed: {any}", .{e}),
};
var version_info = std.ArrayList(u8).init(b.allocator);
defer version_info.deinit();
gen_version_info(b, target, version_info.writer()) catch {
version_info.clearAndFree();
version_info.appendSlice("unknown") catch {};
};
const wf = b.addWriteFiles();
const version_info_file = wf.add("version", version_info.items);
const vaxis_dep = b.dependency("vaxis", .{
.target = target,
.optimize = optimize,
});
const vaxis_mod = vaxis_dep.module("vaxis");
const flags_dep = b.dependency("flags", .{
.target = target,
.optimize = optimize,
});
const dizzy_dep = b.dependency("dizzy", .{
.target = target,
.optimize = optimize,
});
const fuzzig_dep = b.dependency("fuzzig", .{
.target = target,
.optimize = optimize,
});
const tracy_dep = if (tracy_enabled) b.dependency("tracy", .{
.target = target,
.optimize = optimize,
}) else undefined;
const tracy_mod = if (tracy_enabled) tracy_dep.module("tracy") else b.createModule(.{
.root_source_file = b.path("src/tracy_noop.zig"),
});
const zg_dep = vaxis_dep.builder.dependency("zg", .{
.target = target,
.optimize = optimize,
});
const zeit_dep = b.dependency("zeit", .{
.target = target,
.optimize = optimize,
});
const zeit_mod = zeit_dep.module("zeit");
const themes_dep = b.dependency("themes", .{});
const syntax_dep = b.dependency("syntax", .{
.target = target,
.optimize = optimize_deps,
.use_tree_sitter = use_tree_sitter,
});
const syntax_mod = syntax_dep.module("syntax");
const thespian_dep = b.dependency("thespian", .{
.target = target,
.optimize = optimize_deps,
.enable_tracy = tracy_enabled,
});
const thespian_mod = thespian_dep.module("thespian");
const cbor_mod = thespian_dep.module("cbor");
const help_mod = b.createModule(.{
.root_source_file = b.path("help.md"),
});
const config_mod = b.createModule(.{
.root_source_file = b.path("src/config.zig"),
.imports = &.{
.{ .name = "cbor", .module = cbor_mod },
},
});
const gui_config_mod = b.createModule(.{
.root_source_file = b.path("src/gui_config.zig"),
.imports = &.{
.{ .name = "cbor", .module = cbor_mod },
},
});
const log_mod = b.createModule(.{
.root_source_file = b.path("src/log.zig"),
.imports = &.{
.{ .name = "thespian", .module = thespian_mod },
},
});
const command_mod = b.createModule(.{
.root_source_file = b.path("src/command.zig"),
.imports = &.{
.{ .name = "thespian", .module = thespian_mod },
.{ .name = "log", .module = log_mod },
},
});
const EventHandler_mod = b.createModule(.{
.root_source_file = b.path("src/EventHandler.zig"),
.imports = &.{
.{ .name = "thespian", .module = thespian_mod },
},
});
const color_mod = b.createModule(.{
.root_source_file = b.path("src/color.zig"),
});
const Buffer_mod = b.createModule(.{
.root_source_file = b.path("src/buffer/Buffer.zig"),
.imports = &.{
.{ .name = "cbor", .module = cbor_mod },
},
});
const input_mod = b.createModule(.{
.root_source_file = b.path("src/renderer/vaxis/input.zig"),
.imports = &.{
.{ .name = "vaxis", .module = vaxis_mod },
},
});
const tui_renderer_mod = b.createModule(.{
.root_source_file = b.path("src/renderer/vaxis/renderer.zig"),
.imports = &.{
.{ .name = "vaxis", .module = vaxis_mod },
.{ .name = "input", .module = input_mod },
.{ .name = "theme", .module = themes_dep.module("theme") },
.{ .name = "cbor", .module = cbor_mod },
.{ .name = "log", .module = log_mod },
.{ .name = "thespian", .module = thespian_mod },
.{ .name = "Buffer", .module = Buffer_mod },
.{ .name = "color", .module = color_mod },
},
});
const renderer_mod = blk: {
if (gui) switch (target.result.os.tag) {
.windows => {
const win32_dep = b.lazyDependency("win32", .{}) orelse break :blk tui_renderer_mod;
const win32_mod = win32_dep.module("win32");
const gui_mod = b.createModule(.{
.root_source_file = b.path("src/win32/gui.zig"),
.imports = &.{
.{ .name = "build_options", .module = options_mod },
.{ .name = "win32", .module = win32_mod },
.{ .name = "cbor", .module = cbor_mod },
.{ .name = "thespian", .module = thespian_mod },
.{ .name = "input", .module = input_mod },
// TODO: we should be able to work without these modules
.{ .name = "vaxis", .module = vaxis_mod },
.{ .name = "color", .module = color_mod },
.{ .name = "gui_config", .module = gui_config_mod },
.{ .name = "tracy", .module = tracy_mod },
},
});
gui_mod.addIncludePath(b.path("src/win32"));
const mod = b.createModule(.{
.root_source_file = b.path("src/renderer/win32/renderer.zig"),
.imports = &.{
.{ .name = "theme", .module = themes_dep.module("theme") },
.{ .name = "win32", .module = win32_mod },
.{ .name = "cbor", .module = cbor_mod },
.{ .name = "thespian", .module = thespian_mod },
.{ .name = "input", .module = input_mod },
.{ .name = "gui", .module = gui_mod },
// TODO: we should be able to work without these modules
.{ .name = "tuirenderer", .module = tui_renderer_mod },
.{ .name = "vaxis", .module = vaxis_mod },
},
});
break :blk mod;
},
else => |tag| {
std.log.err("OS '{s}' does not support -Dgui mode", .{@tagName(tag)});
std.process.exit(0xff);
},
};
break :blk tui_renderer_mod;
};
const keybind_mod = b.createModule(.{
.root_source_file = b.path("src/keybind/keybind.zig"),
.imports = &.{
.{ .name = "cbor", .module = cbor_mod },
.{ .name = "command", .module = command_mod },
.{ .name = "EventHandler", .module = EventHandler_mod },
.{ .name = "input", .module = input_mod },
.{ .name = "thespian", .module = thespian_mod },
.{ .name = "log", .module = log_mod },
.{ .name = "Buffer", .module = Buffer_mod },
},
});
const keybind_test_run_cmd = blk: {
const tests = b.addTest(.{
.root_source_file = b.path("src/keybind/keybind.zig"),
.target = target,
.optimize = optimize,
});
tests.root_module.addImport("cbor", cbor_mod);
tests.root_module.addImport("command", command_mod);
tests.root_module.addImport("EventHandler", EventHandler_mod);
tests.root_module.addImport("input", input_mod);
tests.root_module.addImport("thespian", thespian_mod);
tests.root_module.addImport("log", log_mod);
tests.root_module.addImport("Buffer", Buffer_mod);
// b.installArtifact(tests);
break :blk b.addRunArtifact(tests);
};
const shell_mod = b.createModule(.{
.root_source_file = b.path("src/shell.zig"),
.imports = &.{
.{ .name = "thespian", .module = thespian_mod },
.{ .name = "cbor", .module = cbor_mod },
.{ .name = "log", .module = log_mod },
},
});
const ripgrep_mod = b.createModule(.{
.root_source_file = b.path("src/ripgrep.zig"),
.imports = &.{
.{ .name = "thespian", .module = thespian_mod },
.{ .name = "cbor", .module = cbor_mod },
.{ .name = "log", .module = log_mod },
},
});
const location_history_mod = b.createModule(.{
.root_source_file = b.path("src/location_history.zig"),
.imports = &.{
.{ .name = "thespian", .module = thespian_mod },
},
});
const project_manager_mod = b.createModule(.{
.root_source_file = b.path("src/project_manager.zig"),
.imports = &.{
.{ .name = "log", .module = log_mod },
.{ .name = "cbor", .module = cbor_mod },
.{ .name = "thespian", .module = thespian_mod },
.{ .name = "Buffer", .module = Buffer_mod },
.{ .name = "tracy", .module = tracy_mod },
.{ .name = "syntax", .module = syntax_mod },
.{ .name = "dizzy", .module = dizzy_dep.module("dizzy") },
.{ .name = "fuzzig", .module = fuzzig_dep.module("fuzzig") },
},
});
const diff_mod = b.createModule(.{
.root_source_file = b.path("src/diff.zig"),
.imports = &.{
.{ .name = "thespian", .module = thespian_mod },
.{ .name = "Buffer", .module = Buffer_mod },
.{ .name = "tracy", .module = tracy_mod },
.{ .name = "dizzy", .module = dizzy_dep.module("dizzy") },
.{ .name = "log", .module = log_mod },
.{ .name = "cbor", .module = cbor_mod },
},
});
const text_manip_mod = b.createModule(.{
.root_source_file = b.path("src/text_manip.zig"),
.imports = &.{},
});
const tui_mod = b.createModule(.{
.root_source_file = b.path("src/tui/tui.zig"),
.imports = &.{
.{ .name = "renderer", .module = renderer_mod },
.{ .name = "input", .module = input_mod },
.{ .name = "thespian", .module = thespian_mod },
.{ .name = "cbor", .module = cbor_mod },
.{ .name = "config", .module = config_mod },
.{ .name = "gui_config", .module = gui_config_mod },
.{ .name = "log", .module = log_mod },
.{ .name = "command", .module = command_mod },
.{ .name = "EventHandler", .module = EventHandler_mod },
.{ .name = "location_history", .module = location_history_mod },
.{ .name = "project_manager", .module = project_manager_mod },
.{ .name = "syntax", .module = syntax_mod },
.{ .name = "text_manip", .module = text_manip_mod },
.{ .name = "Buffer", .module = Buffer_mod },
.{ .name = "keybind", .module = keybind_mod },
.{ .name = "shell", .module = shell_mod },
.{ .name = "ripgrep", .module = ripgrep_mod },
.{ .name = "theme", .module = themes_dep.module("theme") },
.{ .name = "themes", .module = themes_dep.module("themes") },
.{ .name = "tracy", .module = tracy_mod },
.{ .name = "build_options", .module = options_mod },
.{ .name = "color", .module = color_mod },
.{ .name = "diff", .module = diff_mod },
.{ .name = "help.md", .module = help_mod },
.{ .name = "CaseData", .module = zg_dep.module("CaseData") },
.{ .name = "fuzzig", .module = fuzzig_dep.module("fuzzig") },
.{ .name = "zeit", .module = zeit_mod },
},
});
const exe_name = if (gui) "flow-gui" else "flow";
const exe = b.addExecutable(.{
.name = exe_name,
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.strip = strip,
.win32_manifest = b.path("src/win32/flow.manifest"),
});
if (use_llvm) |value| {
exe.use_llvm = value;
exe.use_lld = value;
}
if (pie) |value| exe.pie = value;
exe.root_module.addImport("build_options", options_mod);
exe.root_module.addImport("flags", flags_dep.module("flags"));
exe.root_module.addImport("cbor", cbor_mod);
exe.root_module.addImport("config", config_mod);
exe.root_module.addImport("tui", tui_mod);
exe.root_module.addImport("thespian", thespian_mod);
exe.root_module.addImport("log", log_mod);
exe.root_module.addImport("tracy", tracy_mod);
exe.root_module.addImport("renderer", renderer_mod);
exe.root_module.addImport("input", input_mod);
exe.root_module.addImport("syntax", syntax_mod);
exe.root_module.addImport("version_info", b.createModule(.{ .root_source_file = version_info_file }));
if (target.result.os.tag == .windows) {
exe.addWin32ResourceFile(.{
.file = b.path("src/win32/flow.rc"),
});
if (gui) {
exe.subsystem = .Windows;
}
}
const exe_install = b.addInstallArtifact(exe, exe_install_options);
b.getInstallStep().dependOn(&exe_install.step);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
run_step.dependOn(&run_cmd.step);
const check_exe = b.addExecutable(.{
.name = exe_name,
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
check_exe.root_module.addImport("build_options", options_mod);
check_exe.root_module.addImport("flags", flags_dep.module("flags"));
check_exe.root_module.addImport("cbor", cbor_mod);
check_exe.root_module.addImport("config", config_mod);
check_exe.root_module.addImport("tui", tui_mod);
check_exe.root_module.addImport("thespian", thespian_mod);
check_exe.root_module.addImport("log", log_mod);
check_exe.root_module.addImport("tracy", tracy_mod);
check_exe.root_module.addImport("renderer", renderer_mod);
check_exe.root_module.addImport("input", input_mod);
check_exe.root_module.addImport("syntax", syntax_mod);
check_exe.root_module.addImport("version_info", b.createModule(.{ .root_source_file = version_info_file }));
check_step.dependOn(&check_exe.step);
const tests = b.addTest(.{
.root_source_file = b.path("test/tests.zig"),
.target = target,
.optimize = optimize,
.use_llvm = use_llvm,
.use_lld = use_llvm,
.strip = strip,
});
tests.pie = pie;
tests.root_module.addImport("build_options", options_mod);
tests.root_module.addImport("log", log_mod);
tests.root_module.addImport("Buffer", Buffer_mod);
tests.root_module.addImport("color", color_mod);
// b.installArtifact(tests);
const test_run_cmd = b.addRunArtifact(tests);
test_step.dependOn(&test_run_cmd.step);
test_step.dependOn(&keybind_test_run_cmd.step);
const lints = b.addFmt(.{
.paths = &.{ "src", "test", "build.zig" },
.check = true,
});
lint_step.dependOn(&lints.step);
// b.default_step.dependOn(lint_step);
}
fn gen_version_info(
b: *std.Build,
target: std.Build.ResolvedTarget,
writer: anytype,
) !void {
var code: u8 = 0;
const describe = try b.runAllowFail(&[_][]const u8{ "git", "describe", "--always", "--tags" }, &code, .Ignore);
const branch_ = try b.runAllowFail(&[_][]const u8{ "git", "rev-parse", "--abbrev-ref", "HEAD" }, &code, .Ignore);
const branch = std.mem.trimRight(u8, branch_, "\r\n ");
const tracking_branch_ = blk: {
var buf = std.ArrayList(u8).init(b.allocator);
defer buf.deinit();
try buf.appendSlice(branch);
try buf.appendSlice("@{upstream}");
break :blk try b.runAllowFail(&[_][]const u8{ "git", "rev-parse", "--abbrev-ref", buf.items }, &code, .Ignore);
};
const tracking_remote_name = if (std.mem.indexOfScalar(u8, tracking_branch_, '/')) |pos| tracking_branch_[0..pos] else "";
const tracking_remote_ = if (tracking_remote_name.len > 0) blk: {
var remote_config_path = std.ArrayList(u8).init(b.allocator);
defer remote_config_path.deinit();
try remote_config_path.writer().print("remote.{s}.url", .{tracking_remote_name});
break :blk b.runAllowFail(&[_][]const u8{ "git", "config", remote_config_path.items }, &code, .Ignore) catch "(remote not found)";
} else "";
const remote_ = b.runAllowFail(&[_][]const u8{ "git", "config", "remote.origin.url" }, &code, .Ignore) catch "(origin not found)";
const log_ = b.runAllowFail(&[_][]const u8{ "git", "log", "--pretty=oneline", "@{u}..." }, &code, .Ignore) catch "";
const diff_ = b.runAllowFail(&[_][]const u8{ "git", "diff", "--stat", "--patch", "HEAD" }, &code, .Ignore) catch "(git diff failed)";
const version = std.mem.trimRight(u8, describe, "\r\n ");
const tracking_branch = std.mem.trimRight(u8, tracking_branch_, "\r\n ");
const tracking_remote = std.mem.trimRight(u8, tracking_remote_, "\r\n ");
const remote = std.mem.trimRight(u8, remote_, "\r\n ");
const log = std.mem.trimRight(u8, log_, "\r\n ");
const diff = std.mem.trimRight(u8, diff_, "\r\n ");
const target_triple = try target.result.zigTriple(b.allocator);
try writer.print("Flow Control: a programmer's text editor\n\nversion: {s}{s}\ntarget: {s}\n", .{
version,
if (diff.len > 0) "-dirty" else "",
target_triple,
});
if (branch.len > 0) if (tracking_branch.len > 0)
try writer.print("branch: {s} tracking {s} at {s}\n", .{ branch, tracking_branch, tracking_remote })
else
try writer.print("branch: {s} at {s}\n", .{ branch, remote });
if (log.len > 0)
try writer.print("\nwith the following diverging commits:\n{s}\n", .{log});
if (diff.len > 0)
try writer.print("\nwith the following uncommited changes:\n\n{s}\n", .{diff});
}
fn gen_version(b: *std.Build, writer: anytype) !void {
var code: u8 = 0;
const describe = try b.runAllowFail(&[_][]const u8{ "git", "describe", "--always", "--tags" }, &code, .Ignore);
const diff_ = try b.runAllowFail(&[_][]const u8{ "git", "diff", "--stat", "--patch", "HEAD" }, &code, .Ignore);
const diff = std.mem.trimRight(u8, diff_, "\r\n ");
const version = std.mem.trimRight(u8, describe, "\r\n ");
try writer.print("{s}{s}", .{ version, if (diff.len > 0) "-dirty" else "" });
}