Repository navigation
Expand file tree
/
Copy pathbuild.zig
More file actions
236 lines (214 loc) · 9.39 KB
/
Copy pathbuild.zig
File metadata and controls
236 lines (214 loc) · 9.39 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
//! needlefish build graph.
//!
//! Layout mirrors base/base: `src/` holds every library (one `base` module,
//! grouped like base/base's `crates/`), `bin/<name>/main.zig` holds binaries.
//!
//! Steps:
//! zig build install every binary into zig-out/bin
//! zig build test run all unit tests (-Dtest-filter=... to narrow)
//! zig build fuzz build fuzz smoke tests (use `zig build test --fuzz`)
//! zig build sim -- SEED run the deterministic simulator (DST) for SEED
//! zig build bench run micro benchmarks (ReleaseFast recommended)
//! zig build -Ddiff-coverage instrument `base diff-adapter` for coverage-guided fuzzing
const std = @import("std");
/// Every binary lives at `bin/<name>/main.zig`. Add new ones here.
const binaries = [_][]const u8{
"base",
"witness-diff",
"replay-roots",
};
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const test_filter = b.option([]const u8, "test-filter", "Only run tests matching");
const configured = build_options(b);
const options = configured.options;
const base = b.addModule("base", .{
.root_source_file = b.path("src/base.zig"),
.target = target,
.optimize = optimize,
.fuzz = if (configured.diff_coverage) true else null,
});
base.addOptions("build_options", options);
// Unit tests: one test binary over the whole `base` module tree.
const filters: []const []const u8 = if (test_filter) |f| &.{f} else &.{};
const unit_tests = b.addTest(.{ .root_module = base, .filters = filters });
const run_unit_tests = b.addRunArtifact(unit_tests);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_unit_tests.step);
// The edge counters (`-ffuzz`) need the LLVM backend and linker.
const use_llvm: ?bool = if (configured.diff_coverage) true else null;
add_binaries(b, base, target, optimize, test_step, filters, use_llvm);
// Deterministic simulation testing and micro benchmarks.
const sim = add_tool(b, base, target, optimize, .{
.name = "simulator",
.path = "src/testing/simulator_main.zig",
.step = "sim",
.description = "Run the deterministic simulator: zig build sim -- [seed]",
});
b.installArtifact(sim);
const bench = add_tool(b, base, target, optimize, .{
.name = "bench",
.path = "src/testing/bench_main.zig",
.step = "bench",
.description = "Run micro benchmarks",
});
const statetest = add_vector_tools(b, base, target, optimize);
// `etc/bench/evm2.sh` installs the state-test runner (`statetest --bench`) per build mode.
const bench_tools = b.step("bench-tools", "Install the benchmark tools (statetest)");
bench_tools.dependOn(&b.addInstallArtifact(statetest, .{}).step);
// Compile (not run) the state-test runner on every `zig build test`: it is the EVM's
// conformance oracle, and cross-lane API changes once broke it unnoticed.
test_step.dependOn(&statetest.step);
// Compile-only check for editors and CI speed.
const check_step = b.step("check", "Compile everything without running");
check_step.dependOn(&unit_tests.step);
check_step.dependOn(&sim.step);
check_step.dependOn(&bench.step);
check_step.dependOn(&statetest.step);
}
const Configured = struct { options: *std.Build.Step.Options, diff_coverage: bool };
/// Build options shared by every compilation (`build_options`).
fn build_options(b: *std.Build) Configured {
const options = b.addOptions();
options.addOption([]const u8, "version", "0.0.0");
const git_rev = b.option([]const u8, "git-rev", "Git revision baked into binaries");
options.addOption([]const u8, "git_rev", git_rev orelse "unknown");
const slow_tests = b.option(bool, "slow-tests", "Also run large simulation tests") orelse false;
options.addOption(bool, "slow_tests", slow_tests);
const require_io_uring = b.option(
bool,
"require-io-uring",
"Fail (instead of skip) OS IO tests when io_uring is unavailable",
) orelse false;
options.addOption(bool, "require_io_uring", require_io_uring);
const fuzz_soak = b.option(u32, "fuzz-soak", "Multiply seeded fuzz iterations") orelse 1;
options.addOption(u32, "fuzz_soak", fuzz_soak);
const fuzz_seed = b.option(u64, "fuzz-seed", "Mixed into every seeded fuzz seed") orelse 0;
options.addOption(u64, "fuzz_seed", fuzz_seed);
const opcode_profile = b.option(bool, "evm-opcode-profile", "Time EVM opcodes") orelse false;
options.addOption(bool, "evm_opcode_profile", opcode_profile);
// `base diff-adapter` coverage (BASE_DIFF_COVERAGE): semantic hooks plus the compiler's
// edge counters on the `base` module (`-ffuzz` instrumentation).
const diff_coverage = b.option(bool, "diff-coverage", "Diff-adapter coverage") orelse false;
options.addOption(bool, "diff_coverage", diff_coverage);
return .{ .options = options, .diff_coverage = diff_coverage };
}
/// Runners over public vectors fetched by `just vectors`.
fn add_vector_tools(
b: *std.Build,
base: *std.Build.Module,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
) *std.Build.Step.Compile {
// ethereum/tests GeneralStateTests runner (vectors from `just vectors`).
const statetest = add_tool(b, base, target, optimize, .{
.name = "statetest",
.path = "src/execution/evm/state_test_main.zig",
.step = "statetest",
.description = "Run state tests: zig build statetest -- <file-or-dir>",
});
// Real chain blocks, one transaction at a time on its prestateTracer pre-state.
_ = add_tool(b, base, target, optimize, .{
.name = "txcheck",
.path = "src/execution/evm/tx_check_main.zig",
.step = "txcheck",
.description = "Check txs against receipts: zig build txcheck -- <block-dir>...",
});
// ethereum/tests TransactionTests through the strict tx decoder.
_ = add_tool(b, base, target, optimize, .{
.name = "txtest",
.path = "src/common/consensus/tx_test_main.zig",
.step = "txtest",
.description = "Run transaction tests: zig build txtest -- <file-or-dir>",
});
// ethereum/tests BlockchainTests block RLP through the strict block decoder.
_ = add_tool(b, base, target, optimize, .{
.name = "blockrlp",
.path = "src/common/consensus/block_test_main.zig",
.step = "blockrlp",
.description = "Check block RLP: zig build blockrlp -- <file-or-dir>",
});
// base/base genesis parity (files from `just vectors`).
_ = add_tool(b, base, target, optimize, .{
.name = "genesischeck",
.path = "src/common/genesis/check_main.zig",
.step = "genesischeck",
.description = "Check genesis files: zig build genesischeck -- <genesis.json>...",
});
return statetest;
}
const Tool = struct {
name: []const u8,
path: []const u8,
step: []const u8,
description: []const u8,
};
/// An executable over the `base` module with a run step that forwards `-- args`.
fn add_tool(
b: *std.Build,
base: *std.Build.Module,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
tool: Tool,
) *std.Build.Step.Compile {
const exe = b.addExecutable(.{
.name = tool.name,
.root_module = b.createModule(.{
.root_source_file = b.path(tool.path),
.target = target,
.optimize = optimize,
.imports = &.{.{ .name = "base", .module = base }},
}),
});
const run = b.addRunArtifact(exe);
run.addPassthruArgs();
b.step(tool.step, tool.description).dependOn(&run.step);
return exe;
}
/// Each binary gets an install, a `run-<name>` step, and its own test binary.
fn add_binaries(
b: *std.Build,
base: *std.Build.Module,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
test_step: *std.Build.Step,
filters: []const []const u8,
use_llvm: ?bool,
) void {
for (binaries) |name| {
const exe = b.addExecutable(.{
.name = name,
.use_llvm = use_llvm,
.root_module = b.createModule(.{
.root_source_file = b.path(b.fmt("bin/{s}/main.zig", .{name})),
.target = target,
.optimize = optimize,
.imports = &.{.{ .name = "base", .module = base }},
}),
});
b.installArtifact(exe);
const run = b.addRunArtifact(exe);
run.addPassthruArgs();
const run_step = b.step(b.fmt("run-{s}", .{name}), b.fmt("Run {s}", .{name}));
run_step.dependOn(&run.step);
if (std.mem.eql(u8, name, "replay-roots")) {
b.step(name, "Check state/receipts/transactions roots: replay-roots -- DIR [BLOCK]")
.dependOn(&run.step);
}
}
// Binary tests (each main.zig may hold integration tests).
for (binaries) |name| {
const bin_tests = b.addTest(.{
.name = b.fmt("test-{s}", .{name}),
.root_module = b.createModule(.{
.root_source_file = b.path(b.fmt("bin/{s}/main.zig", .{name})),
.target = target,
.optimize = optimize,
.imports = &.{.{ .name = "base", .module = base }},
}),
.filters = filters,
});
test_step.dependOn(&b.addRunArtifact(bin_tests).step);
}
}