-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbuild.zig
470 lines (406 loc) · 16.5 KB
/
build.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
const env_parser = @import("src/utils/env_load.zig");
const std = @import("std");
const builtin = @import("builtin");
const min_zig_string = "0.14.0-dev.3008+7cef585f5";
/// Build zabi modules and test runners.
pub fn build(b: *std.Build) void {
comptime {
const current_zig = builtin.zig_version;
const min_zig = std.SemanticVersion.parse(min_zig_string) catch unreachable;
if (current_zig.order(min_zig) == .lt) {
@compileError(std.fmt.comptimePrint("Your Zig version v{} does not meet the minimum build requirement of v{}", .{ current_zig, min_zig }));
}
}
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// Build the library with all modules.
const zabi = b.addModule("zabi", .{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
});
addDependencies(b, zabi, target, optimize);
// Build the library with the abi module.
const zabi_abi = b.addModule("zabi-abi", .{
.root_source_file = b.path("src/abi/root.zig"),
.target = target,
.optimize = optimize,
});
// Build the library with the sol-ast module.
const zabi_ast = b.addModule("zabi-ast", .{
.root_source_file = b.path("src/ast/root.zig"),
.target = target,
.optimize = optimize,
});
// Build the library with the rpc-clients module.
const zabi_clients = b.addModule("zabi-clients", .{
.root_source_file = b.path("src/clients/root.zig"),
.target = target,
.optimize = optimize,
});
addDependencies(b, zabi_clients, target, optimize);
// Build the library with the crypto module.
const zabi_crypto = b.addModule("zabi-crypto", .{
.root_source_file = b.path("src/crypto/root.zig"),
.target = target,
.optimize = optimize,
});
addDependencies(b, zabi_crypto, target, optimize);
// Build the library with the decoding module.
const zabi_decoding = b.addModule("zabi-decoding", .{
.root_source_file = b.path("src/decoding/root.zig"),
.target = target,
.optimize = optimize,
});
// Build the library with the ens-client module.
const zabi_ens = b.addModule("zabi-ens", .{
.root_source_file = b.path("src/clients/ens/root.zig"),
.target = target,
.optimize = optimize,
});
// Build the library with the encoding module.
const zabi_encoding = b.addModule("zabi-encoding", .{
.root_source_file = b.path("src/encoding/root.zig"),
.target = target,
.optimize = optimize,
});
// Build the library with the evm module.
const zabi_evm = b.addModule("zabi-evm", .{
.root_source_file = b.path("src/evm/root.zig"),
.target = target,
.optimize = optimize,
});
// Build the library with the human-readable abi parsing module.
const zabi_human = b.addModule("zabi-human", .{
.root_source_file = b.path("src/human-readable/root.zig"),
.target = target,
.optimize = optimize,
});
// Build the library with the meta-programming module.
const zabi_meta = b.addModule("zabi-meta", .{
.root_source_file = b.path("src/meta/root.zig"),
.target = target,
.optimize = optimize,
});
// Build the library with the op-stack module.
const zabi_op_stack = b.addModule("zabi-op-stack", .{
.root_source_file = b.path("src/clients/optimism/root.zig"),
.target = target,
.optimize = optimize,
});
// Build the library with the types module.
const zabi_types = b.addModule("zabi-types", .{
.root_source_file = b.path("src/types/root.zig"),
.target = target,
.optimize = optimize,
});
addDependencies(b, zabi_types, target, optimize);
// Build the library with the utils module.
const zabi_utils = b.addModule("zabi-utils", .{
.root_source_file = b.path("src/utils/root.zig"),
.target = target,
.optimize = optimize,
});
// Adds the dependencies for `zabi` module.
{
zabi.addImport("zabi-abi", zabi_abi);
zabi.addImport("zabi-ast", zabi_ast);
zabi.addImport("zabi-clients", zabi_clients);
zabi.addImport("zabi-crypto", zabi_crypto);
zabi.addImport("zabi-decoding", zabi_decoding);
zabi.addImport("zabi-encoding", zabi_encoding);
zabi.addImport("zabi-ens", zabi_ens);
zabi.addImport("zabi-evm", zabi_evm);
zabi.addImport("zabi-human", zabi_human);
zabi.addImport("zabi-meta", zabi_meta);
zabi.addImport("zabi-op-stack", zabi_op_stack);
zabi.addImport("zabi-types", zabi_types);
zabi.addImport("zabi-utils", zabi_utils);
}
// Adds the dependencies for `zabi-abi` module.
{
zabi_abi.addImport("zabi-decoding", zabi_decoding);
zabi_abi.addImport("zabi-encoding", zabi_encoding);
zabi_abi.addImport("zabi-human", zabi_human);
zabi_abi.addImport("zabi-meta", zabi_meta);
zabi_abi.addImport("zabi-types", zabi_types);
}
// Adds the dependencies for `zabi-client` module.
{
zabi_clients.addImport("zabi-abi", zabi_abi);
zabi_clients.addImport("zabi-crypto", zabi_crypto);
zabi_clients.addImport("zabi-decoding", zabi_decoding);
zabi_clients.addImport("zabi-encoding", zabi_encoding);
zabi_clients.addImport("zabi-evm", zabi_evm);
zabi_clients.addImport("zabi-meta", zabi_meta);
zabi_clients.addImport("zabi-types", zabi_types);
zabi_clients.addImport("zabi-utils", zabi_utils);
}
// Adds the dependencies for `zabi-crypto` module.
{
zabi_crypto.addImport("zabi-utils", zabi_utils);
zabi_crypto.addImport("zabi-types", zabi_types);
}
// Adds the dependencies for `zabi-decoding` module.
{
zabi_decoding.addImport("zabi-meta", zabi_meta);
zabi_decoding.addImport("zabi-types", zabi_types);
zabi_decoding.addImport("zabi-utils", zabi_utils);
}
// Adds the dependencies for `zabi-encoding` module.
{
zabi_encoding.addImport("zabi-abi", zabi_abi);
zabi_encoding.addImport("zabi-crypto", zabi_crypto);
zabi_encoding.addImport("zabi-meta", zabi_meta);
zabi_encoding.addImport("zabi-types", zabi_types);
zabi_encoding.addImport("zabi-utils", zabi_utils);
}
// Adds the dependencies for `zabi-ens` module.
{
zabi_ens.addImport("zabi-abi", zabi_abi);
zabi_ens.addImport("zabi-clients", zabi_clients);
zabi_ens.addImport("zabi-decoding", zabi_decoding);
zabi_ens.addImport("zabi-encoding", zabi_encoding);
zabi_ens.addImport("zabi-types", zabi_types);
zabi_ens.addImport("zabi-utils", zabi_utils);
}
// Adds the dependencies for `zabi-evm` module.
{
zabi_evm.addImport("zabi-utils", zabi_utils);
zabi_evm.addImport("zabi-meta", zabi_meta);
zabi_evm.addImport("zabi-types", zabi_types);
}
// Adds the dependencies for `zabi-human` module.
{
zabi_human.addImport("zabi-abi", zabi_abi);
zabi_human.addImport("zabi-meta", zabi_meta);
}
// Adds the dependencies for `zabi-meta` module.
{
zabi_meta.addImport("zabi-abi", zabi_abi);
zabi_meta.addImport("zabi-types", zabi_types);
}
// Adds the dependencies for `zabi-op-stack` module.
{
zabi_op_stack.addImport("zabi-abi", zabi_abi);
zabi_op_stack.addImport("zabi-clients", zabi_clients);
zabi_op_stack.addImport("zabi-crypto", zabi_crypto);
zabi_op_stack.addImport("zabi-decoding", zabi_decoding);
zabi_op_stack.addImport("zabi-encoding", zabi_encoding);
zabi_op_stack.addImport("zabi-meta", zabi_meta);
zabi_op_stack.addImport("zabi-types", zabi_types);
zabi_op_stack.addImport("zabi-utils", zabi_utils);
}
// Adds the dependencies for `zabi-types` module.
{
zabi_types.addImport("zabi-abi", zabi_abi);
zabi_types.addImport("zabi-meta", zabi_meta);
zabi_types.addImport("zabi-utils", zabi_utils);
}
// Adds the dependencies for `zabi-utils` module.
{
zabi_utils.addImport("zabi-meta", zabi_meta);
zabi_utils.addImport("zabi-types", zabi_types);
}
// Runs the tests or coverage steps.
buildTestOrCoverage(b, target, optimize, zabi);
// Runs the benchmark
buildBenchmark(b, target, optimize, zabi);
// Builds the examples.
buildExamples(b, target, optimize, zabi);
// Build and generate docs for zabi. Uses the `doc_comments` spread across the codebase.
// Always build in `ReleaseFast`.
buildDocs(b, target);
// Build the wasm file. Always build in `ReleaseSmall` on `wasm32-freestanding`.
buildWasm(b, zabi);
}
// Builds and runs the main tests of zabi or the coverage from kcov.
fn buildTestOrCoverage(
b: *std.Build,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
module: *std.Build.Module,
) void {
const load_variables = b.option(bool, "load_variables", "Load enviroment variables from a \"env\" file.") orelse false;
const env_file_path = b.option([]const u8, "env_file_path", "Specify the location of a env variables file") orelse ".env";
// Builds and runs the main tests of zabi.
{
const lib_unit_tests = b.addTest(.{
.name = "zabi-tests",
.root_source_file = b.path("tests/root.zig"),
.target = target,
.optimize = optimize,
.test_runner = .{
.path = b.path("build/test_runner.zig"),
.mode = .simple,
},
});
lib_unit_tests.root_module.addImport("zabi", module);
addDependencies(b, lib_unit_tests.root_module, target, optimize);
var run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_lib_unit_tests.step);
if (load_variables)
loadVariables(b, env_file_path, run_lib_unit_tests);
}
// Build and run coverage test runner if `zig build coverage` was ran
{
const coverage_lib_unit_tests = b.addTest(.{
.name = "zabi-tests-coverage",
.root_source_file = b.path("tests/root_benchmark.zig"),
.target = target,
.optimize = optimize,
.test_runner = .{
.path = b.path("build/test_runner.zig"),
.mode = .simple,
},
});
coverage_lib_unit_tests.root_module.addImport("zabi", module);
const test_step_coverage = b.step("coverage", "Run unit tests with kcov coverage");
const kcov_collect = std.Build.Step.Run.create(b, "collect coverage");
kcov_collect.rename_step_with_output_arg = false;
if (load_variables)
loadVariables(b, env_file_path, kcov_collect);
kcov_collect.addArgs(&.{
"kcov",
"--clean",
});
kcov_collect.addPrefixedDirectoryArg("--include-pattern=", b.path("src"));
_ = kcov_collect.addOutputFileArg(coverage_lib_unit_tests.name);
kcov_collect.addArtifactArg(coverage_lib_unit_tests);
kcov_collect.enableTestRunnerMode();
const install_coverage = b.addInstallDirectory(.{
.source_dir = kcov_collect.addOutputFileArg("."),
.install_dir = .{ .custom = "coverage" },
.install_subdir = "",
});
test_step_coverage.dependOn(&install_coverage.step);
}
}
/// Build the wasm binary.
fn buildWasm(b: *std.Build, module: *std.Build.Module) void {
const wasm_crosstarget: std.Target.Query = .{
.cpu_arch = .wasm32,
.os_tag = .freestanding,
.cpu_model = .{ .explicit = &std.Target.wasm.cpu.mvp },
.cpu_features_add = std.Target.wasm.featureSet(&.{
.atomics,
.bulk_memory,
.reference_types,
.sign_ext,
}),
};
const wasm = b.addExecutable(.{
.name = "zabi_wasm",
.root_source_file = b.path("src/root_wasm.zig"),
.target = b.resolveTargetQuery(wasm_crosstarget),
.optimize = .ReleaseSmall,
});
wasm.root_module.addImport("zabi", module);
// Browser target
wasm.entry = .disabled;
wasm.rdynamic = true;
// Memory defaults.
wasm.initial_memory = 65536 * 32;
wasm.max_memory = 65536 * 65336;
wasm.root_module.stack_protector = true;
const wasm_install = b.addInstallArtifact(wasm, .{});
const wasm_step = b.step("wasm", "Build wasm library");
wasm_step.dependOn(&wasm_install.step);
}
/// Adds zabi project dependencies.
fn addDependencies(
b: *std.Build,
mod: *std.Build.Module,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
) void {
const c_kzg_4844_dep = b.dependency("c-kzg-4844", .{
.target = target,
.optimize = optimize,
});
mod.addImport("c-kzg-4844", c_kzg_4844_dep.module("c-kzg-4844"));
mod.linkLibrary(c_kzg_4844_dep.artifact("c-kzg-4844"));
}
/// Builds and runs the benchmarks
fn buildBenchmark(
b: *std.Build,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
dependency: *std.Build.Module,
) void {
const bench = b.addTest(.{
.name = "benchmark",
.root_source_file = b.path("tests/root_benchmark.zig"),
.target = target,
.optimize = optimize,
.test_runner = .{
.path = b.path("build/benchmark.zig"),
.mode = .simple,
},
});
bench.root_module.addImport("zabi", dependency);
addDependencies(b, bench.root_module, target, optimize);
var bench_run = b.addRunArtifact(bench);
const bench_step = b.step("bench", "Benchmark zabi");
bench_step.dependOn(&bench_run.step);
}
/// Builds all of zabi examples so that we can leverage this also for CI
fn buildExamples(
b: *std.Build,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
dependency: *std.Build.Module,
) void {
const examples_step = b.step("examples", "Build all of zabi's examples");
const examples: []const []const u8 = &.{
"examples/watch/watch.zig",
"examples/watch/logs.zig",
"examples/transfer/transfer.zig",
"examples/interpreter/interpreter.zig",
"examples/block_explorer/explorer.zig",
"examples/wallet/wallet.zig",
"examples/contract/contract.zig",
"examples/autobahn/autobahn.zig",
};
inline for (examples) |example| {
const index = std.mem.lastIndexOfScalar(u8, example, '/').?;
const example_exe = b.addExecutable(.{
// example name -> filename - .zig extension
.name = example[index + 1 .. example.len - 4],
.root_source_file = b.path(example),
.target = target,
.optimize = optimize,
});
example_exe.root_module.addImport("zabi", dependency);
addDependencies(b, example_exe.root_module, target, optimize);
var install_artifact = b.addInstallArtifact(example_exe, .{});
examples_step.dependOn(&install_artifact.step);
}
}
/// Builds and runs a runner to generate documentation based on the `doc_comments` tokens in the codebase.
fn buildDocs(b: *std.Build, target: std.Build.ResolvedTarget) void {
const docs = b.addExecutable(.{
.name = "docs",
.root_source_file = b.path("build/docs_generate.zig"),
.target = target,
.optimize = .ReleaseFast,
.link_libc = true,
});
var docs_run = b.addRunArtifact(docs);
docs_run.has_side_effects = true;
const docs_step = b.step("docs", "Generate documentation based on the source code.");
docs_step.dependOn(&docs_run.step);
}
/// Loads enviroment variables from a `.env` file in case they aren't already present.
fn loadVariables(b: *std.Build, env_path: []const u8, exe: *std.Build.Step.Run) void {
var file = std.fs.cwd().openFile(env_path, .{}) catch |err|
std.debug.panic("Failed to read from {s} file! Error: {s}", .{ env_path, @errorName(err) });
defer file.close();
const source = file.readToEndAllocOptions(b.allocator, std.math.maxInt(u32), null, @alignOf(u8), 0) catch |err|
std.debug.panic("Failed to read from {s} file! Error: {s}", .{ env_path, @errorName(err) });
defer b.allocator.free(source);
const env = exe.getEnvMap();
env_parser.parseToEnviromentVariables(b.allocator, source, env) catch |err|
std.debug.panic("Failed to load from {s} file! Error: {s}", .{ env_path, @errorName(err) });
}