forked from swaywm/wlroots
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.zig
429 lines (408 loc) · 18.6 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
const std = @import("std");
const WlScannerStep = @import("wlscannerstep").WlScannerStep;
fn prependToAll(this: []const u8, tothat: anytype) []const []const u8 {
const thattype = @TypeOf(tothat);
const thatinfo = @typeInfo(thattype);
var new: [thatinfo.Struct.fields.len][]const u8 = undefined;
inline for (thatinfo.Struct.fields, 0..) |field, idx| {
new[idx] = this ++ @field(tothat, field.name);
}
return &new;
}
const WlrootsBuild = struct {
lib: *std.Build.Step.Compile,
pub_config: *std.Build.Step.ConfigHeader,
write_config_step: *std.Build.Step.WriteFile,
fn addPubConsumer(self: *WlrootsBuild, consumer: *std.Build.Step.Compile) void {
consumer.addConfigHeader(self.pub_config);
consumer.addIncludePath(.{ .generated = &self.write_config_step.generated_directory });
}
};
const WlrootsIncludeShadersStep = struct {
const ShaderApi = enum { gles, vulkan };
const ShaderQueue = std.SinglyLinkedList(struct { api: ShaderApi, file: std.Build.LazyPath });
const Self = @This();
step: std.Build.Step,
output_dir: std.Build.GeneratedFile,
shaders: ShaderQueue,
glslangvalidator: []const u8,
pub fn init(b: *std.Build) !*Self {
var res = b.allocator.create(Self) catch @panic("OOM");
res.step = std.Build.Step.init(.{ .id = .custom, .name = "wlroots shaders", .owner = b, .makeFn = make });
res.output_dir = .{
.step = &res.step,
};
res.shaders = .{};
res.glslangvalidator = b.findProgram(&.{"glslangValidator"}, &.{}) catch |err| {
std.log.err("Couldn't find glslangValidator. Make sure it's in your PATH!", .{});
return err;
};
return res;
}
fn addShader(self: *Self, api: ShaderApi, file: std.Build.LazyPath) void {
var shader = self.step.owner.allocator.create(ShaderQueue.Node) catch @panic("OOM");
shader.data.api = api;
shader.data.file = file;
self.shaders.prepend(shader);
}
fn addShaders(self: *Self, api: ShaderApi, files: []const std.Build.LazyPath) void {
for (files) |file| {
self.addShader(api, file);
}
}
fn writeGlesShader(in: *std.fs.File, out: *std.fs.File, name: []const u8) !void {
var buf_writer = std.io.bufferedWriter(out.writer());
var buf_reader = std.io.bufferedReader(in.reader());
var writer = buf_writer.writer();
var reader = buf_reader.reader();
try writer.print("static const char {s}[] = {{\n", .{name});
var readbuf: [1]u8 = undefined;
while (try reader.read(readbuf[0..]) == 1) {
try writer.print("\t0x{x:0>2},\n", .{readbuf[0]});
}
try writer.writeAll("\t0x00,\n");
try writer.writeAll("};\n");
try buf_writer.flush();
}
fn makeGles(output_dir: *std.fs.Dir, path: []const u8) !void {
var out_name_buf: [256]u8 = undefined;
const out_name = try std.fmt.bufPrint(out_name_buf[0..], "{s}_{s}_src.h", .{ std.fs.path.stem(path), std.fs.path.extension(path)[1..] });
var out = try output_dir.createFile(out_name, .{});
defer out.close();
var in = try std.fs.cwd().openFile(path, .{});
defer in.close();
try WlrootsIncludeShadersStep.writeGlesShader(&in, &out, out_name[0 .. out_name.len - 2]);
}
fn makeVulkan(self: *Self, output_dir: []const u8, path: []const u8) !void {
const ext = std.fs.path.extension(path)[1..];
var out_name_buf: [128]u8 = undefined;
var out_data_name_buf: [128]u8 = undefined;
const out_name = try std.fmt.bufPrint(out_name_buf[0..], "{s}.{s}.h", .{ std.fs.path.stem(path), ext });
const out_data_name = try std.fmt.bufPrint(&out_data_name_buf, "{s}_{s}_data", .{ std.fs.path.stem(path), ext });
var child = std.ChildProcess.init(&.{ self.glslangvalidator, "-V", path, "-o", out_name, "--vn", out_data_name }, self.step.owner.allocator);
child.cwd = output_dir;
try child.spawn();
_ = try child.wait();
}
pub fn make(step: *std.Build.Step, progress: *std.Progress.Node) !void {
_ = progress;
const self = @fieldParentPtr(Self, "step", step);
var it = self.shaders.first;
if (it == null) {
return;
}
// For now this regens all shaders even if just one is changed
// Worth fixing?
var cache = self.step.owner.graph.cache.obtain();
defer cache.deinit();
cache.hash.addBytes("shader_asdfg");
while (it) |node| : (it = node.next) {
_ = try cache.addFile(node.data.file.getPath(step.owner), null);
}
self.step.result_cached = try cache.hit();
self.output_dir.path = try step.owner.cache_root.join(step.owner.allocator, &.{ "shaders", &cache.final() });
if (self.step.result_cached) {
return;
}
it = self.shaders.first;
var output_dir = try std.fs.cwd().makeOpenPath(self.output_dir.path.?, .{});
defer output_dir.close();
var vulkan_out_dir = try output_dir.makeOpenPath("render/vulkan/shaders", .{});
defer vulkan_out_dir.close();
const vulkan_out_dir_path = try vulkan_out_dir.realpathAlloc(self.step.owner.allocator, ".");
while (it) |node| : (it = node.next) {
switch (node.data.api) {
.vulkan => try self.makeVulkan(vulkan_out_dir_path, node.data.file.getPath(step.owner)),
.gles => try Self.makeGles(&output_dir, node.data.file.getPath(step.owner)),
}
}
try cache.writeManifest();
}
};
fn addRecursiveCFiles(lib: *std.Build.Step.Compile, base: std.fs.Dir, dirname: []const u8) !void {
const b = lib.step.owner;
var iterdir = try base.openDir(dirname, .{ .iterate = true });
defer iterdir.close();
var walker = try iterdir.walk(b.allocator);
while (try walker.next()) |d| {
if (d.kind == .file) {
const ext = std.fs.path.extension(d.path);
if (std.mem.eql(u8, ext, ".c")) {
var buf: [512]u8 = undefined;
// fix this weird path stuff..
const file = try std.fmt.bufPrint(buf[0..], "{s}/{s}", .{ dirname, d.path });
lib.addCSourceFile(.{ .file = .{ .path = file }, .flags = &.{} });
}
}
}
}
const GenPnpIdsStep = struct {
step: std.Build.Step,
out_file: std.Build.GeneratedFile,
const out_file_name = "pnpids.c";
pub fn init(b: *std.Build) *GenPnpIdsStep {
const step = b.allocator.create(GenPnpIdsStep) catch @panic("OOM");
step.* = .{ .step = std.Build.Step.init(.{ .id = .custom, .name = "generate " ++ out_file_name, .owner = b, .makeFn = make }), .out_file = .{ .step = &step.step } };
return step;
}
fn getPnpIdsPath(b: *std.Build) ![]const u8 {
// More pkg-config stuff, ugh.
var code: u8 = undefined;
const pkgdatadir = try b.runAllowFail(&.{ "pkg-config", "--variable=pkgdatadir", "hwdata" }, &code, .Ignore);
return b.pathJoin(&.{ pkgdatadir[0 .. pkgdatadir.len - 1], "pnp.ids" });
}
pub fn make(step: *std.Build.Step, progress: *std.Progress.Node) !void {
_ = progress;
const self = @fieldParentPtr(GenPnpIdsStep, "step", step);
var man = step.owner.graph.cache.obtain();
defer man.deinit();
const in_path = try getPnpIdsPath(step.owner);
man.hash.add(@as(u32, 0x275fffa));
_ = try man.addFile(in_path, null);
if (try step.cacheHit(&man)) {
const digest = man.final();
self.out_file.path = try step.owner.cache_root.join(step.owner.allocator, &.{ "o", &digest, out_file_name });
return;
}
const digest = man.final();
const out_path = "o" ++ std.fs.path.sep_str ++ digest;
const infile = try std.fs.cwd().openFile(in_path, .{});
defer infile.close();
var outdir = try step.owner.cache_root.handle.makeOpenPath(out_path, .{});
defer outdir.close();
const outfile = try outdir.createFile(out_file_name, .{});
defer outfile.close();
var bufwrit = std.io.bufferedWriter(outfile.writer());
var stdout = bufwrit.writer();
var br = std.io.bufferedReader(infile.reader());
const reader = br.reader();
var readBuf: [256]u8 = undefined;
var line: ?[]u8 = reader.readUntilDelimiter(readBuf[0..], '\n') catch null;
try stdout.writeAll(
\\#include "backend/drm/util.h"
\\#define PNP_ID(a, b, c) ((a & 0x1f) << 10) | ((b & 0x1f) << 5) | (c & 0x1f)
\\const char *get_pnp_manufacturer(const char code[static 3]) {
\\ switch (PNP_ID(code[0], code[1], code[2])) {
\\
);
while (line) |l| {
const tabpos = std.mem.indexOf(u8, l, "\t") orelse continue;
const id = l[0..tabpos];
const fullname = l[tabpos + 1 ..];
try stdout.print(" case PNP_ID('{c}', '{c}', '{c}'): return \"{s}\";\n", .{ id[0], id[1], id[2], fullname });
line = reader.readUntilDelimiter(readBuf[0..], '\n') catch null;
}
try stdout.writeAll(
\\ }
\\ return NULL;
\\}
\\#undef PNP_ID
\\
);
try bufwrit.flush();
self.out_file.path = try step.owner.cache_root.join(step.owner.allocator, &.{ "o", &digest, out_file_name });
try step.writeManifest(&man);
}
};
fn findXwayland(b: *std.Build) []const u8 {
return b.findProgram(&.{"Xwayland"}, &.{}) catch {
std.debug.print("Couldn't find Xwayland. Either set xwayland-path or disable Xwayland support.\n", .{});
std.process.exit(1);
};
}
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const wlrscannerstep = try WlScannerStep.create(b, .{ .target = target, .optimize = optimize, .server_headers = true });
const lib = b.addStaticLibrary(.{
.name = "wlrootsx",
.optimize = optimize,
.target = target
});
b.installArtifact(lib);
const xwayland_enabled = b.option(bool, "xwayland", "Xwayland support") orelse true;
const xwayland_path_opt = b.option([]const u8, "xwayland_path", "Path to Xwayland binary") orelse "auto";
const xwayland_path = if (std.mem.eql(u8, xwayland_path_opt, "auto")) findXwayland(b) else xwayland_path_opt;
lib.addCSourceFiles(.{ .files = comptime prependToAll("backend/drm/", .{
"atomic.c",
"backend.c",
"drm.c",
"fb.c",
"legacy.c",
"monitor.c",
"properties.c",
"renderer.c",
"util.c",
}) });
try addRecursiveCFiles(lib, b.build_root.handle, "backend/headless");
try addRecursiveCFiles(lib, b.build_root.handle, "backend/libinput");
try addRecursiveCFiles(lib, b.build_root.handle, "backend/multi");
try addRecursiveCFiles(lib, b.build_root.handle, "backend/session");
try addRecursiveCFiles(lib, b.build_root.handle, "backend/wayland");
try addRecursiveCFiles(lib, b.build_root.handle, "backend/x11");
// Ugly hardcoded path...
lib.addCSourceFile(.{ .file = .{ .path = "backend/backend.c" }, .flags = &.{} });
try addRecursiveCFiles(lib, b.build_root.handle, "render");
try addRecursiveCFiles(lib, b.build_root.handle, "types");
try addRecursiveCFiles(lib, b.build_root.handle, "util");
try addRecursiveCFiles(lib, b.build_root.handle, "xcursor");
lib.linkLibC();
lib.linkSystemLibrary("pixman-1");
lib.linkSystemLibrary("libdrm");
lib.linkSystemLibrary("libinput");
lib.linkSystemLibrary("xkbcommon");
if (xwayland_enabled) {
try addRecursiveCFiles(lib, b.build_root.handle, "xwayland");
lib.linkSystemLibrary("xcb-composite");
lib.linkSystemLibrary("xcb-icccm");
lib.linkSystemLibrary("xcb-res");
lib.linkSystemLibrary("xcb-shape");
lib.linkSystemLibrary("xcb-ewmh");
}
lib.linkSystemLibrary("xcb");
lib.linkSystemLibrary("xcb-dri3");
lib.linkSystemLibrary("xcb-xfixes");
lib.linkSystemLibrary("xcb-shm");
lib.linkSystemLibrary("xcb-renderutil");
lib.linkSystemLibrary("xcb-errors");
lib.linkSystemLibrary("xcb-xinput");
lib.linkSystemLibrary("xcb-present");
lib.linkSystemLibrary("libudev");
lib.linkSystemLibrary("gbm");
lib.linkSystemLibrary("libdisplay-info");
lib.linkSystemLibrary("vulkan");
lib.linkSystemLibrary("egl");
lib.linkSystemLibrary("gl");
lib.linkSystemLibrary("libseat");
lib.linkSystemLibrary("wayland-server");
lib.linkSystemLibrary("wayland-client");
wlrscannerstep.linkWith(lib);
wlrscannerstep.addSystemProtocols(&.{
"stable/presentation-time/presentation-time.xml",
"stable/viewporter/viewporter.xml",
"stable/xdg-shell/xdg-shell.xml",
"stable/linux-dmabuf/linux-dmabuf-v1.xml",
"unstable/pointer-constraints/pointer-constraints-unstable-v1.xml",
"unstable/pointer-gestures/pointer-gestures-unstable-v1.xml",
"unstable/relative-pointer/relative-pointer-unstable-v1.xml",
"unstable/xdg-decoration/xdg-decoration-unstable-v1.xml",
"unstable/xdg-foreign/xdg-foreign-unstable-v2.xml",
"unstable/xdg-foreign/xdg-foreign-unstable-v1.xml",
"unstable/xdg-output/xdg-output-unstable-v1.xml",
"unstable/text-input/text-input-unstable-v3.xml",
"unstable/keyboard-shortcuts-inhibit/keyboard-shortcuts-inhibit-unstable-v1.xml",
"unstable/input-method/input-method-unstable-v1.xml",
"unstable/fullscreen-shell/fullscreen-shell-unstable-v1.xml",
"unstable/primary-selection/primary-selection-unstable-v1.xml",
"unstable/idle-inhibit/idle-inhibit-unstable-v1.xml",
"unstable/tablet/tablet-unstable-v2.xml",
"staging/xdg-activation/xdg-activation-v1.xml",
"staging/ext-session-lock/ext-session-lock-v1.xml",
"staging/ext-idle-notify/ext-idle-notify-v1.xml",
"staging/drm-lease/drm-lease-v1.xml",
"staging/single-pixel-buffer/single-pixel-buffer-v1.xml",
"staging/xwayland-shell/xwayland-shell-v1.xml",
"staging/fractional-scale/fractional-scale-v1.xml",
"staging/tearing-control/tearing-control-v1.xml",
"staging/security-context/security-context-v1.xml",
"staging/ext-transient-seat/ext-transient-seat-v1.xml",
"staging/ext-foreign-toplevel-list/ext-foreign-toplevel-list-v1.xml",
"staging/cursor-shape/cursor-shape-v1.xml",
"staging/content-type/content-type-v1.xml",
});
wlrscannerstep.addProtocolsFromPath(b.pathFromRoot("protocol"), &.{
"drm.xml",
"wlr-data-control-unstable-v1.xml",
"wlr-export-dmabuf-unstable-v1.xml",
"wlr-virtual-pointer-unstable-v1.xml",
"wlr-screencopy-unstable-v1.xml",
"wlr-gamma-control-unstable-v1.xml",
"wlr-output-management-unstable-v1.xml",
"wlr-output-power-management-unstable-v1.xml",
"virtual-keyboard-unstable-v1.xml",
"wlr-layer-shell-unstable-v1.xml",
"wlr-foreign-toplevel-management-unstable-v1.xml",
"server-decoration.xml",
"input-method-unstable-v2.xml",
});
lib.defineCMacro("WLR_USE_UNSTABLE", null);
const endian = target.result.cpu.arch.endian();
lib.defineCMacro("WLR_LITTLE_ENDIAN", if (endian == .little) "1" else "0");
lib.defineCMacro("WLR_BIG_ENDIAN", if (endian == .big) "1" else "0");
lib.addIncludePath(.{ .path = "include" });
// config..
// TODO: dynamically set all of these
const configoptions = .{
.HAVE_XCB_XFIXED_SET_CLIENT_DISCONNECT_MODE = true,
.HAVE_EGL = true,
.HAVE_GBM_BO_GET_FD_FOR_PLANE = true,
.HAVE_LIBINPUT_HOLD_GESTURES = true,
.HAVE_LIBINPUT_SCROLL_VALUE120 = true,
.HAVE_XCB_ERRORS = true,
.HAVE_XWAYLAND_FORCE_XRANDR_EMULATION = true,
.HAVE_XWAYLAND_LISTENFD = true,
.HAVE_XWAYLAND_NO_TOUCH_POINTER_EMULATION = true,
.HAVE_WAYLAND_TERMINATE_DELAY = true,
.XWAYLAND_PATH = xwayland_path,
.ICONDIR = "/usr/share/icons",
};
const wlrconfigoptions = .{
.WLR_HAS_DRM_BACKEND = true,
.WLR_HAS_LIBINPUT_BACKEND = true,
.WLR_HAS_X11_BACKEND = true,
.WLR_HAS_GLES2_RENDERER = true,
.WLR_HAS_VULKAN_RENDERER = true,
.WLR_HAS_GBM_ALLOCATOR = true,
.WLR_HAS_XWAYLAND = xwayland_enabled,
.WLR_HAS_SESSION = true,
};
const configheader = b.addConfigHeader(.{}, configoptions);
const wlrpubconfigheader = b.addConfigHeader(.{ .include_path = "wlr/config.h" }, wlrconfigoptions);
// This should be converted to ConfigHeaderStep. Maybe when it supports meson style.
const wlrversioncontent =
\\#ifndef WLR_VERSION_H
\\#define WLR_VERSION_H
\\#define WLR_VERSION_STR "ziginfinity"
\\#define WLR_VERSION_MAJOR 0
\\#define WLR_VERSION_MINOR 18
\\#define WLR_VERSION_MICRO 0
\\#define WLR_VERSION_NUM ((WLR_VERSION_MAJOR << 16) | (WLR_VERSION_MINOR << 8) | WLR_VERSION_MICRO)
\\#endif
\\
;
const writeconfig = b.addWriteFile("wlr/version.h", wlrversioncontent);
lib.addIncludePath(.{ .generated = &writeconfig.generated_directory });
const shaders = try WlrootsIncludeShadersStep.init(b);
shaders.addShaders(.gles, &.{
.{ .path = "render/gles2/shaders/common.vert" },
.{ .path = "render/gles2/shaders/quad.frag" },
.{ .path = "render/gles2/shaders/tex_external.frag" },
.{ .path = "render/gles2/shaders/tex_rgba.frag" },
.{ .path = "render/gles2/shaders/tex_rgbx.frag" },
});
shaders.addShaders(.vulkan, &.{
.{ .path = "render/vulkan/shaders/common.vert" },
.{ .path = "render/vulkan/shaders/output.frag" },
.{ .path = "render/vulkan/shaders/quad.frag" },
.{ .path = "render/vulkan/shaders/texture.frag" },
});
lib.addIncludePath(.{ .generated = &shaders.output_dir });
lib.addConfigHeader(wlrpubconfigheader);
lib.addConfigHeader(configheader);
const genpnp = GenPnpIdsStep.init(b);
lib.addCSourceFile(.{ .file = .{ .generated = &genpnp.out_file }, .flags = &.{} });
lib.installHeadersDirectoryOptions(.{
.source_dir = .{ .path = "include/wlr"},
.install_dir = .header,
.install_subdir = "wlr",
.include_extensions = &. {"h"}
});
lib.installConfigHeader(wlrpubconfigheader, .{});
const install_version_file = b.addInstallFileWithDir(
.{ .generated = &writeconfig.files.items[0].generated_file },
.header,
"wlr/version.h",
);
b.getInstallStep().dependOn(&install_version_file.step);
lib.installed_headers.append(&install_version_file.step) catch @panic("OOM");
}