-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
1291 lines (1124 loc) · 50.3 KB
/
build.zig
File metadata and controls
1291 lines (1124 loc) · 50.3 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
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;
// Build Solidity libraries using CMake
const cmake_step = buildSolidityLibraries(b, target, optimize);
// 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,
});
// 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);
// 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);
// Build and link Yul wrapper
const yul_wrapper = buildYulWrapper(b, target, optimize, cmake_step);
exe.addObject(yul_wrapper);
// Add include path for yul_wrapper.h
exe.addIncludePath(b.path("src"));
lib.addIncludePath(b.path("src"));
// Link Solidity libraries to the executable
linkSolidityLibraries(b, exe, cmake_step, target);
// Build and link MLIR (required)
const mlir_step = buildMlirLibraries(b, target, optimize);
linkMlirLibraries(b, exe, mlir_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);
// Create yul test executable
const yul_test_mod = b.createModule(.{
.root_source_file = b.path("examples/demos/yul_test.zig"),
.target = target,
.optimize = optimize,
});
yul_test_mod.addImport("ora_lib", lib_mod);
const yul_test = b.addExecutable(.{
.name = "yul_test",
.root_module = yul_test_mod,
});
// Link Yul wrapper and Solidity libraries
const yul_test_wrapper = buildYulWrapper(b, target, optimize, cmake_step);
yul_test.addObject(yul_test_wrapper);
// Add include path for yul_wrapper.h
yul_test.addIncludePath(b.path("src"));
linkSolidityLibraries(b, yul_test, cmake_step, target);
const run_yul_test = b.addRunArtifact(yul_test);
run_yul_test.step.dependOn(b.getInstallStep());
const yul_test_step = b.step("yul-test", "Run the Yul integration test");
yul_test_step.dependOn(&run_yul_test.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);
// Add comprehensive compiler testing framework
addCompilerTestFramework(b, lib_mod, target, optimize);
// Create MLIR demo executable (Ora -> AST -> MLIR IR file)
const mlir_demo_mod = b.createModule(.{
.root_source_file = b.path("examples/demos/mlir_demo.zig"),
.target = target,
.optimize = optimize,
});
mlir_demo_mod.addImport("ora_lib", lib_mod);
const mlir_demo = b.addExecutable(.{
.name = "mlir_demo",
.root_module = mlir_demo_mod,
});
// Reuse MLIR build step
linkMlirLibraries(b, mlir_demo, mlir_step, target);
b.installArtifact(mlir_demo);
const run_mlir_demo = b.addRunArtifact(mlir_demo);
run_mlir_demo.step.dependOn(b.getInstallStep());
const mlir_demo_step = b.step("mlir-demo", "Run the MLIR hello-world demo");
mlir_demo_step.dependOn(&run_mlir_demo.step);
// Add 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());
// Add new lexer testing framework
addLexerTestFramework(b, lib_mod, target, optimize);
// Add individual test suites - always included (files are present in repo)
const ast_tests = b.addTest(.{
.root_source_file = b.path("tests/ast_test.zig"),
.target = target,
.optimize = optimize,
});
ast_tests.root_module.addImport("ora", lib_mod);
const run_ast_tests = b.addRunArtifact(ast_tests);
const ast_clean_tests = b.addTest(.{
.root_source_file = b.path("tests/ast_test_clean.zig"),
.target = target,
.optimize = optimize,
});
ast_clean_tests.root_module.addImport("ora", lib_mod);
const run_ast_clean_tests = b.addRunArtifact(ast_clean_tests);
// Add AST visitor tests
const ast_visitor_tests = b.addTest(.{
.root_source_file = b.path("tests/ast_visitor_test.zig"),
.target = target,
.optimize = optimize,
});
ast_visitor_tests.root_module.addImport("ora", lib_mod);
const run_ast_visitor_tests = b.addRunArtifact(ast_visitor_tests);
// Legacy lexer test (keep for compatibility)
const simple_lexer_test = b.addTest(.{
.root_source_file = b.path("tests/lexer_test.zig"),
.target = target,
.optimize = optimize,
});
simple_lexer_test.root_module.addImport("ora", lib_mod);
const run_simple_lexer_test = b.addRunArtifact(simple_lexer_test);
const test_lexer_legacy_step = b.step("test-lexer-legacy", "Run legacy lexer tests");
test_lexer_legacy_step.dependOn(&run_simple_lexer_test.step);
// Add expression parser tests
const expression_parser_tests = b.addTest(.{
.root_source_file = b.path("tests/expression_parser_test.zig"),
.target = target,
.optimize = optimize,
});
expression_parser_tests.root_module.addImport("ora", lib_mod);
const run_expression_parser_tests = b.addRunArtifact(expression_parser_tests);
const test_expression_parser_step = b.step("test-expression-parser", "Run expression parser tests");
test_expression_parser_step.dependOn(&run_expression_parser_tests.step);
// Create AST test step
const test_ast_step = b.step("test-ast", "Run AST tests");
test_ast_step.dependOn(&run_ast_tests.step);
test_ast_step.dependOn(&run_ast_clean_tests.step);
test_ast_step.dependOn(&run_ast_visitor_tests.step);
// Create comprehensive test step that runs all tests
const test_all_step = b.step("test", "Run all tests");
test_all_step.dependOn(b.getInstallStep()); // Ensure everything is built first
// Add all test categories
const test_framework_internal = b.addTest(.{
.root_source_file = b.path("tests/test_framework.zig"),
.target = target,
.optimize = optimize,
});
test_framework_internal.root_module.addImport("ora", lib_mod);
test_all_step.dependOn(&b.addRunArtifact(test_framework_internal).step);
test_all_step.dependOn(test_ast_step);
test_all_step.dependOn(&run_expression_parser_tests.step);
// Optionally: expose lexer-suite-verbose as a separate step (not part of default test)
// Run invalid parser fixtures
const invalid_parser_tests = b.addTest(.{
.root_source_file = b.path("tests/parser/parser_invalid_fixture_suite.zig"),
.target = target,
.optimize = optimize,
});
invalid_parser_tests.root_module.addImport("ora", lib_mod);
test_all_step.dependOn(&b.addRunArtifact(invalid_parser_tests).step);
// Lossless round-trip tests
const lossless_tests = b.addTest(.{
.root_source_file = b.path("tests/lossless_roundtrip_test.zig"),
.target = target,
.optimize = optimize,
});
lossless_tests.root_module.addImport("ora", lib_mod);
test_all_step.dependOn(&b.addRunArtifact(lossless_tests).step);
// Doc comment extraction tests
const doc_tests = b.addTest(.{
.root_source_file = b.path("tests/doc_comment_extraction_test.zig"),
.target = target,
.optimize = optimize,
});
doc_tests.root_module.addImport("ora", lib_mod);
test_all_step.dependOn(&b.addRunArtifact(doc_tests).step);
// CST token stream tests
const cst_tests = b.addTest(.{
.root_source_file = b.path("tests/cst_token_stream_test.zig"),
.target = target,
.optimize = optimize,
});
cst_tests.root_module.addImport("ora", lib_mod);
test_all_step.dependOn(&b.addRunArtifact(cst_tests).step);
// CST parser integration test
const cst_parser_tests = b.addTest(.{
.root_source_file = b.path("tests/cst_parser_top_level_test.zig"),
.target = target,
.optimize = optimize,
});
cst_parser_tests.root_module.addImport("ora", lib_mod);
test_all_step.dependOn(&b.addRunArtifact(cst_parser_tests).step);
// Semantics fixtures suite
const semantics_fixture_tests = b.addTest(.{
.root_source_file = b.path("tests/semantics/semantics_fixture_suite.zig"),
.target = target,
.optimize = optimize,
});
semantics_fixture_tests.root_module.addImport("ora", lib_mod);
test_all_step.dependOn(&b.addRunArtifact(semantics_fixture_tests).step);
// TypeInfo render and equality tests
const type_info_tests = b.addTest(.{
.root_source_file = b.path("tests/type_info_render_and_eq_test.zig"),
.target = target,
.optimize = optimize,
});
type_info_tests.root_module.addImport("ora", lib_mod);
test_all_step.dependOn(&b.addRunArtifact(type_info_tests).step);
// Byte offset span tests
const span_tests = b.addTest(.{
.root_source_file = b.path("tests/byte_offset_span_test.zig"),
.target = target,
.optimize = optimize,
});
span_tests.root_module.addImport("ora", lib_mod);
test_all_step.dependOn(&b.addRunArtifact(span_tests).step);
// Quantified expression tests
const quantified_tests = b.addTest(.{
.root_source_file = b.path("tests/test_quantified.zig"),
.target = target,
.optimize = optimize,
});
quantified_tests.root_module.addImport("ora", lib_mod);
test_all_step.dependOn(&b.addRunArtifact(quantified_tests).step);
// Verification attributes tests
const verification_tests = b.addTest(.{
.root_source_file = b.path("tests/test_verification_attributes.zig"),
.target = target,
.optimize = optimize,
});
verification_tests.root_module.addImport("ora", lib_mod);
test_all_step.dependOn(&b.addRunArtifact(verification_tests).step);
// Function contract verification tests
const function_contract_tests = b.addTest(.{
.root_source_file = b.path("tests/test_function_contracts.zig"),
.target = target,
.optimize = optimize,
});
function_contract_tests.root_module.addImport("ora", lib_mod);
test_all_step.dependOn(&b.addRunArtifact(function_contract_tests).step);
// Documentation generation
const install_docs = b.addInstallDirectory(.{
.source_dir = lib.getEmittedDocs(),
.install_dir = .prefix,
.install_subdir = "docs",
});
const docs_step = b.step("docs", "Generate and install documentation");
docs_step.dependOn(&install_docs.step);
// Add formal verification test
const formal_test_mod = b.createModule(.{
.root_source_file = b.path("examples/demos/formal_test.zig"),
.target = target,
.optimize = optimize,
});
formal_test_mod.addImport("ora_lib", lib_mod);
const formal_test = b.addExecutable(.{
.name = "formal_test",
.root_module = formal_test_mod,
});
// Don't install by default - only run when requested
const formal_test_step = b.step("formal-test", "Run formal verification test");
const formal_test_run = b.addRunArtifact(formal_test);
formal_test_step.dependOn(&formal_test_run.step);
// Add example testing step
const test_examples_step = b.step("test-examples", "Test all example .ora files");
const test_examples_run = createExampleTestStep(b, exe);
test_examples_step.dependOn(test_examples_run);
}
/// 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 Solidity libraries using CMake
fn buildSolidityLibraries(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *std.Build.Step {
_ = target;
_ = optimize;
const cmake_step = b.allocator.create(std.Build.Step) catch @panic("OOM");
cmake_step.* = std.Build.Step.init(.{
.id = .custom,
.name = "cmake-build-solidity",
.owner = b,
.makeFn = buildSolidityLibrariesImpl,
});
return cmake_step;
}
/// Implementation of CMake build for Solidity libraries
fn buildSolidityLibrariesImpl(step: *std.Build.Step, options: std.Build.Step.MakeOptions) anyerror!void {
_ = options;
const b = step.owner;
const allocator = b.allocator;
// Detect target platform at runtime for CMake configuration
const builtin = @import("builtin");
// Create build directory
const build_dir = "vendor/solidity/build";
std.fs.cwd().makeDir(build_dir) catch |err| switch (err) {
error.PathAlreadyExists => {},
else => return err,
};
// Determine platform-specific CMake flags for C++ ABI compatibility
var cmake_args = std.ArrayList([]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/solidity",
"-B",
build_dir,
"-DCMAKE_BUILD_TYPE=Release",
"-DONLY_BUILD_SOLIDITY_LIBRARIES=ON",
"-DTESTS=OFF",
});
// Add platform-specific flags to ensure consistent C++ ABI
if (builtin.os.tag == .linux) {
std.log.info("Adding Boost paths for Linux", .{});
// Force libc++ usage on Linux to match macOS ABI
try cmake_args.append("-DCMAKE_CXX_FLAGS=-stdlib=libc++ -lc++abi");
// Ensure the linker uses libc++ and c++abi as well
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) {
std.log.info("Adding Boost paths for Apple Silicon Mac", .{});
// macOS already uses libc++ by default, but be explicit
try cmake_args.append("-DCMAKE_CXX_FLAGS=-stdlib=libc++");
// Allow forcing CMake arch when cross-compiling on macOS via env var
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) {
std.log.info("Adding Boost paths for Windows", .{});
// Windows: Use MSVC and configure Boost paths
try cmake_args.append("-DCMAKE_CXX_FLAGS=/std:c++20");
// Try multiple possible Boost locations for Windows
try cmake_args.append("-DCMAKE_PREFIX_PATH=C:/vcpkg/installed/x64-windows;C:/ProgramData/chocolatey/lib/boost-msvc-14.3/lib/native;C:/tools/boost");
// Set Boost-specific variables for older CMake compatibility
try cmake_args.append("-DBoost_USE_STATIC_LIBS=ON");
try cmake_args.append("-DBoost_USE_MULTITHREADED=ON");
try cmake_args.append("-DBoost_USE_STATIC_RUNTIME=OFF");
// Suppress CMake developer warnings
try cmake_args.append("-Wno-dev");
}
// Configure CMake
const cmake_configure = std.process.Child.run(.{
.allocator = allocator,
.argv = cmake_args.items,
.cwd = ".",
}) catch |err| {
std.log.err("Failed to configure CMake: {}", .{err});
return err;
};
if (cmake_configure.term.Exited != 0) {
std.log.err("CMake configure failed with exit code: {}", .{cmake_configure.term.Exited});
std.log.err("CMake stderr: {s}", .{cmake_configure.stderr});
return error.CMakeConfigureFailed;
}
// Build libraries
const cmake_build = std.process.Child.run(.{
.allocator = allocator,
.argv = &[_][]const u8{
"cmake",
"--build",
build_dir,
"--parallel",
"--target",
"solutil",
"--target",
"langutil",
"--target",
"smtutil",
"--target",
"evmasm",
"--target",
"yul",
},
}) catch |err| {
std.log.err("Failed to build with CMake: {}", .{err});
return err;
};
if (cmake_build.term.Exited != 0) {
std.log.err("CMake build failed with exit code: {}", .{cmake_build.term.Exited});
std.log.err("CMake stderr: {s}", .{cmake_build.stderr});
return error.CMakeBuildFailed;
}
std.log.info("Successfully built Solidity libraries", .{});
}
/// Build the Yul wrapper C++ library
fn buildYulWrapper(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, cmake_step: *std.Build.Step) *std.Build.Step.Compile {
const yul_wrapper_mod = b.createModule(.{
.target = target,
.optimize = optimize,
});
const yul_wrapper = b.addObject(.{
.name = "yul_wrapper",
.root_module = yul_wrapper_mod,
});
// Depend on CMake build
yul_wrapper.step.dependOn(cmake_step);
// Add the C++ source file
// Configure C++ flags and ensure libc++ on Linux to match CMake configuration
var cpp_flags = std.ArrayList([]const u8).init(b.allocator);
defer cpp_flags.deinit();
cpp_flags.appendSlice(&[_][]const u8{
"-std=c++20",
"-fPIC",
"-Wno-deprecated",
}) catch @panic("OOM");
if (target.result.os.tag == .linux) {
// Use libc++ headers on Linux to align with CMake's -stdlib=libc++
cpp_flags.append("-stdlib=libc++") catch @panic("OOM");
}
yul_wrapper.addCSourceFile(.{
.file = b.path("src/yul_wrapper.cpp"),
.flags = cpp_flags.items,
});
// Add include directories
yul_wrapper.addIncludePath(b.path("src"));
yul_wrapper.addIncludePath(b.path("vendor/solidity"));
yul_wrapper.addIncludePath(b.path("vendor/solidity/libsolutil"));
yul_wrapper.addIncludePath(b.path("vendor/solidity/liblangutil"));
yul_wrapper.addIncludePath(b.path("vendor/solidity/libsmtutil"));
yul_wrapper.addIncludePath(b.path("vendor/solidity/libevmasm"));
yul_wrapper.addIncludePath(b.path("vendor/solidity/libyul"));
yul_wrapper.addIncludePath(b.path("vendor/solidity/deps/fmtlib/include"));
yul_wrapper.addIncludePath(b.path("vendor/solidity/deps/range-v3/include"));
yul_wrapper.addIncludePath(b.path("vendor/solidity/deps/nlohmann-json/include"));
// Add platform-specific Boost paths
addBoostPaths(b, yul_wrapper, target);
// Link C++ standard library
// Use the appropriate C++ stdlib based on the target
switch (target.result.os.tag) {
.linux => {
// On Linux, use libc++ and c++abi to match CMake build
yul_wrapper.linkLibCpp();
yul_wrapper.linkSystemLibrary("c++abi");
},
.macos => {
// On macOS, use libc++
yul_wrapper.linkLibCpp();
},
else => {
// Default to libc++
yul_wrapper.linkLibCpp();
},
}
return yul_wrapper;
}
/// Link Solidity libraries to the executable
fn linkSolidityLibraries(b: *std.Build, exe: *std.Build.Step.Compile, cmake_step: *std.Build.Step, target: std.Build.ResolvedTarget) void {
// Make executable depend on CMake build
exe.step.dependOn(cmake_step);
// Add library paths
exe.addLibraryPath(b.path("vendor/solidity/build/libsolutil"));
exe.addLibraryPath(b.path("vendor/solidity/build/liblangutil"));
exe.addLibraryPath(b.path("vendor/solidity/build/libsmtutil"));
exe.addLibraryPath(b.path("vendor/solidity/build/libevmasm"));
exe.addLibraryPath(b.path("vendor/solidity/build/libyul"));
// Link libraries (order matters - dependencies first)
exe.linkSystemLibrary("solutil");
exe.linkSystemLibrary("langutil");
exe.linkSystemLibrary("smtutil");
exe.linkSystemLibrary("evmasm");
exe.linkSystemLibrary("yul");
// Link C++ standard library
// Use the appropriate C++ stdlib based on the target
switch (target.result.os.tag) {
.linux => {
// On Linux, use libc++ and c++abi to match CMake build
exe.linkLibCpp();
exe.linkSystemLibrary("c++abi");
},
.macos => {
// On macOS, use libc++
exe.linkLibCpp();
},
else => {
// Default to libc++
exe.linkLibCpp();
},
}
// Add include directories for headers
exe.addIncludePath(b.path("vendor/solidity"));
exe.addIncludePath(b.path("vendor/solidity/libsolutil"));
exe.addIncludePath(b.path("vendor/solidity/liblangutil"));
exe.addIncludePath(b.path("vendor/solidity/libsmtutil"));
exe.addIncludePath(b.path("vendor/solidity/libevmasm"));
exe.addIncludePath(b.path("vendor/solidity/libyul"));
}
/// 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,
};
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.ArrayList([]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",
"-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_BUILD_LLVM_DYLIB=OFF",
"-DLLVM_LINK_LLVM_DYLIB=OFF",
"-DLLVM_BUILD_TOOLS=ON", // needed for tblgen
"-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++ -lc++abi");
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++");
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", .{});
}
/// Link MLIR to the given executable using the installed prefix
fn linkMlirLibraries(b: *std.Build, exe: *std.Build.Step.Compile, mlir_step: *std.Build.Step, target: std.Build.ResolvedTarget) void {
// Depend on MLIR build
exe.step.dependOn(mlir_step);
const include_path = b.path("vendor/mlir/include");
const lib_path = b.path("vendor/mlir/lib");
exe.addIncludePath(include_path);
exe.addLibraryPath(lib_path);
exe.linkSystemLibrary("MLIR-C");
switch (target.result.os.tag) {
.linux => {
exe.linkLibCpp();
exe.linkSystemLibrary("c++abi");
exe.addRPath(lib_path);
},
.macos => {
exe.linkLibCpp();
exe.addRPath(lib_path);
},
else => {
exe.linkLibCpp();
},
}
}
/// Create example testing step that runs the compiler on all .ora files
fn createExampleTestStep(b: *std.Build, exe: *std.Build.Step.Compile) *std.Build.Step {
const test_step = b.allocator.create(std.Build.Step) catch @panic("OOM");
test_step.* = std.Build.Step.init(.{
.id = .custom,
.name = "test-examples",
.owner = b,
.makeFn = runExampleTests,
});
// Depend on the main executable being built
test_step.dependOn(&exe.step);
return test_step;
}
/// Implementation of example testing
fn runExampleTests(step: *std.Build.Step, options: std.Build.Step.MakeOptions) anyerror!void {
_ = options;
const b = step.owner;
const allocator = b.allocator;
std.log.info("Testing all .ora example files...", .{});
// Get examples directory
var examples_dir = std.fs.cwd().openDir("ora-example", .{ .iterate = true }) catch |err| {
std.log.err("Failed to open examples directory: {}", .{err});
return err;
};
defer examples_dir.close();
// Iterate through all .ora files
var walker = examples_dir.walk(allocator) catch |err| {
std.log.err("Failed to walk examples directory: {}", .{err});