Repository navigation
Expand file tree
/
Copy pathbuild.zig
More file actions
242 lines (224 loc) · 10.6 KB
/
Copy pathbuild.zig
File metadata and controls
242 lines (224 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const websocket_vendor_mod = b.createModule(.{
.root_source_file = b.path("vendor/websocket.zig/src/websocket.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
});
const bounded_queue_mod = b.createModule(.{
.root_source_file = b.path("lib/bounded_queue.zig"),
.target = target,
.optimize = optimize,
});
{
const options = b.addOptions();
options.addOption(bool, "websocket_blocking", false);
websocket_vendor_mod.addOptions("build", options);
}
const websocket_mod = b.createModule(.{
.root_source_file = b.path("lib/websocket.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "websocket_vendor", .module = websocket_vendor_mod },
},
});
const logger_mod = b.createModule(.{
.root_source_file = b.path("lib/logger.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "bounded_queue", .module = bounded_queue_mod },
},
});
const dynlib_mod = b.createModule(.{
.root_source_file = b.path("lib/dynlib.zig"),
.target = target,
.optimize = optimize,
});
const counting_allocator_mod = b.createModule(.{
.root_source_file = b.path("lib/counting_allocator.zig"),
.target = target,
.optimize = optimize,
});
const translate_c = b.addTranslateC(.{
.root_source_file = b.path("src/c.h"),
.target = target,
.optimize = optimize,
});
translate_c.addIncludePath(b.path("vendor/fff"));
translate_c.addIncludePath(b.path("vendor/sqlite"));
const c_mod = translate_c.createModule();
// Generate the models.dev context-window catalogue at build time. A small
// host tool reads the vendored snapshot and emits a static Zig table that
// `compaction.zig` imports as `model_catalog`. Keeps accurate per-model
// context windows in the binary without a runtime JSON parse; refresh by
// re-curling vendor/models.dev/models.json and rebuilding.
const model_catalog_gen = b.addExecutable(.{
.name = "gen-model-catalog",
.root_module = b.createModule(.{
.root_source_file = b.path("tools/gen_model_catalog.zig"),
.target = b.graph.host,
.optimize = .Debug,
}),
});
const model_catalog_run = b.addRunArtifact(model_catalog_gen);
model_catalog_run.addFileArg(b.path("vendor/models.dev/models.json"));
const model_catalog_zig = model_catalog_run.addOutputFileArg("model_catalog.zig");
const model_catalog_mod = b.createModule(.{
.root_source_file = model_catalog_zig,
.target = target,
.optimize = optimize,
});
const mod = b.addModule("nova", .{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "bounded_queue", .module = bounded_queue_mod },
.{ .name = "websocket", .module = websocket_mod },
.{ .name = "logger", .module = logger_mod },
.{ .name = "dynlib", .module = dynlib_mod },
.{ .name = "counting_allocator", .module = counting_allocator_mod },
.{ .name = "c", .module = c_mod },
.{ .name = "model_catalog", .module = model_catalog_mod },
},
});
mod.link_libc = true;
// Native keychain backends (src/keyring.zig): Windows Credential Manager
// lives in advapi32; macOS Keychain Services needs Security (+ CoreFoundation
// for CFRelease). Other targets use the plaintext file fallback.
switch (target.result.os.tag) {
.windows => mod.linkSystemLibrary("advapi32", .{}),
.macos => {
mod.linkFramework("Security", .{});
mod.linkFramework("CoreFoundation", .{});
},
else => {},
}
mod.addCSourceFile(.{
.file = b.path("vendor/sqlite/sqlite3.c"),
.flags = &.{
"-DSQLITE_THREADSAFE=2",
"-DSQLITE_DEFAULT_FOREIGN_KEYS=1",
"-DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1",
"-DSQLITE_OMIT_DEPRECATED",
"-DSQLITE_OMIT_LOAD_EXTENSION",
"-DSQLITE_DQS=0",
"-DSQLITE_USE_URI=1",
// JSON1 and FTS5 are deliberately absent: the session store keeps
// payloads as opaque text and never queries into them, so enabling
// either only added compile time and binary size. Re-add the flag if a
// query ever needs `json_*` or a full-text index.
"-Wno-implicit-function-declaration",
"-Wno-unused-but-set-variable",
},
});
// If neither case applies to you, feel free to delete the declaration you
// don't need and to put everything under a single module.
const exe = b.addExecutable(.{
.name = "nova",
.root_module = b.createModule(.{
// b.createModule defines a new module just like b.addModule but,
// unlike b.addModule, it does not expose the module to consumers of
// this package, which is why in this case we don't have to give it a name.
.root_source_file = b.path("src/main.zig"),
// Target and optimization levels must be explicitly wired in when
// defining an executable or library (in the root module), and you
// can also hardcode a specific target for an executable or library
// definition if desireable (e.g. firmware for embedded devices).
.target = target,
.optimize = optimize,
// List of modules available for import in source files part of the
// root module.
.imports = &.{
// Here "nova" is the name you will use in your source code to
// import this module (e.g. `@import("nova")`). The name is
// repeated because you are allowed to rename your imports, which
// can be extremely useful in case of collisions (which can happen
// importing modules from different packages).
.{ .name = "nova", .module = mod },
.{ .name = "websocket", .module = websocket_mod },
.{ .name = "logger", .module = logger_mod },
.{ .name = "dynlib", .module = dynlib_mod },
},
}),
});
// This declares intent for the executable to be installed into the
// install prefix when running `zig build` (i.e. when executing the default
// step). By default the install prefix is `zig-out/` but can be overridden
// by passing `--prefix` or `-p`.
b.installArtifact(exe);
// This creates a top level step. Top level steps have a name and can be
// invoked by name when running `zig build` (e.g. `zig build run`).
// This will evaluate the `run` step rather than the default step.
// For a top level step to actually do something, it must depend on other
// steps (e.g. a Run step, as we will see in a moment).
const run_step = b.step("run", "Run the app");
// This creates a RunArtifact step in the build graph. A RunArtifact step
// invokes an executable compiled by Zig. Steps will only be executed by the
// runner if invoked directly by the user (in the case of top level steps)
// or if another step depends on it, so it's up to you to define when and
// how this Run step will be executed. In our case we want to run it when
// the user runs `zig build run`, so we create a dependency link.
const run_cmd = b.addRunArtifact(exe);
run_step.dependOn(&run_cmd.step);
// By making the run step depend on the default step, it will be run from the
// installation directory rather than directly from within the cache directory.
run_cmd.step.dependOn(b.getInstallStep());
// This allows the user to pass arguments to the application in the build
// command itself, like this: `zig build run -- arg1 arg2 etc`
if (b.args) |args| {
run_cmd.addArgs(args);
}
// Creates an executable that will run `test` blocks from the provided module.
// Here `mod` needs to define a target, which is why earlier we made sure to
// set the releative field.
// Optional substring filter: `zig build test -Dtest-filter=checkpoint`
// compiles and runs only tests whose fully-qualified name contains it.
const test_filter = b.option([]const u8, "test-filter", "Only run tests whose name contains this substring");
const test_filters: []const []const u8 = if (test_filter) |f| &.{f} else &.{};
const mod_tests = b.addTest(.{
.root_module = mod,
.filters = test_filters,
});
// A run step that will run the test executable.
const run_mod_tests = b.addRunArtifact(mod_tests);
// Creates an executable that will run `test` blocks from the executable's
// root module. Note that test executables only test one module at a time,
// hence why we have to create two separate ones.
const exe_tests = b.addTest(.{
.root_module = exe.root_module,
.filters = test_filters,
});
// A run step that will run the second test executable.
const run_exe_tests = b.addRunArtifact(exe_tests);
// A top level step for running all tests. dependOn can be called multiple
// times and since the two run steps do not depend on one another, this will
// make the two of them run in parallel.
const test_step = b.step("test", "Run tests");
test_step.dependOn(&run_mod_tests.step);
test_step.dependOn(&run_exe_tests.step);
for ([_]*std.Build.Module{
bounded_queue_mod,
logger_mod,
websocket_mod,
}) |lib_mod| {
const lib_tests = b.addTest(.{ .root_module = lib_mod, .filters = test_filters });
test_step.dependOn(&b.addRunArtifact(lib_tests).step);
}
// Just like flags, top level steps are also listed in the `--help` menu.
//
// The Zig build system is entirely implemented in userland, which means
// that it cannot hook into private compiler APIs. All compilation work
// orchestrated by the build system will result in other Zig compiler
// subcommands being invoked with the right flags defined. You can observe
// these invocations when one fails (or you pass a flag to increase
// verbosity) to validate assumptions and diagnose problems.
//
// Lastly, the Zig build system is relatively simple and self-contained,
// and reading its source code will allow you to master it.
}