forked from mitchellh/libxev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.zig
348 lines (302 loc) · 11.5 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
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
const std = @import("std");
const CompileStep = std.build.Step.Compile;
const ScdocStep = @import("src/build/ScdocStep.zig");
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 man_pages = b.option(
bool,
"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 bench_name = b.option(
[]const u8,
"bench-name",
"Build and install a single benchmark",
);
const bench_install = b.option(
bool,
"bench",
"Install the benchmark binaries to zig-out/bench",
) orelse (bench_name != null);
const example_name = b.option(
[]const u8,
"example-name",
"Build and install a single example",
);
const example_install = b.option(
bool,
"example",
"Install the example binaries to zig-out/example",
) orelse (example_name != null);
const test_install = b.option(
bool,
"install-tests",
"Install the test binaries into zig-out",
) orelse false;
// Our tests require libc on Linux and Mac. Note that libxev itself
// does NOT require libc.
const test_libc = switch (target.result.os.tag) {
.linux, .macos => true,
else => false,
};
// We always build our test exe as part of `zig build` so that
// we can easily run it manually without digging through the cache.
const test_exe = b.addTest(.{
.name = "xev-test",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
if (test_libc) test_exe.linkLibC(); // Tests depend on libc, libxev does not
if (test_install) b.installArtifact(test_exe);
// zig build test test binary and runner.
const tests_run = b.addRunArtifact(test_exe);
const test_step = b.step("test", "Run tests");
test_step.dependOn(&tests_run.step);
// Static C lib
const static_c_lib: ?*std.Build.Step.Compile = if (target.result.os.tag != .wasi) lib: {
const static_lib = b.addStaticLibrary(.{
.name = "xev",
.root_source_file = b.path("src/c_api.zig"),
.target = target,
.optimize = optimize,
});
static_lib.linkLibC();
// Link required libraries if targeting Windows
if (target.result.os.tag == .windows) {
static_lib.linkSystemLibrary("ws2_32");
static_lib.linkSystemLibrary("mswsock");
}
b.installArtifact(static_lib);
b.default_step.dependOn(&static_lib.step);
const static_binding_test = b.addExecutable(.{
.name = "static-binding-test",
.target = target,
.optimize = optimize,
});
static_binding_test.linkLibC();
static_binding_test.addIncludePath(b.path("include"));
static_binding_test.addCSourceFile(.{
.file = b.path("examples/_basic.c"),
.flags = &[_][]const u8{ "-Wall", "-Wextra", "-pedantic", "-std=c99", "-D_POSIX_C_SOURCE=199309L" },
});
static_binding_test.linkLibrary(static_lib);
if (test_install) b.installArtifact(static_binding_test);
const static_binding_test_run = b.addRunArtifact(static_binding_test);
test_step.dependOn(&static_binding_test_run.step);
break :lib static_lib;
} else null;
// Dynamic C lib. We only build this if this is the native target so we
// can link to libxml2 on our native system.
if (target.query.isNative()) {
const dynamic_lib_name = "xev";
const dynamic_lib = b.addSharedLibrary(.{
.name = dynamic_lib_name,
.root_source_file = b.path("src/c_api.zig"),
.target = target,
.optimize = optimize,
});
b.installArtifact(dynamic_lib);
b.default_step.dependOn(&dynamic_lib.step);
const dynamic_binding_test = b.addExecutable(.{
.name = "dynamic-binding-test",
.target = target,
.optimize = optimize,
});
dynamic_binding_test.linkLibC();
dynamic_binding_test.addIncludePath(b.path("include"));
dynamic_binding_test.addCSourceFile(.{
.file = b.path("examples/_basic.c"),
.flags = &[_][]const u8{ "-Wall", "-Wextra", "-pedantic", "-std=c99" },
});
dynamic_binding_test.linkLibrary(dynamic_lib);
if (test_install) b.installArtifact(dynamic_binding_test);
const dynamic_binding_test_run = b.addRunArtifact(dynamic_binding_test);
test_step.dependOn(&dynamic_binding_test_run.step);
}
// C Headers
const c_header = b.addInstallFileWithDir(
b.path("include/xev.h"),
.header,
"xev.h",
);
b.getInstallStep().dependOn(&c_header.step);
// pkg-config
{
const file = try b.cache_root.join(b.allocator, &[_][]const u8{"libxev.pc"});
const pkgconfig_file = try std.fs.cwd().createFile(file, .{});
const writer = pkgconfig_file.writer();
try writer.print(
\\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});
defer pkgconfig_file.close();
b.getInstallStep().dependOn(&b.addInstallFileWithDir(
.{ .cwd_relative = file },
.prefix,
"share/pkgconfig/libxev.pc",
).step);
}
// Benchmarks
_ = try benchTargets(b, target, optimize, bench_install, bench_name);
// Examples
_ = try exampleTargets(b, target, optimize, static_c_lib, example_install, example_name);
// Man pages
if (man_pages) {
const scdoc_step = ScdocStep.create(b);
try scdoc_step.install();
}
}
fn benchTargets(
b: *std.Build,
target: std.Build.ResolvedTarget,
mode: std.builtin.OptimizeMode,
install: bool,
install_name: ?[]const u8,
) !std.StringHashMap(*std.Build.Step.Compile) {
_ = mode;
var map = std.StringHashMap(*std.Build.Step.Compile).init(b.allocator);
// Open the directory
const c_dir_path = "src/bench";
var c_dir = try std.fs.cwd().openDir(comptime thisDir() ++ "/" ++ c_dir_path, .{ .iterate = true });
defer c_dir.close();
// Go through and add each as a step
var c_dir_it = c_dir.iterate();
while (try c_dir_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 path = try std.fs.path.join(b.allocator, &[_][]const u8{
c_dir_path,
entry.name,
});
// If we have specified a specific name, only install that one.
if (install_name) |n| {
if (!std.mem.eql(u8, n, name)) continue;
}
// Executable builder.
const c_exe = b.addExecutable(.{
.name = name,
.root_source_file = b.path(path),
.target = target,
.optimize = .ReleaseFast, // benchmarks are always release fast
});
c_exe.root_module.addImport("xev", b.modules.get("xev").?);
if (install) {
const install_step = b.addInstallArtifact(c_exe, .{
.dest_dir = .{ .override = .{ .custom = "bench" } },
});
b.getInstallStep().dependOn(&install_step.step);
}
// Store the mapping
try map.put(try b.allocator.dupe(u8, name), c_exe);
}
return map;
}
fn exampleTargets(
b: *std.Build,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
c_lib_: ?*std.Build.Step.Compile,
install: bool,
install_name: ?[]const u8,
) !void {
// Ignore if we're not installing
if (!install) return;
// Open the directory
const c_dir_path = (comptime thisDir()) ++ "/examples";
var c_dir = try std.fs.cwd().openDir(c_dir_path, .{ .iterate = true });
defer c_dir.close();
// Go through and add each as a step
var c_dir_it = c_dir.iterate();
while (try c_dir_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;
// If we have specified a specific name, only install that one.
if (install_name) |n| {
if (!std.mem.eql(u8, n, entry.name)) continue;
}
// Name of the app and full path to the entrypoint.
const name = entry.name[0..index];
const path = try std.fs.path.join(b.allocator, &[_][]const u8{
c_dir_path,
entry.name,
});
const is_zig = std.mem.eql(u8, entry.name[index + 1 ..], "zig");
if (is_zig) {
const c_exe = b.addExecutable(.{
.name = name,
.root_source_file = .{ .cwd_relative = path },
.target = target,
.optimize = optimize,
});
c_exe.root_module.addImport("xev", b.modules.get("xev").?);
if (install) {
const install_step = b.addInstallArtifact(c_exe, .{
.dest_dir = .{ .override = .{ .custom = "example" } },
});
b.getInstallStep().dependOn(&install_step.step);
}
} else {
const c_lib = c_lib_ orelse return error.UnsupportedPlatform;
const c_exe = b.addExecutable(.{
.name = name,
.target = target,
.optimize = optimize,
});
c_exe.linkLibC();
c_exe.addIncludePath(b.path("include"));
c_exe.addCSourceFile(.{
.file = .{ .cwd_relative = path },
.flags = &[_][]const u8{
"-Wall",
"-Wextra",
"-pedantic",
"-std=c99",
"-D_POSIX_C_SOURCE=199309L",
},
});
c_exe.linkLibrary(c_lib);
if (install) {
const install_step = b.addInstallArtifact(c_exe, .{
.dest_dir = .{ .override = .{ .custom = "example" } },
});
b.getInstallStep().dependOn(&install_step.step);
}
}
// If we have specified a specific name, only install that one.
if (install_name) |_| break;
} else {
if (install_name) |n| {
std.debug.print("No example file named: {s}\n", .{n});
std.debug.print("Choices:\n", .{});
var c_dir_it2 = c_dir.iterate();
while (try c_dir_it2.next()) |entry| {
std.debug.print("\t{s}\n", .{entry.name});
}
return error.InvalidExampleName;
}
}
}
/// Path to the directory with the build.zig.
fn thisDir() []const u8 {
return std.fs.path.dirname(@src().file) orelse unreachable;
}