-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
fetch.zig
409 lines (366 loc) 路 15.5 KB
/
fetch.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
const std = @import("std");
const string = []const u8;
const ansi = @import("ansi");
const root = @import("root");
const zigmod = @import("../lib.zig");
const u = @import("./../util/index.zig");
const common = @import("./../common.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 fn execute(args: [][]u8) !void {
//
const gpa = std.heap.c_allocator;
const cachepath = try std.fs.path.join(gpa, &.{ ".zigmod", "deps" });
const dir = std.fs.cwd();
const should_update = !(args.len >= 1 and std.mem.eql(u8, args[0], "--no-update"));
var options = common.CollectOptions{
.log = should_update,
.update = should_update,
.alloc = gpa,
};
const top_module = try common.collect_deps_deep(cachepath, dir, &options);
var list = std.ArrayList(zigmod.Module).init(gpa);
try common.collect_pkgs(top_module, &list);
try create_depszig(gpa, cachepath, dir, top_module, &list);
if (bootstrap) return;
try create_lockfile(gpa, &list, cachepath, dir);
try diff_lockfile(gpa);
}
pub fn create_depszig(alloc: std.mem.Allocator, cachepath: string, dir: std.fs.Dir, top_module: zigmod.Module, list: *std.ArrayList(zigmod.Module)) !void {
const f = try dir.createFile("deps.zig", .{});
defer f.close();
const w = f.writer();
try w.writeAll("// zig fmt: off\n");
try w.writeAll("const std = @import(\"std\");\n");
try w.writeAll("const builtin = @import(\"builtin\");\n");
try w.writeAll("const Pkg = std.build.Pkg;\n");
try w.writeAll("const string = []const u8;\n");
try w.writeAll("\n");
try w.print("pub const cache = \"{}\";\n", .{std.zig.fmtEscapes(cachepath)});
try w.writeAll("\n");
try w.writeAll(
\\pub fn addAllTo(exe: *std.build.LibExeObjStep) void {
\\ checkMinZig(builtin.zig_version, exe);
\\ @setEvalBranchQuota(1_000_000);
\\ for (packages) |pkg| {
\\ exe.addPackage(pkg.pkg.?);
\\ }
\\ var llc = false;
\\ var vcpkg = false;
\\ inline for (comptime std.meta.declarations(package_data)) |decl| {
\\ const pkg = @as(Package, @field(package_data, decl.name));
\\ for (pkg.system_libs) |item| {
\\ exe.linkSystemLibrary(item);
\\ llc = true;
\\ }
\\ for (pkg.frameworks) |item| {
\\ if (!std.Target.current.isDarwin()) @panic(exe.builder.fmt("a dependency is attempting to link to the framework {s}, which is only possible under Darwin", .{item}));
\\ exe.linkFramework(item);
\\ llc = true;
\\ }
\\ inline for (pkg.c_include_dirs) |item| {
\\ exe.addIncludeDir(@field(dirs, decl.name) ++ "/" ++ item);
\\ llc = true;
\\ }
\\ inline for (pkg.c_source_files) |item| {
\\ exe.addCSourceFile(@field(dirs, decl.name) ++ "/" ++ item, pkg.c_source_flags);
\\ llc = true;
\\ }
\\ vcpkg = vcpkg or pkg.vcpkg;
\\ }
\\ if (llc) exe.linkLibC();
\\ if (builtin.os.tag == .windows and vcpkg) exe.addVcpkgPaths(.static) catch |err| @panic(@errorName(err));
\\}
\\
\\pub const Package = struct {
\\ directory: string,
\\ pkg: ?Pkg = null,
\\ c_include_dirs: []const string = &.{},
\\ c_source_files: []const string = &.{},
\\ c_source_flags: []const string = &.{},
\\ system_libs: []const string = &.{},
\\ frameworks: []const string = &.{},
\\ vcpkg: bool = false,
\\};
\\
\\
);
try w.print(
\\fn checkMinZig(current: std.SemanticVersion, exe: *std.build.LibExeObjStep) void {{
\\ const min = std.SemanticVersion.parse("{?}") catch return;
\\ if (current.order(min).compare(.lt)) @panic(exe.builder.fmt("Your Zig version v{{}} does not meet the minimum build requirement of v{{}}", .{{current, min}}));
\\}}
\\
\\
, .{top_module.minZigVersion()});
try w.writeAll("pub const dirs = struct {\n");
try print_dirs(w, list.items);
try w.writeAll("};\n\n");
try w.writeAll("pub const package_data = struct {\n");
var duped = std.ArrayList(zigmod.Module).init(alloc);
for (list.items) |mod| {
if (mod.is_sys_lib or mod.is_framework) {
continue;
}
try duped.append(mod);
}
try print_pkg_data_to(w, &duped, &std.ArrayList(zigmod.Module).init(alloc));
try w.writeAll("};\n\n");
try w.writeAll("pub const packages = ");
try print_deps(w, top_module);
try w.writeAll(";\n\n");
try w.writeAll("pub const pkgs = ");
try print_pkgs(alloc, w, top_module);
try w.writeAll(";\n\n");
try w.writeAll("pub const imports = struct {\n");
try print_imports(alloc, w, top_module, cachepath);
try w.writeAll("};\n");
}
fn create_lockfile(alloc: std.mem.Allocator, list: *std.ArrayList(zigmod.Module), path: string, dir: std.fs.Dir) !void {
const fl = try dir.createFile("zigmod.lock", .{});
defer fl.close();
const wl = fl.writer();
try wl.writeAll("2\n");
for (list.items) |m| {
if (m.dep) |md| {
if (md.type.isLocal()) continue;
const mpath = try std.fs.path.join(alloc, &.{ path, m.clean_path });
const version = try md.exact_version(alloc, mpath);
try wl.print("{s} {s} {s}\n", .{ @tagName(md.type), md.path, version });
}
}
}
const DiffChange = struct {
from: string,
to: string,
};
fn diff_lockfile(alloc: std.mem.Allocator) !void {
const max = std.math.maxInt(usize);
if (try u.does_folder_exist(".git")) {
const result = try u.run_cmd_raw(alloc, null, &.{ "git", "diff", "zigmod.lock" });
const r = std.io.fixedBufferStream(result.stdout).reader();
while (try r.readUntilDelimiterOrEofAlloc(alloc, '\n', max)) |line| {
if (std.mem.startsWith(u8, line, "@@")) break;
}
var rems = std.ArrayList(string).init(alloc);
var adds = std.ArrayList(string).init(alloc);
while (try r.readUntilDelimiterOrEofAlloc(alloc, '\n', max)) |line| {
if (line[0] == ' ') continue;
if (line[0] == '-') try rems.append(line[1..]);
if (line[0] == '+') if (line[1] == '2') continue else try adds.append(line[1..]);
}
var changes = std.StringHashMap(DiffChange).init(alloc);
var didbreak = false;
var i: usize = 0;
while (i < rems.items.len) {
const it = rems.items[i];
const sni = u.indexOfN(it, ' ', 2).?;
var j: usize = 0;
while (j < adds.items.len) {
const jt = adds.items[j];
const snj = u.indexOfN(jt, ' ', 2).?;
if (std.mem.eql(u8, it[0..sni], jt[0..snj])) {
try changes.put(it[0..sni], .{
.from = it[u.indexOfAfter(it, '-', sni).? + 1 .. it.len],
.to = jt[u.indexOfAfter(jt, '-', snj).? + 1 .. jt.len],
});
_ = rems.orderedRemove(i);
_ = adds.orderedRemove(j);
didbreak = true;
break;
}
if (!didbreak) j += 1;
}
if (!didbreak) i += 1;
if (didbreak) didbreak = false;
}
if (adds.items.len > 0) {
std.debug.print(comptime ansi.color.Faint("Newly added packages:\n"), .{});
defer std.debug.print("\n", .{});
for (adds.items) |it| {
std.debug.print("- {s}\n", .{it});
}
}
if (rems.items.len > 0) {
std.debug.print(comptime ansi.color.Faint("Removed packages:\n"), .{});
defer std.debug.print("\n", .{});
for (rems.items) |it| {
std.debug.print("- {s}\n", .{it});
}
}
if (changes.unmanaged.size > 0) std.debug.print(comptime ansi.color.Faint("Updated packages:\n"), .{});
var iter = changes.iterator();
while (iter.next()) |it| {
if (diff_printchange("git https://github.com", "- {s}/compare/{s}...{s}\n", it)) continue;
if (diff_printchange("git https://gitlab.com", "- {s}/-/compare/{s}...{s}\n", it)) continue;
if (diff_printchange("git https://gitea.com", "- {s}/compare/{s}...{s}\n", it)) continue;
std.debug.print("- {s}\n", .{it.key_ptr.*});
std.debug.print(" - {s} ... {s}\n", .{ it.value_ptr.from, it.value_ptr.to });
}
}
}
fn diff_printchange(comptime testt: string, comptime replacement: string, item: std.StringHashMap(DiffChange).Entry) bool {
if (std.mem.startsWith(u8, item.key_ptr.*, testt)) {
if (std.mem.eql(u8, item.value_ptr.from, item.value_ptr.to)) return true;
std.debug.print(replacement, .{ item.key_ptr.*[4..], item.value_ptr.from, item.value_ptr.to });
return true;
}
return false;
}
fn print_dirs(w: std.fs.File.Writer, list: []const zigmod.Module) !void {
for (list) |mod| {
if (mod.is_sys_lib or mod.is_framework) continue;
if (std.mem.eql(u8, mod.id, "root")) {
try w.writeAll(" pub const _root = \"\";\n");
continue;
}
try w.print(" pub const _{s} = cache ++ \"/{}\";\n", .{ mod.short_id(), std.zig.fmtEscapes(mod.clean_path) });
}
}
fn print_deps(w: std.fs.File.Writer, m: zigmod.Module) !void {
try w.writeAll("&[_]Package{\n");
for (m.deps) |d| {
if (d.main.len == 0) {
continue;
}
if (d.for_build) {
continue;
}
try w.print(" package_data._{s},\n", .{d.id[0..12]});
}
try w.writeAll("}");
}
fn print_pkg_data_to(w: std.fs.File.Writer, notdone: *std.ArrayList(zigmod.Module), done: *std.ArrayList(zigmod.Module)) !void {
var len: usize = notdone.items.len;
while (notdone.items.len > 0) {
for (notdone.items) |mod, i| {
if (contains_all(mod.deps, done.items)) {
try w.print(
\\ pub const _{s} = Package{{
\\ .directory = dirs._{s},
\\
, .{
mod.short_id(),
mod.short_id(),
});
if (mod.main.len > 0 and !std.mem.eql(u8, mod.id, "root")) {
try w.print(
\\ .pkg = Pkg{{ .name = "{s}", .source = .{{ .path = dirs._{s} ++ "/{s}" }}, .dependencies =
, .{
mod.name,
mod.short_id(),
mod.main,
});
if (mod.has_no_zig_deps()) {
try w.writeAll(" null },\n");
} else {
try w.writeAll(" &.{");
for (mod.deps) |moddep, j| {
if (moddep.main.len == 0) continue;
try w.print(" _{s}.pkg.?", .{moddep.id[0..12]});
if (j != mod.deps.len - 1) try w.writeAll(",");
}
try w.writeAll(" } },\n");
}
}
if (mod.c_include_dirs.len > 0) {
try w.writeAll(" .c_include_dirs = &.{");
for (mod.c_include_dirs) |item, j| {
try w.print(" \"{}\"", .{std.zig.fmtEscapes(item)});
if (j != mod.c_include_dirs.len - 1) try w.writeAll(",");
}
try w.writeAll(" },\n");
}
if (mod.c_source_files.len > 0) {
try w.writeAll(" .c_source_files = &.{");
for (mod.c_source_files) |item, j| {
try w.print(" \"{}\"", .{std.zig.fmtEscapes(item)});
if (j != mod.c_source_files.len - 1) try w.writeAll(",");
}
try w.writeAll(" },\n");
}
if (mod.c_source_flags.len > 0) {
try w.writeAll(" .c_source_flags = &.{");
for (mod.c_source_flags) |item, j| {
try w.print(" \"{}\"", .{std.zig.fmtEscapes(item)});
if (j != mod.c_source_flags.len - 1) try w.writeAll(",");
}
try w.writeAll(" },\n");
}
if (mod.has_syslib_deps()) {
try w.writeAll(" .system_libs = &.{");
for (mod.deps) |item, j| {
if (!item.is_sys_lib) continue;
try w.print(" \"{}\"", .{std.zig.fmtEscapes(item.name)});
if (j != mod.deps.len - 1) try w.writeAll(",");
}
try w.writeAll(" },\n");
}
if (mod.has_framework_deps()) {
try w.writeAll(" .frameworks = &.{");
for (mod.deps) |item, j| {
if (!item.is_sys_lib) continue;
try w.print(" \"{}\"", .{std.zig.fmtEscapes(item.name)});
if (j != mod.deps.len - 1) try w.writeAll(",");
}
try w.writeAll(" },\n");
}
if (mod.vcpkg) {
try w.writeAll(" .vcpkg = true,\n");
}
try w.writeAll(" };\n");
try done.append(mod);
_ = notdone.orderedRemove(i);
break;
}
}
if (notdone.items.len == len) {
u.fail("notdone still has {d} items", .{len});
}
len = notdone.items.len;
}
}
/// returns if all of the zig modules in needles are in haystack
fn contains_all(needles: []zigmod.Module, haystack: []const zigmod.Module) bool {
for (needles) |item| {
if (item.main.len > 0 and !u.list_contains_gen(zigmod.Module, haystack, item)) {
return false;
}
}
return true;
}
fn print_pkgs(alloc: std.mem.Allocator, w: std.fs.File.Writer, m: zigmod.Module) !void {
try w.writeAll("struct {\n");
for (m.deps) |d| {
if (d.main.len == 0) {
continue;
}
if (d.for_build) {
continue;
}
const ident = try zig_name_from_pkg_name(alloc, d.name);
try w.print(" pub const {s} = package_data._{s};\n", .{ ident, d.id[0..12] });
}
try w.writeAll("}");
}
fn print_imports(alloc: std.mem.Allocator, w: std.fs.File.Writer, m: zigmod.Module, path: string) !void {
for (m.deps) |d| {
if (d.main.len == 0) {
continue;
}
if (!d.for_build) {
continue;
}
const ident = try zig_name_from_pkg_name(alloc, d.name);
try w.print(" pub const {s} = @import(\"{}/{}/{s}\");\n", .{ ident, std.zig.fmtEscapes(path), std.zig.fmtEscapes(d.clean_path), d.main });
}
}
fn zig_name_from_pkg_name(alloc: std.mem.Allocator, name: string) !string {
var legal = name;
legal = try std.mem.replaceOwned(u8, alloc, legal, "-", "_");
legal = try std.mem.replaceOwned(u8, alloc, legal, "/", "_");
legal = try std.mem.replaceOwned(u8, alloc, legal, ".", "_");
return legal;
}