-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.zig
663 lines (616 loc) · 26.5 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
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
const std = @import("std");
const assert = std.debug.assert;
const builtin = @import("builtin");
const arch = builtin.cpu.arch;
const os = builtin.os.tag;
fn discover_brew_prefix(b: *std.Build, library_name: []const u8) ![]const u8 {
assert(os == .macos);
const result = try std.process.Child.run(.{
.allocator = b.allocator,
.argv = &.{ "brew", "--prefix", library_name },
});
var i = result.stdout.len - 1;
while (result.stdout[i] == '\n' or result.stdout[i] == ' ') {
i -= 1;
}
const trimmed_path = result.stdout[0 .. i + 1];
return trimmed_path;
}
pub fn build(b: *std.Build) !void {
const self_hosted_ci = b.option(bool, "self_hosted_ci", "This option enables the self-hosted CI behavior") orelse false;
const third_party_ci = b.option(bool, "third_party_ci", "This option enables the third-party CI behavior") orelse false;
const is_ci = self_hosted_ci or third_party_ci;
const print_stack_trace = b.option(bool, "print_stack_trace", "This option enables printing stack traces inside the compiler") orelse is_ci;
const native_target = b.resolveTargetQuery(.{});
const optimization = b.standardOptimizeOption(.{});
const enable_editor = false;
const use_editor = b.option(bool, "editor", "Use the GUI editor to play around the programming language") orelse (!is_ci and enable_editor);
const use_debug = b.option(bool, "use_debug", "This option enables the LLVM debug build in the development PC") orelse false;
const sanitize = b.option(bool, "sanitize", "This option enables sanitizers for the compiler") orelse false;
const sleep_on_thread_hot_loops = b.option(bool, "sleep", "This option enables sleep calls on hot threaded loops") orelse false;
const static = b.option(bool, "static", "This option enables the compiler to be built statically") orelse switch (@import("builtin").os.tag) {
else => use_debug,
.windows => true,
// .macos => true,
};
const timers = b.option(bool, "timers", "This option enables to make and print timers") orelse !is_ci and switch (optimization) {
.Debug => false,
else => true,
};
const fetcher = b.addExecutable(.{
.name = "llvm_fetcher",
.root_source_file = b.path("build/fetcher.zig"),
.target = native_target,
.optimize = .Debug,
});
var target_query = b.standardTargetOptionsQueryOnly(switch (@import("builtin").os.tag) {
else => .{},
.windows => .{
.default_target = .{
.cpu_model = .{ .explicit = &std.Target.x86.cpu.x86_64_v3 },
.cpu_arch = .x86_64,
.os_tag = .windows,
.abi = .gnu,
},
},
});
const abi = b.option(std.Target.Abi, "abi", "This option modifies the ABI used for the compiler") orelse if (static) switch (target_query.os_tag orelse @import("builtin").os.tag) {
else => target_query.abi,
.linux => b: {
const os_release = try std.fs.cwd().readFileAlloc(b.allocator, "/etc/os-release", 0xffff);
const attribute_name = "ID=";
const i = std.mem.indexOf(u8, os_release, attribute_name) orelse unreachable;
const maybe_start_quote_index = i + attribute_name.len;
const value = if (os_release[maybe_start_quote_index] == '\"') attr: {
const end_quote_index = std.mem.indexOf(u8, os_release[maybe_start_quote_index + 1 ..], "\"") orelse unreachable;
const attribute_value = os_release[maybe_start_quote_index + 1 ..][0..end_quote_index];
break :attr attribute_value;
} else attr: {
const line_feed = std.mem.indexOf(u8, os_release[maybe_start_quote_index..], "\n") orelse unreachable;
const attribute_value = os_release[maybe_start_quote_index..][0..line_feed];
break :attr attribute_value;
};
break :b if (std.mem.eql(u8, value, "arch") or std.mem.eql(u8, value, "endeavouros")) .musl else target_query.abi;
},
} else target_query.abi;
target_query.abi = abi;
const target = b.resolveTargetQuery(target_query);
const compiler = b.addExecutable(.{
.name = "nat",
.root_source_file = b.path("bootstrap/main.zig"),
.target = target,
.optimize = optimization,
});
const cpp_files = .{
"src/llvm/llvm.cpp",
"src/llvm/lld.cpp",
"src/llvm/clang_main.cpp",
"src/llvm/clang_cc1.cpp",
"src/llvm/clang_cc1as.cpp",
// "src/llvm/ar.cpp",
};
compiler.addCSourceFiles(.{
.files = &cpp_files,
.flags = &.{
"-g",
"-std=c++17",
"-D__STDC_CONSTANT_MACROS",
"-D__STDC_FORMAT_MACROS",
"-D__STDC_LIMIT_MACROS",
"-D_GNU_SOURCE",
"-fno-exceptions",
"-fno-rtti",
"-fno-stack-protector",
"-fvisibility-inlines-hidden",
"-Wno-type-limits",
"-Wno-missing-braces",
"-Wno-comment",
},
});
compiler.formatted_panics = print_stack_trace;
compiler.root_module.unwind_tables = print_stack_trace or target.result.os.tag == .windows;
compiler.root_module.omit_frame_pointer = false;
compiler.root_module.error_tracing = print_stack_trace;
compiler.want_lto = false;
if (sanitize) {
compiler.root_module.sanitize_thread = true;
}
compiler.linkLibC();
const zstd = if (target.result.os.tag == .windows) "zstd.lib" else "libzstd.a";
const static_llvm_libraries = [_][]const u8{
"libLLVMAArch64AsmParser.a",
"libLLVMAArch64CodeGen.a",
"libLLVMAArch64Desc.a",
"libLLVMAArch64Disassembler.a",
"libLLVMAArch64Info.a",
"libLLVMAArch64Utils.a",
"libLLVMAggressiveInstCombine.a",
"libLLVMAMDGPUAsmParser.a",
"libLLVMAMDGPUCodeGen.a",
"libLLVMAMDGPUDesc.a",
"libLLVMAMDGPUDisassembler.a",
"libLLVMAMDGPUInfo.a",
"libLLVMAMDGPUTargetMCA.a",
"libLLVMAMDGPUUtils.a",
"libLLVMAnalysis.a",
"libLLVMARMAsmParser.a",
"libLLVMARMCodeGen.a",
"libLLVMARMDesc.a",
"libLLVMARMDisassembler.a",
"libLLVMARMInfo.a",
"libLLVMARMUtils.a",
"libLLVMAsmParser.a",
"libLLVMAsmPrinter.a",
"libLLVMAVRAsmParser.a",
"libLLVMAVRCodeGen.a",
"libLLVMAVRDesc.a",
"libLLVMAVRDisassembler.a",
"libLLVMAVRInfo.a",
"libLLVMBinaryFormat.a",
"libLLVMBitReader.a",
"libLLVMBitstreamReader.a",
"libLLVMBitWriter.a",
"libLLVMBPFAsmParser.a",
"libLLVMBPFCodeGen.a",
"libLLVMBPFDesc.a",
"libLLVMBPFDisassembler.a",
"libLLVMBPFInfo.a",
"libLLVMCFGuard.a",
"libLLVMCFIVerify.a",
"libLLVMCodeGen.a",
"libLLVMCodeGenTypes.a",
"libLLVMCore.a",
"libLLVMCoroutines.a",
"libLLVMCoverage.a",
"libLLVMDebugInfoBTF.a",
"libLLVMDebugInfoCodeView.a",
"libLLVMDebuginfod.a",
"libLLVMDebugInfoDWARF.a",
"libLLVMDebugInfoGSYM.a",
"libLLVMDebugInfoLogicalView.a",
"libLLVMDebugInfoMSF.a",
"libLLVMDebugInfoPDB.a",
"libLLVMDemangle.a",
"libLLVMDiff.a",
"libLLVMDlltoolDriver.a",
"libLLVMDWARFLinker.a",
"libLLVMDWARFLinkerParallel.a",
"libLLVMDWP.a",
"libLLVMExecutionEngine.a",
"libLLVMExtensions.a",
"libLLVMFileCheck.a",
"libLLVMFrontendHLSL.a",
"libLLVMFrontendOpenACC.a",
"libLLVMFrontendOpenMP.a",
"libLLVMFuzzerCLI.a",
"libLLVMFuzzMutate.a",
"libLLVMGlobalISel.a",
"libLLVMHexagonAsmParser.a",
"libLLVMHexagonCodeGen.a",
"libLLVMHexagonDesc.a",
"libLLVMHexagonDisassembler.a",
"libLLVMHexagonInfo.a",
"libLLVMInstCombine.a",
"libLLVMInstrumentation.a",
"libLLVMInterfaceStub.a",
"libLLVMInterpreter.a",
"libLLVMipo.a",
"libLLVMIRPrinter.a",
"libLLVMIRReader.a",
"libLLVMJITLink.a",
"libLLVMLanaiAsmParser.a",
"libLLVMLanaiCodeGen.a",
"libLLVMLanaiDesc.a",
"libLLVMLanaiDisassembler.a",
"libLLVMLanaiInfo.a",
"libLLVMLibDriver.a",
"libLLVMLineEditor.a",
"libLLVMLinker.a",
"libLLVMLoongArchAsmParser.a",
"libLLVMLoongArchCodeGen.a",
"libLLVMLoongArchDesc.a",
"libLLVMLoongArchDisassembler.a",
"libLLVMLoongArchInfo.a",
"libLLVMLTO.a",
"libLLVMMC.a",
"libLLVMMCA.a",
"libLLVMMCDisassembler.a",
"libLLVMMCJIT.a",
"libLLVMMCParser.a",
"libLLVMMipsAsmParser.a",
"libLLVMMipsCodeGen.a",
"libLLVMMipsDesc.a",
"libLLVMMipsDisassembler.a",
"libLLVMMipsInfo.a",
"libLLVMMIRParser.a",
"libLLVMMSP430AsmParser.a",
"libLLVMMSP430CodeGen.a",
"libLLVMMSP430Desc.a",
"libLLVMMSP430Disassembler.a",
"libLLVMMSP430Info.a",
"libLLVMNVPTXCodeGen.a",
"libLLVMNVPTXDesc.a",
"libLLVMNVPTXInfo.a",
"libLLVMObjCARCOpts.a",
"libLLVMObjCopy.a",
"libLLVMObject.a",
"libLLVMObjectYAML.a",
"libLLVMOption.a",
"libLLVMOrcJIT.a",
"libLLVMOrcShared.a",
"libLLVMOrcTargetProcess.a",
"libLLVMPasses.a",
"libLLVMPowerPCAsmParser.a",
"libLLVMPowerPCCodeGen.a",
"libLLVMPowerPCDesc.a",
"libLLVMPowerPCDisassembler.a",
"libLLVMPowerPCInfo.a",
"libLLVMProfileData.a",
"libLLVMRemarks.a",
"libLLVMRISCVAsmParser.a",
"libLLVMRISCVCodeGen.a",
"libLLVMRISCVDesc.a",
"libLLVMRISCVDisassembler.a",
"libLLVMRISCVInfo.a",
"libLLVMRISCVTargetMCA.a",
"libLLVMRuntimeDyld.a",
"libLLVMScalarOpts.a",
"libLLVMSelectionDAG.a",
"libLLVMSparcAsmParser.a",
"libLLVMSparcCodeGen.a",
"libLLVMSparcDesc.a",
"libLLVMSparcDisassembler.a",
"libLLVMSparcInfo.a",
"libLLVMSupport.a",
"libLLVMSymbolize.a",
"libLLVMSystemZAsmParser.a",
"libLLVMSystemZCodeGen.a",
"libLLVMSystemZDesc.a",
"libLLVMSystemZDisassembler.a",
"libLLVMSystemZInfo.a",
"libLLVMTableGen.a",
"libLLVMTableGenCommon.a",
"libLLVMTableGenGlobalISel.a",
"libLLVMTarget.a",
"libLLVMTargetParser.a",
"libLLVMTextAPI.a",
"libLLVMTransformUtils.a",
"libLLVMVEAsmParser.a",
"libLLVMVECodeGen.a",
"libLLVMVectorize.a",
"libLLVMVEDesc.a",
"libLLVMVEDisassembler.a",
"libLLVMVEInfo.a",
"libLLVMWebAssemblyAsmParser.a",
"libLLVMWebAssemblyCodeGen.a",
"libLLVMWebAssemblyDesc.a",
"libLLVMWebAssemblyDisassembler.a",
"libLLVMWebAssemblyInfo.a",
"libLLVMWebAssemblyUtils.a",
"libLLVMWindowsDriver.a",
"libLLVMWindowsManifest.a",
"libLLVMX86AsmParser.a",
"libLLVMX86CodeGen.a",
"libLLVMX86Desc.a",
"libLLVMX86Disassembler.a",
"libLLVMX86Info.a",
"libLLVMX86TargetMCA.a",
"libLLVMXCoreCodeGen.a",
"libLLVMXCoreDesc.a",
"libLLVMXCoreDisassembler.a",
"libLLVMXCoreInfo.a",
"libLLVMXRay.a",
//LLD
"liblldCOFF.a",
"liblldCommon.a",
"liblldELF.a",
"liblldMachO.a",
"liblldMinGW.a",
"liblldWasm.a",
// Zlib
"libz.a",
// Zstd
zstd,
// Clang
"libclangAnalysis.a",
"libclangAnalysisFlowSensitive.a",
"libclangAnalysisFlowSensitiveModels.a",
"libclangAPINotes.a",
"libclangARCMigrate.a",
"libclangAST.a",
"libclangASTMatchers.a",
"libclangBasic.a",
"libclangCodeGen.a",
"libclangCrossTU.a",
"libclangDependencyScanning.a",
"libclangDirectoryWatcher.a",
"libclangDriver.a",
"libclangDynamicASTMatchers.a",
"libclangEdit.a",
"libclangExtractAPI.a",
"libclangFormat.a",
"libclangFrontend.a",
"libclangFrontendTool.a",
"libclangHandleCXX.a",
"libclangHandleLLVM.a",
"libclangIndex.a",
"libclangIndexSerialization.a",
"libclangInterpreter.a",
"libclangLex.a",
"libclangParse.a",
"libclangRewrite.a",
"libclangRewriteFrontend.a",
"libclangSema.a",
"libclangSerialization.a",
"libclangStaticAnalyzerCheckers.a",
"libclangStaticAnalyzerCore.a",
"libclangStaticAnalyzerFrontend.a",
"libclangSupport.a",
"libclangTooling.a",
"libclangToolingASTDiff.a",
"libclangToolingCore.a",
"libclangToolingInclusions.a",
"libclangToolingInclusionsStdlib.a",
"libclangToolingRefactoring.a",
"libclangToolingSyntax.a",
"libclangTransformer.a",
};
var include_paths = std.ArrayList([]const u8).init(b.allocator);
if (static or target.result.os.tag == .windows) {
if (target.result.os.tag == .linux) compiler.linkage = .static;
compiler.linkLibCpp();
const prefix = "nat/cache";
const llvm_version = "17.0.6";
const llvm_path = b.option([]const u8, "llvm_path", "LLVM prefix path") orelse blk: {
assert(!self_hosted_ci);
if (third_party_ci or (!target.query.isNativeOs() or !target.query.isNativeCpu())) {
var llvm_directory = try std.ArrayListUnmanaged(u8).initCapacity(b.allocator, 128);
const cpu = if (std.mem.eql(u8, target.result.cpu.model.name, @tagName(target.result.cpu.arch))) "baseline" else target.result.cpu.model.name;
if (!is_ci) {
llvm_directory.appendSliceAssumeCapacity(prefix ++ "/");
llvm_directory.appendSliceAssumeCapacity("llvm-");
llvm_directory.appendSliceAssumeCapacity(llvm_version);
llvm_directory.appendSliceAssumeCapacity("-");
llvm_directory.appendSliceAssumeCapacity(@tagName(target.result.cpu.arch));
llvm_directory.appendSliceAssumeCapacity("-");
llvm_directory.appendSliceAssumeCapacity(@tagName(target.result.os.tag));
llvm_directory.appendSliceAssumeCapacity("-");
llvm_directory.appendSliceAssumeCapacity(@tagName(target.result.abi));
llvm_directory.appendSliceAssumeCapacity("-");
llvm_directory.appendSliceAssumeCapacity(cpu);
} else {
llvm_directory.appendSliceAssumeCapacity(prefix ++ "/x86_64-linux-gnu-x86_64_v3-release-static");
}
const url = if (is_ci) "https://github.com/birth-software/fetch-llvm/releases/download/v17.0.6/x86_64-linux-gnu-x86_64_v3-release-static.tar.gz" else try std.mem.concat(b.allocator, u8, &.{ "https://github.com/birth-software/fetch-llvm/releases/download/v", llvm_version, "/llvm-", llvm_version, "-", @tagName(target.result.cpu.arch), "-", @tagName(target.result.os.tag), "-", @tagName(target.result.abi), "-", cpu, ".tar.xz" });
var dir = std.fs.cwd().openDir(llvm_directory.items, .{}) catch {
const run = b.addRunArtifact(fetcher);
compiler.step.dependOn(&run.step);
run.addArg("-prefix");
run.addArg(prefix);
run.addArg("-url");
run.addArg(url);
break :blk llvm_directory.items;
};
dir.close();
break :blk llvm_directory.items;
} else {
break :blk switch (use_debug) {
true => "../zig-bootstrap/out/x86_64-linux-musl-native",
false => "../zig-bootstrap/out/x86_64-linux-musl-native",
};
}
};
const llvm_include_dir = try std.mem.concat(b.allocator, u8, &.{ llvm_path, "/include" });
compiler.addIncludePath(std.Build.path(b, llvm_include_dir));
const llvm_lib_dir = try std.mem.concat(b.allocator, u8, &.{ llvm_path, "/lib" });
for (static_llvm_libraries) |llvm_library| {
compiler.addObjectFile(std.Build.path(b, try std.mem.concat(b.allocator, u8, &.{ llvm_lib_dir, "/", llvm_library })));
}
} else {
compiler.linkSystemLibrary("LLVM");
compiler.linkSystemLibrary("clang-cpp");
compiler.linkSystemLibrary("lldCommon");
compiler.linkSystemLibrary("lldCOFF");
compiler.linkSystemLibrary("lldELF");
compiler.linkSystemLibrary("lldMachO");
compiler.linkSystemLibrary("lldWasm");
// compiler.linkSystemLibrary("unwind");
compiler.linkSystemLibrary(if (is_ci or builtin.os.tag == .macos) "z" else "zlib");
compiler.linkSystemLibrary("zstd");
var llvm_prefix: []const u8 = "";
switch (target.result.os.tag) {
.linux => {
if (third_party_ci) {
compiler.addObjectFile(.{ .cwd_relative = "/lib/x86_64-linux-gnu/libstdc++.so.6" });
try include_paths.append("/usr/include");
try include_paths.append("/usr/include" );
try include_paths.append("/usr/include/x86_64-linux-gnu" );
try include_paths.append("/usr/include/c++/11" );
try include_paths.append("/usr/include/x86_64-linux-gnu/c++/11" );
try include_paths.append("/usr/lib/llvm-18/include" );
llvm_prefix = "/usr/lib/llvm-18";
compiler.addLibraryPath(.{ .cwd_relative = "/lib/x86_64-linux-gnu" });
compiler.addLibraryPath(.{ .cwd_relative = "/usr/lib/llvm-18/lib" });
} else {
const result = try std.process.Child.run(.{
.allocator = b.allocator,
.argv = &.{
"c++", "--version",
},
.max_output_bytes = 0xffffffffffffff,
});
const success = switch (result.term) {
.Exited => |exit_code| exit_code == 0,
else => false,
};
if (!success) {
unreachable;
}
var tokenizer = std.mem.tokenizeScalar(u8, result.stdout, ' ');
const cxx_version = while (tokenizer.next()) |chunk| {
if (std.ascii.isDigit(chunk[0])) {
if (std.SemanticVersion.parse(chunk)) |sema_version| {
break switch (builtin.cpu.arch) {
.x86_64 => try std.fmt.allocPrint(b.allocator, "{}.{}.{}", .{sema_version.major, sema_version.minor, sema_version.patch}),
.aarch64 => try std.fmt.allocPrint(b.allocator, "{}", .{sema_version.major}),
else => @compileError("Architecture not supported"),
};
} else |err| err catch {};
}
} else {
unreachable;
};
const cxx_include_base = try std.mem.concat(b.allocator, u8, &.{ "/usr/include/c++/", cxx_version });
const cxx_include_arch = try std.mem.concat(b.allocator, u8, &.{ cxx_include_base, "/" ++ @tagName(builtin.cpu.arch) ++ switch (builtin.cpu.arch) {
.x86_64 => "-pc-linux-gnu",
.aarch64 => "-redhat-linux",
else => @compileError("Architecture not supported"),
}
});
compiler.addObjectFile(.{ .cwd_relative = "/usr/lib64/libstdc++.so.6" });
llvm_prefix = if (use_debug) "../../local/llvm18-debug/" else "../../local/llvm18-release";
try include_paths.append(try std.mem.concat(b.allocator, u8, &.{llvm_prefix, "/include"}));
try include_paths.append("/usr/include");
try include_paths.append(cxx_include_base);
try include_paths.append(cxx_include_arch);
compiler.addLibraryPath(.{ .cwd_relative = if (use_debug) "../../local/llvm18-debug/lib" else "../../local/llvm18-release/lib" });
compiler.addLibraryPath(.{ .cwd_relative = "/usr/lib64" });
}
},
.macos => {
compiler.linkLibCpp();
if (discover_brew_prefix(b, "llvm@18")) |prefix| {
llvm_prefix = prefix;
const llvm_include_path = try std.mem.concat(b.allocator, u8, &.{ llvm_prefix, "/include" });
const llvm_lib_path = try std.mem.concat(b.allocator, u8, &.{ llvm_prefix, "/lib" });
try include_paths.append(llvm_include_path);
compiler.addLibraryPath(.{ .cwd_relative = llvm_lib_path });
} else |err| {
return err;
}
if (discover_brew_prefix(b, "zstd")) |zstd_prefix| {
const zstd_lib_path = try std.mem.concat(b.allocator, u8, &.{ zstd_prefix, "/lib" });
compiler.addLibraryPath(.{ .cwd_relative = zstd_lib_path });
} else |err| {
return err;
}
if (discover_brew_prefix(b, "zlib")) |zlib_prefix| {
const zlib_lib_path = try std.mem.concat(b.allocator, u8, &.{ zlib_prefix, "/lib" });
compiler.addLibraryPath(.{ .cwd_relative = zlib_lib_path });
} else |err| {
return err;
}
},
.windows => {},
else => |tag| @panic(@tagName(tag)),
}
for (include_paths.items) |include_path| {
compiler.addIncludePath(.{ .cwd_relative = include_path });
}
try include_paths.append(try std.mem.concat(b.allocator, u8, &.{llvm_prefix, "/lib/clang/18/include"}));
if (use_editor) {
compiler.linkSystemLibrary("glfw");
compiler.linkSystemLibrary("GL");
compiler.addCSourceFiles(.{
.root = b.path("dependencies/imgui"),
.files = &.{
"imgui.cpp",
"imgui_draw.cpp",
"imgui_tables.cpp",
"imgui_widgets.cpp",
"backends/imgui_impl_glfw.cpp",
"backends/imgui_impl_opengl3.cpp",
},
.flags = &.{},
});
compiler.addCSourceFile(.{
.file = b.path("bootstrap/editor.cpp"),
.flags = &.{ "-g" },
});
compiler.addIncludePath(b.path("dependencies/imgui"));
}
}
const compiler_options = b.addOptions();
compiler_options.addOption(bool, "print_stack_trace", print_stack_trace);
compiler_options.addOption(bool, "ci", is_ci);
compiler_options.addOption(bool, "editor", use_editor);
compiler_options.addOption(bool, "sleep_on_thread_hot_loops", sleep_on_thread_hot_loops);
compiler_options.addOption([]const []const u8, "include_paths", include_paths.items);
compiler_options.addOption(bool, "timers", timers);
compiler.root_module.addOptions("configuration", compiler_options);
if (target.result.os.tag == .windows) {
compiler.linkSystemLibrary("ole32");
compiler.linkSystemLibrary("version");
compiler.linkSystemLibrary("uuid");
}
const install_exe = b.addInstallArtifact(compiler, .{});
b.getInstallStep().dependOn(&install_exe.step);
b.installDirectory(.{
.source_dir = std.Build.path(b, "lib"),
.install_dir = .bin,
.install_subdir = "lib",
});
const compiler_exe_path = b.fmt("zig-out/bin/{s}", .{install_exe.dest_sub_path});
const run_command = b.addSystemCommand(&.{compiler_exe_path});
run_command.step.dependOn(b.getInstallStep());
const debug_command = switch (os) {
.linux => blk: {
const result = b.addSystemCommand(&.{"gf2"});
result.addArgs(&.{ "-ex", "set debuginfod enabled off" });
result.addArgs(&.{ "-ex", "set disassembly-flavor intel" });
result.addArg("-ex=r");
result.addArgs(&.{ "-ex", "up" });
result.addArg("--args");
break :blk result;
},
.windows => blk: {
const result = b.addSystemCommand(&.{"remedybg"});
result.addArg("-g");
break :blk result;
},
.macos => blk: {
const result = b.addSystemCommand(&.{"lldb"});
result.addArg("--");
break :blk result;
},
else => @compileError("OS not supported"),
};
debug_command.step.dependOn(b.getInstallStep());
debug_command.addArg(compiler_exe_path);
const test_runner = b.addExecutable(.{
.name = "test_runner",
.root_source_file = b.path("build/test_runner.zig"),
.target = native_target,
.optimize = optimization,
});
b.default_step.dependOn(&test_runner.step);
const test_command = b.addRunArtifact(test_runner);
test_command.step.dependOn(&compiler.step);
b.installArtifact(test_runner);
test_command.step.dependOn(b.getInstallStep());
const new_test = b.addExecutable(.{
.name = "new_test",
.target = native_target,
.root_source_file = b.path("build/new_test.zig"),
.optimize = .ReleaseSmall,
});
b.default_step.dependOn(&new_test.step);
const new_test_command = b.addRunArtifact(new_test);
new_test_command.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_command.addArgs(args);
debug_command.addArgs(args);
test_command.addArgs(args);
new_test_command.addArgs(args);
}
const run_step = b.step("run", "Test the Nativity compiler");
run_step.dependOn(&run_command.step);
const debug_step = b.step("debug", "Debug the Nativity compiler");
debug_step.dependOn(&debug_command.step);
const test_step = b.step("test", "Test the Nativity compiler");
test_step.dependOn(&test_command.step);
const new_test_step = b.step("new_test", "Script to make a new test");
new_test_step.dependOn(&new_test_command.step);
const test_all = b.step("test_all", "Test all");
test_all.dependOn(&test_command.step);
}