build.zig 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 lib = b.addStaticLibrary(.{
  16. .name = "sdl-window-test",
  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 = b.path("src/root.zig"),
  20. .target = target,
  21. .optimize = optimize,
  22. });
  23. // This declares intent for the library to be installed into the standard
  24. // location when the user invokes the "install" step (the default step when
  25. // running `zig build`).
  26. b.installArtifact(lib);
  27. const exe = b.addExecutable(.{
  28. .name = "sdl-window-test",
  29. .root_source_file = b.path("src/main.zig"),
  30. .target = target,
  31. .optimize = optimize,
  32. });
  33. if (target.result.os.tag == .linux) {
  34. // The SDL package doesn't work for Linux yet, so we rely on system
  35. // packages for now.
  36. exe.linkSystemLibrary("SDL2");
  37. exe.linkLibC();
  38. } else {
  39. const sdl_dep = b.dependency("SDL", .{
  40. .optimize = .ReleaseFast,
  41. .target = target,
  42. });
  43. exe.linkLibrary(sdl_dep.artifact("SDL2"));
  44. }
  45. // This declares intent for the executable to be installed into the
  46. // standard location when the user invokes the "install" step (the default
  47. // step when running `zig build`).
  48. b.installArtifact(exe);
  49. // This *creates* a Run step in the build graph, to be executed when another
  50. // step is evaluated that depends on it. The next line below will establish
  51. // such a dependency.
  52. const run_cmd = b.addRunArtifact(exe);
  53. // By making the run step depend on the install step, it will be run from the
  54. // installation directory rather than directly from within the cache directory.
  55. // This is not necessary, however, if the application depends on other installed
  56. // files, this ensures they will be present and in the expected location.
  57. run_cmd.step.dependOn(b.getInstallStep());
  58. // This allows the user to pass arguments to the application in the build
  59. // command itself, like this: `zig build run -- arg1 arg2 etc`
  60. if (b.args) |args| {
  61. run_cmd.addArgs(args);
  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. // Creates a step for unit testing. This only builds the test executable
  69. // but does not run it.
  70. const lib_unit_tests = b.addTest(.{
  71. .root_source_file = b.path("src/root.zig"),
  72. .target = target,
  73. .optimize = optimize,
  74. });
  75. const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
  76. const exe_unit_tests = b.addTest(.{
  77. .root_source_file = b.path("src/main.zig"),
  78. .target = target,
  79. .optimize = optimize,
  80. });
  81. const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
  82. // Similar to creating the run step earlier, this exposes a `test` step to
  83. // the `zig build --help` menu, providing a way for the user to request
  84. // running the unit tests.
  85. const test_step = b.step("test", "Run unit tests");
  86. test_step.dependOn(&run_lib_unit_tests.step);
  87. test_step.dependOn(&run_exe_unit_tests.step);
  88. }