build.zig 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. // This creates a "module", which represents a collection of source files alongside
  16. // some compilation options, such as optimization mode and linked system libraries.
  17. // Every executable or library we compile will be based on one or more modules.
  18. const lib_mod = b.createModule(.{
  19. // `root_source_file` is the Zig "entry point" of the module. If a module
  20. // only contains e.g. external object files, you can make this `null`.
  21. // In this case the main source file is merely a path, however, in more
  22. // complicated build scripts, this could be a generated file.
  23. .root_source_file = b.path("src/berlisp.zig"),
  24. .target = target,
  25. .optimize = optimize,
  26. });
  27. // We will also create a module for our other entry point, 'main.zig'.
  28. const exe_mod = b.createModule(.{
  29. // `root_source_file` is the Zig "entry point" of the module. If a module
  30. // only contains e.g. external object files, you can make this `null`.
  31. // In this case the main source file is merely a path, however, in more
  32. // complicated build scripts, this could be a generated file.
  33. .root_source_file = b.path("src/main.zig"),
  34. .target = target,
  35. .optimize = optimize,
  36. });
  37. // Modules can depend on one another using the `std.Build.Module.addImport` function.
  38. // This is what allows Zig source code to use `@import("foo")` where 'foo' is not a
  39. // file path. In this case, we set up `exe_mod` to import `lib_mod`.
  40. exe_mod.addImport("berlisp_lib", lib_mod);
  41. // Now, we will create a static library based on the module we created above.
  42. // This creates a `std.Build.Step.Compile`, which is the build step responsible
  43. // for actually invoking the compiler.
  44. const lib = b.addLibrary(.{
  45. .linkage = .static,
  46. .name = "z",
  47. .root_module = lib_mod,
  48. });
  49. // This declares intent for the library to be installed into the standard
  50. // location when the user invokes the "install" step (the default step when
  51. // running `zig build`).
  52. b.installArtifact(lib);
  53. // This creates another `std.Build.Step.Compile`, but this one builds an executable
  54. // rather than a static library.
  55. const exe = b.addExecutable(.{
  56. .name = "berlisp",
  57. .root_module = exe_mod,
  58. });
  59. // This declares intent for the executable to be installed into the
  60. // standard location when the user invokes the "install" step (the default
  61. // step when running `zig build`).
  62. b.installArtifact(exe);
  63. // This *creates* a Run step in the build graph, to be executed when another
  64. // step is evaluated that depends on it. The next line below will establish
  65. // such a dependency.
  66. const run_cmd = b.addRunArtifact(exe);
  67. // By making the run step depend on the install step, it will be run from the
  68. // installation directory rather than directly from within the cache directory.
  69. // This is not necessary, however, if the application depends on other installed
  70. // files, this ensures they will be present and in the expected location.
  71. run_cmd.step.dependOn(b.getInstallStep());
  72. // This allows the user to pass arguments to the application in the build
  73. // command itself, like this: `zig build run -- arg1 arg2 etc`
  74. if (b.args) |args| {
  75. run_cmd.addArgs(args);
  76. }
  77. // This creates a build step. It will be visible in the `zig build --help` menu,
  78. // and can be selected like this: `zig build run`
  79. // This will evaluate the `run` step rather than the default, which is "install".
  80. const run_step = b.step("run", "Run the app");
  81. run_step.dependOn(&run_cmd.step);
  82. // Creates a step for unit testing. This only builds the test executable
  83. // but does not run it.
  84. const lib_unit_tests = b.addTest(.{
  85. .root_module = lib_mod,
  86. });
  87. const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
  88. const exe_unit_tests = b.addTest(.{
  89. .root_module = exe_mod,
  90. });
  91. const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
  92. // Similar to creating the run step earlier, this exposes a `test` step to
  93. // the `zig build --help` menu, providing a way for the user to request
  94. // running the unit tests.
  95. const test_step = b.step("test", "Run unit tests");
  96. test_step.dependOn(&run_lib_unit_tests.step);
  97. test_step.dependOn(&run_exe_unit_tests.step);
  98. const test_exe = b.addTest(.{
  99. .name = "unit_tests",
  100. .root_source_file = b.path("src/main.zig"),
  101. });
  102. b.installArtifact(test_exe);
  103. const zg = b.dependency("zg", .{});
  104. lib_mod.addImport("code_point", zg.module("code_point"));
  105. lib_mod.addImport("PropsData", zg.module("PropsData"));
  106. const mecha = b.dependency("mecha", .{});
  107. exe.root_module.addImport("mecha", mecha.module("mecha"));
  108. }