-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
1326 lines (1240 loc) · 51.9 KB
/
Copy pathbuild.zig
File metadata and controls
1326 lines (1240 loc) · 51.9 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");
const EvmzBuildConfig = struct {
profile: []const u8,
native_keccak: []const u8,
native_secp256k1: []const u8,
};
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const profile = buildProfileOption(b);
const is_native_profile = std.mem.eql(u8, profile, "native");
const requested_native_keccak = nativeKeccakOption(b);
const native_keccak = resolveNativeKeccak(profile, target, requested_native_keccak);
const requested_native_secp256k1 = nativeSecp256k1Option(b);
const native_secp256k1 = resolveNativeSecp256k1(profile, target, requested_native_secp256k1);
const evmz_build = EvmzBuildConfig{
.profile = profile,
.native_keccak = native_keccak,
.native_secp256k1 = native_secp256k1,
};
const use_xkcp = std.mem.eql(u8, native_keccak, "xkcp");
const xkcp_dep = if (use_xkcp) b.lazyDependency("xkcp", .{}) else null;
if (use_xkcp and xkcp_dep == null) return;
const xkcp_object = if (xkcp_dep) |dep|
buildXkcpObject(b, target, optimize, dep, "xkcp", null)
else
null;
const xkcp_pic_object = if (xkcp_dep) |dep|
buildXkcpObject(b, target, optimize, dep, "xkcp-pic", true)
else
null;
if (xkcp_dep) |dep| {
const install_license = b.addInstallFile(dep.path("LICENSE"), "share/licenses/evmz/XKCP.txt");
b.getInstallStep().dependOn(&install_license.step);
}
const use_libsecp256k1 = std.mem.eql(u8, native_secp256k1, "libsecp256k1");
const libsecp256k1_dep = if (use_libsecp256k1)
b.lazyDependency("libsecp256k1", .{})
else
null;
if (use_libsecp256k1 and libsecp256k1_dep == null) return;
const libsecp256k1_object = if (libsecp256k1_dep) |dep|
buildLibsecp256k1Object(b, target, optimize, dep, "libsecp256k1", null)
else
null;
const libsecp256k1_pic_object = if (libsecp256k1_dep) |dep|
buildLibsecp256k1Object(b, target, optimize, dep, "libsecp256k1-pic", true)
else
null;
if (libsecp256k1_dep) |dep| {
const install_license = b.addInstallFile(dep.path("COPYING"), "share/licenses/evmz/libsecp256k1.txt");
b.getInstallStep().dependOn(&install_license.step);
}
const build_options = buildOptions(b, profile, native_keccak, native_secp256k1);
const bench_optimize = b.option(
std.builtin.OptimizeMode,
"bench-optimize",
"Optimization mode forwarded to benchmark runners",
) orelse .ReleaseFast;
const bench_support_min = b.option(
[]const u8,
"bench-support-min",
"Minimum Ethereum revision compiled into the VM-loop benchmark",
);
const bench_support_max = b.option(
[]const u8,
"bench-support-max",
"Maximum Ethereum revision compiled into the VM-loop benchmark",
);
const bench_micro_filter = b.option(
[]const u8,
"micro-filter",
"Only run benchmark micro tests whose names contain this filter",
);
const evmone_dep = b.dependency("evmone", .{ .target = target, .optimize = optimize });
const native_precompile_deps = if (is_native_profile)
nativePrecompileDeps(b, target, optimize)
else
null;
// Frame pointers cost ~3 instructions per tail-dispatch handler; bench
// builds already omit them, so keep shipped release artifacts identical.
const omit_frame_pointer = optimize != .Debug;
const evmz_mod = b.addModule("evmz", .{
.root_source_file = b.path("src/evm.zig"),
.target = target,
.optimize = optimize,
.omit_frame_pointer = omit_frame_pointer,
});
evmz_mod.addOptions("build_options", build_options);
evmz_mod.addIncludePath(b.path("include"));
evmz_mod.addIncludePath(evmone_dep.path("evmc/include"));
if (native_precompile_deps) |deps| {
addPrecompileNative(b, evmz_mod, deps, evmone_dep);
}
addNativeKeccak(evmz_mod, xkcp_object);
addNativeSecp256k1(evmz_mod, libsecp256k1_object);
const ssz_mod = b.addModule("ssz", .{
.root_source_file = b.path("pkg/ssz/src/lib.zig"),
.target = target,
.optimize = optimize,
});
const rlp_mod = b.addModule("rlp", .{
.root_source_file = b.path("pkg/rlp/src/lib.zig"),
.target = target,
.optimize = optimize,
});
evmz_mod.addImport("ssz", ssz_mod);
evmz_mod.addImport("rlp", rlp_mod);
const mpt_mod = b.addModule("mpt", .{
.root_source_file = b.path("pkg/mpt/src/lib.zig"),
.target = target,
.optimize = optimize,
});
mpt_mod.addImport("rlp", rlp_mod);
evmz_mod.addImport("mpt", mpt_mod);
const static_c_lib = if (is_native_profile) static_c_lib: {
const c_lib_mod = b.createModule(.{
.root_source_file = b.path("src/evmc.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
.link_libcpp = true,
.omit_frame_pointer = omit_frame_pointer,
});
c_lib_mod.addOptions("build_options", build_options);
c_lib_mod.addImport("ssz", ssz_mod);
c_lib_mod.addImport("rlp", rlp_mod);
c_lib_mod.addImport("mpt", mpt_mod);
c_lib_mod.addIncludePath(b.path("include"));
c_lib_mod.addIncludePath(evmone_dep.path("evmc/include"));
addPrecompileNative(b, c_lib_mod, native_precompile_deps.?, evmone_dep);
addNativeKeccak(c_lib_mod, xkcp_pic_object);
addNativeSecp256k1(c_lib_mod, libsecp256k1_pic_object);
const static_c_lib = b.addLibrary(.{
.name = "evmz",
.root_module = c_lib_mod,
.linkage = .static,
});
b.installArtifact(static_c_lib);
b.default_step.dependOn(&static_c_lib.step);
const shared_c_lib = b.addLibrary(.{
.name = "evmz",
.root_module = c_lib_mod,
.linkage = .dynamic,
});
b.installArtifact(shared_c_lib);
b.default_step.dependOn(&shared_c_lib.step);
// C headers.
const evmz_compat_header = b.addInstallHeaderFile(b.path("include/evmz.h"), "evmz.h");
const evmz_evmc_header = b.addInstallHeaderFile(b.path("include/evmz/evmc.h"), "evmz/evmc.h");
const evmz_native_header = b.addInstallHeaderFile(b.path("include/evmz/evmz.h"), "evmz/evmz.h");
const evmc_header = b.addInstallHeaderFile(evmone_dep.path("evmc/include/evmc/evmc.h"), "evmc/evmc.h");
b.getInstallStep().dependOn(&evmz_compat_header.step);
b.getInstallStep().dependOn(&evmz_evmc_header.step);
b.getInstallStep().dependOn(&evmz_native_header.step);
b.getInstallStep().dependOn(&evmc_header.step);
break :static_c_lib static_c_lib;
} else null;
// test
{
const lib_unit_tests_mod = b.createModule(.{
.root_source_file = b.path("src/evm.zig"),
.target = target,
.optimize = optimize,
.link_libcpp = is_native_profile,
});
lib_unit_tests_mod.addOptions("build_options", build_options);
lib_unit_tests_mod.addImport("ssz", ssz_mod);
lib_unit_tests_mod.addImport("rlp", rlp_mod);
lib_unit_tests_mod.addImport("mpt", mpt_mod);
lib_unit_tests_mod.addIncludePath(b.path("include"));
lib_unit_tests_mod.addIncludePath(evmone_dep.path("evmc/include"));
if (native_precompile_deps) |deps| {
addPrecompileNative(b, lib_unit_tests_mod, deps, evmone_dep);
}
addNativeKeccak(lib_unit_tests_mod, xkcp_object);
addNativeSecp256k1(lib_unit_tests_mod, libsecp256k1_object);
const lib_unit_tests = b.addTest(.{
.root_module = lib_unit_tests_mod,
.filters = b.args orelse &.{},
});
// Zig 0.16's self-hosted x86_64 backend cannot lower `.always_tail`.
// Keep the test build in Debug while using LLVM for tail dispatch.
lib_unit_tests.use_llvm = true;
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
const ssz_unit_tests = b.addTest(.{
.root_module = b.createModule(.{
.root_source_file = b.path("pkg/ssz/src/test.zig"),
.target = target,
.optimize = optimize,
}),
.filters = b.args orelse &.{},
});
const rlp_unit_tests = b.addTest(.{
.root_module = b.createModule(.{
.root_source_file = b.path("pkg/rlp/src/test.zig"),
.target = target,
.optimize = optimize,
}),
.filters = b.args orelse &.{},
});
const mpt_unit_tests_mod = b.createModule(.{
.root_source_file = b.path("pkg/mpt/test.zig"),
.target = target,
.optimize = optimize,
});
mpt_unit_tests_mod.addImport("mpt", mpt_mod);
const mpt_unit_tests = b.addTest(.{
.root_module = mpt_unit_tests_mod,
.filters = b.args orelse &.{},
});
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_lib_unit_tests.step);
test_step.dependOn(&b.addRunArtifact(ssz_unit_tests).step);
test_step.dependOn(&b.addRunArtifact(rlp_unit_tests).step);
test_step.dependOn(&b.addRunArtifact(mpt_unit_tests).step);
if (is_native_profile) {
const c_api_tests_mod = b.createModule(.{
.root_source_file = b.path("src/evmc.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
.link_libcpp = true,
});
c_api_tests_mod.addOptions("build_options", build_options);
c_api_tests_mod.addImport("ssz", ssz_mod);
c_api_tests_mod.addImport("rlp", rlp_mod);
c_api_tests_mod.addImport("mpt", mpt_mod);
c_api_tests_mod.addIncludePath(b.path("include"));
c_api_tests_mod.addIncludePath(evmone_dep.path("evmc/include"));
addPrecompileNative(b, c_api_tests_mod, native_precompile_deps.?, evmone_dep);
addNativeKeccak(c_api_tests_mod, xkcp_object);
addNativeSecp256k1(c_api_tests_mod, libsecp256k1_object);
const c_api_tests = b.addTest(.{
.root_module = c_api_tests_mod,
.filters = b.args orelse &.{},
});
c_api_tests.use_llvm = true;
test_step.dependOn(&b.addRunArtifact(c_api_tests).step);
}
}
{
// Keep builtin-fuzzer targets pure Zig: Zig 0.16's fuzzer currently trips
// on native precompile coverage, and default-runner fuzz needs no traces.
const uint256_fuzz_mod = b.createModule(.{
.root_source_file = b.path("src/uint256.zig"),
.target = target,
.optimize = optimize,
.error_tracing = false,
});
const uint256_fuzz_tests = b.addTest(.{
.name = "uint256-fuzz",
.root_module = uint256_fuzz_mod,
});
uint256_fuzz_tests.use_llvm = true;
const run_uint256_fuzz_tests = b.addRunArtifact(uint256_fuzz_tests);
const modexp_fuzz_mod = b.createModule(.{
.root_source_file = b.path("src/precompile/modexp.zig"),
.target = target,
.optimize = optimize,
.error_tracing = false,
});
const modexp_fuzz_tests = b.addTest(.{
.name = "modexp-fuzz",
.root_module = modexp_fuzz_mod,
});
modexp_fuzz_tests.use_llvm = true;
const run_modexp_fuzz_tests = b.addRunArtifact(modexp_fuzz_tests);
const rlp_fuzz_mod = b.createModule(.{
.root_source_file = b.path("pkg/rlp/src/fuzz.zig"),
.target = target,
.optimize = optimize,
.error_tracing = false,
});
const rlp_fuzz_tests = b.addTest(.{
.name = "rlp-fuzz",
.root_module = rlp_fuzz_mod,
});
const run_rlp_fuzz_tests = b.addRunArtifact(rlp_fuzz_tests);
const mpt_fuzz_mod = b.createModule(.{
.root_source_file = b.path("pkg/mpt/src/fuzz.zig"),
.target = target,
.optimize = optimize,
.error_tracing = false,
});
mpt_fuzz_mod.addImport("mpt", mpt_mod);
const mpt_fuzz_tests = b.addTest(.{
.name = "mpt-fuzz",
.root_module = mpt_fuzz_mod,
});
const run_mpt_fuzz_tests = b.addRunArtifact(mpt_fuzz_tests);
const fuzz_step = b.step("fuzz", "Run fuzzable pure-Zig unit tests");
fuzz_step.dependOn(&run_uint256_fuzz_tests.step);
fuzz_step.dependOn(&run_modexp_fuzz_tests.step);
fuzz_step.dependOn(&run_rlp_fuzz_tests.step);
fuzz_step.dependOn(&run_mpt_fuzz_tests.step);
const uint256_fuzz_step = b.step("fuzz-uint256", "Run uint256 fuzz tests");
uint256_fuzz_step.dependOn(&run_uint256_fuzz_tests.step);
const modexp_fuzz_step = b.step("fuzz-modexp", "Run modexp fuzz tests");
modexp_fuzz_step.dependOn(&run_modexp_fuzz_tests.step);
}
const optimize_name = @tagName(optimize);
const bench_optimize_name = @tagName(bench_optimize);
if (pathExists(b, "eest/build.zig")) {
addEestDelegate(b, "eest-test", "Run sidecar EEST runner tests", "test", optimize_name, null, evmz_build);
addEestDelegate(b, "eest", "Run EEST state-test fixtures", "eest", optimize_name, null, evmz_build);
addEestDelegate(b, "eest-classify", "Classify EEST state-test fixtures", "eest-classify", optimize_name, null, evmz_build);
addEestDelegate(b, "eest-scope", "Report downloaded EEST fixture scope and support status", "eest-scope", optimize_name, null, evmz_build);
addEestDelegate(b, "eest-tx", "Run EEST raw transaction-test fixtures", "eest-tx", optimize_name, null, evmz_build);
addEestDelegate(b, "zkevm", "Run EEST zkEVM stateless SSZ fixtures", "zkevm", optimize_name, null, evmz_build);
addEestDelegate(b, "zkevm-input", "Extract one EEST zkEVM stateless input as ZisK stdin", "zkevm-input", optimize_name, null, evmz_build);
addEestDelegate(b, "zkevm-ere", "Run raw ERE stateless input through native adapter", "zkevm-ere", optimize_name, null, evmz_build);
addEestDelegate(b, "zkevm-ere-bench", "Emit ERE BenchmarkRun rows for zkEVM stateless fixtures", "zkevm-ere-bench", null, bench_optimize_name, evmz_build);
addEestDelegate(b, "eest-block-stf", "Run regular EEST blockchain_tests through BlockSTF", "eest-block-stf", optimize_name, null, evmz_build);
addEestDelegate(b, "eest-stateless-block-stf", "Run witness-backed zkEVM blockchain_tests through stateless BlockSTF", "eest-stateless-block-stf", optimize_name, null, evmz_build);
addEestDelegate(b, "ssz-conformance", "Run consensus-spec generic SSZ fixtures", "ssz-conformance", optimize_name, null, evmz_build);
}
if (pathExists(b, "bench/build.zig")) {
addBenchDelegate(b, "bench-test", "Run benchmark sidecar tests", "test", null, evmz_build);
addBenchVmLoopDelegate(b, bench_optimize_name, bench_support_min, bench_support_max, evmz_build);
addBenchDelegate(b, "bench-evmone-vm-loop", "Run standalone evmone VM-loop fixture runner", "evmone-vm-loop", bench_optimize_name, evmz_build);
addBenchDelegate(b, "bench-revm-vm-loop", "Run revm VM-loop fixture runner", "revm-vm-loop", null, evmz_build);
addBenchCompareDelegate(b, bench_optimize_name, bench_support_min, bench_support_max, evmz_build);
addBenchDelegate(b, "bench-block-lifecycle", "Run VM block lifecycle benchmark", "block-lifecycle", bench_optimize_name, evmz_build);
addBenchDelegate(b, "bench-host-boundary", "Run host-boundary benchmark runner", "host-boundary", bench_optimize_name, evmz_build);
addBenchDelegate(b, "bench-host-matrix", "Run host-boundary CSV matrix", "host-matrix", bench_optimize_name, evmz_build);
addBenchDelegate(b, "bench-kernel", "Run pure opcode kernel benchmark", "kernel", bench_optimize_name, evmz_build);
addBenchDelegate(b, "bench-code-analysis", "Run code-analysis morphology and timing report", "code-analysis", bench_optimize_name, evmz_build);
addBenchDelegate(b, "bench-revm-kernel", "Run revm opcode kernel benchmark", "revm-kernel", null, evmz_build);
addBenchDelegate(b, "bench-report", "Run all benchmark layers and write a comparison report", "report", bench_optimize_name, evmz_build);
addBenchMicroDelegate(b, bench_optimize_name, bench_micro_filter, evmz_build);
}
if (pathExists(b, "pkg/ssz/build.zig")) {
addSszBenchDelegate(b, bench_optimize_name);
}
if (is_native_profile) {
addGuestPayloadTest(b, target, optimize, evmz_mod);
} else {
const fail = b.addFail("guest-payload-test is native-only; use guest-zisk-run with -Dziskos-staticlib for zkvm proof");
const guest_payload_test_step = b.step("guest-payload-test", "Run native tests for guest payload fixtures");
guest_payload_test_step.dependOn(&fail.step);
}
const ziskos_staticlib_path = b.option(
[]const u8,
"ziskos-staticlib",
"Path to a ZisK libziskos_staticlib.a provider for guest-zisk",
);
const guest_input_path = b.option([]const u8, "guest-input", "Path to ZisK stdin input file for guest-zisk-run");
const guest_output_path = b.option([]const u8, "guest-output", "Path to write ZisK public output from guest-zisk-run");
const guest_payload = guestPayloadOption(b);
addGuestZisk(b, optimize, evmone_dep, ziskos_staticlib_path, guest_payload, guest_input_path, guest_output_path);
// example
{
const example_name = b.option(
[]const u8,
"example-name",
"Name of the example",
) orelse "basic.zig";
const is_zig = std.mem.endsWith(u8, example_name, ".zig");
const path = b.fmt("examples/{s}", .{example_name});
const root_source_file = b.path(path);
const example_exe_name = std.fs.path.stem(std.fs.path.basename(example_name));
if (is_zig) {
const example_mod = b.createModule(.{
.root_source_file = b.path("src/evm.zig"),
.target = target,
.optimize = optimize,
.link_libcpp = is_native_profile,
});
example_mod.addOptions("build_options", build_options);
example_mod.addImport("ssz", ssz_mod);
example_mod.addImport("rlp", rlp_mod);
example_mod.addImport("mpt", mpt_mod);
example_mod.addIncludePath(evmone_dep.path("evmc/include"));
if (native_precompile_deps) |deps| {
addPrecompileNative(b, example_mod, deps, evmone_dep);
}
addNativeKeccak(example_mod, xkcp_object);
addNativeSecp256k1(example_mod, libsecp256k1_object);
const example = b.addExecutable(.{
.name = example_exe_name,
.root_module = b.createModule(.{
.root_source_file = root_source_file,
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "evmz", .module = example_mod },
},
}),
});
const run_example = b.addRunArtifact(example);
const run_step = b.step("example", "Run the example");
run_step.dependOn(&run_example.step);
const example_tests = b.addTest(.{
.root_module = b.createModule(.{
.root_source_file = root_source_file,
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "evmz", .module = example_mod },
},
}),
});
example_tests.use_llvm = true;
const example_test_step = b.step("example-test", "Run tests in the selected Zig example");
example_test_step.dependOn(&b.addRunArtifact(example_tests).step);
} else {
if (!is_native_profile) {
std.debug.panic("C examples require -Dprofile=native", .{});
}
const example_c = b.addExecutable(.{
.name = example_exe_name,
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
}),
});
example_c.root_module.addIncludePath(b.path("include"));
example_c.root_module.addIncludePath(evmone_dep.path("evmc/include"));
example_c.root_module.addCSourceFile(.{
.file = b.path(path),
.flags = &[_][]const u8{
"-Wall",
"-Wextra",
"-pedantic",
"-std=c99",
},
});
example_c.root_module.linkLibrary(static_c_lib.?);
var run_example = b.addRunArtifact(example_c);
run_example.has_side_effects = true;
const run_step = b.step("example", "Run the example");
run_step.dependOn(&run_example.step);
}
}
}
fn buildProfileOption(b: *std.Build) []const u8 {
const profile = b.option([]const u8, "profile", "Build profile: native or zkvm") orelse "native";
if (!std.mem.eql(u8, profile, "native") and !std.mem.eql(u8, profile, "zkvm")) {
std.debug.panic("unsupported profile '{s}' (expected native or zkvm)", .{profile});
}
return profile;
}
fn nativeKeccakOption(b: *std.Build) []const u8 {
const backend = b.option(
[]const u8,
"native-keccak",
"Native Keccak backend: std or xkcp (ignored by profile=zkvm)",
) orelse "std";
if (!std.mem.eql(u8, backend, "std") and !std.mem.eql(u8, backend, "xkcp")) {
std.debug.panic("unsupported native Keccak backend '{s}' (expected std or xkcp)", .{backend});
}
return backend;
}
fn nativeSecp256k1Option(b: *std.Build) []const u8 {
const backend = b.option(
[]const u8,
"native-secp256k1",
"Native secp256k1 backend: std or libsecp256k1 (ignored by profile=zkvm)",
) orelse "std";
if (!std.mem.eql(u8, backend, "std") and !std.mem.eql(u8, backend, "libsecp256k1")) {
std.debug.panic("unsupported native secp256k1 backend '{s}' (expected std or libsecp256k1)", .{backend});
}
return backend;
}
fn resolveNativeKeccak(
profile: []const u8,
target: std.Build.ResolvedTarget,
requested: []const u8,
) []const u8 {
if (!std.mem.eql(u8, profile, "native") or !std.mem.eql(u8, requested, "xkcp")) return "std";
return switch (target.result.cpu.arch) {
.x86_64, .aarch64, .riscv64 => "xkcp",
else => "std",
};
}
fn resolveNativeSecp256k1(
profile: []const u8,
target: std.Build.ResolvedTarget,
requested: []const u8,
) []const u8 {
if (!std.mem.eql(u8, profile, "native") or !std.mem.eql(u8, requested, "libsecp256k1")) return "std";
return switch (target.result.cpu.arch) {
.x86_64, .aarch64, .riscv64 => "libsecp256k1",
else => "std",
};
}
fn buildOptions(
b: *std.Build,
profile: []const u8,
native_keccak: []const u8,
native_secp256k1: []const u8,
) *std.Build.Step.Options {
const options = b.addOptions();
options.addOption([]const u8, "profile", profile);
options.addOption([]const u8, "native_keccak", native_keccak);
options.addOption([]const u8, "native_secp256k1", native_secp256k1);
return options;
}
fn guestOptions(b: *std.Build, use_ziskos_staticlib: bool) *std.Build.Step.Options {
const options = b.addOptions();
options.addOption(bool, "use_ziskos_staticlib", use_ziskos_staticlib);
return options;
}
fn guestZiskTarget(b: *std.Build) std.Build.ResolvedTarget {
const query = std.Target.Query.parse(.{
.arch_os_abi = "riscv64-freestanding",
.cpu_features = "generic_rv64+m+a",
}) catch @panic("invalid ZisK guest target");
return b.resolveTargetQuery(query);
}
fn guestPayloadOption(b: *std.Build) []const u8 {
const payload = b.option([]const u8, "guest-payload", "Guest payload: basic, stateless-smoke, stateless-ssz-smoke, stateless-ere-smoke, or stateless-ere") orelse "basic";
_ = guestPayloadSource(payload) catch |err| switch (err) {
error.UnknownGuestPayload => std.debug.panic("unsupported guest payload '{s}' (expected basic, stateless-smoke, stateless-ssz-smoke, stateless-ere-smoke, or stateless-ere)", .{payload}),
};
return payload;
}
fn guestPayloadSource(payload: []const u8) error{UnknownGuestPayload}![]const u8 {
if (std.mem.eql(u8, payload, "basic")) return "guest/payload/basic.zig";
if (std.mem.eql(u8, payload, "stateless-smoke")) return "guest/payload/stateless_smoke.zig";
if (std.mem.eql(u8, payload, "stateless-ssz-smoke")) return "guest/payload/stateless_ssz_smoke.zig";
if (std.mem.eql(u8, payload, "stateless-ere-smoke")) return "guest/payload/stateless_ere_smoke.zig";
if (std.mem.eql(u8, payload, "stateless-ere")) return "guest/payload/stateless_ere.zig";
return error.UnknownGuestPayload;
}
fn addGuestPayloadTest(
b: *std.Build,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
evmz_mod: *std.Build.Module,
) void {
const guest_options = guestOptions(b, false);
const guest_options_mod = guest_options.createModule();
const guest_allocator_mod = b.createModule(.{
.root_source_file = b.path("guest/allocator.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "evmz", .module = evmz_mod },
.{ .name = "guest_options", .module = guest_options_mod },
},
});
const basic_payload_mod = b.createModule(.{
.root_source_file = b.path("guest/payload/basic.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "evmz", .module = evmz_mod },
.{ .name = "guest_options", .module = guest_options_mod },
.{ .name = "guest_allocator", .module = guest_allocator_mod },
},
});
const guest_payload_tests_mod = b.createModule(.{
.root_source_file = b.path("guest/payload/basic_test.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "guest_payload_basic", .module = basic_payload_mod },
},
});
const guest_payload_tests = b.addTest(.{
.name = "guest-payload-basic",
.root_module = guest_payload_tests_mod,
});
const stateless_smoke_payload_mod = b.createModule(.{
.root_source_file = b.path("guest/payload/stateless_smoke.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "evmz", .module = evmz_mod },
.{ .name = "guest_options", .module = guest_options_mod },
.{ .name = "guest_allocator", .module = guest_allocator_mod },
},
});
const stateless_smoke_tests_mod = b.createModule(.{
.root_source_file = b.path("guest/payload/stateless_smoke_test.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "guest_payload_stateless_smoke", .module = stateless_smoke_payload_mod },
},
});
const stateless_smoke_tests = b.addTest(.{
.name = "guest-payload-stateless-smoke",
.root_module = stateless_smoke_tests_mod,
});
const stateless_ssz_smoke_payload_mod = b.createModule(.{
.root_source_file = b.path("guest/payload/stateless_ssz_smoke.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "evmz", .module = evmz_mod },
.{ .name = "guest_options", .module = guest_options_mod },
.{ .name = "guest_allocator", .module = guest_allocator_mod },
},
});
const stateless_ssz_smoke_tests_mod = b.createModule(.{
.root_source_file = b.path("guest/payload/stateless_ssz_smoke_test.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "guest_payload_stateless_ssz_smoke", .module = stateless_ssz_smoke_payload_mod },
},
});
const stateless_ssz_smoke_tests = b.addTest(.{
.name = "guest-payload-stateless-ssz-smoke",
.root_module = stateless_ssz_smoke_tests_mod,
});
const stateless_ere_smoke_payload_mod = b.createModule(.{
.root_source_file = b.path("guest/payload/stateless_ere_smoke.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "evmz", .module = evmz_mod },
.{ .name = "guest_options", .module = guest_options_mod },
.{ .name = "guest_allocator", .module = guest_allocator_mod },
},
});
const stateless_ere_smoke_tests_mod = b.createModule(.{
.root_source_file = b.path("guest/payload/stateless_ere_smoke_test.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "guest_payload_stateless_ere_smoke", .module = stateless_ere_smoke_payload_mod },
},
});
const stateless_ere_smoke_tests = b.addTest(.{
.name = "guest-payload-stateless-ere-smoke",
.root_module = stateless_ere_smoke_tests_mod,
});
const guest_io_mod = b.createModule(.{
.root_source_file = b.path("guest/io.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "evmz", .module = evmz_mod },
.{ .name = "guest_options", .module = guest_options_mod },
},
});
const stateless_ere_payload_mod = b.createModule(.{
.root_source_file = b.path("guest/payload/stateless_ere.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "evmz", .module = evmz_mod },
.{ .name = "guest_options", .module = guest_options_mod },
.{ .name = "guest_io", .module = guest_io_mod },
.{ .name = "guest_allocator", .module = guest_allocator_mod },
},
});
const stateless_ere_tests_mod = b.createModule(.{
.root_source_file = b.path("guest/payload/stateless_ere_test.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "evmz", .module = evmz_mod },
.{ .name = "guest_payload_stateless_ere", .module = stateless_ere_payload_mod },
},
});
const stateless_ere_tests = b.addTest(.{
.name = "guest-payload-stateless-ere",
.root_module = stateless_ere_tests_mod,
});
const guest_payload_test_step = b.step("guest-payload-test", "Run native tests for guest payload fixtures");
guest_payload_test_step.dependOn(&b.addRunArtifact(guest_payload_tests).step);
guest_payload_test_step.dependOn(&b.addRunArtifact(stateless_smoke_tests).step);
guest_payload_test_step.dependOn(&b.addRunArtifact(stateless_ssz_smoke_tests).step);
guest_payload_test_step.dependOn(&b.addRunArtifact(stateless_ere_smoke_tests).step);
guest_payload_test_step.dependOn(&b.addRunArtifact(stateless_ere_tests).step);
}
fn addGuestZisk(
b: *std.Build,
optimize: std.builtin.OptimizeMode,
evmone_dep: *std.Build.Dependency,
ziskos_staticlib_path: ?[]const u8,
guest_payload: []const u8,
guest_input_path: ?[]const u8,
guest_output_path: ?[]const u8,
) void {
const provider_path = ziskos_staticlib_path orelse {
const fail = b.addFail("guest-zisk requires -Dziskos-staticlib=<path>/libziskos_staticlib.a");
const guest_step = b.step("guest-zisk", "Build the ZisK rv64 guest ELF");
guest_step.dependOn(&fail.step);
const run_step = b.step("guest-zisk-run", "Run the ZisK guest ELF with ziskemu");
run_step.dependOn(&fail.step);
return;
};
const target = guestZiskTarget(b);
const build_options = buildOptions(b, "zkvm", "std", "std");
const guest_options = guestOptions(b, true);
const guest_options_mod = guest_options.createModule();
const guest_payload_source = guestPayloadSource(guest_payload) catch unreachable;
const evmz_mod = b.createModule(.{
.root_source_file = b.path("src/evm.zig"),
.target = target,
.optimize = optimize,
.code_model = .medium,
.error_tracing = false,
.pic = false,
.single_threaded = true,
.strip = true,
.unwind_tables = .none,
});
evmz_mod.addOptions("build_options", build_options);
evmz_mod.addIncludePath(b.path("include"));
evmz_mod.addIncludePath(evmone_dep.path("evmc/include"));
const ssz_mod = b.createModule(.{
.root_source_file = b.path("pkg/ssz/src/lib.zig"),
.target = target,
.optimize = optimize,
});
const rlp_mod = b.createModule(.{
.root_source_file = b.path("pkg/rlp/src/lib.zig"),
.target = target,
.optimize = optimize,
});
evmz_mod.addImport("ssz", ssz_mod);
evmz_mod.addImport("rlp", rlp_mod);
const mpt_mod = b.createModule(.{
.root_source_file = b.path("pkg/mpt/src/lib.zig"),
.target = target,
.optimize = optimize,
});
mpt_mod.addImport("rlp", rlp_mod);
evmz_mod.addImport("mpt", mpt_mod);
const guest_allocator_mod = b.createModule(.{
.root_source_file = b.path("guest/allocator.zig"),
.target = target,
.optimize = optimize,
.code_model = .medium,
.error_tracing = false,
.imports = &.{
.{ .name = "evmz", .module = evmz_mod },
.{ .name = "guest_options", .module = guest_options_mod },
},
.pic = false,
.single_threaded = true,
.strip = true,
.unwind_tables = .none,
});
const payload_mod = b.createModule(.{
.root_source_file = b.path(guest_payload_source),
.target = target,
.optimize = optimize,
.code_model = .medium,
.error_tracing = false,
.imports = &.{
.{ .name = "evmz", .module = evmz_mod },
.{ .name = "guest_options", .module = guest_options_mod },
.{ .name = "guest_allocator", .module = guest_allocator_mod },
},
.pic = false,
.single_threaded = true,
.strip = true,
.unwind_tables = .none,
});
const guest_io_mod = b.createModule(.{
.root_source_file = b.path("guest/io.zig"),
.target = target,
.optimize = optimize,
.code_model = .medium,
.error_tracing = false,
.imports = &.{
.{ .name = "evmz", .module = evmz_mod },
.{ .name = "guest_options", .module = guest_options_mod },
},
.pic = false,
.single_threaded = true,
.strip = true,
.unwind_tables = .none,
});
payload_mod.addImport("guest_io", guest_io_mod);
const root_imports: []const std.Build.Module.Import = &.{
.{ .name = "guest_payload", .module = payload_mod },
};
const root_mod = b.createModule(.{
.root_source_file = b.path("guest/runtime/zisk/root.zig"),
.target = target,
.optimize = optimize,
.code_model = .medium,
.error_tracing = false,
.imports = root_imports,
.pic = false,
.single_threaded = true,
.strip = true,
.unwind_tables = .none,
});
root_mod.addObjectFile(.{ .cwd_relative = provider_path });
const guest = b.addExecutable(.{
.name = "evmz-guest-zisk",
.root_module = root_mod,
});
guest.entry = .{ .symbol_name = "_start" };
guest.link_gc_sections = true;
guest.setLinkerScript(b.path("guest/runtime/zisk/zisk-rv64.ld"));
const install_guest = b.addInstallArtifact(guest, .{
.dest_dir = .{ .override = .{ .custom = "guest/zisk" } },
.dest_sub_path = "evmz-guest-zisk.elf",
});
const guest_step = b.step("guest-zisk", "Build the ZisK rv64 guest ELF");
guest_step.dependOn(&install_guest.step);
const ziskemu = b.option([]const u8, "ziskemu", "Path to ziskemu for guest-zisk-run") orelse "ziskemu";
const ziskemu_steps = b.option([]const u8, "ziskemu-steps", "Maximum ziskemu steps for guest-zisk-run") orelse "5000000";
const run = b.addSystemCommand(&.{ ziskemu, "-e" });
run.addFileArg(guest.getEmittedBin());
if (guest_input_path) |path| run.addArgs(&.{ "-i", path });
if (guest_output_path) |path| run.addArgs(&.{ "-o", path });
run.addArgs(&.{ "-n", ziskemu_steps, "-m", "--steps", "-c" });
run.has_side_effects = true;
const run_step = b.step("guest-zisk-run", "Run the ZisK guest ELF with ziskemu");
run_step.dependOn(&run.step);
}
fn pathExists(b: *std.Build, sub_path: []const u8) bool {
std.Io.Dir.accessAbsolute(b.graph.io, b.pathFromRoot(sub_path), .{}) catch return false;
return true;
}
const NativePrecompileDeps = struct {
ckzg_dep: *std.Build.Dependency,
blst_dep: *std.Build.Dependency,
mcl_dep: *std.Build.Dependency,
trusted_setup_mod: *std.Build.Module,
};
fn nativePrecompileDeps(
b: *std.Build,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
) NativePrecompileDeps {
const ckzg_dep = b.dependency("ckzg", .{ .target = target, .optimize = optimize });
const blst_dep = b.dependency("blst", .{ .target = target, .optimize = optimize });
const mcl_dep = b.dependency("mcl", .{});
return .{
.ckzg_dep = ckzg_dep,
.blst_dep = blst_dep,
.mcl_dep = mcl_dep,
.trusted_setup_mod = buildTrustedSetupModule(b, ckzg_dep.path("src/trusted_setup.txt")),
};
}
fn addEestDelegate(
b: *std.Build,
step_name: []const u8,
description: []const u8,
child_step: []const u8,
optimize_name: ?[]const u8,
bench_optimize_name: ?[]const u8,
config: EvmzBuildConfig,
) void {
const run = b.addSystemCommand(&.{
b.graph.zig_exe,
"build",
});
if (optimize_name) |name| {
run.addArg(b.fmt("-Doptimize={s}", .{name}));
}
if (bench_optimize_name) |name| {
run.addArg(b.fmt("-Dbench-optimize={s}", .{name}));
}
addEvmzBuildArgs(run, b, config);
run.addArg(child_step);
if (b.args) |args| {
run.addArg("--");
run.addArgs(args);
}
run.setCwd(b.path("eest"));
const step = b.step(step_name, description);
step.dependOn(&run.step);
}
fn addBenchDelegate(
b: *std.Build,
step_name: []const u8,
description: []const u8,
child_step: []const u8,
optimize_name: ?[]const u8,
config: EvmzBuildConfig,
) void {
const run = b.addSystemCommand(&.{
b.graph.zig_exe,
"build",
});
if (optimize_name) |name| {
run.addArg(b.fmt("-Doptimize={s}", .{name}));
}
addEvmzBuildArgs(run, b, config);
run.addArg(child_step);
if (b.args) |args| {
run.addArg("--");
run.addArgs(args);
}
run.setCwd(b.path("bench"));
const step = b.step(step_name, description);
step.dependOn(&run.step);
}
fn addSszBenchDelegate(b: *std.Build, optimize_name: []const u8) void {
const run = b.addSystemCommand(&.{
b.graph.zig_exe,
"build",
b.fmt("-Doptimize={s}", .{optimize_name}),
"bench",
});
if (b.args) |args| {
run.addArg("--");
run.addArgs(args);
}
run.setCwd(b.path("pkg/ssz"));
const step = b.step("ssz-bench", "Run standalone SSZ codec benchmarks");
step.dependOn(&run.step);
}
fn addBenchCompareDelegate(
b: *std.Build,
optimize_name: []const u8,
support_min: ?[]const u8,
support_max: ?[]const u8,
config: EvmzBuildConfig,
) void {
const run = b.addSystemCommand(&.{
b.graph.zig_exe,
"build",
});
run.addArg(b.fmt("-Doptimize={s}", .{optimize_name}));
addEvmzBuildArgs(run, b, config);
if (support_min) |revision| {
run.addArg(b.fmt("-Dbench-support-min={s}", .{revision}));
}
if (support_max) |revision| {
run.addArg(b.fmt("-Dbench-support-max={s}", .{revision}));
}
run.addArg("compare");
if (b.args) |args| {
run.addArg("--");
run.addArgs(args);
}
run.setCwd(b.path("bench"));
const step = b.step("bench-compare", "Run VM-core comparison");
step.dependOn(&run.step);
}
fn addBenchVmLoopDelegate(