-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.zig
197 lines (169 loc) · 7.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
const std = @import ("std");
const toolbox = @import ("toolbox");
const Paths = struct
{
// prefixed attributes
__tmp: [] const u8 = undefined,
__wayland: [] const u8 = undefined,
// mandatory getters
pub fn getTmp (self: @This ()) [] const u8 { return self.__tmp; }
pub fn getWayland (self: @This ()) [] const u8 { return self.__wayland; }
// mandatory init
pub fn init (builder: *std.Build) !@This ()
{
var self = @This () {
.__wayland = try builder.build_root.join (builder.allocator,
&.{ "wayland", }),
};
self.__tmp = try std.fs.path.join (builder.allocator,
&.{ self.getWayland (), "tmp", });
return self;
}
};
fn update_wayland (builder: *std.Build, path: *const Paths,
dependencies: *const toolbox.Dependencies) !void
{
const tmp_src_path =
try std.fs.path.join (builder.allocator, &.{ path.getTmp (), "src", });
const xml_path = try std.fs.path.join (builder.allocator,
&.{ path.getTmp (), "protocol", "wayland.xml", });
try toolbox.make (path.getWayland ());
try dependencies.clone (builder, "wayland", path.getTmp ());
var tmp_dir =
try std.fs.openDirAbsolute (tmp_src_path, .{ .iterate = true, });
defer tmp_dir.close ();
var it = tmp_dir.iterate ();
while (try it.next ()) |*entry|
{
if ((std.mem.startsWith (u8, entry.name, "wayland-client") or
std.mem.startsWith (u8, entry.name, "wayland-server") or
std.mem.startsWith (u8, entry.name, "wayland-util")) and
!std.mem.endsWith (u8, entry.name, "private.h") and
toolbox.isCHeader (entry.name) and entry.kind == .file)
try toolbox.copy (try std.fs.path.join (builder.allocator,
&.{ tmp_src_path, entry.name, }), try std.fs.path.join (
builder.allocator, &.{ path.getWayland (), entry.name, }));
}
const wayland_version = try toolbox.reference (builder, "wayland");
var wayland_version_h = try tmp_dir.readFileAlloc (
builder.allocator, "wayland-version.h.in", std.math.maxInt (usize));
wayland_version_h = try std.mem.replaceOwned (u8, builder.allocator,
wayland_version_h, "@WAYLAND_VERSION@", wayland_version);
var tokit = std.mem.tokenizeScalar (u8, wayland_version, '.');
const match = [_][] const u8 { "@WAYLAND_VERSION_MAJOR@",
"@WAYLAND_VERSION_MINOR@", "@WAYLAND_VERSION_MICRO@", };
var index: usize = 0;
while (tokit.next ()) |*token|
{
wayland_version_h = try std.mem.replaceOwned (u8, builder.allocator,
wayland_version_h, match [index], token.*);
index += 1;
}
try toolbox.write (path.getWayland (), "wayland-version.h",
wayland_version_h);
try toolbox.run (builder, .{ .argv = &[_][] const u8 { "wayland-scanner",
"server-header", xml_path, try std.fs.path.join (builder.allocator,
&.{ path.getWayland (), "wayland-server-protocol.h", }), }, });
try toolbox.run (builder, .{ .argv = &[_][] const u8 { "wayland-scanner",
"client-header", xml_path, try std.fs.path.join (builder.allocator,
&.{ path.getWayland (), "wayland-client-protocol.h", }), }, });
try toolbox.run (builder, .{ .argv = &[_][] const u8 { "wayland-scanner",
"private-code", xml_path, try std.fs.path.join (builder.allocator,
&.{ path.getWayland (), "wayland-client-protocol-code.h", }), }, });
try std.fs.deleteTreeAbsolute (path.getTmp ());
}
fn update_protocols (builder: *std.Build, path: *const Paths,
dependencies: *const toolbox.Dependencies) !void
{
try dependencies.clone (builder, "wayland-protocols", path.getTmp ());
for ([_] struct { name: [] const u8, xml: [] const u8, }
{
.{ .name = "xdg-shell", .xml = try std.fs.path.join (builder.allocator,
&.{ path.getTmp (), "stable", "xdg-shell", "xdg-shell.xml", }), },
.{ .name = "xdg-decoration-unstable-v1", .xml = try std.fs.path.join (
builder.allocator, &.{ path.getTmp (), "unstable", "xdg-decoration",
"xdg-decoration-unstable-v1.xml", }), },
.{ .name = "viewporter", .xml = try std.fs.path.join (builder.allocator,
&.{ path.getTmp (), "stable", "viewporter", "viewporter.xml", }), },
.{ .name = "relative-pointer-unstable-v1", .xml = try std.fs.path.join (
builder.allocator, &.{ path.getTmp (), "unstable", "relative-pointer",
"relative-pointer-unstable-v1.xml", }), },
.{ .name = "pointer-constraints-unstable-v1", .xml =
try std.fs.path.join (builder.allocator, &.{ path.getTmp (), "unstable",
"pointer-constraints", "pointer-constraints-unstable-v1.xml", }), },
.{ .name = "fractional-scale-v1", .xml = try std.fs.path.join (
builder.allocator, &.{ path.getTmp (), "staging", "fractional-scale",
"fractional-scale-v1.xml", }), },
.{ .name = "xdg-activation-v1", .xml = try std.fs.path.join (
builder.allocator, &.{ path.getTmp (), "staging", "xdg-activation",
"xdg-activation-v1.xml", }), },
.{ .name = "idle-inhibit-unstable-v1", .xml = try std.fs.path.join (
builder.allocator, &.{ path.getTmp (), "unstable", "idle-inhibit",
"idle-inhibit-unstable-v1.xml", }), },
}) |gen|
{
const protocol_h = builder.fmt ("{s}-client-protocol.h", .{ gen.name, });
const protocol_code_h = builder.fmt ("{s}-client-protocol-code.h", .{ gen.name, });
try toolbox.run (builder, .{ .argv = &[_][] const u8 { "wayland-scanner",
"client-header", gen.xml, try std.fs.path.join (builder.allocator,
&.{ path.getWayland (), protocol_h, }), }, });
try toolbox.run (builder, .{ .argv = &[_][] const u8 { "wayland-scanner",
"private-code", gen.xml, try std.fs.path.join (builder.allocator,
&.{ path.getWayland (), protocol_code_h, }), }, });
}
try std.fs.deleteTreeAbsolute (path.getTmp ());
}
fn update (builder: *std.Build,
dependencies: *const toolbox.Dependencies) !void
{
const path = try Paths.init (builder);
std.fs.deleteTreeAbsolute (path.getWayland ()) catch |err|
{
switch (err)
{
error.FileNotFound => {},
else => return err,
}
};
try update_wayland (builder, &path, dependencies);
try update_protocols (builder, &path, dependencies);
try toolbox.clean (builder, &.{ "wayland", }, &.{});
}
pub fn build (builder: *std.Build) !void
{
const target = builder.standardTargetOptions (.{});
const optimize = builder.standardOptimizeOption (.{});
const dependencies = try toolbox.Dependencies.init (builder, "wayland.zig",
&.{ "wayland", },
.{
.toolbox = .{
.name = "tiawl/toolbox",
.host = toolbox.Repository.Host.github,
.ref = toolbox.Repository.Reference.tag,
},
}, .{
.wayland = .{
.name = "wayland/wayland",
.domain = "freedesktop.org",
.host = toolbox.Repository.Host.gitlab,
.ref = toolbox.Repository.Reference.tag,
},
.@"wayland-protocols" = .{
.name = "wayland/wayland-protocols",
.domain = "freedesktop.org",
.host = toolbox.Repository.Host.gitlab,
.ref = toolbox.Repository.Reference.tag,
},
});
if (builder.option (bool, "update", "Update binding") orelse false)
try update (builder, &dependencies);
const lib = builder.addStaticLibrary (.{
.name = "wayland",
.root_source_file = builder.addWriteFiles ().add ("empty.c", ""),
.target = target,
.optimize = optimize,
});
toolbox.addHeader (lib, try builder.build_root.join (builder.allocator,
&.{ "wayland", }), ".", &.{ ".h", });
builder.installArtifact (lib);
}