build.zig (3738B)
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 opts = .{ .target = target, .optimize = optimize }; 19 20 const exe = b.addExecutable(.{ 21 .name = "comments", 22 // In this case the main source file is merely a path, however, in more 23 // complicated build scripts, this could be a generated file. 24 .root_source_file = b.path("src/main.zig"), 25 .target = target, 26 .optimize = optimize, 27 }); 28 29 const zws = b.dependency("zigwebserver", opts); 30 exe.root_module.addImport("zws", zws.module("zigwebserver")); 31 32 const pg = b.dependency("pg", opts); 33 exe.root_module.addImport("pg", pg.module("pg")); 34 35 const mustache = b.dependency("mustache", opts); 36 exe.root_module.addImport("mustache", mustache.module("mustache")); 37 38 const smtp = b.dependency("smtp", opts); 39 exe.root_module.addImport("smtp", smtp.module("smtp")); 40 41 // This declares intent for the executable to be installed into the 42 // standard location when the user invokes the "install" step (the default 43 // step when running `zig build`). 44 b.installArtifact(exe); 45 46 // This *creates* a Run step in the build graph, to be executed when another 47 // step is evaluated that depends on it. The next line below will establish 48 // such a dependency. 49 const run_cmd = b.addRunArtifact(exe); 50 51 // By making the run step depend on the install step, it will be run from the 52 // installation directory rather than directly from within the cache directory. 53 // This is not necessary, however, if the application depends on other installed 54 // files, this ensures they will be present and in the expected location. 55 run_cmd.step.dependOn(b.getInstallStep()); 56 57 // This allows the user to pass arguments to the application in the build 58 // command itself, like this: `zig build run -- arg1 arg2 etc` 59 if (b.args) |args| { 60 run_cmd.addArgs(args); 61 } 62 63 // This creates a build step. It will be visible in the `zig build --help` menu, 64 // and can be selected like this: `zig build run` 65 // This will evaluate the `run` step rather than the default, which is "install". 66 const run_step = b.step("run", "Run the app"); 67 run_step.dependOn(&run_cmd.step); 68 69 // Creates a step for unit testing. This only builds the test executable 70 // but does not run it. 71 const unit_tests = b.addTest(.{ 72 .root_source_file = b.path("src/main.zig"), 73 .target = target, 74 .optimize = optimize, 75 }); 76 unit_tests.root_module.addImport("mustache", mustache.module("mustache")); 77 78 const run_unit_tests = b.addRunArtifact(unit_tests); 79 80 // Similar to creating the run step earlier, this exposes a `test` step to 81 // the `zig build --help` menu, providing a way for the user to request 82 // running the unit tests. 83 const test_step = b.step("test", "Run unit tests"); 84 test_step.dependOn(&run_unit_tests.step); 85 }