This repository was archived by the owner on Dec 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
346 lines (294 loc) · 11.7 KB
/
build.zig
File metadata and controls
346 lines (294 loc) · 11.7 KB
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
const std = @import("std");
const AvailableDep = struct { []const u8, []const u8 };
const AvailableDeps = []const AvailableDep;
pub const PhantomModule = struct {
// TODO: expected version of core and sdk
provides: ?Provides = null,
dependencies: ?[]const []const u8 = null,
pub const Provides = struct {
scenes: ?[]const []const u8 = null,
displays: ?[]const []const u8 = null,
pub fn value(self: Provides, kind: std.meta.FieldEnum(Provides)) []const []const u8 {
return (switch (kind) {
.scenes => self.scenes,
.displays => self.displays,
}) orelse &[_][]const u8{};
}
pub fn count(self: Provides, kind: std.meta.FieldEnum(Provides)) usize {
return self.value(kind).len;
}
};
pub fn getProvider(self: PhantomModule) Provides {
return if (self.provides) |value| value else .{};
}
pub fn getDependencies(self: PhantomModule) []const []const u8 {
return self.dependencies orelse &[_][]const u8{};
}
};
pub const availableDepenencies = blk: {
const buildDeps = @import("root").dependencies;
var count: usize = 0;
for (buildDeps.root_deps) |dep| {
const pkg = @field(buildDeps.packages, dep[1]);
if (@hasDecl(pkg, "build_zig")) {
const buildZig = pkg.build_zig;
if (@hasDecl(buildZig, "phantomModule") and @TypeOf(@field(buildZig, "phantomModule")) == PhantomModule) {
count += 1;
}
}
}
var i: usize = 0;
var deps: [count]AvailableDep = undefined;
for (buildDeps.root_deps) |dep| {
const pkg = @field(buildDeps.packages, dep[1]);
if (@hasDecl(pkg, "build_zig")) {
const buildZig = pkg.build_zig;
if (@hasDecl(buildZig, "phantomModule") and @TypeOf(@field(buildZig, "phantomModule")) == PhantomModule) {
deps[i] = dep;
i += 1;
}
}
}
break :blk deps;
};
pub fn TypeFor(comptime kind: std.meta.FieldEnum(PhantomModule.Provides)) type {
const buildDeps = @import("root").dependencies;
var fieldCount: usize = 0;
for (buildDeps.root_deps) |dep| {
const pkg = @field(buildDeps.packages, dep[1]);
if (@hasDecl(pkg, "build_zig")) {
const buildZig = pkg.build_zig;
if (@hasDecl(buildZig, "phantomModule") and @TypeOf(@field(buildZig, "phantomModule")) == PhantomModule) {
const mod = buildZig.phantomModule;
fieldCount += mod.getProvider().count(kind);
}
}
}
if (fieldCount == 0) {
return @Type(.{
.Enum = .{
.tag_type = u0,
.fields = &.{},
.decls = &.{},
.is_exhaustive = true,
},
});
}
var fields: [fieldCount]std.builtin.Type.EnumField = undefined;
var i: usize = 0;
for (buildDeps.root_deps) |dep| {
const pkg = @field(buildDeps.packages, dep[1]);
if (@hasDecl(pkg, "build_zig")) {
const buildZig = pkg.build_zig;
if (@hasDecl(buildZig, "phantomModule") and @TypeOf(@field(buildZig, "phantomModule")) == PhantomModule) {
const mod = buildZig.phantomModule;
for (mod.getProvider().value(kind)) |name| {
fields[i] = .{
.name = name,
.value = i,
};
i += 1;
}
}
}
}
return @Type(.{
.Enum = .{
.tag_type = std.math.IntFittingRange(0, fields.len - 1),
.fields = &fields,
.decls = &.{},
.is_exhaustive = true,
},
});
}
fn importPkgCustom(b: *std.Build, name: []const u8, buildRoot: []const u8, comptime pkgId: []const u8, comptime exclude: []const []const u8, args: anytype) *std.Build.Dependency {
const buildDeps = @import("root").dependencies;
const pkg = @field(buildDeps.packages, pkgId);
const deps: AvailableDeps = comptime blk: {
var count: usize = 0;
inline for (pkg.deps) |d| {
inline for (exclude) |e| {
if (!std.mem.eql(u8, d[0], e)) {
count += 1;
break;
}
}
}
var deps: [count]AvailableDep = undefined;
var i: usize = 0;
inline for (pkg.deps) |d| {
inline for (exclude) |e| {
if (!std.mem.eql(u8, d[0], e)) {
deps[i] = d;
i += 1;
break;
}
}
}
break :blk &deps;
};
return b.dependencyInner(name, buildRoot, if (@hasDecl(pkg, "build_zig")) pkg.build_zig else null, deps, args);
}
fn importPkg(b: *std.Build, name: []const u8, comptime pkgId: []const u8, args: anytype) *std.Build.Dependency {
const buildDeps = @import("root").dependencies;
const pkg = @field(buildDeps.packages, pkgId);
return b.dependencyInner(name, pkg.build_root, if (@hasDecl(pkg, "build_zig")) pkg.build_zig else null, pkg.deps, args);
}
pub fn Dependencies() type {
var fields: [availableDepenencies.len]std.builtin.Type.StructField = undefined;
for (availableDepenencies, &fields, 0..) |dep, *field, i| {
field.* = .{
.name = dep[0],
.type = *std.Build.Dependency,
.default_value = null,
.is_comptime = false,
.alignment = i,
};
}
return @Type(.{
.Struct = .{
.layout = .Auto,
.fields = &fields,
.decls = &.{},
.is_tuple = false,
},
});
}
pub fn dependencies(b: *std.Build, args: anytype) Dependencies() {
var self: Dependencies() = undefined;
inline for (availableDepenencies) |dep| {
@field(self, dep[0]) = importPkg(b, dep[0], dep[1], args);
}
return self;
}
inline fn doesExist(path: []const u8, flags: std.fs.File.OpenFlags) bool {
std.fs.accessAbsolute(path, flags) catch return false;
return true;
}
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const no_importer = b.option(bool, "no-importer", "disables the import system (not recommended)") orelse false;
const modulesRaw = b.option([]const []const u8, "modules", "List of modules") orelse &[_][]const u8{
"display.backends",
"gpu.backends",
"painting.image.formats",
"scene.backends",
"i18n",
};
if (no_importer) return;
const deps = dependencies(b, .{
.target = target,
.optimize = optimize,
.@"no-importer" = true,
});
const gen = b.addWriteFiles();
var importer_data = std.ArrayList(u8).init(b.allocator);
defer importer_data.deinit();
// TODO: as all modules are expected to follow the same layout as core,
// we can do a file/directory check for specific files.
// However, there could be some exceptions...
for (modulesRaw) |module| {
var modSplit = std.mem.splitAny(u8, module, ".");
while (modSplit.next()) |el| {
importer_data.writer().print(
\\pub const {s} = struct {{
, .{el}) catch |e| @panic(@errorName(e));
}
inline for (availableDepenencies) |dep| {
importer_data.writer().print(
\\pub usingnamespace blk: {{
\\ const imports = @import("{s}");
, .{dep[0]}) catch |e| @panic(@errorName(e));
modSplit.reset();
var i: usize = 0;
while (modSplit.next()) |el| {
const end = if (modSplit.index) |x| x + 1 else module.len - 1;
const y = std.mem.lastIndexOfLinear(u8, module[0..end], ".") orelse end;
const d = if (i < 1) "" else b.fmt(".{s}", .{module[0..y]});
importer_data.writer().print(
\\if (@hasDecl(imports{s}, "{s}")) {{
, .{ d, el }) catch |e| @panic(@errorName(e));
i += 1;
}
importer_data.writer().print(
\\break :blk imports.{s};
, .{module}) catch |e| @panic(@errorName(e));
modSplit.reset();
while (modSplit.next()) |_| {
importer_data.writer().print(
\\}}
, .{}) catch |e| @panic(@errorName(e));
}
importer_data.writer().print(
\\ break :blk struct {{}};
\\}};
, .{}) catch |e| @panic(@errorName(e));
}
modSplit.reset();
while (modSplit.next()) |_| {
importer_data.writer().print(
\\}};
, .{}) catch |e| @panic(@errorName(e));
}
}
var importer_deps: [availableDepenencies.len]std.Build.ModuleDependency = undefined;
inline for (availableDepenencies, 0..) |dep, i| {
const pkg = @field(@import("root").dependencies.packages, dep[1]);
const imported_dep = @field(deps, dep[0]);
const origModule = imported_dep.module(dep[0]);
// TODO: expected version check
var depsList = std.ArrayList(std.Build.ModuleDependency).init(b.allocator);
errdefer depsList.deinit();
const phantomCore = blk: {
inline for (pkg.deps) |childDeps| {
if (std.mem.eql(u8, childDeps[0], "phantom")) {
const subpkg = @field(@import("root").dependencies.packages, childDeps[1]);
// TODO: expected version check
const newPath = b.pathJoin(&.{ b.cache_root.path.?, "p", std.fs.path.basename(dep[1]), std.fs.path.basename(childDeps[1]) });
if (!doesExist(std.fs.path.dirname(newPath).?, .{})) {
std.fs.cwd().makePath(std.fs.path.dirname(newPath).?) catch |e| std.debug.panic("Failed to create path {s}: {s}", .{ std.fs.path.dirname(newPath).?, @errorName(e) });
}
if (!doesExist(newPath, .{})) {
std.fs.symLinkAbsolute(subpkg.build_root, newPath, .{ .is_directory = true }) catch |e| std.debug.panic("Failed to create symlink {s}: {s}", .{ newPath, @errorName(e) });
}
break :blk importPkgCustom(origModule.builder, childDeps[0], newPath, childDeps[1], &.{"phantom-sdk"}, .{
.target = target,
.optimize = optimize,
.@"no-importer" = true,
});
}
}
break :blk null;
};
if (phantomCore) |m| {
depsList.append(.{
.name = "phantom",
.module = m.module("phantom"),
}) catch @panic("OOM");
}
for (pkg.build_zig.phantomModule.getDependencies()) |depName| {
if (std.mem.eql(u8, depName, "phantom")) continue;
depsList.append(.{
.name = depName,
.module = origModule.builder.dependency(depName, .{
.target = target,
.optimize = optimize,
}).module(depName),
}) catch @panic("OOM");
}
importer_deps[i] = .{
.name = dep[0],
.module = imported_dep.builder.createModule(.{
.source_file = origModule.source_file,
.dependencies = depsList.items,
}),
};
}
const importer_gen = gen.add("phantom.imports.zig", importer_data.items);
_ = b.addModule("phantom.imports", .{
.source_file = importer_gen,
.dependencies = &importer_deps,
});
b.getInstallStep().dependOn(&gen.step);
}