From 0cfb4dde371260a9616d7595f8de87f4c424e1d0 Mon Sep 17 00:00:00 2001 From: Meghan Date: Mon, 28 Dec 2020 19:16:35 -0800 Subject: [PATCH] add id attribute to module structures --- src/cmd_fetch.zig | 59 ++++++++++++++++++++++++++++++++++++++++---- src/common.zig | 4 +++ src/util/dep.zig | 1 + src/util/modfile.zig | 4 +++ src/util/module.zig | 2 ++ 5 files changed, 65 insertions(+), 5 deletions(-) diff --git a/src/cmd_fetch.zig b/src/cmd_fetch.zig index 5b39bd68b0994c482eac0c74150f06cd36e48d90..41c056498b9750709c43c7f61423a716fd40ee9a 100644 --- a/src/cmd_fetch.zig +++ b/src/cmd_fetch.zig @@ -49,6 +49,14 @@ pub fn execute(args: [][]u8) !void { \\} }); try w.print("\n", .{}); + try w.print("pub const _ids = {}\n", .{".{"}); + try print_ids(w, top_module, &std.ArrayList([]const u8).init(gpa)); + try w.print("{}\n", .{"};"}); + try w.print("\n", .{}); + try w.print("pub const _paths = {}\n", .{".{"}); + try print_paths(w, top_module, &std.ArrayList([]const u8).init(gpa)); + try w.print("{}\n", .{"};"}); + try w.print("\n", .{}); try w.print("pub const packages = ", .{}); try print_deps(w, dir, top_module, 0, true); try w.print(";\n", .{}); @@ -162,6 +170,7 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module { .system_lib => { if (d.is_for_this()) try moduledeps.append(u.Module{ .is_sys_lib = true, + .id = "", .name = d.path, .only_os = d.only_os, .except_os = d.except_os, @@ -178,6 +187,7 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module { error.FileNotFound => { if (d.main.len > 0 or d.c_include_dirs.len > 0 or d.c_source_files.len > 0) { var mod_from = try u.Module.from(d); + mod_from.id = try u.random_string(48); mod_from.clean_path = u.trim_prefix(moddir, dir)[1..]; if (mod_from.is_for_this()) try moduledeps.append(mod_from); } @@ -187,6 +197,7 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module { }; dd.clean_path = u.trim_prefix(moddir, dir)[1..]; + if (dd.id.len == 0) dd.id = try u.random_string(48); if (d.name.len > 0) dd.name = d.name; if (d.main.len > 0) dd.main = d.main; if (d.c_include_dirs.len > 0) dd.c_include_dirs = d.c_include_dirs; @@ -201,6 +212,7 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module { } return u.Module{ .is_sys_lib = false, + .id = m.id, .name = m.name, .main = m.main, .c_include_dirs = m.c_include_dirs, @@ -213,6 +225,43 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module { }; } +fn print_ids(w: fs.File.Writer, mod: u.Module, list: *std.ArrayList([]const u8)) anyerror!void { + if (u.list_contains(list.items, mod.clean_path)) { + return; + } + try list.append(mod.clean_path); + // + if (mod.clean_path.len == 0) { + try w.print(" \"\",\n", .{}); + } else { + try w.print(" \"{}\",\n", .{mod.id}); + } + // + for (mod.deps) |d| { + if (d.is_sys_lib) continue; + try print_ids(w, d, list); + } +} + +fn print_paths(w: fs.File.Writer, mod: u.Module, list: *std.ArrayList([]const u8)) anyerror!void { + if (u.list_contains(list.items, mod.clean_path)) { + return; + } + try list.append(mod.clean_path); + // + if (mod.clean_path.len == 0) { + try w.print(" \"\",\n", .{}); + } else { + const s = std.fs.path.sep_str; + try w.print(" \"{Z}{Z}{Z}\",\n", .{s, mod.clean_path, s}); + } + // + for (mod.deps) |d| { + if (d.is_sys_lib) continue; + try print_paths(w, d, list); + } +} + fn print_deps(w: fs.File.Writer, dir: []const u8, m: u.Module, tabs: i32, array: bool) anyerror!void { if (m.deps.len == 0 and tabs > 0) { try w.print("null", .{}); @@ -253,9 +302,9 @@ fn print_incl_dirs_to(w: fs.File.Writer, mod: u.Module, list: *std.ArrayList([]c try list.append(mod.clean_path); for (mod.c_include_dirs) |it| { if (!local) { - try w.print(" cache ++ \"/{}/{}\",\n", .{mod.clean_path, it}); + try w.print(" cache ++ _paths[{}] ++ \"{Z}\",\n", .{list.items.len-1, it}); } else { - try w.print(" \".{}/{}\",\n", .{mod.clean_path, it}); + // try w.print(" ,\n", .{mod.clean_path, it}); } } for (mod.deps) |d| { @@ -271,9 +320,9 @@ fn print_csrc_dirs_to(w: fs.File.Writer, mod: u.Module, list: *std.ArrayList([]c try list.append(mod.clean_path); for (mod.c_source_files) |it| { if (!local) { - try w.print(" {}comptime get_flags({}), cache ++ \"/{}/{}\"{},\n", .{"[_][]const u8{", list.items.len-1, mod.clean_path, it, "}"}); + try w.print(" {}_ids[{}], cache ++ _paths[{}] ++ \"{}\"{},\n", .{"[_][]const u8{", list.items.len-1, list.items.len-1, it, "}"}); } else { - try w.print(" {}comptime get_flags({}), \".{}/{}\"{},\n", .{"[_][]const u8{", list.items.len-1, mod.clean_path, it, "}"}); + try w.print(" {}\"{}\", \".{}/{}\"{},\n", .{"[_][]const u8{", mod.clean_path, mod.clean_path, it, "}"}); } } for (mod.deps) |d| { @@ -291,7 +340,7 @@ fn print_csrc_flags_to(w: fs.File.Writer, mod: u.Module, list: *std.ArrayList([] try w.print(" pub const @\"{}\" = {};\n", .{"", "&[_][]const u8{}"}); } else { - try w.print(" pub const @\"{}\" = {}", .{mod.clean_path, "&[_][]const u8{"}); + try w.print(" pub const @\"{}\" = {}", .{mod.id, "&[_][]const u8{"}); for (mod.c_source_flags) |it| { try w.print("\"{Z}\",", .{it}); } diff --git a/src/common.zig b/src/common.zig index 06916e981d8e876e809a229833fcb9b864bc8983..76cdf3be0651cfd554e72d94e32ae7a22e499719 100644 --- a/src/common.zig +++ b/src/common.zig @@ -24,6 +24,7 @@ pub fn collect_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module { .system_lib => { if (d.is_for_this()) try moduledeps.append(u.Module{ .is_sys_lib = true, + .id = d.id, .name = d.path, .only_os = d.only_os, .except_os = d.except_os, @@ -40,6 +41,7 @@ pub fn collect_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module { error.FileNotFound => { if (d.c_include_dirs.len > 0 or d.c_source_files.len > 0) { var mod_from = try u.Module.from(d); + mod_from.id = try u.random_string(48); mod_from.clean_path = u.trim_prefix(moddir, dir)[1..]; if (mod_from.is_for_this()) try moduledeps.append(mod_from); } @@ -49,6 +51,7 @@ pub fn collect_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module { }; dd.clean_path = u.trim_prefix(moddir, dir)[1..]; + if (dd.id.len == 0) dd.id = try u.random_string(48); if (d.name.len > 0) dd.name = d.name; if (d.main.len > 0) dd.main = d.main; if (d.c_include_dirs.len > 0) dd.c_include_dirs = d.c_include_dirs; @@ -63,6 +66,7 @@ pub fn collect_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module { } return u.Module{ .is_sys_lib = false, + .id = m.id, .name = m.name, .main = m.main, .c_include_dirs = m.c_include_dirs, diff --git a/src/util/dep.zig b/src/util/dep.zig index 50db5ce9700c85088db2ece7e8c4faa6e1bfdc7c..148a8bc40fab3dfe0f47599ee4a96b2092c89e75 100644 --- a/src/util/dep.zig +++ b/src/util/dep.zig @@ -13,6 +13,7 @@ pub const Dep = struct { type: u.DepType, path: []const u8, + id: []const u8, name: []const u8, main: []const u8, version: []const u8, diff --git a/src/util/modfile.zig b/src/util/modfile.zig index 62ab2cbc45154cfab8085dba230c1dc89d27c2a6..3ca9779ee40221264b3cc48ffd7b1eb2756aa0ae 100644 --- a/src/util/modfile.zig +++ b/src/util/modfile.zig @@ -14,6 +14,7 @@ pub const ModFile = struct { const Self = @This(); alloc: *std.mem.Allocator, + id: []const u8, name: []const u8, main: []const u8, c_include_dirs: [][]const u8, @@ -29,6 +30,7 @@ pub const ModFile = struct { const input = try file.reader().readAllAlloc(alloc, mb); const doc = try yaml.parse(alloc, input); + const id = doc.mapping.get_string("id"); const name = doc.mapping.get("name").?.string; const main = doc.mapping.get("main").?.string; @@ -43,6 +45,7 @@ pub const ModFile = struct { try dep_list.append(u.Dep{ .type = dep_type, .path = path, + .id = "", .name = item.mapping.get_string("name"), .main = item.mapping.get_string("main"), .version = item.mapping.get_string("version"), @@ -58,6 +61,7 @@ pub const ModFile = struct { return Self{ .alloc = alloc, + .id = if (id.len == 0) try u.random_string(48) else id, .name = name, .main = main, .c_include_dirs = try doc.mapping.get_string_array(alloc, "c_include_dirs"), diff --git a/src/util/module.zig b/src/util/module.zig index dbc8ca1518f498687103799881cbb1b41f5f3072..4ec15833841bd048607093328805b19a98babc34 100644 --- a/src/util/module.zig +++ b/src/util/module.zig @@ -9,6 +9,7 @@ const u = @import("index.zig"); pub const Module = struct { is_sys_lib: bool, + id: []const u8, name: []const u8, main: []const u8, c_include_dirs: [][]const u8, @@ -23,6 +24,7 @@ pub const Module = struct { pub fn from(dep: u.Dep) !Module { return Module{ .is_sys_lib = false, + .id = dep.id, .name = dep.name, .main = dep.main, .c_include_dirs = dep.c_include_dirs, -- 2.54.0