size: 6 KiB
| 1 | const std = @import("std"); |
| 2 | |
| 3 | // Although this function looks imperative, it does not perform the build |
| 4 | // directly and instead it mutates the build graph (`b`) that will be then |
| 5 | // executed by an external runner. The functions in `std.Build` implement a DSL |
| 6 | // for defining build steps and express dependencies between them, allowing the |
| 7 | // build runner to parallelize the build automatically (and the cache system to |
| 8 | // know when a step doesn't need to be re-run). |
| 9 | pub fn build(b: *std.Build) void { |
| 10 | // Standard target options allow the person running `zig build` to choose |
| 11 | // what target to build for. Here we do not override the defaults, which |
| 12 | // means any target is allowed, and the default is native. Other options |
| 13 | // for restricting supported target set are available. |
| 14 | const target = b.standardTargetOptions(.{}); |
| 15 | // Standard optimization options allow the person running `zig build` to select |
| 16 | // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not |
| 17 | // set a preferred release mode, allowing the user to decide how to optimize. |
| 18 | const optimize = b.standardOptimizeOption(.{}); |
| 19 | // It's also possible to define more custom flags to toggle optional features |
| 20 | // of this build script using `b.option()`. All defined flags (including |
| 21 | // target and optimize options) will be listed when running `zig build --help` |
| 22 | // in this directory. |
| 23 | |
| 24 | // Here we define an executable. An executable needs to have a root module |
| 25 | // which needs to expose a `main` function. While we could add a main function |
| 26 | // to the module defined above, it's sometimes preferable to split business |
| 27 | // logic and the CLI into two separate modules. |
| 28 | // |
| 29 | // If your goal is to create a Zig library for others to use, consider if |
| 30 | // it might benefit from also exposing a CLI tool. A parser library for a |
| 31 | // data serialization format could also bundle a CLI syntax checker, for example. |
| 32 | // |
| 33 | // If instead your goal is to create an executable, consider if users might |
| 34 | // be interested in also being able to embed the core functionality of your |
| 35 | // program in their own executable in order to avoid the overhead involved in |
| 36 | // subprocessing your CLI tool. |
| 37 | // |
| 38 | // If neither case applies to you, feel free to delete the declaration you |
| 39 | // don't need and to put everything under a single module. |
| 40 | const exe = b.addExecutable(.{ |
| 41 | .name = "khoe", |
| 42 | .root_module = b.createModule(.{ |
| 43 | // b.createModule defines a new module just like b.addModule but, |
| 44 | // unlike b.addModule, it does not expose the module to consumers of |
| 45 | // this package, which is why in this case we don't have to give it a name. |
| 46 | .root_source_file = b.path("src/main.zig"), |
| 47 | // Target and optimization levels must be explicitly wired in when |
| 48 | // defining an executable or library (in the root module), and you |
| 49 | // can also hardcode a specific target for an executable or library |
| 50 | // definition if desireable (e.g. firmware for embedded devices). |
| 51 | .target = target, |
| 52 | .optimize = optimize, |
| 53 | // List of modules available for import in source files part of the |
| 54 | // root module. |
| 55 | .imports = &.{}, |
| 56 | }), |
| 57 | }); |
| 58 | |
| 59 | // This declares intent for the executable to be installed into the |
| 60 | // install prefix when running `zig build` (i.e. when executing the default |
| 61 | // step). By default the install prefix is `zig-out/` but can be overridden |
| 62 | // by passing `--prefix` or `-p`. |
| 63 | b.installArtifact(exe); |
| 64 | |
| 65 | // This creates a top level step. Top level steps have a name and can be |
| 66 | // invoked by name when running `zig build` (e.g. `zig build run`). |
| 67 | // This will evaluate the `run` step rather than the default step. |
| 68 | // For a top level step to actually do something, it must depend on other |
| 69 | // steps (e.g. a Run step, as we will see in a moment). |
| 70 | const run_step = b.step("run", "Run the app"); |
| 71 | |
| 72 | // This creates a RunArtifact step in the build graph. A RunArtifact step |
| 73 | // invokes an executable compiled by Zig. Steps will only be executed by the |
| 74 | // runner if invoked directly by the user (in the case of top level steps) |
| 75 | // or if another step depends on it, so it's up to you to define when and |
| 76 | // how this Run step will be executed. In our case we want to run it when |
| 77 | // the user runs `zig build run`, so we create a dependency link. |
| 78 | const run_cmd = b.addRunArtifact(exe); |
| 79 | run_step.dependOn(&run_cmd.step); |
| 80 | |
| 81 | // By making the run step depend on the default step, it will be run from the |
| 82 | // installation directory rather than directly from within the cache directory. |
| 83 | run_cmd.step.dependOn(b.getInstallStep()); |
| 84 | |
| 85 | // This allows the user to pass arguments to the application in the build |
| 86 | // command itself, like this: `zig build run -- arg1 arg2 etc` |
| 87 | if (b.args) |args| { |
| 88 | run_cmd.addArgs(args); |
| 89 | } |
| 90 | |
| 91 | // Creates an executable that will run `test` blocks from the executable's |
| 92 | // root module. Note that test executables only test one module at a time, |
| 93 | // hence why we have to create two separate ones. |
| 94 | const exe_tests = b.addTest(.{ |
| 95 | .root_module = exe.root_module, |
| 96 | }); |
| 97 | |
| 98 | // A run step that will run the second test executable. |
| 99 | const run_exe_tests = b.addRunArtifact(exe_tests); |
| 100 | |
| 101 | // A top level step for running all tests. dependOn can be called multiple |
| 102 | // times and since the two run steps do not depend on one another, this will |
| 103 | // make the two of them run in parallel. |
| 104 | const test_step = b.step("test", "Run tests"); |
| 105 | test_step.dependOn(&run_exe_tests.step); |
| 106 | |
| 107 | // Just like flags, top level steps are also listed in the `--help` menu. |
| 108 | // |
| 109 | // The Zig build system is entirely implemented in userland, which means |
| 110 | // that it cannot hook into private compiler APIs. All compilation work |
| 111 | // orchestrated by the build system will result in other Zig compiler |
| 112 | // subcommands being invoked with the right flags defined. You can observe |
| 113 | // these invocations when one fails (or you pass a flag to increase |
| 114 | // verbosity) to validate assumptions and diagnose problems. |
| 115 | // |
| 116 | // Lastly, the Zig build system is relatively simple and self-contained, |
| 117 | // and reading its source code will allow you to master it. |
| 118 | } |