Repository navigation
Expand file tree
/
Copy pathbuild.zig
More file actions
382 lines (315 loc) · 13.8 KB
/
Copy pathbuild.zig
File metadata and controls
382 lines (315 loc) · 13.8 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
const std = @import("std");
// Always keep these in sorted order
const targets: []const std.Target.Query = &.{
.{ .cpu_arch = .aarch64, .os_tag = .freebsd },
.{ .cpu_arch = .aarch64, .os_tag = .linux },
.{ .cpu_arch = .aarch64, .os_tag = .macos },
.{ .cpu_arch = .aarch64, .os_tag = .netbsd },
.{ .cpu_arch = .aarch64, .os_tag = .windows },
.{ .cpu_arch = .aarch64_be, .os_tag = .linux },
.{ .cpu_arch = .aarch64_be, .os_tag = .netbsd },
.{ .cpu_arch = .arm, .os_tag = .freebsd },
.{ .cpu_arch = .arm, .os_tag = .linux },
.{ .cpu_arch = .arm, .os_tag = .netbsd },
.{ .cpu_arch = .armeb, .os_tag = .linux },
.{ .cpu_arch = .armeb, .os_tag = .netbsd },
.{ .cpu_arch = .powerpc, .os_tag = .linux },
.{ .cpu_arch = .powerpc, .os_tag = .netbsd },
.{ .cpu_arch = .powerpc64, .os_tag = .freebsd },
.{ .cpu_arch = .powerpc64, .os_tag = .linux },
.{ .cpu_arch = .powerpc64le, .os_tag = .freebsd },
.{ .cpu_arch = .powerpc64le, .os_tag = .linux },
.{ .cpu_arch = .riscv32, .os_tag = .linux },
.{ .cpu_arch = .riscv64, .os_tag = .freebsd },
.{ .cpu_arch = .riscv64, .os_tag = .linux },
.{ .cpu_arch = .thumb, .os_tag = .linux },
.{ .cpu_arch = .thumbeb, .os_tag = .linux },
.{ .cpu_arch = .x86, .os_tag = .linux },
.{ .cpu_arch = .x86, .os_tag = .linux, .abi = .gnu },
.{ .cpu_arch = .x86, .os_tag = .linux, .abi = .musl },
.{ .cpu_arch = .x86, .os_tag = .windows },
.{ .cpu_arch = .x86_64, .os_tag = .freebsd },
.{ .cpu_arch = .x86_64, .os_tag = .linux, .abi = .gnu },
.{ .cpu_arch = .x86_64, .os_tag = .linux, .abi = .musl },
.{ .cpu_arch = .x86_64, .os_tag = .macos },
.{ .cpu_arch = .x86_64, .os_tag = .netbsd },
.{ .cpu_arch = .x86_64, .os_tag = .windows },
};
const third_party_path = "third_party";
// Since Zig uses utf8 strings, we'll use pcre with 8bit support.
const pcre_code_unit_width_name = "PCRE2_CODE_UNIT_WIDTH";
const pcre_code_unit_width_value = "8";
const pcre_prefix = "pcre2";
const pcre_include_path = third_party_path ++ "/" ++ pcre_prefix;
const sqlite_flags = &[_][]const u8{
"-std=c99",
};
const wildmatch_flags = &[_][]const u8{
"-std=c99",
};
fn compileSqlite(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *std.Build.Step.Compile {
// refer to the dependency in build.zig.zon
const sqlite_dep = b.dependency("sqlite", .{
.target = target,
.optimize = optimize,
});
const sqlite_mod = b.createModule(.{
.target = target,
.optimize = optimize,
});
b.modules.put(b.dupe("sqlite"), sqlite_mod) catch unreachable;
const sqlite_sl = b.addLibrary(.{
.name = "sqlite",
.root_module = sqlite_mod,
.linkage = .static,
});
// sqlite_sl.linkLibrary(sqlite_dep.artifact("sqlite"));
sqlite_sl.addCSourceFile(.{
.file = sqlite_dep.path("sqlite3.c"),
.flags = sqlite_flags,
});
sqlite_sl.addIncludePath(sqlite_dep.path("."));
// Workaround code
sqlite_sl.addCSourceFile(.{
.file = b.path("src/c/sqlite_transient_workaround.c"),
.flags = sqlite_flags,
});
sqlite_sl.addIncludePath(b.path("src/c"));
sqlite_sl.linkLibC();
return sqlite_sl;
}
fn compilePcre2(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *std.Build.Step.Compile {
// // refer to the dependency in build.zig.zon
// const pcre2_dep = b.dependency("pcre2", .{
// .target = target,
// .optimize = optimize,
// });
// const pcre2_mod = b.createModule(.{
// .target = target,
// .optimize = optimize,
// });
// b.modules.put(b.dupe("pcre2"), pcre2_mod) catch unreachable;
// const pcre2_sl = b.addLibrary(.{
// .name = "sqlite",
// .root_module = pcre2_mod,
// .linkage = .static,
// });
// pcre2_sl.linkLibrary(pcre2_dep.artifact("pcre2-8"));
// pcre2_sl.linkLibC();
const pcreCopyFiles = b.addWriteFiles();
_ = pcreCopyFiles.addCopyFile(b.path(pcre_include_path ++ "/src/config.h.generic"), "config.h");
_ = pcreCopyFiles.addCopyFile(b.path(pcre_include_path ++ "/src/pcre2.h.generic"), "pcre2.h");
const pcre2_mod = b.createModule(.{
.target = target,
.optimize = optimize,
});
const pcre2_sl = b.addLibrary(.{
.name = b.fmt("pcre2-{s}", .{pcre_code_unit_width_value}),
.root_module = pcre2_mod,
.linkage = .static,
});
pcre2_sl.linkLibC();
pcre2_sl.root_module.addCMacro(pcre_code_unit_width_name, pcre_code_unit_width_value);
pcre2_sl.addCSourceFile(.{
.file = pcreCopyFiles.addCopyFile(b.path(pcre_include_path ++ "/src/pcre2_chartables.c.dist"), "pcre2_chartables.c"),
.flags = &.{
"-DHAVE_CONFIG_H",
},
});
pcre2_sl.addIncludePath(b.path(pcre_include_path ++ "/src"));
pcre2_sl.addIncludePath(pcreCopyFiles.getDirectory());
pcre2_sl.addCSourceFiles(.{
.files = &.{
pcre_include_path ++ "/src/pcre2_auto_possess.c",
pcre_include_path ++ "/src/pcre2_chkdint.c",
pcre_include_path ++ "/src/pcre2_compile.c",
pcre_include_path ++ "/src/pcre2_config.c",
pcre_include_path ++ "/src/pcre2_context.c",
pcre_include_path ++ "/src/pcre2_convert.c",
pcre_include_path ++ "/src/pcre2_dfa_match.c",
pcre_include_path ++ "/src/pcre2_error.c",
pcre_include_path ++ "/src/pcre2_extuni.c",
pcre_include_path ++ "/src/pcre2_find_bracket.c",
pcre_include_path ++ "/src/pcre2_maketables.c",
pcre_include_path ++ "/src/pcre2_match.c",
pcre_include_path ++ "/src/pcre2_match_data.c",
pcre_include_path ++ "/src/pcre2_newline.c",
pcre_include_path ++ "/src/pcre2_ord2utf.c",
pcre_include_path ++ "/src/pcre2_pattern_info.c",
pcre_include_path ++ "/src/pcre2_script_run.c",
pcre_include_path ++ "/src/pcre2_serialize.c",
pcre_include_path ++ "/src/pcre2_string_utils.c",
pcre_include_path ++ "/src/pcre2_study.c",
pcre_include_path ++ "/src/pcre2_substitute.c",
pcre_include_path ++ "/src/pcre2_substring.c",
pcre_include_path ++ "/src/pcre2_tables.c",
pcre_include_path ++ "/src/pcre2_ucd.c",
pcre_include_path ++ "/src/pcre2_valid_utf.c",
pcre_include_path ++ "/src/pcre2_xclass.c",
},
.flags = &.{
"-DHAVE_CONFIG_H",
"-DPCRE2_STATIC",
},
});
pcre2_sl.installHeader(b.path(pcre_include_path ++ "/src/pcre2.h.generic"), "pcre2.h");
return pcre2_sl;
}
fn compileWildmatch(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *std.Build.Step.Compile {
// refer to the dependency in build.zig.zon
const wildmatch_dep = b.dependency("wildmatch", .{
.target = target,
.optimize = optimize,
});
const wildmatch_mod = b.createModule(.{
.target = target,
.optimize = optimize,
});
b.modules.put(b.dupe("wildmatch"), wildmatch_mod) catch unreachable;
const wildmatch_sl = b.addLibrary(.{
.name = "wildmatch",
.root_module = wildmatch_mod,
.linkage = .static,
});
wildmatch_sl.addCSourceFile(.{
.file = wildmatch_dep.path("wildmatch/wildmatch.c"),
.flags = wildmatch_flags,
});
wildmatch_sl.addIncludePath(wildmatch_dep.path("wildmatch"));
wildmatch_sl.linkLibC();
return wildmatch_sl;
}
fn compileExe(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *std.Build.Step.Compile {
const sqlite_dep = b.dependency("sqlite", .{
.target = target,
.optimize = optimize,
});
const wildmatch_dep = b.dependency("wildmatch", .{
.target = target,
.optimize = optimize,
});
const sqlite_sl = compileSqlite(b, target, optimize);
const pcre2_sl = compilePcre2(b, target, optimize);
const wildmatch_sl = compileWildmatch(b, target, optimize);
const exe = b.addExecutable(.{
.name = "hvrt",
// In this case the main source file is merely a path, however, in more
// complicated build scripts, this could be a generated file.
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
}),
});
exe.linkLibrary(sqlite_sl);
exe.linkLibrary(pcre2_sl);
exe.linkLibrary(wildmatch_sl);
exe.addIncludePath(sqlite_dep.path("."));
exe.addIncludePath(wildmatch_dep.path("wildmatch"));
exe.addIncludePath(b.path("src/c"));
// Some systems other than Linux use libc as the system interface, so it
// must be linked for now.
exe.linkLibC();
return exe;
}
// Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external
// runner.
pub fn build(b: *std.Build) void {
const report_coverage = b.option(bool, "report-coverage", "Generates a test code coverage report using kcov") orelse false;
const build_all_targets = b.option(bool, "build-all-targets", "Build exe for all targets") orelse false;
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});
// Standard optimization options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
// set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption(.{});
const sqlite_sl = compileSqlite(b, target, optimize);
const pcre2_sl = compilePcre2(b, target, optimize);
const native_exe = compileExe(b, target, optimize);
// This declares intent for the executable to be installed into the
// standard location when the user invokes the "install" step (the default
// step when running `zig build`).
b.installArtifact(native_exe);
if (build_all_targets) {
// Build artifacts for all desired targets
for (targets) |t| {
const exe = compileExe(b, b.resolveTargetQuery(t), optimize);
const custom_dest = t.zigTriple(b.allocator) catch unreachable;
const target_output = b.addInstallArtifact(exe, .{
.dest_dir = .{
.override = .{
.custom = custom_dest,
},
},
});
b.getInstallStep().dependOn(&target_output.step);
}
}
// This *creates* a Run step in the build graph, to be executed when another
// step is evaluated that depends on it. The next line below will establish
// such a dependency.
const run_cmd = b.addRunArtifact(native_exe);
// By making the run step depend on the install step, it will be run from the
// installation directory rather than directly from within the cache directory.
// This is not necessary, however, if the application depends on other installed
// files, this ensures they will be present and in the expected location.
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);
}
// This creates a build step. It will be visible in the `zig build --help` menu,
// and can be selected like this: `zig build run`
// This will evaluate the `run` step rather than the default, which is "install".
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
// Creates a step for unit testing. This only builds the test executable
// but does not run it.
const unit_tests = b.addTest(.{
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
}),
});
const sqlite_dep = b.dependency("sqlite", .{
.target = target,
.optimize = optimize,
});
unit_tests.linkLibrary(sqlite_sl);
unit_tests.linkLibrary(pcre2_sl);
unit_tests.addIncludePath(sqlite_dep.path("."));
unit_tests.addIncludePath(b.path("src/c"));
const test_step = b.step("test", "Run unit tests");
if (report_coverage) {
// Generate coverage report with kcov
var run_kcov = b.addSystemCommand(&.{"kcov"});
run_kcov.addPrefixedDirectoryArg("--include-path=", b.path("src"));
run_kcov.addArg("--clean");
// Patterns to ignore coverage for ranges and single lines
run_kcov.addArg("--exclude-region=NO_COV_START:NO_COV_END");
run_kcov.addArg("--exclude-line=NO_COV_LINE");
const kcov_out_dir_path = run_kcov.addPrefixedOutputDirectoryArg("", "kcov-out");
run_kcov.addFileArg(unit_tests.getEmittedBin());
run_kcov.has_side_effects = true;
run_kcov.stdio = .inherit;
const install_kcov_dir = b.addInstallDirectory(.{
.source_dir = kcov_out_dir_path,
.install_dir = .prefix,
.install_subdir = "kcov-out",
});
install_kcov_dir.step.dependOn(&run_kcov.step);
test_step.dependOn(&install_kcov_dir.step);
} else {
const run_unit_tests = b.addRunArtifact(unit_tests);
// Similar to creating the run step earlier, this exposes a `test` step to
// the `zig build --help` menu, providing a way for the user to request
// running the unit tests.
test_step.dependOn(&run_unit_tests.step);
}
}