-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
common.zig
400 lines (377 loc) 路 15.3 KB
/
common.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
const std = @import("std");
const string = []const u8;
const builtin = @import("builtin");
const ansi = @import("ansi");
const root = @import("root");
const zigmod = @import("./lib.zig");
const u = @import("./util/index.zig");
const build_options = if (@hasDecl(root, "build_options")) root.build_options else struct {};
const bootstrap = if (@hasDecl(build_options, "bootstrap")) build_options.bootstrap else false;
//
//
pub const CollectOptions = struct {
log: bool,
update: bool,
lock: ?[]const [4]string = null,
alloc: std.mem.Allocator,
already_fetched: *std.ArrayList(string) = undefined,
pub fn init(self: *CollectOptions) !void {
self.already_fetched = try self.alloc.create(std.ArrayList(string));
self.already_fetched.* = std.ArrayList(string).init(self.alloc);
}
};
pub fn collect_deps_deep(cachepath: string, mdir: std.fs.Dir, options: *CollectOptions) !zigmod.Module {
try std.fs.cwd().makePath(cachepath);
const m = try zigmod.ModFile.from_dir(options.alloc, mdir);
try options.init();
var moduledeps = std.ArrayList(zigmod.Module).init(options.alloc);
defer moduledeps.deinit();
if (m.root_files.len > 0) {
try moduledeps.append(try add_files_package(options.alloc, cachepath, "root", mdir, m.root_files));
}
try moduledeps.append(try collect_deps(cachepath, mdir, options));
for (m.rootdeps) |*d| {
if (try get_module_from_dep(d, cachepath, options)) |founddep| {
try moduledeps.append(founddep);
}
}
for (m.builddeps) |*d| {
if (try get_module_from_dep(d, cachepath, options)) |founddep| {
try moduledeps.append(founddep);
}
}
return zigmod.Module{
.is_sys_lib = false,
.is_framework = false,
.id = "root",
.name = "root",
.main = m.main,
.deps = moduledeps.toOwnedSlice(),
.clean_path = "",
.yaml = m.yaml,
.dep = null,
.min_zig_version = m.min_zig_version,
.vcpkg = m.vcpkg,
};
}
pub fn collect_deps(cachepath: string, mdir: std.fs.Dir, options: *CollectOptions) anyerror!zigmod.Module {
try std.fs.cwd().makePath(cachepath);
const m = try zigmod.ModFile.from_dir(options.alloc, mdir);
var moduledeps = std.ArrayList(zigmod.Module).init(options.alloc);
defer moduledeps.deinit();
if (m.files.len > 0) {
try moduledeps.append(try add_files_package(options.alloc, cachepath, m.id, mdir, m.files));
}
for (m.deps) |*d| {
if (try get_module_from_dep(d, cachepath, options)) |founddep| {
try moduledeps.append(founddep);
}
}
return zigmod.Module{
.is_sys_lib = false,
.is_framework = false,
.id = m.id,
.name = m.name,
.main = m.main,
.c_include_dirs = m.c_include_dirs,
.c_source_flags = m.c_source_flags,
.c_source_files = m.c_source_files,
.deps = moduledeps.toOwnedSlice(),
.clean_path = "../..",
.yaml = m.yaml,
.dep = null,
.min_zig_version = m.min_zig_version,
.vcpkg = m.vcpkg,
};
}
pub fn collect_pkgs(mod: zigmod.Module, list: *std.ArrayList(zigmod.Module)) anyerror!void {
if (u.list_contains_gen(zigmod.Module, list.items, mod)) {
return;
}
try list.append(mod);
for (mod.deps) |d| {
try collect_pkgs(d, list);
}
}
pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) !string {
const p = try std.fs.path.join(options.alloc, &.{ cachepath, try d.clean_path(options.alloc) });
const pv = try std.fs.path.join(options.alloc, &.{ cachepath, try d.clean_path_v(options.alloc) });
const nocache = d.type.isLocal();
if (!nocache and u.list_contains(options.already_fetched.items, p)) return p;
if (!nocache and u.list_contains(options.already_fetched.items, pv)) return pv;
if (options.log and d.type != .local) {
std.debug.print("fetch: {s}: {s}\n", .{ @tagName(d.type), d.path });
}
defer {
if (!bootstrap and options.log and d.type != .local) {
std.debug.print("{s}", .{ansi.csi.CursorUp(1)});
std.debug.print("{s}", .{ansi.csi.EraseInLine(0)});
}
}
switch (d.type) {
.local => {
if (!std.mem.endsWith(u8, d.main, ".zig")) {
return d.main;
}
return d.path;
},
.system_lib, .framework => {
// no op
return "";
},
.git => {
if (d.version.len > 0) {
const vers = u.parse_split(zigmod.DepType.Version.Git, "-").do(d.version) catch |e| switch (e) {
error.IterEmpty => unreachable,
error.NoMemberFound => {
const vtype = d.version[0..std.mem.indexOf(u8, d.version, "-").?];
u.fail("fetch: git: version type '{s}' is invalid.", .{vtype});
},
};
if (try u.does_folder_exist(pv)) {
if (vers.id == .branch) {
if (options.update) {
try d.type.update(options.alloc, pv, d.path);
}
}
return pv;
}
try d.type.pull(options.alloc, d.path, pv);
if ((try u.run_cmd(options.alloc, pv, &.{ "git", "checkout", vers.string })) > 0) {
u.fail("fetch: git: {s}: {s} {s} does not exist", .{ d.path, @tagName(vers.id), vers.string });
}
if (builtin.os.tag != .windows and vers.id != .branch) {
const pvd = try std.fs.cwd().openDir(pv, .{});
try pvd.deleteTree(".git");
}
return pv;
}
if (!try u.does_folder_exist(p)) {
try d.type.pull(options.alloc, d.path, p);
} else {
if (options.update) {
try d.type.update(options.alloc, p, d.path);
}
}
return p;
},
.hg => {
if (!try u.does_folder_exist(p)) {
try d.type.pull(options.alloc, d.path, p);
} else {
if (options.update) {
try d.type.update(options.alloc, p, d.path);
}
}
return p;
},
.http => {
if (try u.does_folder_exist(pv)) {
return pv;
}
const file_name = try u.last(try u.split(options.alloc, d.path, "/"));
if (d.version.len > 0) {
if (try u.does_folder_exist(pv)) {
return pv;
}
const file_path = try std.fs.path.join(options.alloc, &.{ pv, file_name });
try d.type.pull(options.alloc, d.path, pv);
if (try u.validate_hash(options.alloc, d.version, file_path)) {
try std.fs.cwd().deleteFile(file_path);
return pv;
}
try std.fs.cwd().deleteTree(pv);
u.fail("{s} does not match hash {s}", .{ d.path, d.version });
return p;
}
if (try u.does_folder_exist(p)) {
try std.fs.cwd().deleteTree(p);
}
const file_path = try std.fs.path.resolve(options.alloc, &.{ p, file_name });
try d.type.pull(options.alloc, d.path, p);
try std.fs.deleteFileAbsolute(file_path);
return p;
},
}
}
pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectOptions) anyerror!?zigmod.Module {
if (options.lock) |lock| {
for (lock) |item| {
if (std.mem.eql(u8, item[0], try d.clean_path(options.alloc))) {
d.type = std.meta.stringToEnum(zigmod.DepType, item[1]).?;
d.path = item[2];
d.version = item[3];
break;
}
}
}
if (!d.is_for_this()) return null;
const modpath = try get_modpath(cachepath, d.*, options);
const moddir = if (std.mem.eql(u8, modpath, "files") or modpath.len == 0) try std.fs.cwd().openDir(cachepath, .{}) else try std.fs.cwd().openDir(modpath, .{});
const nocache = d.type.isLocal();
if (!nocache) try options.already_fetched.append(modpath);
switch (d.type) {
.system_lib, .framework => {
return zigmod.Module{
.is_sys_lib = d.type == .system_lib,
.is_framework = d.type == .framework,
.id = try u.do_hash(options.alloc, std.crypto.hash.sha3.Sha3_384, d.path),
.name = d.path,
.only_os = d.only_os,
.except_os = d.except_os,
.main = "",
.deps = &[_]zigmod.Module{},
.clean_path = d.path,
.yaml = null,
.dep = d.*,
.for_build = d.for_build,
.min_zig_version = null,
.vcpkg = false,
};
},
else => {
var dd = try collect_deps(cachepath, moddir, options) catch |e| switch (e) {
error.ManifestNotFound => {
if (d.main.len > 0 or d.c_include_dirs.len > 0 or d.c_source_files.len > 0 or d.keep) {
var mod_from = try zigmod.Module.from(options.alloc, d.*, modpath, options);
if (d.type != .local) mod_from.clean_path = u.trim_prefix(modpath, cachepath)[1..];
if (mod_from.is_for_this()) return mod_from;
return null;
}
const moddirO = try std.fs.cwd().openDir(modpath, .{});
const tryname = try u.detect_pkgname(options.alloc, d.name, modpath);
const trymain = u.detct_mainfile(options.alloc, d.main, moddirO, tryname) catch |err| switch (err) {
error.CantFindMain => null,
else => return err,
};
if (trymain) |_| {
d.*.name = tryname;
d.*.main = trymain.?;
var mod_from = try zigmod.Module.from(options.alloc, d.*, cachepath, options);
if (d.type != .local) mod_from.clean_path = u.trim_prefix(modpath, cachepath)[1..];
if (mod_from.is_for_this()) return mod_from;
return null;
}
u.fail("no zig.mod or zigmod.yml found and no override props defined. unable to use add this dependency!", .{});
},
else => e,
};
dd.dep = d.*;
dd.for_build = d.for_build;
const save = dd;
if (d.type != .local) dd.clean_path = u.trim_prefix(modpath, cachepath)[1..];
if (dd.id.len == 0) dd.id = try u.random_string(options.alloc, 48);
if (d.name.len > 0) dd.name = d.name;
if (d.main.len > 0) dd.main = d.main;
if (d.c_include_dirs.len > 0) dd.c_include_dirs = d.c_include_dirs;
if (d.c_source_flags.len > 0) dd.c_source_flags = d.c_source_flags;
if (d.c_source_files.len > 0) dd.c_source_files = d.c_source_files;
if (d.only_os.len > 0) dd.only_os = d.only_os;
if (d.except_os.len > 0) dd.except_os = d.except_os;
if (d.type == .local) dd.main = try std.fs.path.join(options.alloc, &.{ d.main, save.main });
if (std.mem.eql(u8, modpath, "files")) dd.clean_path = modpath;
if (dd.is_for_this()) return dd;
return null;
},
}
}
pub fn add_files_package(alloc: std.mem.Allocator, cachepath: string, pkg_name: string, mdir: std.fs.Dir, dirs: []const string) !zigmod.Module {
const fname = try std.mem.join(alloc, "", &.{ pkg_name, ".zig" });
var map = std.StringHashMap(string).init(alloc);
defer map.deinit();
for (dirs) |dir_path| {
const dir = try mdir.openIterableDir(dir_path, .{});
var walker = try dir.walk(alloc);
defer walker.deinit();
while (try walker.next()) |p| {
if (p.kind == .Directory) {
continue;
}
const path = try alloc.dupe(u8, p.path);
try map.put(path, try std.fmt.allocPrint(alloc, "{s}/{s}", .{ dir_path, path }));
}
}
const cwdpath = try std.fs.cwd().realpathAlloc(alloc, ".");
const mpath = try mdir.realpathAlloc(alloc, ".");
var fpath = u.trim_prefix(mpath, cwdpath);
if (fpath.len == 0) fpath = std.fs.path.sep_str;
var cachedir = try std.fs.cwd().openDir(cachepath, .{});
defer cachedir.close();
try cachedir.makePath("files");
var destdir = try cachedir.openDir("files", .{});
defer destdir.close();
const rff = try destdir.createFile(fname, .{});
defer rff.close();
const w = rff.writer();
try w.print("const srcpath = \"../../../{}\";\n\n", .{std.zig.fmtEscapes(fpath[1..])});
var iter = map.iterator();
while (iter.next()) |item| {
try w.print("pub const @\"/{}\" = @embedFile(srcpath ++ \"/{}\");\n", .{ std.zig.fmtEscapes(item.key_ptr.*), std.zig.fmtEscapes(item.value_ptr.*) });
}
var d: zigmod.Dep = .{
.type = .local,
.path = "files",
.id = "",
.name = "self/files",
.main = fname,
.version = "absolute",
.yaml = null,
.deps = &.{},
};
var options = CollectOptions{
.log = false,
.update = false,
.alloc = alloc,
};
const filesdestpath = try std.fs.path.join(alloc, &.{ cachepath, "files" });
return (try get_module_from_dep(&d, filesdestpath, &options)).?;
}
pub fn parse_lockfile(alloc: std.mem.Allocator, dir: std.fs.Dir) ![]const [4]string {
var list = std.ArrayList([4]string).init(alloc);
const max = std.math.maxInt(usize);
if (!try u.does_file_exist(dir, "zigmod.lock")) return &[_][4]string{};
const f = try dir.openFile("zigmod.lock", .{});
const r = f.reader();
var i: usize = 0;
var v: usize = 1;
while (try r.readUntilDelimiterOrEofAlloc(alloc, '\n', max)) |line| : (i += 1) {
if (i == 0 and std.mem.eql(u8, line, "2")) {
v = 2;
continue;
}
switch (v) {
1 => {
var iter = std.mem.split(u8, line, " ");
try list.append([4]string{
iter.next().?,
iter.next().?,
iter.next().?,
iter.next().?,
});
},
2 => {
var iter = std.mem.split(u8, line, " ");
const asdep = zigmod.Dep{
.type = std.meta.stringToEnum(zigmod.DepType, iter.next().?).?,
.path = iter.next().?,
.version = iter.next().?,
.id = "",
.name = "",
.main = "",
.yaml = null,
.deps = &.{},
};
try list.append([4]string{
try asdep.clean_path(alloc),
@tagName(asdep.type),
asdep.path,
asdep.version,
});
},
else => {
u.fail("invalid zigmod.lock version: {d}", .{v});
},
}
}
return list.toOwnedSlice();
}