-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
229 lines (210 loc) · 9.86 KB
/
build.zig
File metadata and controls
229 lines (210 loc) · 9.86 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
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const strip = b.option(bool, "strip", "Strip debug info from binaries") orelse (optimize != .Debug);
const app_version = b.option([]const u8, "version", "Application version") orelse "0.0.1-dev";
const build_options = b.addOptions();
build_options.addOption([]const u8, "version", app_version);
build_options.addOption([]const u8, "embedded_zi_doc_lua", readBuildFile(b, "lua/zi/doc.lua"));
const self_docs_generated = generateSelfDocsModule(b);
const generate_models = b.addExecutable(.{
.name = "generate-models",
.root_module = b.createModule(.{
.root_source_file = b.path("scripts/generate-models.zig"),
.target = b.graph.host,
.optimize = .Debug,
}),
});
const generate_models_run = b.addRunArtifact(generate_models);
generate_models_run.stdio = .inherit;
generate_models_run.addArg(b.pathFromRoot("src/ai/models_generated.zig"));
b.step("generate-models", "Generate AI model catalog").dependOn(&generate_models_run.step);
const exe_mod = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.strip = strip,
});
exe_mod.addOptions("build_options", build_options);
exe_mod.addImport("self_docs_embedded", b.createModule(.{ .root_source_file = self_docs_generated }));
exe_mod.addImport("mdman_parser", b.createModule(.{ .root_source_file = b.path("website/mdman/parser.zig") }));
const lua_dep = b.dependency("lua", .{});
const lua_lib = b.addLibrary(.{
.name = "lua",
.linkage = .static,
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.strip = strip,
.link_libc = true,
}),
});
const lua_c_sources = [_][]const u8{
"src/lapi.c", "src/lauxlib.c", "src/lbaselib.c", "src/lcode.c",
"src/lcorolib.c", "src/lctype.c", "src/ldblib.c", "src/ldebug.c",
"src/ldo.c", "src/ldump.c", "src/lfunc.c", "src/lgc.c",
"src/linit.c", "src/liolib.c", "src/llex.c", "src/lmathlib.c",
"src/lmem.c", "src/loadlib.c", "src/lobject.c", "src/lopcodes.c",
"src/loslib.c", "src/lparser.c", "src/lstate.c", "src/lstring.c",
"src/lstrlib.c", "src/ltable.c", "src/ltablib.c", "src/ltm.c",
"src/lundump.c", "src/lutf8lib.c", "src/lvm.c", "src/lzio.c",
};
const lua_cflags = [_][]const u8{
"-std=gnu99",
"-DLUA_USE_POSIX",
"-DLUA_USE_DLOPEN",
"-fno-sanitize=undefined",
};
lua_lib.root_module.addCSourceFiles(.{
.root = lua_dep.path("."),
.files = &lua_c_sources,
.flags = &lua_cflags,
});
lua_lib.root_module.addIncludePath(lua_dep.path("src"));
lua_lib.installHeadersDirectory(lua_dep.path("src"), "", .{
.include_extensions = &.{".h"},
});
const env_mod = b.createModule(.{ .root_source_file = b.path("src/env.zig") });
exe_mod.addImport("env", env_mod);
exe_mod.addIncludePath(lua_dep.path("src"));
exe_mod.linkLibrary(lua_lib);
if (target.result.os.tag == .macos) {
exe_mod.addCSourceFile(.{ .file = b.path("src/tui/terminal/clipboard_macos.m"), .flags = &.{"-fobjc-arc"} });
exe_mod.linkFramework("AppKit", .{});
exe_mod.linkFramework("Foundation", .{});
}
const exe = b.addExecutable(.{
.name = "zi",
.root_module = exe_mod,
});
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| run_cmd.addArgs(args);
b.step("run", "Run zi").dependOn(&run_cmd.step);
const test_mod = b.createModule(.{
.root_source_file = b.path("src/test_root.zig"),
.target = target,
.optimize = optimize,
.strip = strip,
});
test_mod.addOptions("build_options", build_options);
test_mod.addImport("self_docs_embedded", b.createModule(.{ .root_source_file = self_docs_generated }));
test_mod.addImport("mdman_parser", b.createModule(.{ .root_source_file = b.path("website/mdman/parser.zig") }));
test_mod.addImport("env", env_mod);
test_mod.addIncludePath(lua_dep.path("src"));
test_mod.linkLibrary(lua_lib);
if (target.result.os.tag == .macos) {
test_mod.addCSourceFile(.{ .file = b.path("src/tui/terminal/clipboard_macos.m"), .flags = &.{"-fobjc-arc"} });
test_mod.linkFramework("AppKit", .{});
test_mod.linkFramework("Foundation", .{});
}
const tests = b.addTest(.{ .root_module = test_mod });
const test_step = b.step("test", "Run all tests");
test_step.dependOn(&b.addRunArtifact(tests).step);
}
const DocMeta = struct {
path: []const u8,
slug: []const u8,
title: []const u8,
order: i32,
aliases: []const []const u8,
body: []const u8,
};
fn generateSelfDocsModule(b: *std.Build) std.Build.LazyPath {
const allocator = b.allocator;
const docs_dir = b.pathFromRoot("website/docs/man");
var dir = std.Io.Dir.cwd().openDir(std.Options.debug_io, docs_dir, .{ .iterate = true }) catch @panic("open website/docs/man failed");
defer dir.close(std.Options.debug_io);
var metas: std.ArrayList(DocMeta) = .empty;
var walker = dir.iterate();
while (walker.next(std.Options.debug_io) catch @panic("iterate website/docs/man failed")) |entry| {
if (entry.kind != .file or !std.mem.endsWith(u8, entry.name, ".md")) continue;
const rel = b.fmt("website/docs/man/{s}", .{entry.name});
const abs = b.pathFromRoot(rel);
const content = std.Io.Dir.cwd().readFileAlloc(std.Options.debug_io, abs, allocator, .limited(1024 * 1024)) catch @panic("read self doc failed");
const meta = parseDocMeta(allocator, rel, content) catch @panic("parse self doc frontmatter failed");
metas.append(allocator, meta) catch @panic("append self doc meta failed");
}
std.mem.sort(DocMeta, metas.items, {}, struct {
fn lessThan(_: void, a: DocMeta, rhs: DocMeta) bool {
if (a.order == rhs.order) return std.mem.lessThan(u8, a.slug, rhs.slug);
return a.order < rhs.order;
}
}.lessThan);
var out: std.Io.Writer.Allocating = .init(allocator);
const w = &out.writer;
w.writeAll("pub const Doc = struct { slug: []const u8, title: []const u8, order: i32, path: []const u8, aliases: []const []const u8, body: []const u8 };\n\n") catch @panic("write self docs failed");
w.writeAll("pub const docs = [_]Doc{\n") catch @panic("write self docs failed");
for (metas.items) |meta| {
w.print(" .{{ .slug = ", .{}) catch unreachable;
writeZigString(w, meta.slug) catch unreachable;
w.print(", .title = ", .{}) catch unreachable;
writeZigString(w, meta.title) catch unreachable;
w.print(", .order = {d}, .path = ", .{meta.order}) catch unreachable;
writeZigString(w, meta.path) catch unreachable;
w.writeAll(", .aliases = &.{") catch unreachable;
for (meta.aliases, 0..) |alias, i| {
if (i > 0) w.writeAll(", ") catch unreachable;
writeZigString(w, alias) catch unreachable;
}
w.writeAll("}, .body = ") catch unreachable;
writeZigString(w, meta.body) catch unreachable;
w.writeAll(" },\n") catch unreachable;
}
w.writeAll("};\n") catch @panic("write self docs failed");
const wf = b.addWriteFiles();
return wf.add("self_docs_embedded.zig", out.written());
}
fn readBuildFile(b: *std.Build, rel_path: []const u8) []const u8 {
return std.Io.Dir.cwd().readFileAlloc(std.Options.debug_io, b.pathFromRoot(rel_path), b.allocator, .limited(1024 * 1024)) catch @panic("read build file failed");
}
fn parseDocMeta(allocator: std.mem.Allocator, path: []const u8, content: []const u8) !DocMeta {
if (!std.mem.startsWith(u8, content, "---\n")) return error.MissingFrontmatter;
var lines = std.mem.splitScalar(u8, content, '\n');
_ = lines.next();
var slug: ?[]const u8 = null;
var title: ?[]const u8 = null;
var order: ?i32 = null;
var aliases: std.ArrayList([]const u8) = .empty;
var in_aliases = false;
while (lines.next()) |raw_line| {
const line = std.mem.trim(u8, raw_line, "\r");
if (std.mem.eql(u8, line, "---")) break;
if (std.mem.startsWith(u8, line, " - ") and in_aliases) {
try aliases.append(allocator, try allocator.dupe(u8, std.mem.trim(u8, line[4..], " \t")));
continue;
}
in_aliases = false;
if (std.mem.startsWith(u8, line, "slug:")) {
slug = try allocator.dupe(u8, std.mem.trim(u8, line[5..], " \t"));
} else if (std.mem.startsWith(u8, line, "title:")) {
title = try allocator.dupe(u8, std.mem.trim(u8, line[6..], " \t"));
} else if (std.mem.startsWith(u8, line, "order:")) {
order = try std.fmt.parseInt(i32, std.mem.trim(u8, line[6..], " \t"), 10);
} else if (std.mem.eql(u8, line, "aliases:")) {
in_aliases = true;
}
}
return .{
.path = try allocator.dupe(u8, path),
.slug = slug orelse return error.MissingSlug,
.title = title orelse return error.MissingTitle,
.order = order orelse return error.MissingOrder,
.aliases = try aliases.toOwnedSlice(allocator),
.body = try allocator.dupe(u8, content),
};
}
fn writeZigString(writer: anytype, value: []const u8) !void {
try writer.writeByte('"');
for (value) |c| switch (c) {
'\\' => try writer.writeAll("\\\\"),
'"' => try writer.writeAll("\\\""),
'\n' => try writer.writeAll("\\n"),
'\r' => try writer.writeAll("\\r"),
'\t' => try writer.writeAll("\\t"),
else => try writer.writeByte(c),
};
try writer.writeByte('"');
}