forked from riverwm/river
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.zig
286 lines (236 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
const std = @import("std");
const assert = std.debug.assert;
const fs = std.fs;
const mem = std.mem;
const zbs = std.build;
const ScanProtocolsStep = @import("deps/zig-wayland/build.zig").ScanProtocolsStep;
/// While a river release is in development, this string should contain the version in development
/// with the "-dev" suffix.
/// When a release is tagged, the "-dev" suffix should be removed for the commit that gets tagged.
/// Directly after the tagged commit, the version should be bumped and the "-dev" suffix added.
const version = "0.2.0-dev";
pub fn build(b: *zbs.Builder) !void {
const target = b.standardTargetOptions(.{});
const mode = b.standardReleaseOptions();
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 scdoc_found: {
_ = b.findProgram(&[_][]const u8{"scdoc"}, &[_][]const u8{}) catch |err| switch (err) {
error.FileNotFound => break :scdoc_found false,
else => return err,
};
break :scdoc_found true;
};
const bash_completion = b.option(
bool,
"bash-completion",
"Set to true to install bash completion for riverctl. Defaults to true.",
) orelse true;
const zsh_completion = b.option(
bool,
"zsh-completion",
"Set to true to install zsh completion for riverctl. Defaults to true.",
) orelse true;
const fish_completion = b.option(
bool,
"fish-completion",
"Set to true to install fish completion for riverctl. Defaults to true.",
) orelse true;
const xwayland = b.option(
bool,
"xwayland",
"Set to true to enable xwayland support",
) orelse false;
const full_version = blk: {
if (mem.endsWith(u8, version, "-dev")) {
var ret: u8 = undefined;
const git_dir = try fs.path.join(b.allocator, &[_][]const u8{ b.build_root, ".git" });
const git_commit_hash = b.execAllowFail(
&[_][]const u8{ "git", "--git-dir", git_dir, "--work-tree", b.build_root, "rev-parse", "--short", "HEAD" },
&ret,
.Inherit,
) catch break :blk version;
break :blk try std.fmt.allocPrintZ(b.allocator, "{s}-{s}", .{
version,
mem.trim(u8, git_commit_hash, &std.ascii.spaces),
});
} else {
break :blk version;
}
};
const options = b.addOptions();
options.addOption(bool, "xwayland", xwayland);
options.addOption([]const u8, "version", full_version);
const scanner = ScanProtocolsStep.create(b);
scanner.addSystemProtocol("stable/xdg-shell/xdg-shell.xml");
scanner.addSystemProtocol("unstable/pointer-gestures/pointer-gestures-unstable-v1.xml");
scanner.addSystemProtocol("unstable/xdg-output/xdg-output-unstable-v1.xml");
scanner.addSystemProtocol("unstable/pointer-constraints/pointer-constraints-unstable-v1.xml");
scanner.addProtocolPath("protocol/river-control-unstable-v1.xml");
scanner.addProtocolPath("protocol/river-status-unstable-v1.xml");
scanner.addProtocolPath("protocol/river-layout-v3.xml");
scanner.addProtocolPath("protocol/wlr-layer-shell-unstable-v1.xml");
scanner.addProtocolPath("protocol/wlr-output-power-management-unstable-v1.xml");
{
const river = b.addExecutable("river", "river/main.zig");
river.setTarget(target);
river.setBuildMode(mode);
river.addOptions("build_options", options);
addServerDeps(river, scanner);
river.install();
}
{
const riverctl = b.addExecutable("riverctl", "riverctl/main.zig");
riverctl.setTarget(target);
riverctl.setBuildMode(mode);
riverctl.addOptions("build_options", options);
riverctl.step.dependOn(&scanner.step);
riverctl.addPackagePath("flags", "common/flags.zig");
riverctl.addPackage(.{
.name = "wayland",
.path = .{ .generated = &scanner.result },
});
riverctl.linkLibC();
riverctl.linkSystemLibrary("wayland-client");
scanner.addCSource(riverctl);
riverctl.install();
}
{
const rivertile = b.addExecutable("rivertile", "rivertile/main.zig");
rivertile.setTarget(target);
rivertile.setBuildMode(mode);
rivertile.addOptions("build_options", options);
rivertile.step.dependOn(&scanner.step);
rivertile.addPackagePath("flags", "common/flags.zig");
rivertile.addPackage(.{
.name = "wayland",
.path = .{ .generated = &scanner.result },
});
rivertile.linkLibC();
rivertile.linkSystemLibrary("wayland-client");
scanner.addCSource(rivertile);
rivertile.install();
}
{
const file = try fs.path.join(b.allocator, &[_][]const u8{ b.cache_root, "river-protocols.pc" });
const pkgconfig_file = try fs.cwd().createFile(file, .{});
const writer = pkgconfig_file.writer();
try writer.print(
\\prefix={s}
\\datadir=${{prefix}}/share
\\pkgdatadir=${{datadir}}/river-protocols
\\
\\Name: river-protocols
\\URL: https://github.com/riverwm/river
\\Description: protocol files for the river wayland compositor
\\Version: {s}
, .{ b.install_prefix, full_version });
defer pkgconfig_file.close();
b.installFile("protocol/river-layout-v3.xml", "share/river-protocols/river-layout-v3.xml");
b.installFile(file, "share/pkgconfig/river-protocols.pc");
}
if (man_pages) {
const scdoc_step = ScdocStep.create(b);
try scdoc_step.install();
}
if (bash_completion) {
b.installFile("completions/bash/riverctl", "share/bash-completion/completions/riverctl");
}
if (zsh_completion) {
b.installFile("completions/zsh/_riverctl", "share/zsh/site-functions/_riverctl");
}
if (fish_completion) {
b.installFile("completions/fish/riverctl.fish", "share/fish/vendor_completions.d/riverctl.fish");
}
{
const river_test = b.addTest("river/test_main.zig");
river_test.setTarget(target);
river_test.setBuildMode(mode);
river_test.addOptions("build_options", options);
addServerDeps(river_test, scanner);
const test_step = b.step("test", "Run the tests");
test_step.dependOn(&river_test.step);
}
}
fn addServerDeps(exe: *zbs.LibExeObjStep, scanner: *ScanProtocolsStep) void {
const wayland = zbs.Pkg{
.name = "wayland",
.path = .{ .generated = &scanner.result },
};
const xkbcommon = zbs.Pkg{
.name = "xkbcommon",
.path = .{ .path = "deps/zig-xkbcommon/src/xkbcommon.zig" },
};
const pixman = zbs.Pkg{
.name = "pixman",
.path = .{ .path = "deps/zig-pixman/pixman.zig" },
};
const wlroots = zbs.Pkg{
.name = "wlroots",
.path = .{ .path = "deps/zig-wlroots/src/wlroots.zig" },
.dependencies = &[_]zbs.Pkg{ wayland, xkbcommon, pixman },
};
exe.step.dependOn(&scanner.step);
exe.linkLibC();
exe.linkSystemLibrary("libevdev");
exe.linkSystemLibrary("libinput");
exe.addPackage(wayland);
exe.linkSystemLibrary("wayland-server");
exe.addPackage(xkbcommon);
exe.linkSystemLibrary("xkbcommon");
exe.addPackage(pixman);
exe.linkSystemLibrary("pixman-1");
exe.addPackage(wlroots);
exe.linkSystemLibrary("wlroots");
exe.addPackagePath("flags", "common/flags.zig");
exe.addCSourceFile("river/wlroots_log_wrapper.c", &[_][]const u8{ "-std=c99", "-O2" });
// TODO: remove when zig issue #131 is implemented
scanner.addCSource(exe);
}
const ScdocStep = struct {
const scd_paths = [_][]const u8{
"doc/river.1.scd",
"doc/riverctl.1.scd",
"doc/rivertile.1.scd",
};
builder: *zbs.Builder,
step: zbs.Step,
fn create(builder: *zbs.Builder) *ScdocStep {
const self = builder.allocator.create(ScdocStep) catch @panic("out of memory");
self.* = init(builder);
return self;
}
fn init(builder: *zbs.Builder) ScdocStep {
return ScdocStep{
.builder = builder,
.step = zbs.Step.init(.custom, "Generate man pages", builder.allocator, make),
};
}
fn make(step: *zbs.Step) !void {
const self = @fieldParentPtr(ScdocStep, "step", step);
for (scd_paths) |path| {
const command = try std.fmt.allocPrint(
self.builder.allocator,
"scdoc < {s} > {s}",
.{ path, path[0..(path.len - 4)] },
);
_ = try self.builder.exec(&[_][]const u8{ "sh", "-c", command });
}
}
fn install(self: *ScdocStep) !void {
self.builder.getInstallStep().dependOn(&self.step);
for (scd_paths) |path| {
const path_no_ext = path[0..(path.len - 4)];
const basename_no_ext = fs.path.basename(path_no_ext);
const section = path_no_ext[(path_no_ext.len - 1)..];
const output = try std.fmt.allocPrint(
self.builder.allocator,
"share/man/man{s}/{s}",
.{ section, basename_no_ext },
);
self.builder.installFile(path_no_ext, output);
}
}
};