-
Notifications
You must be signed in to change notification settings - Fork 30
/
build.zig
89 lines (82 loc) · 3.51 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
const std = @import("std");
const builtin = @import("builtin");
const fs = std.fs;
const allocPrint = std.fmt.allocPrint;
const print = std.debug.print;
pub fn build(b: *std.Build) !void {
const run_all_step = b.step("run-all", "Run all examples");
try addExample(b, run_all_step);
}
fn addExample(b: *std.Build, run_all: *std.Build.Step) !void {
const is_latest_zig = builtin.zig_version.minor > 13;
const src_dir = try fs.cwd().openDir("src", .{ .iterate = true });
const target = b.standardTargetOptions(.{});
var it = src_dir.iterate();
const check = b.step("check", "Check if it compiles");
while (try it.next()) |entry| {
switch (entry.kind) {
.file => {
const name = std.mem.trimRight(u8, entry.name, ".zig");
const exe = b.addExecutable(.{
.name = try allocPrint(b.allocator, "examples-{s}", .{name}),
.root_source_file = b.path(try allocPrint(b.allocator, "src/{s}.zig", .{name})),
.target = target,
.optimize = .Debug,
});
check.dependOn(&exe.step);
var opts = b.addOptions();
opts.addOption(bool, "is_latest_zig", is_latest_zig);
exe.root_module.addOptions("build-info", opts);
if (std.mem.eql(u8, "13-01", name)) {
const zigcli = b.dependency("zigcli", .{});
exe.root_module.addImport("simargs", zigcli.module("simargs"));
} else if (std.mem.eql(u8, "14-01", name)) {
exe.linkSystemLibrary("sqlite3");
exe.linkLibC();
} else if (std.mem.eql(u8, "14-02", name)) {
exe.linkSystemLibrary("libpq");
exe.linkLibC();
} else if (std.mem.eql(u8, "14-03", name)) {
exe.linkSystemLibrary("mysqlclient");
exe.linkLibC();
} else if (std.mem.eql(u8, "15-01", name)) {
const lib = b.addStaticLibrary(.{
.name = "regex_slim",
.optimize = .Debug,
.target = target,
});
lib.addCSourceFiles(.{
.files = &.{"lib/regex_slim.c"},
.flags = &.{"-std=c99"},
});
lib.linkLibC();
exe.linkLibrary(lib);
exe.addIncludePath(b.path("lib"));
exe.linkLibC();
}
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = &run_cmd.step;
b.step(try allocPrint(b.allocator, "run-{s}", .{name}), try allocPrint(
b.allocator,
"Run example {s}",
.{name},
)).dependOn(run_step);
// 04-01 start tcp server, and won't stop so we skip it here
// 04-02 is the server's client.
// 04-03 starts udp listener.
if (std.mem.eql(u8, "04-01", name) or
std.mem.eql(u8, "04-02", name) or
std.mem.eql(u8, "04-03", name))
{
continue;
}
run_all.dependOn(run_step);
},
else => {},
}
}
}