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