build.zig (3728B)
1 const std = @import("std"); 2 3 // Although this function looks imperative, note that its job is to 4 // declaratively construct a build graph that will be executed by an external 5 // runner. 6 pub fn build(b: *std.Build) void { 7 // Standard target options allows the person running `zig build` to choose 8 // what target to build for. Here we do not override the defaults, which 9 // means any target is allowed, and the default is native. Other options 10 // for restricting supported target set are available. 11 const target = b.standardTargetOptions(.{}); 12 13 // Standard optimization options allow the person running `zig build` to select 14 // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not 15 // set a preferred release mode, allowing the user to decide how to optimize. 16 const optimize = b.standardOptimizeOption(.{}); 17 18 const linenoise_dep = b.dependency("linenoise", .{ 19 .target = target, 20 .optimize = optimize, 21 }); 22 23 const linenoise_tc = b.addTranslateC(.{ 24 .root_source_file = linenoise_dep.path("linenoise.h"), 25 .target = target, 26 .optimize = optimize, 27 }); 28 29 const linenoise_mod = linenoise_tc.createModule(); 30 31 linenoise_mod.addCSourceFile(.{ 32 .file = linenoise_dep.path("linenoise.c"), 33 .language = .c, 34 }); 35 36 const exe = b.addExecutable(.{ 37 .name = "zlox", 38 .root_module = b.createModule(.{ 39 .root_source_file = b.path("src/main.zig"), 40 .target = target, 41 .optimize = optimize, 42 .imports = &.{ 43 .{ 44 .name = "linenoise", 45 .module = linenoise_mod, 46 }, 47 }, 48 }), 49 }); 50 51 // This declares intent for the executable to be installed into the 52 // standard location when the user invokes the "install" step (the default 53 // step when running `zig build`). 54 b.installArtifact(exe); 55 56 // This *creates* a Run step in the build graph, to be executed when another 57 // step is evaluated that depends on it. The next line below will establish 58 // such a dependency. 59 const run_cmd = b.addRunArtifact(exe); 60 61 // By making the run step depend on the install step, it will be run from the 62 // installation directory rather than directly from within the cache directory. 63 // This is not necessary, however, if the application depends on other installed 64 // files, this ensures they will be present and in the expected location. 65 run_cmd.step.dependOn(b.getInstallStep()); 66 67 // This allows the user to pass arguments to the application in the build 68 // command itself, like this: `zig build run -- arg1 arg2 etc` 69 run_cmd.addPassthruArgs(); 70 71 // This creates a build step. It will be visible in the `zig build --help` menu, 72 // and can be selected like this: `zig build run` 73 // This will evaluate the `run` step rather than the default, which is "install". 74 const run_step = b.step("run", "Run the app"); 75 run_step.dependOn(&run_cmd.step); 76 77 // Creates a step for unit testing. This only builds the test executable 78 // but does not run it. 79 const unit_tests = b.addTest(.{ 80 .root_module = b.createModule(.{ 81 .root_source_file = b.path("src/test.zig"), 82 .target = target, 83 .optimize = optimize, 84 }), 85 }); 86 87 const run_unit_tests = b.addRunArtifact(unit_tests); 88 89 // Similar to creating the run step earlier, this exposes a `test` step to 90 // the `zig build --help` menu, providing a way for the user to request 91 // running the unit tests. 92 const test_step = b.step("test", "Run unit tests"); 93 test_step.dependOn(&run_unit_tests.step); 94 }