From a3772eac0fda962d3fbdf5ef73dcc7aa936a493e Mon Sep 17 00:00:00 2001 From: Meghan Date: Mon, 16 Nov 2020 23:30:10 -0800 Subject: [PATCH] fetch: only print c/h files once even if dep shows up in tree twice --- src/cmd_fetch.zig | 20 ++++++++++++++------ src/util/funcs.zig | 9 +++++++++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/cmd_fetch.zig b/src/cmd_fetch.zig index 794f1519b4f98e96a9669b6b9fb2be807bbcaa03..d78384beabae8e7bc2514502a17fa47f513b72ea 100644 --- a/src/cmd_fetch.zig +++ b/src/cmd_fetch.zig @@ -47,11 +47,11 @@ pub fn execute(args: [][]u8) !void { try w.print(";\n", .{}); try w.print("\n", .{}); try w.print("{}\n", .{"pub const c_include_dirs = &[_][]const u8{"}); - try print_incl_dirs_to(w, top_module); + try print_incl_dirs_to(w, top_module, &std.ArrayList([]const u8).init(gpa)); try w.print("{};\n", .{"}"}); try w.print("\n", .{}); try w.print("{}\n", .{"pub const c_source_files = &[_][]const u8{"}); - try print_csrc_dirs_to(w, top_module); + try print_csrc_dirs_to(w, top_module, &std.ArrayList([]const u8).init(gpa)); try w.print("{};\n", .{"}"}); } @@ -119,20 +119,28 @@ fn print_deps(w: std.fs.File.Writer, dir: []const u8, m: u.Module, tabs: i32) an try w.print("{}", .{try u.concat(&[_][]const u8{r,"}"})}); } -fn print_incl_dirs_to(w: std.fs.File.Writer, mod: u.Module) anyerror!void { +fn print_incl_dirs_to(w: std.fs.File.Writer, mod: u.Module, list: *std.ArrayList([]const u8)) anyerror!void { + if (u.list_contains(list, mod.clean_path)) { + return; + } + try list.append(mod.clean_path); for (mod.c_include_dirs) |it| { try w.print(" cache ++ \"/{}/{}\",\n", .{mod.clean_path, it}); } for (mod.deps) |d| { - try print_incl_dirs_to(w, d); + try print_incl_dirs_to(w, d, list); } } -fn print_csrc_dirs_to(w: std.fs.File.Writer, mod: u.Module) anyerror!void { +fn print_csrc_dirs_to(w: std.fs.File.Writer, mod: u.Module, list: *std.ArrayList([]const u8)) anyerror!void { + if (u.list_contains(list, mod.clean_path)) { + return; + } + try list.append(mod.clean_path); for (mod.c_source_files) |it| { try w.print(" cache ++ \"/{}/{}\",\n", .{mod.clean_path, it}); } for (mod.deps) |d| { - try print_csrc_dirs_to(w, d); + try print_csrc_dirs_to(w, d, list); } } diff --git a/src/util/funcs.zig b/src/util/funcs.zig index 8cc3dc4cba7b60f44b2f823f82d4c201b0e0702f..3877519457118d2a5cb879f4ce3b5a28ddb05870 100644 --- a/src/util/funcs.zig +++ b/src/util/funcs.zig @@ -106,3 +106,12 @@ pub fn print_all(w: std.fs.File.Writer, items: anytype, ln: bool) !void { try w.print("\n", .{}); } } + +pub fn list_contains(haystack: *std.ArrayList([]const u8), needle: []const u8) bool { + for (haystack.items) |item| { + if (std.mem.eql(u8, item, needle)) { + return true; + } + } + return false; +} -- 2.54.0