-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.zig
628 lines (548 loc) · 19 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
const std = @import("std");
const builtin = @import("builtin");
pub const generate_ide = @import("src/tools/generate_ide.zig");
const min_zig_version = std.SemanticVersion.parse("0.14.0") catch @panic("Where is .zigversion?");
const cetech1_version = std.SemanticVersion.parse(@embedFile(".version")) catch @panic("Where is .version?");
pub fn useSystemSDK(b: *std.Build, target: std.Build.ResolvedTarget, e: *std.Build.Step.Compile) void {
switch (target.result.os.tag) {
.windows => {
if (target.result.cpu.arch.isX86()) {
if (target.result.abi.isGnu() or target.result.abi.isMusl()) {
if (b.lazyDependency("system_sdk", .{})) |system_sdk| {
e.addLibraryPath(system_sdk.path("windows/lib/x86_64-windows-gnu"));
}
}
}
},
.macos => {
if (b.lazyDependency("system_sdk", .{})) |system_sdk| {
e.addLibraryPath(system_sdk.path("macos12/usr/lib"));
e.addFrameworkPath(system_sdk.path("macos12/System/Library/Frameworks"));
}
},
.linux => {
if (target.result.cpu.arch.isX86()) {
if (b.lazyDependency("system_sdk", .{})) |system_sdk| {
e.addLibraryPath(system_sdk.path("linux/lib/x86_64-linux-gnu"));
}
} else if (target.result.cpu.arch == .aarch64) {
if (b.lazyDependency("system_sdk", .{})) |system_sdk| {
e.addLibraryPath(system_sdk.path("linux/lib/aarch64-linux-gnu"));
}
}
},
else => {},
}
}
pub fn createRunStep(b: *std.Build, exe: *std.Build.Step.Compile) void {
const run_exe = b.addRunArtifact(exe);
run_exe.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_exe.addArgs(args);
}
const run_step = b.step("run", "Run Forest run");
run_step.dependOn(&run_exe.step);
}
pub fn initStep(
b: *std.Build,
step: *std.Build.Step,
comptime cetech_dir: []const u8,
) void {
_ = b;
_ = step;
_ = cetech_dir;
// const init_lfs_writerside = b.addSystemCommand(&.{
// "git",
// "-C",
// cetech_dir,
// "lfs",
// "pull",
// "--include",
// "docs/images/**/*",
// });
// const init_lfs_fonts = b.addSystemCommand(&.{
// "git",
// "-C",
// cetech_dir,
// "lfs",
// "pull",
// "--include",
// "externals/shared/fonts/*",
// });
// const init_lfs_system_sdk = b.addSystemCommand(&.{
// "git",
// "-C",
// cetech_dir ++ "externals/shared/lib/system_sdk",
// "lfs",
// "pull",
// });
// step.dependOn(&init_lfs_writerside.step);
// step.dependOn(&init_lfs_fonts.step);
// step.dependOn(&init_lfs_system_sdk.step);
// const init_submodules = b.addSystemCommand(&.{
// "git",
// "-C",
// cetech_dir,
// "submodule",
// "update",
// "--init",
// "externals/shared",
// });
// step.dependOn(&init_submodules.step);
// init_lfs_system_sdk.step.dependOn(&init_submodules.step);
}
pub fn updateCectechStep(
b: *std.Build,
step: *std.Build.Step,
comptime cetech_dir: []const u8,
) void {
const sync_remote_submodules = b.addSystemCommand(&.{
"git",
"submodule",
"update",
"--init",
"--remote",
cetech_dir,
});
step.dependOn(&sync_remote_submodules.step);
}
pub fn createKernelExe(
b: *std.Build,
comptime bin_name: []const u8,
runner_main: std.Build.LazyPath,
cetech1_kernel: *std.Build.Module,
cetech1_kernel_lib: *std.Build.Step.Compile,
versionn: std.SemanticVersion,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
) *std.Build.Step.Compile {
const exe = b.addExecutable(.{
.name = bin_name,
.version = versionn,
.root_source_file = runner_main,
.target = target,
.optimize = optimize,
});
exe.linkLibC();
exe.root_module.addImport("kernel", cetech1_kernel);
exe.linkLibrary(cetech1_kernel_lib);
b.installArtifact(exe);
useSystemSDK(b, target, exe);
createRunStep(b, exe);
return exe;
}
pub fn build(b: *std.Build) !void {
try ensureZigVersion();
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
//
// OPTIONS
//
const options = .{
// Modules
.enable_samples = b.option(bool, "with_samples", "build with sample modules.") orelse true,
.enable_editor = b.option(bool, "with_editor", "build with editor modules.") orelse true,
.modules = b.option([]const []const u8, "with_module", "build with this modules."),
.static_modules = b.option(bool, "static_modules", "build all modules in static mode.") orelse false,
.dynamic_modules = b.option(bool, "dynamic_modules", "build all modules in dynamic mode.") orelse true,
// Tracy options
.enable_tracy = b.option(bool, "with_tracy", "build with tracy.") orelse true,
.tracy_on_demand = b.option(bool, "tracy_on_demand", "build tracy with TRACY_ON_DEMAND") orelse true,
// NFD options
.enable_nfd = b.option(bool, "with_nfd", "build with NFD (Native File Dialog).") orelse true,
.nfd_portal = b.option(bool, "nfd_portal", "build NFD with xdg-desktop-portal instead of GTK. ( Linux, nice for steamdeck;) )") orelse true,
.with_freetype = b.option(bool, "with_freetype", "build coreui with freetype support") orelse true,
.externals_optimize = b.option(std.builtin.OptimizeMode, "externals_optimize", "Optimize for externals libs") orelse .ReleaseFast,
.enable_shaderc = b.option(bool, "with_shaderc", "build with shaderc support") orelse true,
.app_name = b.option([]const u8, "app_name", "App name") orelse "CETech1",
.ide = b.option(generate_ide.EditorType, "ide", "IDE for gen-ide command") orelse .vscode,
};
const external_credits = b.option([]std.Build.LazyPath, "external_credits", "Path to additional .external_credits.json .");
const authors = b.option(std.Build.LazyPath, "authors", "Path to AUTHORS.");
const options_step = b.addOptions();
options_step.addOption(std.SemanticVersion, "version", cetech1_version);
// add build args
inline for (std.meta.fields(@TypeOf(options))) |field| {
options_step.addOption(field.type, field.name, @field(options, field.name));
}
const options_module = options_step.createModule();
//
// Extrnals
//
const uuid = b.dependency(
"uuid",
.{
.target = target,
.optimize = options.externals_optimize,
},
);
// ZF
const zf = b.dependency(
"zf",
.{
.target = target,
.optimize = options.externals_optimize,
.with_tui = false,
},
);
// ZNFDE
const znfde = b.dependency(
"znfde",
.{
.target = target,
.optimize = options.externals_optimize,
.with_portal = options.nfd_portal,
},
);
// Tracy
const ztracy = b.dependency(
"ztracy",
.{
.target = target,
.optimize = options.externals_optimize,
.enable_ztracy = options.enable_tracy,
.enable_fibers = false,
.on_demand = options.tracy_on_demand,
},
);
// ZGUI
const zgui = b.dependency(
"zgui",
.{
.target = target,
.optimize = options.externals_optimize,
.backend = .glfw,
.with_implot = true,
.with_gizmo = true,
.with_node_editor = true,
.with_te = true,
.with_freetype = options.with_freetype,
},
);
// ZGLFW
const zglfw = b.dependency(
"zglfw",
.{
.target = target,
.optimize = options.externals_optimize,
},
);
// ZFLECS
const zflecs = b.dependency(
"zflecs",
.{
.target = target,
.optimize = options.externals_optimize,
},
);
// ZBGFX
const zbgfx = b.dependency(
"zbgfx",
.{
.target = target,
.optimize = options.externals_optimize,
.imgui_include = zgui.path("libs").getPath(b),
},
);
//
// TOOLS
//
// const copy_tool = b.addExecutable(.{
// .name = "copy",
// .root_source_file = .{ .path = "tools/copy.zig" },
// .target = target,
// });
const generate_static_tool = b.addExecutable(.{
.name = "generate_static",
.root_source_file = b.path("src/tools/generate_static.zig"),
.target = b.graph.host,
});
const generate_externals_tool = b.addExecutable(.{
.name = "generate_externals",
.root_source_file = b.path("src/tools/generate_externals.zig"),
.target = b.graph.host,
});
const generate_ide_tool = b.addExecutable(.{
.name = "generate_ide",
.root_source_file = b.path("src/tools/generate_ide.zig"),
.target = b.graph.host,
});
b.installArtifact(generate_ide_tool);
const ModulesSet = std.StringHashMapUnmanaged(void);
var module_set = ModulesSet{};
defer module_set.deinit(b.allocator);
for (all_modules) |module| {
try module_set.put(b.allocator, module, {});
}
// Modules
var enabled_modules = std.ArrayListUnmanaged([]const u8){};
defer enabled_modules.deinit(b.allocator);
if (options.modules) |modules| {
try enabled_modules.appendSlice(b.allocator, modules);
} else {
try enabled_modules.appendSlice(b.allocator, &core_modules);
if (options.enable_samples) try enabled_modules.appendSlice(b.allocator, &samples_modules);
if (options.enable_editor) try enabled_modules.appendSlice(b.allocator, &editor_modules);
}
//
// Generated content
//
const generated_files = b.addUpdateSourceFiles();
// _static.zig
const gen_static = b.addRunArtifact(generate_static_tool);
const _static_output_file = gen_static.addOutputFileArg("_static.zig");
if (options.static_modules) {
const modules_arg = try std.mem.join(b.allocator, ",", enabled_modules.items);
defer b.allocator.free(modules_arg);
gen_static.addArg(modules_arg);
} else {
gen_static.addArg("");
}
// Extrenals credits/license
const gen_externals = b.addRunArtifact(generate_externals_tool);
const external_credits_file = gen_externals.addOutputFileArg("externals_credit.md");
gen_externals.addFileArg(b.path(".external_credits.zon"));
if (external_credits) |credits| {
for (credits) |ec| {
gen_externals.addFileArg(ec);
}
}
//
// Init repository step
//
const init_step = b.step("init", "init repository");
initStep(b, init_step, "./");
//
// Gen IDE config
//
const gen_ide_step = b.step("gen-ide", "init/update IDE configs");
{
const gen_ide = b.addRunArtifact(generate_ide_tool);
gen_ide.addArgs(&.{ "--ide", @tagName(options.ide) });
gen_ide.addArg("--bin-path");
gen_ide.addDirectoryArg(b.path("zig-out/bin/cetech1"));
gen_ide.addArg("--project-path");
gen_ide.addDirectoryArg(b.path(""));
gen_ide.addArg("--fixtures");
gen_ide.addDirectoryArg(b.path("fixtures/"));
gen_ide.addArg("--config");
gen_ide.addDirectoryArg(b.path(".generate_ide.zon"));
gen_ide_step.dependOn(&gen_ide.step);
}
//
// CETech1 core build
//
const cetech1 = b.dependency(
"cetech1",
.{
.target = target,
.optimize = optimize,
.with_tracy = options.enable_tracy,
},
);
const static_module_module = b.addModule("static_module", .{
.root_source_file = _static_output_file,
.imports = &.{
.{ .name = "cetech1", .module = cetech1.module("cetech1") },
},
});
if (options.enable_shaderc) {
b.installArtifact(zbgfx.artifact("shaderc"));
}
if (options.dynamic_modules) {
var buff: [256:0]u8 = undefined;
for (enabled_modules.items) |m| {
if (!module_set.contains(m)) continue;
const artifact_name = try std.fmt.bufPrintZ(&buff, "ct_{s}", .{m});
const art = b.lazyDependency(m, .{
.target = target,
.optimize = optimize,
.link_mode = .dynamic,
}).?.artifact(artifact_name);
const step = b.addInstallArtifact(art, .{});
b.default_step.dependOn(&step.step);
}
}
const imports = [_]std.Build.Module.Import{
.{ .name = "cetech1", .module = cetech1.module("cetech1") },
.{ .name = "cetech1_options", .module = options_module },
.{ .name = "static_module", .module = static_module_module },
// Deps
.{ .name = "ztracy", .module = ztracy.module("root") },
.{ .name = "zglfw", .module = zglfw.module("root") },
.{ .name = "zgui", .module = zgui.module("root") },
.{ .name = "zflecs", .module = zflecs.module("root") },
.{ .name = "zf", .module = zf.module("zf") },
.{ .name = "Uuid", .module = uuid.module("Uuid") },
.{ .name = "zbgfx", .module = zbgfx.module("zbgfx") },
.{ .name = "znfde", .module = znfde.module("root") },
// Generated stuff
.{
.name = "externals_credit",
.module = b.createModule(.{ .root_source_file = external_credits_file }),
},
.{
.name = "authors",
.module = b.createModule(.{ .root_source_file = authors orelse b.path("AUTHORS.md") }),
},
.{
.name = "gamecontrollerdb",
.module = b.createModule(.{ .root_source_file = b.path("externals/shared/lib/SDL_GameControllerDB/gamecontrollerdb.txt") }),
},
.{ .name = "authors", .module = b.createModule(.{ .root_source_file = b.path("externals/shared/lib/SDL_GameControllerDB/gamecontrollerdb.txt") }) },
.{
.name = "fa-solid-900",
.module = b.createModule(.{ .root_source_file = b.path("externals/shared/fonts/fa-solid-900.ttf") }),
},
.{
.name = "Roboto-Medium",
.module = b.createModule(.{ .root_source_file = b.path("externals/shared/fonts/Roboto-Medium.ttf") }),
},
};
//
// CETech1 kernel lib
//
const kernel_lib = b.addStaticLibrary(.{
.name = "cetech1_kernel",
.version = cetech1_version,
.root_source_file = b.path("src/private.zig"),
.target = target,
.optimize = optimize,
});
useSystemSDK(b, target, kernel_lib);
b.installArtifact(kernel_lib);
kernel_lib.linkLibC();
kernel_lib.linkLibrary(ztracy.artifact("tracy"));
kernel_lib.linkLibrary(zglfw.artifact("glfw"));
kernel_lib.linkLibrary(zgui.artifact("imgui"));
kernel_lib.linkLibrary(zbgfx.artifact("bgfx"));
kernel_lib.linkLibrary(zflecs.artifact("flecs"));
if (options.enable_nfd) {
kernel_lib.root_module.addImport("znfde", znfde.module("root"));
kernel_lib.linkLibrary(znfde.artifact("nfde"));
}
const kernel_module = b.addModule("kernel", .{
.root_source_file = b.path("src/private.zig"),
.imports = &imports,
});
//
// CETech1 kernel standalone exe
//
const exe = createKernelExe(
b,
"cetech1",
b.path("src/main.zig"),
kernel_module,
kernel_lib,
cetech1_version,
target,
optimize,
);
// Make exe depends on generated files.
exe.step.dependOn(&generated_files.step);
//
// CETech1 kernel standalone tests
//
const tests = b.addTest(.{
.name = "cetech1_test",
.version = cetech1_version,
.root_module = b.createModule(.{
.root_source_file = b.path("src/tests.zig"),
.target = target,
.optimize = optimize,
.imports = &imports,
}),
.target = target,
.optimize = optimize,
});
useSystemSDK(b, target, tests);
b.installArtifact(tests);
tests.linkLibC();
tests.linkLibrary(kernel_lib);
tests.step.dependOn(&generated_files.step);
const run_unit_tests = b.addRunArtifact(tests);
run_unit_tests.step.dependOn(b.getInstallStep());
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_unit_tests.step);
const run_tests_ui = b.addRunArtifact(exe);
run_tests_ui.addArgs(&.{ "--test-ui", "--headless" });
run_tests_ui.step.dependOn(b.getInstallStep());
const testui_step = b.step("test-ui", "Run UI headless test");
testui_step.dependOn(&run_tests_ui.step);
//
// Static modules linking
//
inline for (.{ tests, kernel_lib }) |e| {
if (options.static_modules) {
var buff: [256:0]u8 = undefined;
for (enabled_modules.items) |m| {
if (!module_set.contains(m)) continue;
const artifact_name = try std.fmt.bufPrintZ(&buff, "ct_{s}", .{m});
e.linkLibrary(b.lazyDependency(m, .{
.target = target,
.optimize = optimize,
.link_mode = .static,
}).?.artifact(artifact_name));
}
}
}
}
fn ensureZigVersion() !void {
var installed_ver = builtin.zig_version;
installed_ver.build = null;
if (installed_ver.order(min_zig_version) == .lt) {
std.log.err("\n" ++
\\---------------------------------------------------------------------------
\\
\\Installed Zig compiler version is too old.
\\
\\Min. required version: {any}
\\Installed version: {any}
\\
\\Please install newer version and try again.
\\zig/get_zig.sh <ARCH>
\\
\\---------------------------------------------------------------------------
\\
, .{ min_zig_version, installed_ver });
return error.ZigIsTooOld;
}
}
pub const editor_modules = [_][]const u8{
"editor",
"editor_asset",
"editor_asset_browser",
"editor_explorer",
"editor_fixtures",
"editor_inspector",
"editor_obj_buffer",
"editor_tags",
"editor_tree",
"editor_log",
"editor_graph",
"editor_metrics",
"editor_entity_asset",
"editor_entity",
"editor_asset_preview",
"editor_simulation",
"editor_renderer",
};
pub const core_modules = [_][]const u8{
"graphvm",
"renderer",
"default_rg",
"shader_system",
"render_component",
"entity_logic_component",
"transform",
"camera",
};
pub const samples_modules = [_][]const u8{
// Zig based module
"foo",
// Zig editor tab sample
"editor_foo_tab",
// Zig editor viewport tab sample
"editor_foo_viewport_tab",
};
pub const all_modules = editor_modules ++ core_modules ++ samples_modules;