-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.zig
231 lines (195 loc) · 7.96 KB
/
build.zig
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
const std = @import("std");
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const ver = version(b);
const options = b.addOptions();
options.addOption([]const u8, "version", ver);
const verse_lib = b.addModule("verse", .{
.root_source_file = b.path("src/verse.zig"),
.target = target,
.optimize = optimize,
});
verse_lib.addOptions("build_options", options);
const lib_unit_tests = b.addTest(.{
.root_source_file = b.path("src/verse.zig"),
.target = target,
.optimize = optimize,
});
lib_unit_tests.root_module.addOptions("build_options", options);
var compiler = Compiler.init(b);
var comptime_structs: ?*std.Build.Module = null;
var comptime_templates: ?*std.Build.Module = null;
if (std.fs.cwd().access("src/fallback_html/index.html", .{})) {
compiler.addDir("src/fallback_html/");
compiler.addDir("examples/templates/");
compiler.collect() catch unreachable;
comptime_templates = compiler.buildTemplates() catch unreachable;
// Zig build time doesn't expose it's state in a way I know how to check...
// so we yolo it like python :D
lib_unit_tests.root_module.addImport("comptime_templates", comptime_templates orelse unreachable);
comptime_structs = compiler.buildStructs() catch unreachable;
lib_unit_tests.root_module.addImport("comptime_structs", comptime_structs orelse unreachable);
verse_lib.addImport("comptime_structs", comptime_structs orelse @panic("structs missing"));
verse_lib.addImport("comptime_templates", comptime_templates orelse @panic("structs missing"));
} else |_| {}
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_lib_unit_tests.step);
const examples = [_][]const u8{
"basic",
"cookies",
"template",
"endpoint",
"auth-cookie",
"request-userdata",
"api",
};
inline for (examples) |example| {
const example_exe = b.addExecutable(.{
.name = example,
.root_source_file = b.path("examples/" ++ example ++ ".zig"),
.target = target,
.optimize = optimize,
});
// All Examples should compile for tests to pass
test_step.dependOn(&example_exe.step);
example_exe.root_module.addImport("verse", verse_lib);
const run_example = b.addRunArtifact(example_exe);
run_example.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_example.addArgs(args);
}
const run_name = "run-" ++ example;
const run_description = "Run example: " ++ example;
const run_step = b.step(run_name, run_description);
run_step.dependOn(&run_example.step);
}
}
pub const Compiler = struct {
b: *std.Build,
dirs: std.ArrayList([]const u8),
files: std.ArrayList([]const u8),
collected: std.ArrayList([]const u8),
templates: ?*std.Build.Module = null,
structs: ?*std.Build.Module = null,
debugging: bool = false,
pub fn init(b: *std.Build) Compiler {
return .{
.b = b,
.dirs = std.ArrayList([]const u8).init(b.allocator),
.files = std.ArrayList([]const u8).init(b.allocator),
.collected = std.ArrayList([]const u8).init(b.allocator),
};
}
pub fn raze(self: Compiler) void {
for (self.dirs.items) |each| self.b.allocator.free(each);
self.dirs.deinit();
for (self.files.items) |each| self.b.allocator.free(each);
self.files.deinit();
for (self.collected.items) |each| self.b.allocator.free(each);
self.collected.deinit();
}
pub fn addDir(self: *Compiler, dir: []const u8) void {
const copy = self.b.allocator.dupe(u8, dir) catch @panic("OOM");
self.dirs.append(copy) catch @panic("OOM");
self.templates = null;
self.structs = null;
}
pub fn addFile(self: *Compiler, file: []const u8) void {
const copy = self.b.allocator.dupe(u8, file) catch @panic("OOM");
self.files.append(copy) catch @panic("OOM");
self.templates = null;
self.structs = null;
}
pub fn buildTemplates(self: *Compiler) !*std.Build.Module {
if (self.templates) |t| return t;
//std.debug.print("building for {}\n", .{self.collected.items.len});
const local_dir = std.fs.path.dirname(@src().file) orelse ".";
const compiled = self.b.createModule(.{
.root_source_file = .{
.cwd_relative = self.b.pathJoin(&.{ local_dir, "src/template/comptime.zig" }),
},
});
const found = self.b.addOptions();
found.addOption([]const []const u8, "names", self.collected.items);
compiled.addOptions("config", found);
for (self.collected.items) |file| {
_ = compiled.addAnonymousImport(file, .{
.root_source_file = self.b.path(file),
});
}
self.templates = compiled;
return compiled;
}
pub fn buildStructs(self: *Compiler) !*std.Build.Module {
if (self.structs) |s| return s;
if (self.debugging) std.debug.print("building structs for {}\n", .{self.collected.items.len});
const local_dir = std.fs.path.dirname(@src().file) orelse ".";
const t_compiler = self.b.addExecutable(.{
.name = "template-compiler",
.root_source_file = .{
.cwd_relative = self.b.pathJoin(&.{ local_dir, "src/template/struct-emit.zig" }),
},
.target = self.b.host,
});
const comptime_templates = try self.buildTemplates();
t_compiler.root_module.addImport("comptime_templates", comptime_templates);
const tc_build_run = self.b.addRunArtifact(t_compiler);
const tc_structs = tc_build_run.addOutputFileArg("compiled-structs.zig");
const module = self.b.createModule(.{
.root_source_file = tc_structs,
});
self.structs = module;
return module;
}
pub fn collect(self: *Compiler) !void {
var cwd = std.fs.cwd();
for (self.dirs.items) |srcdir| {
var idir = cwd.openDir(srcdir, .{ .iterate = true }) catch |err| {
std.debug.print("template build error {} for srcdir {s}\n", .{ err, srcdir });
return err;
};
defer idir.close();
var itr = idir.iterate();
while (try itr.next()) |file| {
if (!std.mem.endsWith(u8, file.name, ".html")) continue;
try self.collected.append(self.b.pathJoin(&[2][]const u8{ srcdir, file.name }));
}
}
for (self.files.items) |file| {
try self.collected.append(file);
}
}
};
fn version(b: *std.Build) []const u8 {
if (!std.process.can_spawn) {
std.debug.print("Can't get a version number\n", .{});
std.process.exit(1);
}
var code: u8 = undefined;
const git_wide = b.runAllowFail(
&[_][]const u8{
"git",
"-C",
b.build_root.path orelse ".",
"describe",
"--dirty",
"--always",
},
&code,
.Ignore,
) catch @panic("git is having a bad day");
var git = std.mem.trim(u8, git_wide, " \r\n");
if (git[0] == 'v') git = git[1..];
//std.debug.print("version {s}\n", .{git});
// semver is really dumb, so we need to increment this internally
var ver = std.SemanticVersion.parse(git) catch return "v0.0.0-dev";
if (ver.pre != null) {
ver.minor += 1;
ver.pre = std.fmt.allocPrint(b.allocator, "pre-{s}", .{ver.pre.?}) catch @panic("OOM");
}
const final = std.fmt.allocPrint(b.allocator, "{}", .{ver}) catch @panic("OOM");
//std.debug.print("version {s}\n", .{final});
return final;
}