build.zig 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. const std = @import("std");
  2. // Although this function looks imperative, note that its job is to
  3. // declaratively construct a build graph that will be executed by an external
  4. // runner.
  5. pub fn build(b: *std.Build) void {
  6. // Standard target options allows the person running `zig build` to choose
  7. // what target to build for. Here we do not override the defaults, which
  8. // means any target is allowed, and the default is native. Other options
  9. // for restricting supported target set are available.
  10. const target = b.standardTargetOptions(.{});
  11. // Standard optimization options allow the person running `zig build` to select
  12. // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
  13. // set a preferred release mode, allowing the user to decide how to optimize.
  14. const optimize = b.standardOptimizeOption(.{});
  15. const exe = b.addExecutable(.{
  16. .name = "hello",
  17. // In this case the main source file is merely a path, however, in more
  18. // complicated build scripts, this could be a generated file.
  19. .root_source_file = .{ .path = "src/main.zig" },
  20. .target = target,
  21. .optimize = optimize,
  22. });
  23. // This declares intent for the executable to be installed into the
  24. // standard location when the user invokes the "install" step (the default
  25. // step when running `zig build`).
  26. b.installArtifact(exe);
  27. // This *creates* a Run step in the build graph, to be executed when another
  28. // step is evaluated that depends on it. The next line below will establish
  29. // such a dependency.
  30. const run_cmd = b.addRunArtifact(exe);
  31. // By making the run step depend on the install step, it will be run from the
  32. // installation directory rather than directly from within the cache directory.
  33. // This is not necessary, however, if the application depends on other installed
  34. // files, this ensures they will be present and in the expected location.
  35. run_cmd.step.dependOn(b.getInstallStep());
  36. // This allows the user to pass arguments to the application in the build
  37. // command itself, like this: `zig build run -- arg1 arg2 etc`
  38. if (b.args) |args| {
  39. run_cmd.addArgs(args);
  40. }
  41. // This creates a build step. It will be visible in the `zig build --help` menu,
  42. // and can be selected like this: `zig build run`
  43. // This will evaluate the `run` step rather than the default, which is "install".
  44. const run_step = b.step("run", "Run the app");
  45. run_step.dependOn(&run_cmd.step);
  46. // Creates a step for unit testing. This only builds the test executable
  47. // but does not run it.
  48. const unit_tests = b.addTest(.{
  49. .root_source_file = .{ .path = "src/main.zig" },
  50. .target = target,
  51. .optimize = optimize,
  52. });
  53. const run_unit_tests = b.addRunArtifact(unit_tests);
  54. // Similar to creating the run step earlier, this exposes a `test` step to
  55. // the `zig build --help` menu, providing a way for the user to request
  56. // running the unit tests.
  57. const test_step = b.step("test", "Run unit tests");
  58. test_step.dependOn(&run_unit_tests.step);
  59. }