-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
1596 lines (1423 loc) · 64.1 KB
/
build.zig
File metadata and controls
1596 lines (1423 loc) · 64.1 KB
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
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ============================================================================
// Build Script
// ============================================================================
//
// build configuration for the Ora compiler, tests, and toolchain integration.
//
// ============================================================================
const std = @import("std");
// 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.
const target = b.standardTargetOptions(.{});
// 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(.{});
// mlir-specific build options
const enable_mlir_debug = b.option(bool, "mlir-debug", "Enable MLIR debug features and verification passes") orelse false;
const enable_mlir_timing = b.option(bool, "mlir-timing", "Enable MLIR pass timing by default") orelse false;
const mlir_opt_level = b.option([]const u8, "mlir-opt", "Default MLIR optimization level (none, basic, aggressive)") orelse "basic";
const enable_mlir_passes = b.option([]const u8, "mlir-passes", "Default MLIR pass pipeline") orelse null;
const skip_mlir_build = b.option(bool, "skip-mlir", "Skip MLIR/SIR/Ora dialect CMake builds (use existing libs)") orelse false;
// this creates a "module", which represents a collection of source files alongside
// some compilation options, such as optimization mode and linked system libraries.
// every executable or library we compile will be based on one or more modules.
const lib_mod = b.createModule(.{
// `root_source_file` is the Zig "entry point" of the module. If a module
// only contains e.g. external object files, you can make this `null`.
// in this case the main source file is merely a path, however, in more
// complicated build scripts, this could be a generated file.
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
});
// we will also create a module for our other entry point, 'main.zig'.
const exe_mod = b.createModule(.{
// `root_source_file` is the Zig "entry point" of the module. If a module
// only contains e.g. external object files, you can make this `null`.
// in this case the main source file is merely a path, however, in more
// complicated build scripts, this could be a generated file.
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
const log_mod = b.createModule(.{
.root_source_file = b.path("src/logging.zig"),
.target = target,
.optimize = optimize,
});
const mlir_c_mod = b.createModule(.{
.root_source_file = b.path("src/mlir/c.zig"),
.target = target,
.optimize = optimize,
});
mlir_c_mod.addIncludePath(b.path("vendor/mlir/include"));
mlir_c_mod.addIncludePath(b.path("src/mlir/ora/include"));
mlir_c_mod.addIncludePath(b.path("src/mlir/IR/include"));
// modules can depend on one another using the `std.Build.Module.addImport` function.
// this is what allows Zig source code to use `@import("foo")` where 'foo' is not a
// file path. In this case, we set up `exe_mod` to import `lib_mod`.
exe_mod.addImport("ora_lib", lib_mod);
exe_mod.addImport("mlir_c_api", mlir_c_mod);
exe_mod.addImport("log", log_mod);
lib_mod.addImport("mlir_c_api", mlir_c_mod);
lib_mod.addImport("log", log_mod);
// now, we will create a static library based on the module we created above.
// this creates a `std.Build.Step.Compile`, which is the build step responsible
// for actually invoking the compiler.
const lib = b.addLibrary(.{
.linkage = .static,
.name = "ora",
.root_module = lib_mod,
});
// this declares intent for the library to be installed into the standard
// location when the user invokes the "install" step (the default step when
// running `zig build`).
b.installArtifact(lib);
// this creates another `std.Build.Step.Compile`, but this one builds an executable
// rather than a static library.
const exe = b.addExecutable(.{
.name = "ora",
.root_module = exe_mod,
});
// add MLIR build options as compile-time constants
const mlir_options = b.addOptions();
mlir_options.addOption(bool, "mlir_debug", enable_mlir_debug);
mlir_options.addOption(bool, "mlir_timing", enable_mlir_timing);
mlir_options.addOption([]const u8, "mlir_opt_level", mlir_opt_level);
if (enable_mlir_passes) |passes| {
mlir_options.addOption(?[]const u8, "mlir_passes", passes);
} else {
mlir_options.addOption(?[]const u8, "mlir_passes", null);
}
exe.root_module.addOptions("build_options", mlir_options);
lib_mod.addOptions("build_options", mlir_options);
// add include paths
exe.addIncludePath(b.path("src"));
lib.addIncludePath(b.path("src"));
// add Ora dialect include path (for OraCAPI.h)
const ora_dialect_include_path = b.path("src/mlir/ora/include");
exe.addIncludePath(ora_dialect_include_path);
// add SIR dialect include path
const sir_dialect_include_path = b.path("src/mlir/IR/include");
exe.addIncludePath(sir_dialect_include_path);
// build and link MLIR (required) - only for executable, not library
const mlir_step = if (skip_mlir_build) null else buildMlirLibraries(b, target, optimize);
// build SIR dialect first (Ora dialect depends on it)
const sir_dialect_step = if (skip_mlir_build) null else buildSIRDialectLibrary(b, mlir_step.?, target, optimize);
const ora_dialect_step = if (skip_mlir_build) null else buildOraDialectLibrary(b, mlir_step.?, sir_dialect_step.?, target, optimize);
linkMlirLibraries(b, exe, mlir_step, ora_dialect_step, sir_dialect_step, target);
// build and link Z3 (for formal verification) - only for executable
const z3_step = buildZ3Libraries(b, target, optimize);
linkZ3Libraries(b, exe, z3_step, target);
// this declares intent for the executable to be installed into the
// standard location when the user invokes the "install" step (the default
// step when running `zig build`).
b.installArtifact(exe);
// 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);
// Sensei SIR CLI integration (vendored Rust tool)
const sensei_root = "vendor/sensei/senseic";
const sensei_cargo = b.fmt("{s}/Cargo.toml", .{sensei_root});
if (std.fs.cwd().access(sensei_cargo, .{}) catch null) |_| {
const sensei_build_cmd = b.addSystemCommand(&[_][]const u8{
"cargo",
"build",
"-p",
"sir-cli",
"--release",
});
sensei_build_cmd.setCwd(b.path(sensei_root));
const sensei_build_step = b.step("sensei-sir", "Build Sensei SIR CLI");
sensei_build_step.dependOn(&sensei_build_cmd.step);
if (@import("builtin").os.tag != .windows) {
const sir_bin = b.fmt("{s}/target/release/sir", .{sensei_root});
const sample_input = "tests/sensei/sample.sir";
const e2e_script = b.fmt(
"out=$({s} {s}); test \"${{out#0x}}\" != \"$out\" -a ${{#out}} -gt 2",
.{ sir_bin, sample_input },
);
const sensei_e2e_cmd = b.addSystemCommand(&[_][]const u8{
"bash",
"-lc",
e2e_script,
});
sensei_e2e_cmd.step.dependOn(&sensei_build_cmd.step);
const sensei_e2e_step = b.step("sensei-e2e", "Run Sensei SIR -> bytecode smoke test");
sensei_e2e_step.dependOn(&sensei_e2e_cmd.step);
}
}
// create optimization demo executable
const optimization_demo_mod = b.createModule(.{
.root_source_file = b.path("examples/demos/optimization_demo.zig"),
.target = target,
.optimize = optimize,
});
optimization_demo_mod.addImport("ora_lib", lib_mod);
const optimization_demo = b.addExecutable(.{
.name = "optimization_demo",
.root_module = optimization_demo_mod,
});
const run_optimization_demo = b.addRunArtifact(optimization_demo);
run_optimization_demo.step.dependOn(b.getInstallStep());
const optimization_demo_step = b.step("optimization-demo", "Run the optimization demo");
optimization_demo_step.dependOn(&run_optimization_demo.step);
// create formal verification demo executable
const formal_verification_demo_mod = b.createModule(.{
.root_source_file = b.path("examples/demos/formal_verification_demo.zig"),
.target = target,
.optimize = optimize,
});
formal_verification_demo_mod.addImport("ora_lib", lib_mod);
const formal_verification_demo = b.addExecutable(.{
.name = "formal_verification_demo",
.root_module = formal_verification_demo_mod,
});
const run_formal_verification_demo = b.addRunArtifact(formal_verification_demo);
run_formal_verification_demo.step.dependOn(b.getInstallStep());
const formal_verification_demo_step = b.step("formal-verification-demo", "Run the formal verification demo");
formal_verification_demo_step.dependOn(&run_formal_verification_demo.step);
// mlir-specific build steps
const mlir_debug_step = b.step("mlir-debug", "Build with MLIR debug features enabled");
mlir_debug_step.dependOn(b.getInstallStep());
const mlir_release_step = b.step("mlir-release", "Build with aggressive MLIR optimizations");
mlir_release_step.dependOn(b.getInstallStep());
// add step to test MLIR functionality
const test_mlir_step = b.step("test-mlir", "Run MLIR-specific tests");
test_mlir_step.dependOn(b.getInstallStep());
const fast_step = b.step("fast", "Fast build (use -Dskip-mlir=true)");
fast_step.dependOn(b.getInstallStep());
// test suite - Unit tests are co-located with source files
// tests are added to build.zig as they are created (e.g., src/lexer.test.zig)
const test_step = b.step("test", "Run all tests");
// lexer tests
const lexer_test_mod = b.createModule(.{
.root_source_file = b.path("src/lexer.test.zig"),
.target = target,
.optimize = optimize,
});
lexer_test_mod.addImport("ora_root", lib_mod);
const lexer_tests = b.addTest(.{ .root_module = lexer_test_mod });
test_step.dependOn(&b.addRunArtifact(lexer_tests).step);
// lexer error recovery tests
const error_recovery_test_mod = b.createModule(.{
.root_source_file = b.path("src/lexer/error_recovery.test.zig"),
.target = target,
.optimize = optimize,
});
error_recovery_test_mod.addImport("ora_root", lib_mod);
const error_recovery_tests = b.addTest(.{ .root_module = error_recovery_test_mod });
test_step.dependOn(&b.addRunArtifact(error_recovery_tests).step);
// semantics locals binder tests
const locals_binder_test_mod = b.createModule(.{
.root_source_file = b.path("src/semantics/locals_binder.test.zig"),
.target = target,
.optimize = optimize,
});
locals_binder_test_mod.addImport("ora_root", lib_mod);
const locals_binder_tests = b.addTest(.{ .root_module = locals_binder_test_mod });
test_step.dependOn(&b.addRunArtifact(locals_binder_tests).step);
// cli argument parsing tests
const cli_args_test_mod = b.createModule(.{
.root_source_file = b.path("src/cli/args.test.zig"),
.target = target,
.optimize = optimize,
});
const cli_args_tests = b.addTest(.{ .root_module = cli_args_test_mod });
test_step.dependOn(&b.addRunArtifact(cli_args_tests).step);
// project config tests
const project_config_test_mod = b.createModule(.{
.root_source_file = b.path("src/config/mod.test.zig"),
.target = target,
.optimize = optimize,
});
const project_config_tests = b.addTest(.{ .root_module = project_config_test_mod });
test_step.dependOn(&b.addRunArtifact(project_config_tests).step);
// ABI tests
const abi_test_mod = b.createModule(.{
.root_source_file = b.path("src/abi.test.zig"),
.target = target,
.optimize = optimize,
});
abi_test_mod.addImport("ora_root", lib_mod);
const abi_tests = b.addTest(.{ .root_module = abi_test_mod });
test_step.dependOn(&b.addRunArtifact(abi_tests).step);
// scanner tests - Numbers
const numbers_test_mod = b.createModule(.{
.root_source_file = b.path("src/lexer/scanners/numbers.test.zig"),
.target = target,
.optimize = optimize,
});
numbers_test_mod.addImport("ora_root", lib_mod);
const numbers_tests = b.addTest(.{ .root_module = numbers_test_mod });
test_step.dependOn(&b.addRunArtifact(numbers_tests).step);
// scanner tests - Strings
const strings_test_mod = b.createModule(.{
.root_source_file = b.path("src/lexer/scanners/strings.test.zig"),
.target = target,
.optimize = optimize,
});
strings_test_mod.addImport("ora_root", lib_mod);
const strings_tests = b.addTest(.{ .root_module = strings_test_mod });
test_step.dependOn(&b.addRunArtifact(strings_tests).step);
// scanner tests - Identifiers
const identifiers_test_mod = b.createModule(.{
.root_source_file = b.path("src/lexer/scanners/identifiers.test.zig"),
.target = target,
.optimize = optimize,
});
identifiers_test_mod.addImport("ora_root", lib_mod);
const identifiers_tests = b.addTest(.{ .root_module = identifiers_test_mod });
test_step.dependOn(&b.addRunArtifact(identifiers_tests).step);
// parser tests - Expression Parser
const expression_parser_test_mod = b.createModule(.{
.root_source_file = b.path("src/parser/expression_parser.test.zig"),
.target = target,
.optimize = optimize,
});
expression_parser_test_mod.addImport("ora_root", lib_mod);
const expression_parser_tests = b.addTest(.{ .root_module = expression_parser_test_mod });
test_step.dependOn(&b.addRunArtifact(expression_parser_tests).step);
// ast tests - AST Builder
const ast_builder_test_mod = b.createModule(.{
.root_source_file = b.path("src/ast/ast_builder.test.zig"),
.target = target,
.optimize = optimize,
});
ast_builder_test_mod.addImport("ora_root", lib_mod);
const ast_builder_tests = b.addTest(.{ .root_module = ast_builder_test_mod });
test_step.dependOn(&b.addRunArtifact(ast_builder_tests).step);
// parser tests - Statement Parser
const statement_parser_test_mod = b.createModule(.{
.root_source_file = b.path("src/parser/statement_parser.test.zig"),
.target = target,
.optimize = optimize,
});
statement_parser_test_mod.addImport("ora_root", lib_mod);
const statement_parser_tests = b.addTest(.{ .root_module = statement_parser_test_mod });
test_step.dependOn(&b.addRunArtifact(statement_parser_tests).step);
// parser tests - Parser Core
const parser_core_test_mod = b.createModule(.{
.root_source_file = b.path("src/parser/parser_core.test.zig"),
.target = target,
.optimize = optimize,
});
parser_core_test_mod.addImport("ora_root", lib_mod);
const parser_core_tests = b.addTest(.{ .root_module = parser_core_test_mod });
test_step.dependOn(&b.addRunArtifact(parser_core_tests).step);
// parser tests - Declaration Parser
const declaration_parser_test_mod = b.createModule(.{
.root_source_file = b.path("src/parser/declaration_parser.test.zig"),
.target = target,
.optimize = optimize,
});
declaration_parser_test_mod.addImport("ora_root", lib_mod);
const declaration_parser_tests = b.addTest(.{ .root_module = declaration_parser_test_mod });
test_step.dependOn(&b.addRunArtifact(declaration_parser_tests).step);
// parser tests - Type Parser
const type_parser_test_mod = b.createModule(.{
.root_source_file = b.path("src/parser/type_parser.test.zig"),
.target = target,
.optimize = optimize,
});
type_parser_test_mod.addImport("ora_root", lib_mod);
const type_parser_tests = b.addTest(.{ .root_module = type_parser_test_mod });
test_step.dependOn(&b.addRunArtifact(type_parser_tests).step);
// import resolver tests
const import_resolver_test_mod = b.createModule(.{
.root_source_file = b.path("src/imports/mod.test.zig"),
.target = target,
.optimize = optimize,
});
import_resolver_test_mod.addImport("ora_lib", lib_mod);
const import_resolver_tests = b.addTest(.{ .root_module = import_resolver_test_mod });
test_step.dependOn(&b.addRunArtifact(import_resolver_tests).step);
// import program loader tests
const import_program_loader_test_mod = b.createModule(.{
.root_source_file = b.path("src/imports/program_loader.test.zig"),
.target = target,
.optimize = optimize,
});
import_program_loader_test_mod.addImport("ora_lib", lib_mod);
const import_program_loader_tests = b.addTest(.{ .root_module = import_program_loader_test_mod });
test_step.dependOn(&b.addRunArtifact(import_program_loader_tests).step);
// z3 encoder tests (requires MLIR + Z3)
const z3_encoder_test_mod = b.createModule(.{
.root_source_file = b.path("src/z3/encoder.test.zig"),
.target = target,
.optimize = optimize,
});
z3_encoder_test_mod.addImport("mlir_c_api", mlir_c_mod);
const z3_encoder_tests = b.addTest(.{ .root_module = z3_encoder_test_mod });
linkMlirLibraries(b, z3_encoder_tests, mlir_step, ora_dialect_step, sir_dialect_step, target);
linkZ3Libraries(b, z3_encoder_tests, z3_step, target);
test_step.dependOn(&b.addRunArtifact(z3_encoder_tests).step);
// z3 verification tests (requires MLIR + Z3)
const z3_verification_test_mod = b.createModule(.{
.root_source_file = b.path("src/z3/verification.zig"),
.target = target,
.optimize = optimize,
});
z3_verification_test_mod.addImport("mlir_c_api", mlir_c_mod);
z3_verification_test_mod.addImport("ora_lib", lib_mod);
const z3_verification_tests = b.addTest(.{ .root_module = z3_verification_test_mod });
linkMlirLibraries(b, z3_verification_tests, mlir_step, ora_dialect_step, sir_dialect_step, target);
linkZ3Libraries(b, z3_verification_tests, z3_step, target);
test_step.dependOn(&b.addRunArtifact(z3_verification_tests).step);
// mlir type mapper tests
const mlir_types_test_mod = b.createModule(.{
.root_source_file = b.path("src/mlir/types.test.zig"),
.target = target,
.optimize = optimize,
});
mlir_types_test_mod.addImport("ora_lib", lib_mod);
mlir_types_test_mod.addImport("mlir_c_api", mlir_c_mod);
mlir_types_test_mod.addImport("log", log_mod);
const mlir_types_tests = b.addTest(.{ .root_module = mlir_types_test_mod });
linkMlirLibraries(b, mlir_types_tests, mlir_step, ora_dialect_step, sir_dialect_step, target);
test_step.dependOn(&b.addRunArtifact(mlir_types_tests).step);
test_mlir_step.dependOn(&b.addRunArtifact(mlir_types_tests).step);
// refinement guard tests (MLIR)
const refinement_guard_test_mod = b.createModule(.{
.root_source_file = b.path("src/mlir/refinements.test.zig"),
.target = target,
.optimize = optimize,
});
refinement_guard_test_mod.addImport("ora_lib", lib_mod);
refinement_guard_test_mod.addImport("mlir_c_api", mlir_c_mod);
refinement_guard_test_mod.addImport("log", log_mod);
const refinement_guard_tests = b.addTest(.{ .root_module = refinement_guard_test_mod });
linkMlirLibraries(b, refinement_guard_tests, mlir_step, ora_dialect_step, sir_dialect_step, target);
test_step.dependOn(&b.addRunArtifact(refinement_guard_tests).step);
test_mlir_step.dependOn(&b.addRunArtifact(refinement_guard_tests).step);
// MLIR effect metadata tests
const mlir_effects_test_mod = b.createModule(.{
.root_source_file = b.path("src/mlir/effects.test.zig"),
.target = target,
.optimize = optimize,
});
mlir_effects_test_mod.addImport("ora_lib", lib_mod);
mlir_effects_test_mod.addImport("mlir_c_api", mlir_c_mod);
mlir_effects_test_mod.addImport("log", log_mod);
const mlir_effects_tests = b.addTest(.{ .root_module = mlir_effects_test_mod });
linkMlirLibraries(b, mlir_effects_tests, mlir_step, ora_dialect_step, sir_dialect_step, target);
test_step.dependOn(&b.addRunArtifact(mlir_effects_tests).step);
test_mlir_step.dependOn(&b.addRunArtifact(mlir_effects_tests).step);
// ast tests - Expressions
const ast_expressions_test_mod = b.createModule(.{
.root_source_file = b.path("src/ast/expressions.test.zig"),
.target = target,
.optimize = optimize,
});
ast_expressions_test_mod.addImport("ora_root", lib_mod);
const ast_expressions_tests = b.addTest(.{ .root_module = ast_expressions_test_mod });
test_step.dependOn(&b.addRunArtifact(ast_expressions_tests).step);
// ast tests - Statements
const ast_statements_test_mod = b.createModule(.{
.root_source_file = b.path("src/ast/statements.test.zig"),
.target = target,
.optimize = optimize,
});
ast_statements_test_mod.addImport("ora_root", lib_mod);
const ast_statements_tests = b.addTest(.{ .root_module = ast_statements_test_mod });
test_step.dependOn(&b.addRunArtifact(ast_statements_tests).step);
// ast tests - Type Resolver (logs)
const type_resolver_logs_test_mod = b.createModule(.{
.root_source_file = b.path("src/ast/type_resolver_logs.test.zig"),
.target = target,
.optimize = optimize,
});
type_resolver_logs_test_mod.addImport("ora_root", lib_mod);
const type_resolver_logs_tests = b.addTest(.{ .root_module = type_resolver_logs_test_mod });
test_step.dependOn(&b.addRunArtifact(type_resolver_logs_tests).step);
// unit tests will be added here as they are created.
// example pattern:
// const lexer_test_mod = b.createModule(.{
// .root_source_file = b.path("src/lexer.test.zig"),
// .target = target,
// .optimize = optimize,
// });
// lexer_test_mod.addImport("ora_root", lib_mod);
// const lexer_tests = b.addTest(.{ .root_module = lexer_test_mod });
// test_step.dependOn(&b.addRunArtifact(lexer_tests).step);
}
/// Create a step that runs the installed lexer test suite with --verbose
fn createRunLexerVerboseStep(b: *std.Build) *std.Build.Step {
const step = b.allocator.create(std.Build.Step) catch @panic("OOM");
step.* = std.Build.Step.init(.{
.id = .custom,
.name = "lexer-suite-verbose",
.owner = b,
.makeFn = runLexerVerbose,
});
return step;
}
fn runLexerVerbose(step: *std.Build.Step, options: std.Build.Step.MakeOptions) anyerror!void {
_ = options;
const b = step.owner;
const allocator = b.allocator;
const res = std.process.Child.run(.{
.allocator = allocator,
.argv = &[_][]const u8{ "./zig-out/bin/lexer_test_suite", "--verbose" },
.cwd = ".",
}) catch |err| {
std.log.err("Failed to run lexer_test_suite: {}", .{err});
return err;
};
switch (res.term) {
.Exited => |code| {
if (code != 0) {
std.log.err("lexer_test_suite failed with exit code {}", .{code});
std.log.err("stderr: {s}", .{res.stderr});
return error.LexerSuiteFailed;
}
},
.Signal => |sig| {
std.log.err("lexer_test_suite terminated by signal {}", .{sig});
std.log.err("stderr: {s}", .{res.stderr});
return error.LexerSuiteFailed;
},
else => {},
}
}
/// Build MLIR from vendored llvm-project and install into vendor/mlir
fn buildMlirLibraries(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *std.Build.Step {
_ = target;
_ = optimize;
const step = b.allocator.create(std.Build.Step) catch @panic("OOM");
step.* = std.Build.Step.init(.{
.id = .custom,
.name = "cmake-build-mlir",
.owner = b,
.makeFn = buildMlirLibrariesImpl,
});
return step;
}
/// Implementation of CMake build for MLIR libraries
fn buildMlirLibrariesImpl(step: *std.Build.Step, options: std.Build.Step.MakeOptions) anyerror!void {
_ = options;
const b = step.owner;
const allocator = b.allocator;
// ensure submodule exists
const cwd = std.fs.cwd();
_ = cwd.openDir("vendor/llvm-project", .{ .iterate = false }) catch {
std.log.err("Missing submodule: vendor/llvm-project. Add it and pin a commit.", .{});
std.log.err("Example: git submodule add https://github.com/llvm/llvm-project.git vendor/llvm-project", .{});
return error.SubmoduleMissing;
};
// create build and install directories
const build_dir = "vendor/llvm-project/build-mlir";
cwd.makeDir(build_dir) catch |err| switch (err) {
error.PathAlreadyExists => {},
else => return err,
};
// clear CMake cache if it exists to avoid stale SDK paths after system updates
const cache_file = try std.fmt.allocPrint(allocator, "{s}/CMakeCache.txt", .{build_dir});
defer allocator.free(cache_file);
if (cwd.access(cache_file, .{}) catch null) |_| {
std.log.info("Clearing stale MLIR CMake cache after macOS/Xcode update", .{});
cwd.deleteFile(cache_file) catch |err| {
std.log.warn("Could not delete MLIR CMakeCache.txt: {}", .{err});
};
}
// also clear CMakeFiles directory which may contain cached package configs
const cmake_files_dir = try std.fmt.allocPrint(allocator, "{s}/CMakeFiles", .{build_dir});
defer allocator.free(cmake_files_dir);
if (cwd.access(cmake_files_dir, .{}) catch null) |_| {
std.log.info("Clearing MLIR CMakeFiles directory to remove stale package configs", .{});
cwd.deleteTree(cmake_files_dir) catch |err| {
std.log.warn("Could not delete MLIR CMakeFiles directory: {}", .{err});
};
}
const install_prefix = "vendor/mlir";
cwd.makeDir(install_prefix) catch |err| switch (err) {
error.PathAlreadyExists => {},
else => return err,
};
// platform-specific flags
const builtin = @import("builtin");
var cmake_args = std.array_list.Managed([]const u8).init(allocator);
defer cmake_args.deinit();
// prefer Ninja generator when available for faster, more parallel builds
var use_ninja: bool = false;
{
const probe = std.process.Child.run(.{ .allocator = allocator, .argv = &[_][]const u8{ "ninja", "--version" }, .cwd = "." }) catch null;
if (probe) |res| {
switch (res.term) {
.Exited => |code| {
if (code == 0) use_ninja = true;
},
else => {},
}
}
if (!use_ninja) {
const probe_alt = std.process.Child.run(.{ .allocator = allocator, .argv = &[_][]const u8{ "ninja-build", "--version" }, .cwd = "." }) catch null;
if (probe_alt) |res2| {
switch (res2.term) {
.Exited => |code| {
if (code == 0) use_ninja = true;
},
else => {},
}
}
}
}
try cmake_args.append("cmake");
if (use_ninja) {
try cmake_args.append("-G");
try cmake_args.append("Ninja");
}
try cmake_args.appendSlice(&[_][]const u8{
"-S",
"vendor/llvm-project/llvm",
"-B",
build_dir,
"-DCMAKE_BUILD_TYPE=Release",
"-DCMAKE_POSITION_INDEPENDENT_CODE=ON",
"-DLLVM_ENABLE_PROJECTS=mlir",
"-DLLVM_TARGETS_TO_BUILD=Native",
"-DLLVM_INCLUDE_TESTS=OFF",
"-DMLIR_INCLUDE_TESTS=OFF",
"-DLLVM_INCLUDE_BENCHMARKS=OFF",
"-DLLVM_INCLUDE_EXAMPLES=OFF",
"-DLLVM_INCLUDE_DOCS=OFF",
"-DMLIR_INCLUDE_DOCS=OFF",
"-DMLIR_ENABLE_BINDINGS_PYTHON=OFF",
"-DMLIR_ENABLE_EXECUTION_ENGINE=OFF",
"-DMLIR_ENABLE_CUDA=OFF",
"-DMLIR_ENABLE_ROCM=OFF",
"-DMLIR_ENABLE_SPIRV_CPU_RUNNER=OFF",
"-DLLVM_ENABLE_ZLIB=OFF",
"-DLLVM_ENABLE_TERMINFO=OFF",
"-DLLVM_ENABLE_RTTI=ON",
"-DLLVM_ENABLE_EH=ON",
"-DLLVM_ENABLE_PIC=ON",
"-DLLVM_BUILD_LLVM_DYLIB=OFF",
"-DLLVM_LINK_LLVM_DYLIB=OFF",
"-DLLVM_BUILD_TOOLS=ON", // needed for tblgen
"-DLLVM_TOOL_LTO_BUILD=OFF",
"-DLLVM_TOOL_LLVM_LTO_BUILD=OFF",
"-DLLVM_TOOL_LLVM_LTO2_BUILD=OFF",
"-DMLIR_BUILD_MLIR_C_DYLIB=ON",
b.fmt("-DCMAKE_INSTALL_PREFIX={s}", .{install_prefix}),
});
if (builtin.os.tag == .linux) {
try cmake_args.append("-DCMAKE_CXX_FLAGS=-stdlib=libc++");
try cmake_args.append("-DCMAKE_EXE_LINKER_FLAGS=-stdlib=libc++ -lc++abi");
try cmake_args.append("-DCMAKE_SHARED_LINKER_FLAGS=-stdlib=libc++ -lc++abi");
try cmake_args.append("-DCMAKE_MODULE_LINKER_FLAGS=-stdlib=libc++ -lc++abi");
try cmake_args.append("-DCMAKE_CXX_COMPILER=clang++");
try cmake_args.append("-DCMAKE_C_COMPILER=clang");
} else if (builtin.os.tag == .macos) {
try cmake_args.append("-DCMAKE_CXX_FLAGS=-stdlib=libc++");
// fix SDK path issue after macOS/Xcode update
// use xcrun to get the actual SDK path and set it explicitly
const sdk_path_result = std.process.Child.run(.{
.allocator = allocator,
.argv = &[_][]const u8{ "xcrun", "--show-sdk-path" },
.cwd = ".",
}) catch null;
if (sdk_path_result) |result| {
if (result.term.Exited == 0) {
const sdk_path = std.mem.trim(u8, result.stdout, " \n\r\t");
if (sdk_path.len > 0) {
const sysroot_flag = try std.fmt.allocPrint(allocator, "-DCMAKE_OSX_SYSROOT={s}", .{sdk_path});
defer allocator.free(sysroot_flag);
try cmake_args.append(sysroot_flag);
std.log.info("Setting MLIR CMAKE_OSX_SYSROOT={s}", .{sdk_path});
}
}
}
if (std.process.getEnvVarOwned(allocator, "ORA_CMAKE_OSX_ARCH") catch null) |arch| {
defer allocator.free(arch);
const flag = b.fmt("-DCMAKE_OSX_ARCHITECTURES={s}", .{arch});
try cmake_args.append(flag);
std.log.info("Using CMAKE_OSX_ARCHITECTURES={s}", .{arch});
}
} else if (builtin.os.tag == .windows) {
try cmake_args.append("-DCMAKE_CXX_FLAGS=/std:c++20");
}
var cfg_child = std.process.Child.init(cmake_args.items, allocator);
cfg_child.cwd = ".";
cfg_child.stdin_behavior = .Inherit;
cfg_child.stdout_behavior = .Inherit;
cfg_child.stderr_behavior = .Inherit;
const cfg_term = cfg_child.spawnAndWait() catch |err| {
std.log.err("Failed to configure MLIR CMake: {}", .{err});
return err;
};
switch (cfg_term) {
.Exited => |code| if (code != 0) {
std.log.err("MLIR CMake configure failed with exit code: {}", .{code});
return error.CMakeConfigureFailed;
},
else => {
std.log.err("MLIR CMake configure did not exit cleanly", .{});
return error.CMakeConfigureFailed;
},
}
// build and install MLIR (with sparse checkout and minimal flags above this is lightweight)
var build_args = [_][]const u8{ "cmake", "--build", build_dir, "--parallel", "--target", "install" };
var build_child = std.process.Child.init(&build_args, allocator);
build_child.cwd = ".";
build_child.stdin_behavior = .Inherit;
build_child.stdout_behavior = .Inherit;
build_child.stderr_behavior = .Inherit;
const build_term = build_child.spawnAndWait() catch |err| {
std.log.err("Failed to build MLIR with CMake: {}", .{err});
return err;
};
switch (build_term) {
.Exited => |code| if (code != 0) {
std.log.err("MLIR CMake build failed with exit code: {}", .{code});
return error.CMakeBuildFailed;
},
else => {
std.log.err("MLIR CMake build did not exit cleanly", .{});
return error.CMakeBuildFailed;
},
}
std.log.info("Successfully built MLIR libraries", .{});
}
/// Build Ora dialect library using CMake
fn buildOraDialectLibrary(b: *std.Build, mlir_step: *std.Build.Step, sir_dialect_step: *std.Build.Step, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *std.Build.Step {
_ = target;
_ = optimize;
const step = b.allocator.create(std.Build.Step) catch @panic("OOM");
step.* = std.Build.Step.init(.{
.id = .custom,
.name = "cmake-build-ora-dialect",
.owner = b,
.makeFn = buildOraDialectLibraryImpl,
});
step.dependOn(mlir_step);
step.dependOn(sir_dialect_step); // Ora dialect needs SIR headers
return step;
}
/// Implementation of CMake build for Ora dialect library
fn buildOraDialectLibraryImpl(step: *std.Build.Step, options: std.Build.Step.MakeOptions) anyerror!void {
_ = options;
const b = step.owner;
const allocator = b.allocator;
const cwd = std.fs.cwd();
// create build directory (clean if it exists to avoid CMake cache conflicts)
const build_dir = "vendor/ora-dialect-build";
// remove existing build directory to avoid CMake cache conflicts
cwd.deleteTree(build_dir) catch {};
cwd.makeDir(build_dir) catch |err| switch (err) {
error.PathAlreadyExists => {},
else => return err,
};
const install_prefix = "vendor/mlir";
const mlir_dir = b.fmt("{s}/lib/cmake/mlir", .{install_prefix});
// platform-specific flags
const builtin = @import("builtin");
var cmake_args = std.array_list.Managed([]const u8).init(allocator);
defer cmake_args.deinit();
// prefer Ninja generator when available
var use_ninja: bool = false;
{
const probe = std.process.Child.run(.{ .allocator = allocator, .argv = &[_][]const u8{ "ninja", "--version" }, .cwd = "." }) catch null;
if (probe) |res| {
switch (res.term) {
.Exited => |code| {
if (code == 0) use_ninja = true;
},
else => {},
}
}
}
try cmake_args.append("cmake");
if (use_ninja) {
try cmake_args.append("-G");
try cmake_args.append("Ninja");
}
try cmake_args.appendSlice(&[_][]const u8{
"-S",
"src/mlir/ora",
"-B",
build_dir,
"-DCMAKE_BUILD_TYPE=Release",
b.fmt("-DMLIR_DIR={s}", .{mlir_dir}),
b.fmt("-DCMAKE_INSTALL_PREFIX={s}", .{install_prefix}),
});
if (builtin.os.tag == .linux) {
try cmake_args.append("-DCMAKE_CXX_FLAGS=-stdlib=libc++");
try cmake_args.append("-DCMAKE_EXE_LINKER_FLAGS=-stdlib=libc++ -lc++abi");
try cmake_args.append("-DCMAKE_SHARED_LINKER_FLAGS=-stdlib=libc++ -lc++abi");
try cmake_args.append("-DCMAKE_MODULE_LINKER_FLAGS=-stdlib=libc++ -lc++abi");
try cmake_args.append("-DCMAKE_CXX_COMPILER=clang++");
try cmake_args.append("-DCMAKE_C_COMPILER=clang");
} else if (builtin.os.tag == .macos) {
try cmake_args.append("-DCMAKE_CXX_FLAGS=-stdlib=libc++");
}
var cfg_child = std.process.Child.init(cmake_args.items, allocator);
cfg_child.cwd = ".";
cfg_child.stdin_behavior = .Inherit;
cfg_child.stdout_behavior = .Inherit;
cfg_child.stderr_behavior = .Inherit;
const cfg_term = cfg_child.spawnAndWait() catch |err| {
std.log.err("Failed to configure Ora dialect CMake: {}", .{err});
return err;
};
switch (cfg_term) {
.Exited => |code| if (code != 0) {
std.log.err("Ora dialect CMake configure failed with exit code: {}", .{code});
return error.CMakeConfigureFailed;
},
else => {
std.log.err("Ora dialect CMake configure did not exit cleanly", .{});
return error.CMakeConfigureFailed;
},
}
// build and install
var build_args = [_][]const u8{ "cmake", "--build", build_dir, "--parallel", "--target", "install" };
var build_child = std.process.Child.init(&build_args, allocator);
build_child.cwd = ".";
build_child.stdin_behavior = .Inherit;
build_child.stdout_behavior = .Inherit;
build_child.stderr_behavior = .Inherit;
const build_term = build_child.spawnAndWait() catch |err| {
std.log.err("Failed to build Ora dialect with CMake: {}", .{err});
return err;
};
switch (build_term) {
.Exited => |code| if (code != 0) {
std.log.err("Ora dialect CMake build failed with exit code: {}", .{code});
return error.CMakeBuildFailed;
},
else => {
std.log.err("Ora dialect CMake build did not exit cleanly", .{});
return error.CMakeBuildFailed;
},
}
std.log.info("Successfully built Ora dialect library", .{});
}
/// Build SIR dialect library using CMake
fn buildSIRDialectLibrary(b: *std.Build, mlir_step: *std.Build.Step, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *std.Build.Step {
_ = target;
_ = optimize;
const step = b.allocator.create(std.Build.Step) catch @panic("OOM");
step.* = std.Build.Step.init(.{
.id = .custom,
.name = "cmake-build-sir-dialect",
.owner = b,
.makeFn = buildSIRDialectLibraryImpl,
});
step.dependOn(mlir_step);
return step;
}
/// Implementation of CMake build for SIR dialect library
fn buildSIRDialectLibraryImpl(step: *std.Build.Step, options: std.Build.Step.MakeOptions) anyerror!void {
_ = options;
const b = step.owner;
const allocator = b.allocator;
const cwd = std.fs.cwd();
// create build directory (clean if it exists to avoid CMake cache conflicts)
const build_dir = "vendor/sir-dialect-build";
// remove existing build directory to avoid CMake cache conflicts
cwd.deleteTree(build_dir) catch {};
cwd.makeDir(build_dir) catch |err| switch (err) {
error.PathAlreadyExists => {},
else => return err,
};
const install_prefix = "vendor/mlir";
const mlir_dir = b.fmt("{s}/lib/cmake/mlir", .{install_prefix});
// platform-specific flags
const builtin = @import("builtin");
var cmake_args = std.array_list.Managed([]const u8).init(allocator);
defer cmake_args.deinit();
// prefer Ninja generator when available
var use_ninja: bool = false;
{
const probe = std.process.Child.run(.{ .allocator = allocator, .argv = &[_][]const u8{ "ninja", "--version" }, .cwd = "." }) catch null;
if (probe) |res| {
switch (res.term) {
.Exited => |code| {
if (code == 0) use_ninja = true;
},
else => {},
}
}
}
try cmake_args.append("cmake");
if (use_ninja) {
try cmake_args.append("-G");
try cmake_args.append("Ninja");
}
try cmake_args.appendSlice(&[_][]const u8{
"-S",
"src/mlir/IR",
"-B",
build_dir,
"-DCMAKE_BUILD_TYPE=Release",
b.fmt("-DMLIR_DIR={s}", .{mlir_dir}),
b.fmt("-DCMAKE_INSTALL_PREFIX={s}", .{install_prefix}),
});
if (builtin.os.tag == .linux) {
try cmake_args.append("-DCMAKE_CXX_FLAGS=-stdlib=libc++");
try cmake_args.append("-DCMAKE_EXE_LINKER_FLAGS=-stdlib=libc++ -lc++abi");
try cmake_args.append("-DCMAKE_SHARED_LINKER_FLAGS=-stdlib=libc++ -lc++abi");
try cmake_args.append("-DCMAKE_MODULE_LINKER_FLAGS=-stdlib=libc++ -lc++abi");
try cmake_args.append("-DCMAKE_CXX_COMPILER=clang++");
try cmake_args.append("-DCMAKE_C_COMPILER=clang");
} else if (builtin.os.tag == .macos) {
try cmake_args.append("-DCMAKE_CXX_FLAGS=-stdlib=libc++");
}