authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-06-01 19:48:51 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-06-01 19:48:51 -07:00
logca018b40db988497fa08f7cb7ab4f91aab93d8ab
treedaf617962c1e46fe6a7fb620c194ab9cc3633789
parent350281d9ee6729e4ab98498ddb4671b3faf70b6a
signaturebadge-check Signed by SSH key SHA256:4hHJbtBRU58AYXwjL7fkz2fnQHdiye8x1QpTCQ0sHNw

cmd/install: add 0.12 support


2 files changed, 420 insertions(+), 0 deletions(-)

src/cmd/fetch.0.12.zig created+419
......@@ -0,0 +1,419 @@
1// Adapted from https://github.com/nektro/zigmod/blob/r91/src/cmd/fetch.zig
2
3const std = @import("std");
4const string = []const u8;
5const ansi = @import("ansi");
6const extras = @import("extras");
7const nfs = @import("nfs");
8
9const zigmod = @import("../lib.zig");
10const u = @import("./../util/funcs.zig");
11const common = @import("./../common.zig");
12const license = @import("./license.zig");
13
14pub fn create_depszig(alloc: std.mem.Allocator, cachepath: string, dir: nfs.Dir, top_module: zigmod.Module, list: *std.array_list.Managed(zigmod.Module)) !void {
15 const f = try dir.createFile("deps.zig", .{});
16 defer f.close();
17
18 const w = f;
19 try w.writeAll("// zig fmt: off\n");
20 try w.writeAll("const std = @import(\"std\");\n");
21 try w.writeAll("const builtin = @import(\"builtin\");\n");
22 try w.writeAll("const string = []const u8;\n");
23 try w.writeAll("\n");
24 try w.print("pub const cache = \"{}\";\n", .{u.altStringEscape(cachepath)});
25 try w.writeAll("\n");
26 try w.writeAll(
27 \\pub fn addAllTo(exe: *std.Build.Step.Compile) void {
28 \\ checkMinZig(builtin.zig_version, exe);
29 \\ @setEvalBranchQuota(1_000_000);
30 \\ for (packages) |pkg| {
31 \\ const module = pkg.module(exe);
32 \\ exe.root_module.addImport(pkg.import.?[0], module);
33 \\ }
34 \\ for (package_data._root.system_libs) |libname| {
35 \\ exe.linkSystemLibrary(libname);
36 \\ exe.linkLibC();
37 \\ }
38 \\}
39 \\
40 \\var link_lib_c = false;
41 \\pub const Package = struct {
42 \\ directory: string,
43 \\ import: ?struct { string, std.Build.LazyPath } = null,
44 \\ dependencies: []const *Package,
45 \\ c_include_dirs: []const string = &.{},
46 \\ c_source_files: []const string = &.{},
47 \\ c_source_flags: []const string = &.{},
48 \\ system_libs: []const string = &.{},
49 \\ frameworks: []const string = &.{},
50 \\ module_memo: ?*std.Build.Module = null,
51 \\
52 \\ pub fn module(self: *Package, exe: *std.Build.Step.Compile) *std.Build.Module {
53 \\ if (self.module_memo) |cached| {
54 \\ return cached;
55 \\ }
56 \\ const b = exe.step.owner;
57 \\ const result = b.createModule(.{
58 \\ .target = exe.root_module.resolved_target orelse b.host,
59 \\ });
60 \\ if (self.import) |capture| {
61 \\ result.root_source_file = capture[1];
62 \\ }
63 \\ for (self.dependencies) |item| {
64 \\ const module_dep = item.module(exe);
65 \\ if (module_dep.root_source_file != null) {
66 \\ result.addImport(item.import.?[0], module_dep);
67 \\ }
68 \\ for (module_dep.include_dirs.items) |jtem| {
69 \\ switch (jtem) {
70 \\ .path => result.addIncludePath(jtem.path),
71 \\ .path_system, .path_after, .framework_path, .framework_path_system, .other_step, .config_header_step => {},
72 \\ }
73 \\ }
74 \\ }
75 \\ for (self.c_include_dirs) |item| {
76 \\ result.addIncludePath(b.path(b.fmt("{s}/{s}", .{ self.directory, item })));
77 \\ exe.addIncludePath(b.path(b.fmt("{s}/{s}", .{ self.directory, item })));
78 \\ link_lib_c = true;
79 \\ }
80 \\ for (self.c_source_files) |item| {
81 \\ exe.addCSourceFile(.{ .file = b.path(b.fmt("{s}/{s}", .{ self.directory, item })), .flags = self.c_source_flags });
82 \\ link_lib_c = true;
83 \\ }
84 \\ for (self.system_libs) |item| {
85 \\ result.linkSystemLibrary(item, .{});
86 \\ exe.linkSystemLibrary(item);
87 \\ link_lib_c = true;
88 \\ }
89 \\ for (self.frameworks) |item| {
90 \\ result.linkFramework(item, .{});
91 \\ exe.linkFramework(item);
92 \\ link_lib_c = true;
93 \\ }
94 \\ if (link_lib_c) {
95 \\ result.link_libc = true;
96 \\ exe.linkLibC();
97 \\ }
98 \\ self.module_memo = result;
99 \\ return result;
100 \\ }
101 \\};
102 \\
103 \\
104 );
105
106 try w.print(
107 \\fn checkMinZig(current: std.SemanticVersion, exe: *std.Build.Step.Compile) void {{
108 \\ const min = std.SemanticVersion.parse("{?}") catch return;
109 \\ if (current.order(min).compare(.lt)) @panic(exe.step.owner.fmt("Your Zig version v{{}} does not meet the minimum build requirement of v{{}}", .{{current, min}}));
110 \\}}
111 \\
112 \\
113 , .{if (top_module.minZigVersion()) |sv| u.altSemanticVersion(sv) else null});
114
115 try w.writeAll("pub const dirs = struct {\n");
116 try print_dirs(w, list.items, alloc);
117 try w.writeAll("};\n\n");
118
119 try w.writeAll("pub const package_data = struct {\n");
120 var duped = std.array_list.Managed(zigmod.Module).init(alloc);
121 var done = std.array_list.Managed(zigmod.Module).init(alloc);
122 for (list.items) |mod| {
123 if (mod.type == .system_lib or mod.type == .framework) {
124 continue;
125 }
126 try duped.append(mod);
127 }
128 try print_pkg_data_to(w, &duped, &done);
129 try w.writeAll("};\n\n");
130
131 try w.writeAll("pub const packages = ");
132 try print_deps(w, top_module);
133 try w.writeAll(";\n\n");
134
135 try w.writeAll("pub const pkgs = ");
136 try print_pkgs(alloc, w, top_module);
137 try w.writeAll(";\n\n");
138
139 try w.writeAll("pub const imports = struct {\n");
140 try print_imports(alloc, w, top_module, cachepath);
141 try w.writeAll("};\n");
142}
143
144fn create_lockfile(alloc: std.mem.Allocator, list: *std.array_list.Managed(zigmod.Module), path: string, dir: nfs.Dir) !void {
145 const fl = try dir.createFile("zigmod.lock", .{});
146 defer fl.close();
147
148 std.mem.sort(zigmod.Module, list.items, {}, zigmod.Module.lessThan);
149
150 const wl = fl.writer();
151 try wl.writeAll("2\n");
152 for (list.items) |m| {
153 if (m.dep) |md| {
154 if (md.type.isLocal()) continue;
155 const mpath = try std.fs.path.join(alloc, &.{ path, m.clean_path });
156 const version = try md.exact_version(alloc, mpath);
157 try wl.print("{s} {s} {s}\n", .{ @tagName(md.type), md.path, version });
158 }
159 }
160}
161
162const DiffChange = struct {
163 from: string,
164 to: string,
165};
166
167fn diff_lockfile(alloc: std.mem.Allocator) !void {
168 const max = std.math.maxInt(usize);
169
170 if (try extras.doesFolderExist(null, ".git")) {
171 const result = try u.run_cmd_raw(alloc, null, &.{ "git", "diff", "zigmod.lock" });
172 var stdout = std.io.fixedBufferStream(result.stdout);
173 const r = stdout.reader();
174 while (try r.readUntilDelimiterOrEofAlloc(alloc, '\n', max)) |line| {
175 if (std.mem.startsWith(u8, line, "@@")) break;
176 }
177
178 var rems = std.array_list.Managed(string).init(alloc);
179 var adds = std.array_list.Managed(string).init(alloc);
180 while (try r.readUntilDelimiterOrEofAlloc(alloc, '\n', max)) |line| {
181 if (line[0] == ' ') continue;
182 if (line[0] == '-') try rems.append(line[1..]);
183 if (line[0] == '+') if (line[1] == '2') break else try adds.append(line[1..]);
184 }
185
186 var changes = std.StringHashMap(DiffChange).init(alloc);
187
188 var didbreak = false;
189 var i: usize = 0;
190 while (i < rems.items.len) {
191 const it = rems.items[i];
192 const sni = u.indexOfN(it, ' ', 2).?;
193
194 var j: usize = 0;
195 while (j < adds.items.len) {
196 const jt = adds.items[j];
197 const snj = u.indexOfN(jt, ' ', 2).?;
198
199 if (std.mem.eql(u8, it[0..sni], jt[0..snj])) {
200 try changes.put(it[0..sni], .{
201 .from = it[u.indexOfAfter(it, '-', sni).? + 1 .. it.len],
202 .to = jt[u.indexOfAfter(jt, '-', snj).? + 1 .. jt.len],
203 });
204 _ = rems.orderedRemove(i);
205 _ = adds.orderedRemove(j);
206 didbreak = true;
207 break;
208 }
209 if (!didbreak) j += 1;
210 }
211 if (!didbreak) i += 1;
212 if (didbreak) didbreak = false;
213 }
214
215 if (adds.items.len > 0) {
216 std.debug.print(comptime ansi.color.Faint("Newly added packages:\n"), .{});
217 defer std.debug.print("\n", .{});
218
219 for (adds.items) |it| {
220 std.debug.print("- {s}\n", .{it});
221 }
222 }
223
224 if (rems.items.len > 0) {
225 std.debug.print(comptime ansi.color.Faint("Removed packages:\n"), .{});
226 defer std.debug.print("\n", .{});
227
228 for (rems.items) |it| {
229 std.debug.print("- {s}\n", .{it});
230 }
231 }
232
233 if (changes.unmanaged.size > 0) std.debug.print(comptime ansi.color.Faint("Updated packages:\n"), .{});
234 var iter = changes.iterator();
235 while (iter.next()) |it| {
236 if (diff_printchange("git https://github.com", "- {s}/compare/{s}...{s}\n", it)) continue;
237 if (diff_printchange("git https://gitlab.com", "- {s}/-/compare/{s}...{s}\n", it)) continue;
238 if (diff_printchange("git https://gitea.com", "- {s}/compare/{s}...{s}\n", it)) continue;
239
240 std.debug.print("- {s}\n", .{it.key_ptr.*});
241 std.debug.print(" - {s} ... {s}\n", .{ it.value_ptr.from, it.value_ptr.to });
242 }
243 }
244}
245
246fn diff_printchange(comptime testt: string, comptime replacement: string, item: std.StringHashMap(DiffChange).Entry) bool {
247 if (std.mem.startsWith(u8, item.key_ptr.*, testt)) {
248 if (std.mem.eql(u8, item.value_ptr.from, item.value_ptr.to)) return true;
249 std.debug.print(replacement, .{ item.key_ptr.*[4..], item.value_ptr.from, item.value_ptr.to });
250 return true;
251 }
252 return false;
253}
254
255fn print_dirs(w: nfs.File, list: []const zigmod.Module, alloc: std.mem.Allocator) !void {
256 for (list) |mod| {
257 if (mod.type == .system_lib or mod.type == .framework) continue;
258 if (std.mem.eql(u8, &mod.id, &zigmod.Module.ROOT)) {
259 try w.writeAll(" pub const _root = \"\";\n");
260 continue;
261 }
262 if (std.mem.eql(u8, mod.clean_path, "../..")) {
263 const cwd_realpath = try std.fs.cwd().realpathAlloc(alloc, ".");
264 try w.print(" pub const _{s} = \"{}\";\n", .{ mod.short_id(), u.altStringEscape(cwd_realpath) });
265 continue;
266 }
267 try w.print(" pub const _{s} = cache ++ \"/{}\";\n", .{ mod.short_id(), u.altStringEscape(mod.clean_path) });
268 }
269}
270
271fn print_deps(w: nfs.File, m: zigmod.Module) !void {
272 try w.writeAll("&[_]*Package{\n");
273 for (m.deps) |d| {
274 if (d.main.len == 0) {
275 continue;
276 }
277 if (d.for_build) {
278 continue;
279 }
280 try w.print(" &package_data._{s},\n", .{d.id[0..12]});
281 }
282 try w.writeAll("}");
283}
284
285fn print_pkg_data_to(w: nfs.File, notdone: *std.array_list.Managed(zigmod.Module), done: *std.array_list.Managed(zigmod.Module)) !void {
286 var len: usize = notdone.items.len;
287 while (notdone.items.len > 0) {
288 for (notdone.items, 0..) |mod, i| {
289 if (contains_all(mod.deps, done.items)) {
290 try w.print(
291 \\ pub var _{s} = Package{{
292 \\ .directory = dirs._{s},
293 \\
294 , .{
295 mod.short_id(),
296 mod.short_id(),
297 });
298 if (mod.main.len > 0 and !std.mem.eql(u8, &mod.id, &zigmod.Module.ROOT)) {
299 try w.print(
300 \\ .import = .{{ "{s}", .{{ .path = dirs._{s} ++ "/{s}" }} }},
301 \\
302 , .{
303 mod.name,
304 mod.short_id(),
305 mod.main,
306 });
307 }
308 {
309 try w.writeAll(" .dependencies =");
310 try w.writeAll(" &.{");
311 for (mod.deps, 0..) |moddep, j| {
312 if (moddep.type == .system_lib) continue;
313 if (moddep.type == .framework) continue;
314 try w.print(" &_{s}", .{moddep.id[0..12]});
315 if (j != mod.deps.len - 1) try w.writeAll(",");
316 }
317 try w.writeAll(" },\n");
318 }
319 if (mod.c_include_dirs.len > 0) {
320 try w.writeAll(" .c_include_dirs = &.{");
321 for (mod.c_include_dirs, 0..) |item, j| {
322 try w.print(" \"{}\"", .{u.altStringEscape(item)});
323 if (j != mod.c_include_dirs.len - 1) try w.writeAll(",");
324 }
325 try w.writeAll(" },\n");
326 }
327 if (mod.c_source_files.len > 0) {
328 try w.writeAll(" .c_source_files = &.{");
329 for (mod.c_source_files, 0..) |item, j| {
330 try w.print(" \"{}\"", .{u.altStringEscape(item)});
331 if (j != mod.c_source_files.len - 1) try w.writeAll(",");
332 }
333 try w.writeAll(" },\n");
334 }
335 if (mod.c_source_flags.len > 0) {
336 try w.writeAll(" .c_source_flags = &.{");
337 for (mod.c_source_flags, 0..) |item, j| {
338 try w.print(" \"{}\"", .{u.altStringEscape(item)});
339 if (j != mod.c_source_flags.len - 1) try w.writeAll(",");
340 }
341 try w.writeAll(" },\n");
342 }
343 if (mod.has_syslib_deps()) {
344 try w.writeAll(" .system_libs = &.{");
345 for (mod.deps, 0..) |item, j| {
346 if (!(item.type == .system_lib)) continue;
347 try w.print(" \"{}\"", .{u.altStringEscape(item.name)});
348 if (j != mod.deps.len - 1) try w.writeAll(",");
349 }
350 try w.writeAll(" },\n");
351 }
352 if (mod.has_framework_deps()) {
353 try w.writeAll(" .frameworks = &.{");
354 for (mod.deps, 0..) |item, j| {
355 if (!(item.type == .system_lib)) continue;
356 try w.print(" \"{}\"", .{u.altStringEscape(item.name)});
357 if (j != mod.deps.len - 1) try w.writeAll(",");
358 }
359 try w.writeAll(" },\n");
360 }
361 try w.writeAll(" };\n");
362
363 try done.append(mod);
364 _ = notdone.orderedRemove(i);
365 break;
366 }
367 }
368 if (notdone.items.len == len) {
369 u.fail("notdone still has {d} items", .{len});
370 }
371 len = notdone.items.len;
372 }
373}
374
375/// returns if all of the zig modules in needles are in haystack
376fn contains_all(needles: []zigmod.Module, haystack: []const zigmod.Module) bool {
377 for (needles) |item| {
378 if (item.main.len > 0 and !extras.containsAggregate(zigmod.Module, haystack, item)) {
379 return false;
380 }
381 }
382 return true;
383}
384
385fn print_pkgs(alloc: std.mem.Allocator, w: nfs.File, m: zigmod.Module) !void {
386 try w.writeAll("struct {\n");
387 for (m.deps) |d| {
388 if (d.main.len == 0) {
389 continue;
390 }
391 if (d.for_build) {
392 continue;
393 }
394 const ident = try zig_name_from_pkg_name(alloc, d.name);
395 try w.print(" pub const {s} = &package_data._{s};\n", .{ ident, d.id[0..12] });
396 }
397 try w.writeAll("}");
398}
399
400fn print_imports(alloc: std.mem.Allocator, w: nfs.File, m: zigmod.Module, path: string) !void {
401 for (m.deps) |d| {
402 if (d.main.len == 0) {
403 continue;
404 }
405 if (!d.for_build) {
406 continue;
407 }
408 const ident = try zig_name_from_pkg_name(alloc, d.name);
409 try w.print(" pub const {s} = @import(\"{}/{}/{s}\");\n", .{ ident, u.altStringEscape(path), u.altStringEscape(d.clean_path), d.main });
410 }
411}
412
413fn zig_name_from_pkg_name(alloc: std.mem.Allocator, name: string) !string {
414 var legal = name;
415 legal = try std.mem.replaceOwned(u8, alloc, legal, "-", "_");
416 legal = try std.mem.replaceOwned(u8, alloc, legal, "/", "_");
417 legal = try std.mem.replaceOwned(u8, alloc, legal, ".", "_");
418 return legal;
419}
src/cmd/install.zig+1
......@@ -82,6 +82,7 @@ pub fn execute(self_name: []const u8, args: [][:0]u8) !void {
8282 const version_sct: V = .{ .major = @intCast(zigversion_sv.major), .minor = @intCast(zigversion_sv.minor) };
8383 const version_int: u32 = (version_sct.major << 2) | version_sct.minor;
8484 switch (version_int) {
85 0x0000_000c => try @import("./fetch.0.12.zig").create_depszig(gpa, cachepath, moddir, fetch_top_module, &fetch_list),
8586 0x0000_000d => try @import("./fetch.0.13.zig").create_depszig(gpa, cachepath, moddir, fetch_top_module, &fetch_list),
8687 0x0000_000e => try @import("./fetch.0.14.zig").create_depszig(gpa, cachepath, moddir, fetch_top_module, &fetch_list),
8788 0x0000_000f => {}, // that's us, zigmod is already 0.15.2