diff --git a/src/cmd/fetch.zig b/src/cmd/fetch.zig index b23c472bc9dac1ff192e720d3f433c593407bff5..cb551b6c8a2b359d7506882a330ce7cd3daddb6b 100644 --- a/src/cmd/fetch.zig +++ b/src/cmd/fetch.zig @@ -220,31 +220,33 @@ fn print_sys_libs_to(w: std.fs.File.Writer, list: []u.Module, list2: *std.ArrayL } } -fn print_pkg_data_to(w: std.fs.File.Writer, list: *std.ArrayList(u.Module), list2: *std.ArrayList(u.Module)) anyerror!void { - var i: usize = 0; - while (i < list.items.len) : (i += 1) { - const mod = list.items[i]; - if (contains_all(mod.deps, list2)) { - 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 }); - for (mod.deps) |d| { - if (d.main.len > 0) { - try w.print(" _{s},", .{d.id[0..12]}); +fn print_pkg_data_to(w: std.fs.File.Writer, notdone: *std.ArrayList(u.Module), done: *std.ArrayList(u.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} = 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 }); + for (mod.deps) |d| { + if (d.main.len > 0) { + try w.print(" _{s},", .{d.id[0..12]}); + } } - } - try w.print(" }} }};\n", .{}); + try w.print(" }} }};\n", .{}); - try list2.append(mod); - _ = list.orderedRemove(i); - break; + try done.append(mod); + _ = notdone.orderedRemove(i); + break; + } } - } - if (list.items.len > 0) { - try print_pkg_data_to(w, list, list2); + if (notdone.items.len == len) { + u.assert(false, "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: []u.Module, haystack: *std.ArrayList(u.Module)) bool { +fn contains_all(needles: []u.Module, haystack: []const u.Module) bool { for (needles) |item| { if (item.main.len > 0 and !u.list_contains_gen(u.Module, haystack, item)) { return false; diff --git a/src/common.zig b/src/common.zig index b6b56cf37bdd33b4da74dbe4c2ef267b2e168530..ab13cba7683f3adf2758c019ca99a89339a2d838 100644 --- a/src/common.zig +++ b/src/common.zig @@ -65,7 +65,7 @@ pub fn collect_deps(dir: []const u8, mpath: []const u8, options: CollectOptions) } pub fn collect_pkgs(mod: u.Module, list: *std.ArrayList(u.Module)) anyerror!void { - if (u.list_contains_gen(u.Module, list, mod)) { + if (u.list_contains_gen(u.Module, list.items, mod)) { return; } try list.append(mod); @@ -185,7 +185,7 @@ fn get_module_from_dep(list: *std.ArrayList(u.Module), d: u.Dep, dir: []const u8 .yaml = null, }); }, - else => blk: { + else => { var dd = try collect_deps(dir, try u.concat(&.{ moddir, "/zig.mod" }), options) catch |e| switch (e) { error.FileNotFound => { 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 mod_from.clean_path = u.trim_prefix(moddir, dir)[1..]; if (mod_from.is_for_this()) try list.append(mod_from); } - break :blk; + return; }, else => e, }; diff --git a/src/util/funcs.zig b/src/util/funcs.zig index 6c38e03034060912a0536f333261c30fbfb24bdb..a44acfdcc6405def4cfbef0f610cb05034eb2538 100644 --- a/src/util/funcs.zig +++ b/src/util/funcs.zig @@ -135,8 +135,8 @@ pub fn list_contains(haystack: [][]const u8, needle: []const u8) bool { return false; } -pub fn list_contains_gen(comptime T: type, haystack: *std.ArrayList(T), needle: T) bool { - for (haystack.items) |item| { +pub fn list_contains_gen(comptime T: type, haystack: []const T, needle: T) bool { + for (haystack) |item| { if (item.eql(needle)) { return true; } diff --git a/src/util/module.zig b/src/util/module.zig index 9340c89104a6beb99feb9f2533be2893b646117f..bb203f1af53dde317c846a89f56df0b2471c9ff3 100644 --- a/src/util/module.zig +++ b/src/util/module.zig @@ -32,7 +32,7 @@ pub const Module = struct { .c_include_dirs = dep.c_include_dirs, .c_source_flags = dep.c_source_flags, .c_source_files = dep.c_source_files, - .deps = &[_]Module{}, + .deps = &.{}, .clean_path = try dep.clean_path(), .only_os = dep.only_os, .except_os = dep.except_os,