]> gitweb.ps.run Git - ziglmdb/blob - build.zig
refactor
[ziglmdb] / build.zig
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 = "lmdb",
20         // In this case the main source file is merely a path, however, in more
21         // complicated build scripts, this could be a generated file.
22         .root_source_file = .{ .cwd_relative = "src/main.zig" },
23         .target = target,
24         .optimize = optimize,
25     });
26
27     exe.addIncludePath(.{ .cwd_relative = "./lmdb/libraries/liblmdb" });
28
29     exe.addCSourceFiles(.{ .files = &.{
30         "./lmdb/libraries/liblmdb/midl.c",
31         "./lmdb/libraries/liblmdb/mdb.c",
32     } });
33
34     exe.linkLibC();
35
36     // This declares intent for the executable to be installed into the
37     // standard location when the user invokes the "install" step (the default
38     // step when running `zig build`).
39     b.installArtifact(exe);
40
41     // This *creates* a Run step in the build graph, to be executed when another
42     // step is evaluated that depends on it. The next line below will establish
43     // such a dependency.
44     const run_cmd = b.addRunArtifact(exe);
45
46     // By making the run step depend on the install step, it will be run from the
47     // installation directory rather than directly from within the cache directory.
48     // This is not necessary, however, if the application depends on other installed
49     // files, this ensures they will be present and in the expected location.
50     run_cmd.step.dependOn(b.getInstallStep());
51
52     // This allows the user to pass arguments to the application in the build
53     // command itself, like this: `zig build run -- arg1 arg2 etc`
54     if (b.args) |args| {
55         run_cmd.addArgs(args);
56     }
57
58     // This creates a build step. It will be visible in the `zig build --help` menu,
59     // and can be selected like this: `zig build run`
60     // This will evaluate the `run` step rather than the default, which is "install".
61     const run_step = b.step("run", "Run the app");
62     run_step.dependOn(&run_cmd.step);
63
64     // Creates a step for unit testing. This only builds the test executable
65     // but does not run it.
66     const unit_tests = b.addTest(.{
67         .root_source_file = .{ .cwd_relative = "src/main.zig" },
68         .target = target,
69         .optimize = optimize,
70     });
71
72     unit_tests.addIncludePath(.{ .cwd_relative = "./lmdb/libraries/liblmdb" });
73
74     unit_tests.addCSourceFiles(.{ .files = &.{
75         "./lmdb/libraries/liblmdb/midl.c",
76         "./lmdb/libraries/liblmdb/mdb.c",
77     } });
78
79     unit_tests.linkLibC();
80
81     const test_bin = b.addInstallBinFile(unit_tests.getEmittedBin(), "./lmdb_test");
82
83     const run_unit_tests = b.addRunArtifact(unit_tests);
84
85     // Similar to creating the run step earlier, this exposes a `test` step to
86     // the `zig build --help` menu, providing a way for the user to request
87     // running the unit tests.
88     const test_step = b.step("test", "Run unit tests");
89     test_step.dependOn(&run_unit_tests.step);
90     test_step.dependOn(&unit_tests.step);
91     test_step.dependOn(&test_bin.step);
92 }