-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathbuild.zig
313 lines (277 loc) · 9.34 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
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
const std = @import("std");
const Step = std.Build.Step;
/// A note on my build.zig style: I try to create all the artifacts first,
/// unattached to any steps. At the end of the build() function, I create
/// steps or attach unattached artifacts to predefined steps such as
/// install. This means the only thing affecting the `zig build` user
/// interaction is at the end of the build() file and makes it easier
/// to reason about the structure.
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
_ = b.addModule("xev", .{ .root_source_file = b.path("src/main.zig") });
const emit_man = b.option(
bool,
"emit-man-pages",
"Set to true to build man pages. Requires scdoc. Defaults to true if scdoc is found.",
) orelse if (b.findProgram(
&[_][]const u8{"scdoc"},
&[_][]const u8{},
)) |_|
true
else |err| switch (err) {
error.FileNotFound => false,
else => return err,
};
const emit_bench = b.option(
bool,
"emit-bench",
"Install the benchmark binaries to zig-out",
) orelse false;
const emit_examples = b.option(
bool,
"emit-example",
"Install the example binaries to zig-out",
) orelse false;
// Static C lib
const static_lib: ?*Step.Compile = lib: {
if (target.result.os.tag == .wasi) break :lib null;
const static_lib = b.addStaticLibrary(.{
.name = "xev",
.root_source_file = b.path("src/c_api.zig"),
.target = target,
.optimize = optimize,
});
static_lib.linkLibC();
if (target.result.os.tag == .windows) {
static_lib.linkSystemLibrary("ws2_32");
static_lib.linkSystemLibrary("mswsock");
}
break :lib static_lib;
};
// Dynamic C lib
const dynamic_lib: ?*Step.Compile = lib: {
// We require native so we can link to libxml2
if (!target.query.isNative()) break :lib null;
const dynamic_lib = b.addSharedLibrary(.{
.name = "xev",
.root_source_file = b.path("src/c_api.zig"),
.target = target,
.optimize = optimize,
});
break :lib dynamic_lib;
};
// C Headers
const c_header = b.addInstallFileWithDir(
b.path("include/xev.h"),
.header,
"xev.h",
);
// pkg-config
const pc: *Step.InstallFile = pc: {
const file = b.addWriteFile("libxev.pc", b.fmt(
\\prefix={s}
\\includedir=${{prefix}}/include
\\libdir=${{prefix}}/lib
\\
\\Name: libxev
\\URL: https://github.com/mitchellh/libxev
\\Description: High-performance, cross-platform event loop
\\Version: 0.1.0
\\Cflags: -I${{includedir}}
\\Libs: -L${{libdir}} -lxev
, .{b.install_prefix}));
break :pc b.addInstallFileWithDir(
file.getDirectory().path(b, "libxev.pc"),
.prefix,
"share/pkgconfig/libxev.pc",
);
};
// Man pages
const man = try manPages(b);
// Benchmarks and examples
const benchmarks = try buildBenchmarks(b, target);
const examples = try buildExamples(b, target, optimize, static_lib);
// Test Executable
const test_exe: *Step.Compile = test_exe: {
const test_filter = b.option(
[]const u8,
"test-filter",
"Filter for test",
);
const test_exe = b.addTest(.{
.name = "xev-test",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.filter = test_filter,
});
switch (target.result.os.tag) {
.linux, .macos => test_exe.linkLibC(),
else => {},
}
break :test_exe test_exe;
};
// "test" Step
{
const tests_run = b.addRunArtifact(test_exe);
const test_step = b.step("test", "Run tests");
test_step.dependOn(&tests_run.step);
}
if (static_lib) |v| b.installArtifact(v);
if (dynamic_lib) |v| b.installArtifact(v);
b.getInstallStep().dependOn(&c_header.step);
b.getInstallStep().dependOn(&pc.step);
b.installArtifact(test_exe);
if (emit_man) {
for (man) |step| b.getInstallStep().dependOn(step);
}
if (emit_bench) for (benchmarks) |exe| {
b.getInstallStep().dependOn(&b.addInstallArtifact(
exe,
.{ .dest_dir = .{ .override = .{
.custom = "bin/bench",
} } },
).step);
};
if (emit_examples) for (examples) |exe| {
b.getInstallStep().dependOn(&b.addInstallArtifact(
exe,
.{ .dest_dir = .{ .override = .{
.custom = "bin/example",
} } },
).step);
};
}
fn buildBenchmarks(
b: *std.Build,
target: std.Build.ResolvedTarget,
) ![]const *Step.Compile {
var steps = std.ArrayList(*Step.Compile).init(b.allocator);
defer steps.deinit();
var dir = try std.fs.cwd().openDir(try b.build_root.join(
b.allocator,
&.{ "src", "bench" },
), .{ .iterate = true });
defer dir.close();
// Go through and add each as a step
var it = dir.iterate();
while (try it.next()) |entry| {
// Get the index of the last '.' so we can strip the extension.
const index = std.mem.lastIndexOfScalar(
u8,
entry.name,
'.',
) orelse continue;
if (index == 0) continue;
// Name of the app and full path to the entrypoint.
const name = entry.name[0..index];
// Executable builder.
const exe = b.addExecutable(.{
.name = name,
.root_source_file = b.path(b.fmt(
"src/bench/{s}",
.{entry.name},
)),
.target = target,
.optimize = .ReleaseFast, // benchmarks are always release fast
});
exe.root_module.addImport("xev", b.modules.get("xev").?);
// Store the mapping
try steps.append(exe);
}
return try steps.toOwnedSlice();
}
fn buildExamples(
b: *std.Build,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
c_lib_: ?*Step.Compile,
) ![]const *Step.Compile {
var steps = std.ArrayList(*Step.Compile).init(b.allocator);
defer steps.deinit();
var dir = try std.fs.cwd().openDir(try b.build_root.join(
b.allocator,
&.{"examples"},
), .{ .iterate = true });
defer dir.close();
// Go through and add each as a step
var it = dir.iterate();
while (try it.next()) |entry| {
// Get the index of the last '.' so we can strip the extension.
const index = std.mem.lastIndexOfScalar(
u8,
entry.name,
'.',
) orelse continue;
if (index == 0) continue;
// Name of the app and full path to the entrypoint.
const name = entry.name[0..index];
const is_zig = std.mem.eql(u8, entry.name[index + 1 ..], "zig");
const exe: *Step.Compile = if (is_zig) exe: {
const exe = b.addExecutable(.{
.name = name,
.root_source_file = b.path(b.fmt(
"examples/{s}",
.{entry.name},
)),
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("xev", b.modules.get("xev").?);
break :exe exe;
} else exe: {
const c_lib = c_lib_ orelse return error.UnsupportedPlatform;
const exe = b.addExecutable(.{
.name = name,
.target = target,
.optimize = optimize,
});
exe.linkLibC();
exe.addIncludePath(b.path("include"));
exe.addCSourceFile(.{
.file = b.path(b.fmt(
"examples/{s}",
.{entry.name},
)),
.flags = &[_][]const u8{
"-Wall",
"-Wextra",
"-pedantic",
"-std=c99",
"-D_POSIX_C_SOURCE=199309L",
},
});
exe.linkLibrary(c_lib);
break :exe exe;
};
// Store the mapping
try steps.append(exe);
}
return try steps.toOwnedSlice();
}
fn manPages(b: *std.Build) ![]const *Step {
var steps = std.ArrayList(*Step).init(b.allocator);
defer steps.deinit();
var dir = try std.fs.cwd().openDir(try b.build_root.join(
b.allocator,
&.{"docs"},
), .{ .iterate = true });
defer dir.close();
var it = dir.iterate();
while (try it.next()) |*entry| {
// Filenames must end in "{section}.scd" and sections are
// single numerals.
const base = entry.name[0 .. entry.name.len - 4];
const section = base[base.len - 1 ..];
const cmd = b.addSystemCommand(&.{"scdoc"});
cmd.setStdIn(.{ .lazy_path = b.path(
b.fmt("docs/{s}", .{entry.name}),
) });
try steps.append(&b.addInstallFile(
cmd.captureStdOut(),
b.fmt("share/man/man{s}/{s}", .{ section, base }),
).step);
}
return try steps.toOwnedSlice();
}