authorgravatar for hello@nektro.netMeghan <hello@nektro.net> 2021-01-09 18:03:39 -08:00
committergravatar for hello@nektro.netMeghan <hello@nektro.net> 2021-01-09 18:03:39 -08:00
log84ee7dbfd0472e0c008ba4181d198792cd7a6fee
tree5ef22bf73f2f641d559468c0769faabed103217f
parentf1c68f9abc97751be37a7bb0ffdb89521f4a13a8

fetch: collect deps ahead of time so print functions dont have to be recursive


1 files changed, 83 insertions(+), 93 deletions(-)

src/cmd_fetch.zig+83-93
...@@ -47,14 +47,21 @@ pub fn execute(args: [][]u8) !void {...@@ -47,14 +47,21 @@ pub fn execute(args: [][]u8) !void {
47 \\fn get_flags(comptime index: usize) []const u8 {47 \\fn get_flags(comptime index: usize) []const u8 {
48 \\ return @field(c_source_flags, _paths[index]);48 \\ return @field(c_source_flags, _paths[index]);
49 \\}49 \\}
50 \\
50 });51 });
51 try w.writeAll("\n");52
53 const list = &std.ArrayList(u.Module).init(gpa);
54 try collect_pkgs(top_module, list);
55 for (list.items) |m, i| {
56 std.debug.print("module: {} {}\n", .{i, m.clean_path});
57 }
58
52 try w.writeAll("pub const _ids = .{\n");59 try w.writeAll("pub const _ids = .{\n");
53 try print_ids(w, top_module, &std.ArrayList([]const u8).init(gpa));60 try print_ids(w, list.items);
54 try w.writeAll("};\n");61 try w.writeAll("};\n");
55 try w.writeAll("\n");62 try w.writeAll("\n");
56 try w.print("pub const _paths = {}\n", .{".{"});63 try w.print("pub const _paths = {}\n", .{".{"});
57 try print_paths(w, top_module, &std.ArrayList([]const u8).init(gpa));64 try print_paths(w, list.items);
58 try w.writeAll("};\n");65 try w.writeAll("};\n");
59 try w.writeAll("\n");66 try w.writeAll("\n");
60 try w.writeAll("pub const packages = ");67 try w.writeAll("pub const packages = ");
...@@ -66,19 +73,20 @@ pub fn execute(args: [][]u8) !void {...@@ -66,19 +73,20 @@ pub fn execute(args: [][]u8) !void {
66 try w.writeAll(";\n");73 try w.writeAll(";\n");
67 try w.writeAll("\n");74 try w.writeAll("\n");
68 try w.writeAll("pub const c_include_dirs = &[_][]const u8{\n");75 try w.writeAll("pub const c_include_dirs = &[_][]const u8{\n");
69 try print_incl_dirs_to(w, top_module, &std.ArrayList([]const u8).init(gpa), true);76 try print_incl_dirs_to(w, list.items);
70 try w.writeAll("};\n");77 try w.writeAll("};\n");
71 try w.writeAll("\n");78 try w.writeAll("\n");
72 try w.writeAll("pub const c_source_flags = struct {\n");79 try w.writeAll("pub const c_source_flags = struct {\n");
73 try print_csrc_flags_to(w, top_module, &std.ArrayList([]const u8).init(gpa), true);80 try print_csrc_flags_to(w, list.items);
74 try w.writeAll("};\n");81 try w.writeAll("};\n");
75 try w.writeAll("\n");82 try w.writeAll("\n");
76 try w.writeAll("pub const c_source_files = &[_][2][]const u8{\n");83 try w.writeAll("pub const c_source_files = &[_][2][]const u8{\n");
77 try print_csrc_dirs_to(w, top_module, &std.ArrayList([]const u8).init(gpa), true);84 try print_csrc_dirs_to(w, list.items);
78 try w.writeAll("};\n");85 try w.writeAll("};\n");
79 try w.writeAll("\n");86 try w.writeAll("\n");
80 try w.writeAll("pub const system_libs = &[_][]const u8{\n");87 try w.writeAll("pub const system_libs = &[_][]const u8{\n");
81 try print_sys_libs_to(w, top_module, &std.ArrayList([]const u8).init(gpa), &std.ArrayList([]const u8).init(gpa));88 try print_sys_libs_to(w, list.items, &std.ArrayList([]const u8).init(gpa));
89 try w.writeAll("};\n");
82 try w.writeAll("};\n");90 try w.writeAll("};\n");
83}91}
8492
...@@ -225,40 +233,30 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {...@@ -225,40 +233,30 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {
225 };233 };
226}234}
227235
228fn print_ids(w: fs.File.Writer, mod: u.Module, list: *std.ArrayList([]const u8)) anyerror!void {236fn print_ids(w: fs.File.Writer, list: []u.Module) !void {
229 if (u.list_contains(list.items, mod.clean_path)) {237 for (list) |mod| {
230 return;238 if (mod.is_sys_lib) {
231 }239 continue;
232 try list.append(mod.clean_path);240 }
233 //241 if (mod.clean_path.len == 0) {
234 if (mod.clean_path.len == 0) {242 try w.print(" \"\",\n", .{});
235 try w.print(" \"\",\n", .{});243 } else {
236 } else {244 try w.print(" \"{}\",\n", .{mod.id});
237 try w.print(" \"{}\",\n", .{mod.id});245 }
238 }
239 //
240 for (mod.deps) |d| {
241 if (d.is_sys_lib) continue;
242 try print_ids(w, d, list);
243 }246 }
244}247}
245248
246fn print_paths(w: fs.File.Writer, mod: u.Module, list: *std.ArrayList([]const u8)) anyerror!void {249fn print_paths(w: fs.File.Writer, list: []u.Module) !void {
247 if (u.list_contains(list.items, mod.clean_path)) {250 for (list) |mod| {
248 return;251 if (mod.is_sys_lib) {
249 }252 continue;
250 try list.append(mod.clean_path);253 }
251 //254 if (mod.clean_path.len == 0) {
252 if (mod.clean_path.len == 0) {255 try w.print(" \"\",\n", .{});
253 try w.print(" \"\",\n", .{});256 } else {
254 } else {257 const s = std.fs.path.sep_str;
255 const s = std.fs.path.sep_str;258 try w.print(" \"{Z}{Z}{Z}\",\n", .{s, mod.clean_path, s});
256 try w.print(" \"{Z}{Z}{Z}\",\n", .{s, mod.clean_path, s});259 }
257 }
258 //
259 for (mod.deps) |d| {
260 if (d.is_sys_lib) continue;
261 try print_paths(w, d, list);
262 }260 }
263}261}
264262
...@@ -295,77 +293,69 @@ fn print_deps(w: fs.File.Writer, dir: []const u8, m: u.Module, tabs: i32, array:...@@ -295,77 +293,69 @@ fn print_deps(w: fs.File.Writer, dir: []const u8, m: u.Module, tabs: i32, array:
295 try w.print("{}", .{try u.concat(&[_][]const u8{r,"}"})});293 try w.print("{}", .{try u.concat(&[_][]const u8{r,"}"})});
296}294}
297295
298fn print_incl_dirs_to(w: fs.File.Writer, mod: u.Module, list: *std.ArrayList([]const u8), local: bool) anyerror!void {296fn print_incl_dirs_to(w: fs.File.Writer, list: []u.Module) !void {
299 if (u.list_contains(list.items, mod.clean_path)) {297 for (list) |mod, i| {
300 return;298 if (mod.is_sys_lib) {
301 }299 continue;
302 try list.append(mod.clean_path);300 }
303 for (mod.c_include_dirs) |it| {301 for (mod.c_include_dirs) |it| {
304 if (!local) {302 if (i > 0) {
305 try w.print(" cache ++ _paths[{}] ++ \"{Z}\",\n", .{list.items.len-1, it});303 try w.print(" cache ++ _paths[{}] ++ \"{Z}\",\n", .{i, it});
306 } else {304 } else {
307 try w.print(" \"\",\n", .{});305 try w.print(" \"\",\n", .{});
306 }
308 }307 }
309 }
310 for (mod.deps) |d| {
311 if (d.is_sys_lib) continue;
312 try print_incl_dirs_to(w, d, list, false);
313 }308 }
314}309}
315310
316fn print_csrc_dirs_to(w: fs.File.Writer, mod: u.Module, list: *std.ArrayList([]const u8), local: bool) anyerror!void {311fn print_csrc_dirs_to(w: fs.File.Writer, list: []u.Module) !void {
317 if (u.list_contains(list.items, mod.clean_path)) {312 for (list) |mod, i| {
318 return;313 if (mod.is_sys_lib) {
314 continue;
315 }
316 for (mod.c_source_files) |it| {
317 if (i > 0) {
318 try w.print(" {}_ids[{}], cache ++ _paths[{}] ++ \"{}\"{},\n", .{"[_][]const u8{", i, i, it, "}"});
319 } else {
320 try w.print(" {}\"{}\", \".{}/{}\"{},\n", .{"[_][]const u8{", mod.clean_path, mod.clean_path, it, "}"});
321 }
322 }
319 }323 }
320 try list.append(mod.clean_path);324}
321 for (mod.c_source_files) |it| {325
322 if (!local) {326fn print_csrc_flags_to(w: fs.File.Writer, list: []u.Module) !void {
323 try w.print(" {}_ids[{}], cache ++ _paths[{}] ++ \"{}\"{},\n", .{"[_][]const u8{", list.items.len-1, list.items.len-1, it, "}"});327 for (list) |mod, i| {
328 if (mod.is_sys_lib) {
329 continue;
330 }
331 if (i == 0) {
332 try w.print(" pub const @\"{}\" = {};\n", .{"", "&[_][]const u8{}"});
324 } else {333 } else {
325 try w.print(" {}\"{}\", \".{}/{}\"{},\n", .{"[_][]const u8{", mod.clean_path, mod.clean_path, it, "}"});334 try w.print(" pub const @\"{}\" = {}", .{mod.id, "&[_][]const u8{"});
335 for (mod.c_source_flags) |it| {
336 try w.print("\"{Z}\",", .{it});
337 }
338 try w.print("{};\n", .{"}"});
326 }339 }
327 }340 }
328 for (mod.deps) |d| {
329 if (d.is_sys_lib) continue;
330 try print_csrc_dirs_to(w, d, list, false);
331 }
332}341}
333342
334fn print_csrc_flags_to(w: fs.File.Writer, mod: u.Module, list: *std.ArrayList([]const u8), local: bool) anyerror!void {343fn print_sys_libs_to(w: fs.File.Writer, list: []u.Module, list2: *std.ArrayList([]const u8)) !void {
335 if (u.list_contains(list.items, mod.clean_path)) {344 for (list) |mod| {
336 return;345 if (!mod.is_sys_lib) {
337 }346 continue;
338 try list.append(mod.clean_path);
339 if (local) {
340 try w.print(" pub const @\"{}\" = {};\n", .{"", "&[_][]const u8{}"});
341 }
342 else {
343 try w.print(" pub const @\"{}\" = {}", .{mod.id, "&[_][]const u8{"});
344 for (mod.c_source_flags) |it| {
345 try w.print("\"{Z}\",", .{it});
346 }347 }
347 try w.print("{};\n", .{"}"});348 try w.print(" \"{}\",\n", .{mod.name});
348 }
349 for (mod.deps) |d| {
350 if (d.is_sys_lib) continue;
351 try print_csrc_flags_to(w, d, list, false);
352 }349 }
353}350}
354351
355fn print_sys_libs_to(w: fs.File.Writer, mod: u.Module, list: *std.ArrayList([]const u8), list2: *std.ArrayList([]const u8)) anyerror!void {352fn collect_pkgs(mod: u.Module, list: *std.ArrayList(u.Module)) anyerror!void {
356 if (u.list_contains(list.items, mod.clean_path)) {353 //
354 if (u.list_contains_gen(u.Module, list, mod)) {
357 return;355 return;
358 }356 }
359 try list.append(mod.clean_path);357 try list.append(mod);
360 //
361 for (mod.deps) |d| {358 for (mod.deps) |d| {
362 if (!d.is_sys_lib) continue;359 try collect_pkgs(d, list);
363 //
364 if (!u.list_contains(list2.items, d.name)) {
365 try w.print(" \"{}\",\n", .{d.name});
366 try list2.append(d.name);
367 }
368 //
369 try print_sys_libs_to(w, d, list, list2);
370 }360 }
371}361}