|
1 | 1 | const std = @import("std"); |
2 | | -const ModuleMap = std.StringArrayHashMap(*std.Build.Module); |
3 | | -var gpa = std.heap.GeneralPurposeAllocator(.{}){}; |
4 | | -const allocator = gpa.allocator(); |
5 | 2 |
|
6 | | -// Although this function looks imperative, note that its job is to |
7 | | -// declaratively construct a build graph that will be executed by an external |
8 | | -// runner. |
9 | | -pub fn build(b: *std.Build) !void { |
10 | | - // Standard target options allows the person running `zig build` to choose |
11 | | - // what target to build for. Here we do not override the defaults, which |
12 | | - // means any target is allowed, and the default is native. Other options |
13 | | - // for restricting supported target set are available. |
| 3 | +pub fn build(b: *std.Build) void { |
14 | 4 | const target = b.standardTargetOptions(.{}); |
15 | | - |
16 | | - // Standard optimization options allow the person running `zig build` to select |
17 | | - // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do nots |
18 | | - // set a preferred release mode, allowing the user to decide how to optimize. |
19 | 5 | const optimize = b.standardOptimizeOption(.{}); |
20 | 6 |
|
21 | | - const dep_opts = .{ .target = target, .optimize = optimize }; |
22 | | - |
23 | | - const exe = b.addExecutable(.{ |
24 | | - .name = "zap", |
25 | | - // In this case the main source file is merely a path, however, in more |
26 | | - // complicated build scripts, this could be a generated file. |
27 | | - .root_source_file = b.path("src/main.zig"), |
| 7 | + const zap = b.dependency("zap", .{ |
28 | 8 | .target = target, |
29 | 9 | .optimize = optimize, |
| 10 | + .openssl = false, |
30 | 11 | }); |
31 | 12 |
|
32 | | - const zap = b.dependency("zap", .{ |
| 13 | + const pg = b.dependency("pg", .{ |
33 | 14 | .target = target, |
34 | 15 | .optimize = optimize, |
35 | | - .openssl = false, // set to true to enable TLS support |
36 | 16 | }); |
37 | 17 |
|
38 | | - var modules = ModuleMap.init(allocator); |
39 | | - defer modules.deinit(); |
40 | | - |
41 | | - const zap_module = b.dependency("zap", dep_opts).module("zap"); |
42 | | - const pg_module = b.dependency("pg", dep_opts).module("pg"); |
43 | | - |
44 | | - try modules.put("zap", zap_module); |
45 | | - try modules.put("pg", pg_module); |
46 | | - |
47 | | - // // Expose this as a module that others can import |
48 | | - exe.root_module.addImport("zap", zap_module); |
49 | | - exe.root_module.addImport("pg", pg_module); |
50 | | - |
51 | | - exe.linkLibrary(zap.artifact("facil.io")); |
| 18 | + const exe = b.addExecutable(.{ |
| 19 | + .name = "zap", |
| 20 | + .root_module = b.createModule(.{ |
| 21 | + .root_source_file = b.path("src/main.zig"), |
| 22 | + .target = target, |
| 23 | + .optimize = optimize, |
| 24 | + .imports = &.{ |
| 25 | + .{ .name = "zap", .module = zap.module("zap") }, |
| 26 | + .{ .name = "pg", .module = pg.module("pg") }, |
| 27 | + }, |
| 28 | + }), |
| 29 | + }); |
52 | 30 |
|
53 | | - // This declares intent for the executable to be installed into the |
54 | | - // standard location when the user invokes the "install" step (the default |
55 | | - // step when running `zig build`). |
56 | 31 | b.installArtifact(exe); |
57 | 32 |
|
58 | | - // This *creates* a Run step in the build graph, to be executed when another |
59 | | - // step is evaluated that depends on it. The next line below will establish |
60 | | - // such a dependency. |
61 | 33 | const run_cmd = b.addRunArtifact(exe); |
62 | | - |
63 | | - // By making the run step depend on the install step, it will be run from the |
64 | | - // installation directory rather than directly from within the cache directory. |
65 | | - // This is not necessary, however, if the application depends on other installed |
66 | | - // files, this ensures they will be present and in the expected location. |
67 | 34 | run_cmd.step.dependOn(b.getInstallStep()); |
68 | | - |
69 | | - // This allows the user to pass arguments to the application in the build |
70 | | - // command itself, like this: `zig build run -- arg1 arg2 etc` |
71 | 35 | if (b.args) |args| { |
72 | 36 | run_cmd.addArgs(args); |
73 | 37 | } |
74 | 38 |
|
75 | | - // This creates a build step. It will be visible in the `zig build --help` menu, |
76 | | - // and can be selected like this: `zig build run` |
77 | | - // This will evaluate the `run` step rather than the default, which is "install". |
78 | 39 | const run_step = b.step("run", "Run the app"); |
79 | 40 | run_step.dependOn(&run_cmd.step); |
80 | 41 | } |
0 commit comments