authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-06-05 16:12:52 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-06-05 16:12:52 -07:00
log70147b37a52266e970d2e762cf7c54617085af33
tree1217b1f54a289e3bd636feb6f67da28b99582361
parentfe53c603ace946f51220e767e5286349dd4fb531

cmd/fetch- rewrite `print_pkg_data_to` to be non-recursive


4 files changed, 26 insertions(+), 24 deletions(-)

src/cmd/fetch.zig+20-18
......@@ -220,31 +220,33 @@ fn print_sys_libs_to(w: std.fs.File.Writer, list: []u.Module, list2: *std.ArrayL
220220 }
221221}
222222
223fn print_pkg_data_to(w: std.fs.File.Writer, list: *std.ArrayList(u.Module), list2: *std.ArrayList(u.Module)) anyerror!void {
224 var i: usize = 0;
225 while (i < list.items.len) : (i += 1) {
226 const mod = list.items[i];
227 if (contains_all(mod.deps, list2)) {
228 try w.print(" pub const _{s} = std.build.Pkg{{ .name = \"{s}\", .path = cache ++ \"/{}/{s}\", .dependencies = &[_]std.build.Pkg{{", .{ mod.id, mod.name, std.zig.fmtEscapes(mod.clean_path), mod.main });
229 for (mod.deps) |d| {
230 if (d.main.len > 0) {
231 try w.print(" _{s},", .{d.id[0..12]});
223fn print_pkg_data_to(w: std.fs.File.Writer, notdone: *std.ArrayList(u.Module), done: *std.ArrayList(u.Module)) !void {
224 var len: usize = notdone.items.len;
225 while (notdone.items.len > 0) {
226 for (notdone.items) |mod, i| {
227 if (contains_all(mod.deps, done.items)) {
228 try w.print(" pub const _{s} = std.build.Pkg{{ .name = \"{s}\", .path = cache ++ \"/{}/{s}\", .dependencies = &[_]std.build.Pkg{{", .{ mod.id[0..12], mod.name, std.zig.fmtEscapes(mod.clean_path), mod.main });
229 for (mod.deps) |d| {
230 if (d.main.len > 0) {
231 try w.print(" _{s},", .{d.id[0..12]});
232 }
232233 }
233 }
234 try w.print(" }} }};\n", .{});
234 try w.print(" }} }};\n", .{});
235235
236 try list2.append(mod);
237 _ = list.orderedRemove(i);
238 break;
236 try done.append(mod);
237 _ = notdone.orderedRemove(i);
238 break;
239 }
239240 }
240 }
241 if (list.items.len > 0) {
242 try print_pkg_data_to(w, list, list2);
241 if (notdone.items.len == len) {
242 u.assert(false, "notdone still has {d} items", .{len});
243 }
244 len = notdone.items.len;
243245 }
244246}
245247
246248/// returns if all of the zig modules in needles are in haystack
247fn contains_all(needles: []u.Module, haystack: *std.ArrayList(u.Module)) bool {
249fn contains_all(needles: []u.Module, haystack: []const u.Module) bool {
248250 for (needles) |item| {
249251 if (item.main.len > 0 and !u.list_contains_gen(u.Module, haystack, item)) {
250252 return false;
src/common.zig+3-3
......@@ -65,7 +65,7 @@ pub fn collect_deps(dir: []const u8, mpath: []const u8, options: CollectOptions)
6565}
6666
6767pub fn collect_pkgs(mod: u.Module, list: *std.ArrayList(u.Module)) anyerror!void {
68 if (u.list_contains_gen(u.Module, list, mod)) {
68 if (u.list_contains_gen(u.Module, list.items, mod)) {
6969 return;
7070 }
7171 try list.append(mod);
......@@ -185,7 +185,7 @@ fn get_module_from_dep(list: *std.ArrayList(u.Module), d: u.Dep, dir: []const u8
185185 .yaml = null,
186186 });
187187 },
188 else => blk: {
188 else => {
189189 var dd = try collect_deps(dir, try u.concat(&.{ moddir, "/zig.mod" }), options) catch |e| switch (e) {
190190 error.FileNotFound => {
191191 if (d.main.len > 0 or d.c_include_dirs.len > 0 or d.c_source_files.len > 0) {
......@@ -194,7 +194,7 @@ fn get_module_from_dep(list: *std.ArrayList(u.Module), d: u.Dep, dir: []const u8
194194 mod_from.clean_path = u.trim_prefix(moddir, dir)[1..];
195195 if (mod_from.is_for_this()) try list.append(mod_from);
196196 }
197 break :blk;
197 return;
198198 },
199199 else => e,
200200 };
src/util/funcs.zig+2-2
......@@ -135,8 +135,8 @@ pub fn list_contains(haystack: [][]const u8, needle: []const u8) bool {
135135 return false;
136136}
137137
138pub fn list_contains_gen(comptime T: type, haystack: *std.ArrayList(T), needle: T) bool {
139 for (haystack.items) |item| {
138pub fn list_contains_gen(comptime T: type, haystack: []const T, needle: T) bool {
139 for (haystack) |item| {
140140 if (item.eql(needle)) {
141141 return true;
142142 }
src/util/module.zig+1-1
......@@ -32,7 +32,7 @@ pub const Module = struct {
3232 .c_include_dirs = dep.c_include_dirs,
3333 .c_source_flags = dep.c_source_flags,
3434 .c_source_files = dep.c_source_files,
35 .deps = &[_]Module{},
35 .deps = &.{},
3636 .clean_path = try dep.clean_path(),
3737 .only_os = dep.only_os,
3838 .except_os = dep.except_os,