-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbuild.zig
More file actions
75 lines (62 loc) · 3.58 KB
/
Copy pathbuild.zig
File metadata and controls
75 lines (62 loc) · 3.58 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
const std = @import("std");
pub fn build(b: *std.Build) void {
// Standard optimization options. This will respect -Doptimize= from the command line.
// We'll use -Doptimize=ReleaseSmall in the package.json script.
const optimize = b.standardOptimizeOption(.{});
// --- Zabi Dependency ---
// Add zabi as a dependency for the build.
// The .{} for the second argument means we are not overriding target or optimize for the dependency itself here.
const zabi = b.dependency("zabi", .{});
// Explicitly define the target for our WASM build
const wasm_target = b.resolveTargetQuery(.{
.cpu_arch = .wasm32,
.os_tag = .freestanding,
});
// Create a module for our WASM library code.
// This points to your actual Zig code.
const wasm_lib_module = b.createModule(.{
.root_source_file = b.path("src/zig/lib.zig"), // Path to your main Zig library file
.target = wasm_target, // Target this module for WASM
.optimize = optimize, // Apply optimization
});
// Create the WebAssembly artifact using addExecutable
const wasm_exe = b.addExecutable(.{
.name = "evmstate", // The output filename will be evmstate.wasm
.root_module = wasm_lib_module, // Use the module we defined
// .target = wasm_target, // Target is implicitly taken from the root_module's target
// .optimize = optimize, // Optimize is implicitly taken from the root_module's optimize
});
wasm_exe.rdynamic = true; // Required for exported functions to be callable from JS
wasm_exe.entry = .disabled; // Crucial: we are building a library, not an application
wasm_exe.root_module.addImport("zabi-abi", zabi.module("zabi-abi"));
// Define the desired output path for the WASM file directly in the dist directory
const wasm_install_path = "dist/wasm/evmstate.wasm";
// Create a step to install the compiled WASM binary to the specified path.
// This handles the "copying" of the file.
const install_wasm_step = b.addInstallFile(wasm_exe.getEmittedBin(), wasm_install_path);
// Define a "wasm" step in the build graph.
// Running `zig build wasm` will execute this step.
const build_wasm_step = b.step("wasm", "Build the WebAssembly artifact and install it");
build_wasm_step.dependOn(&install_wasm_step.step);
// If you want `zig build` (with no arguments from project root)
// to also build and install the WASM by default:
b.default_step.dependOn(build_wasm_step);
// Get the native target for running tests.
// Pass .{} to use the host's native target.
const native_target = b.standardTargetOptions(.{});
// Add a test executable that includes all test blocks from lib.zig.
// Tests should generally run with the native target.
const main_tests = b.addTest(.{
.root_source_file = b.path("src/zig/lib.zig"), // Points to the file containing the tests
.target = native_target, // Compile tests for the native architecture
.optimize = optimize, // Use the same optimization mode as the rest of the build (can be overridden)
// .filter = b.option([]const u8, "test-filter", "Filter for tests to run"), // Optional: allow test filtering
});
// Make zabi available to your test code
// main_tests.root_module.addImport("zabi", zabi_module);
// Create a "test" step that runs the test executable.
const run_main_tests_step = b.addRunArtifact(main_tests);
// The `test` step will execute `run_main_tests_step`
const test_step = b.step("test", "Run unit tests for lib.zig");
test_step.dependOn(&run_main_tests_step.step);
}