-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathbuild.zig
More file actions
120 lines (112 loc) · 5.15 KB
/
build.zig
File metadata and controls
120 lines (112 loc) · 5.15 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
const std = @import("std");
const builtin = @import("builtin");
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 io = b.graph.io;
var src_dir = try std.Io.Dir.cwd().openDir(io, b.path("assets/src").getPath(b), .{ .iterate = true });
defer src_dir.close(io);
const target = b.standardTargetOptions(.{});
var it = src_dir.iterate();
const check = b.step("check", "Check if it compiles");
LoopExample: while (try it.next(io)) |entry| {
switch (entry.kind) {
.file => {
const name = std.mem.trimEnd(u8, entry.name, ".zig");
const exe = b.addExecutable(.{
.name = try allocPrint(b.allocator, "examples-{s}", .{name}),
.root_module = b.createModule(.{
.root_source_file = b.path(try allocPrint(b.allocator, "assets/src/{s}.zig", .{name})),
.target = target,
.optimize = .Debug,
}),
});
check.dependOn(&exe.step);
if (std.mem.eql(u8, "13-01", name)) {
const zigcli = b.dependency("zigcli", .{});
exe.root_module.addImport("zigcli", zigcli.module("zigcli"));
} else if (std.mem.eql(u8, "14-01", name)) {
const translate_c = b.addTranslateC(.{
.root_source_file = b.path("lib/sqlite3.h"),
.target = target,
.optimize = .Debug,
});
translate_c.linkSystemLibrary("sqlite3", .{});
exe.root_module.addImport("c", translate_c.createModule());
exe.root_module.link_libc = true;
} else if (std.mem.eql(u8, "14-02", name)) {
const translate_c = b.addTranslateC(.{
.root_source_file = b.path("lib/libpq-fe.h"),
.target = target,
.optimize = .Debug,
});
translate_c.linkSystemLibrary("libpq", .{});
exe.root_module.addImport("c", translate_c.createModule());
exe.root_module.link_libc = true;
} else if (std.mem.eql(u8, "14-03", name)) {
const translate_c = b.addTranslateC(.{
.root_source_file = b.path("lib/mysql.h"),
.target = target,
.optimize = .Debug,
});
translate_c.linkSystemLibrary("mysqlclient", .{});
exe.root_module.addImport("c", translate_c.createModule());
exe.root_module.link_libc = true;
} else if (std.mem.eql(u8, "15-01", name)) {
const lib_module = b.createModule(.{
.target = target,
.optimize = .Debug,
.link_libc = true,
});
lib_module.addCSourceFiles(.{
.files = &.{"lib/regex_slim.c"},
.flags = &.{"-std=c99"},
});
const lib = b.addLibrary(.{
.name = "regex_slim",
.root_module = lib_module,
.linkage = .static,
});
exe.root_module.linkLibrary(lib);
exe.root_module.addIncludePath(b.path("lib"));
exe.root_module.link_libc = true;
const translate_c = b.addTranslateC(.{
.root_source_file = b.path("lib/regex_slim_all.h"),
.target = target,
.optimize = .Debug,
});
exe.root_module.addImport("c", translate_c.createModule());
}
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);
// Those examples won't stop so we don't add them to run_all step.
const skip_list = [_][]const u8{
"04-01", // start tcp server
"04-02", // client of tcp server
"04-03", // udp listener
"05-03", // http server
};
for (skip_list) |example| {
if (std.mem.eql(u8, example, name)) {
continue :LoopExample;
}
}
run_all.dependOn(run_step);
},
else => {},
}
}
}