tools

various tools I have been using throughout the years
Log | Files | Refs | README | LICENSE

build.zig (2907B)


      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 exe = b.addExecutable(.{
     19         .name = "pracomer",
     20         .root_module = b.createModule(.{
     21             .root_source_file = b.path("src/main.zig"),
     22             .target = target,
     23             .optimize = optimize,
     24         }),
     25     });
     26 
     27     // This declares intent for the executable to be installed into the
     28     // standard location when the user invokes the "install" step (the default
     29     // step when running `zig build`).
     30     b.installArtifact(exe);
     31 
     32     // This *creates* a Run step in the build graph, to be executed when another
     33     // step is evaluated that depends on it. The next line below will establish
     34     // such a dependency.
     35     const run_cmd = b.addRunArtifact(exe);
     36 
     37     // By making the run step depend on the install step, it will be run from the
     38     // installation directory rather than directly from within the cache directory.
     39     // This is not necessary, however, if the application depends on other installed
     40     // files, this ensures they will be present and in the expected location.
     41     run_cmd.step.dependOn(b.getInstallStep());
     42 
     43     // This allows the user to pass arguments to the application in the build
     44     // command itself, like this: `zig build run -- arg1 arg2 etc`
     45     if (b.args) |args| {
     46         run_cmd.addArgs(args);
     47     }
     48 
     49     // This creates a build step. It will be visible in the `zig build --help` menu,
     50     // and can be selected like this: `zig build run`
     51     // This will evaluate the `run` step rather than the default, which is "install".
     52     const run_step = b.step("run", "Run the app");
     53     run_step.dependOn(&run_cmd.step);
     54 
     55     const exe_unit_tests = b.addTest(.{
     56         .root_module = exe.root_module,
     57     });
     58 
     59     const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
     60 
     61     // Similar to creating the run step earlier, this exposes a `test` step to
     62     // the `zig build --help` menu, providing a way for the user to request
     63     // running the unit tests.
     64     const test_step = b.step("test", "Run unit tests");
     65     test_step.dependOn(&run_exe_unit_tests.step);
     66 }