authorgravatar for hello@nektro.netMeghan <hello@nektro.net> 2020-12-28 19:16:35 -08:00
committergravatar for hello@nektro.netMeghan <hello@nektro.net> 2020-12-28 19:16:35 -08:00
log0cfb4dde371260a9616d7595f8de87f4c424e1d0
treed1b866ab9b86e30d936f59b7eeb52c3444f6ab38
parent377b65529386eec23ab438e6d106b004b4d8c553

add id attribute to module structures


5 files changed, 65 insertions(+), 5 deletions(-)

src/cmd_fetch.zig+54-5
......@@ -49,6 +49,14 @@ pub fn execute(args: [][]u8) !void {
4949 \\}
5050 });
5151 try w.print("\n", .{});
52 try w.print("pub const _ids = {}\n", .{".{"});
53 try print_ids(w, top_module, &std.ArrayList([]const u8).init(gpa));
54 try w.print("{}\n", .{"};"});
55 try w.print("\n", .{});
56 try w.print("pub const _paths = {}\n", .{".{"});
57 try print_paths(w, top_module, &std.ArrayList([]const u8).init(gpa));
58 try w.print("{}\n", .{"};"});
59 try w.print("\n", .{});
5260 try w.print("pub const packages = ", .{});
5361 try print_deps(w, dir, top_module, 0, true);
5462 try w.print(";\n", .{});
......@@ -162,6 +170,7 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {
162170 .system_lib => {
163171 if (d.is_for_this()) try moduledeps.append(u.Module{
164172 .is_sys_lib = true,
173 .id = "",
165174 .name = d.path,
166175 .only_os = d.only_os,
167176 .except_os = d.except_os,
......@@ -178,6 +187,7 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {
178187 error.FileNotFound => {
179188 if (d.main.len > 0 or d.c_include_dirs.len > 0 or d.c_source_files.len > 0) {
180189 var mod_from = try u.Module.from(d);
190 mod_from.id = try u.random_string(48);
181191 mod_from.clean_path = u.trim_prefix(moddir, dir)[1..];
182192 if (mod_from.is_for_this()) try moduledeps.append(mod_from);
183193 }
......@@ -187,6 +197,7 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {
187197 };
188198 dd.clean_path = u.trim_prefix(moddir, dir)[1..];
189199
200 if (dd.id.len == 0) dd.id = try u.random_string(48);
190201 if (d.name.len > 0) dd.name = d.name;
191202 if (d.main.len > 0) dd.main = d.main;
192203 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 {
201212 }
202213 return u.Module{
203214 .is_sys_lib = false,
215 .id = m.id,
204216 .name = m.name,
205217 .main = m.main,
206218 .c_include_dirs = m.c_include_dirs,
......@@ -213,6 +225,43 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {
213225 };
214226}
215227
228fn print_ids(w: fs.File.Writer, mod: u.Module, list: *std.ArrayList([]const u8)) anyerror!void {
229 if (u.list_contains(list.items, mod.clean_path)) {
230 return;
231 }
232 try list.append(mod.clean_path);
233 //
234 if (mod.clean_path.len == 0) {
235 try w.print(" \"\",\n", .{});
236 } else {
237 try w.print(" \"{}\",\n", .{mod.id});
238 }
239 //
240 for (mod.deps) |d| {
241 if (d.is_sys_lib) continue;
242 try print_ids(w, d, list);
243 }
244}
245
246fn print_paths(w: fs.File.Writer, mod: u.Module, list: *std.ArrayList([]const u8)) anyerror!void {
247 if (u.list_contains(list.items, mod.clean_path)) {
248 return;
249 }
250 try list.append(mod.clean_path);
251 //
252 if (mod.clean_path.len == 0) {
253 try w.print(" \"\",\n", .{});
254 } else {
255 const s = std.fs.path.sep_str;
256 try w.print(" \"{Z}{Z}{Z}\",\n", .{s, mod.clean_path, s});
257 }
258 //
259 for (mod.deps) |d| {
260 if (d.is_sys_lib) continue;
261 try print_paths(w, d, list);
262 }
263}
264
216265fn print_deps(w: fs.File.Writer, dir: []const u8, m: u.Module, tabs: i32, array: bool) anyerror!void {
217266 if (m.deps.len == 0 and tabs > 0) {
218267 try w.print("null", .{});
......@@ -253,9 +302,9 @@ fn print_incl_dirs_to(w: fs.File.Writer, mod: u.Module, list: *std.ArrayList([]c
253302 try list.append(mod.clean_path);
254303 for (mod.c_include_dirs) |it| {
255304 if (!local) {
256 try w.print(" cache ++ \"/{}/{}\",\n", .{mod.clean_path, it});
305 try w.print(" cache ++ _paths[{}] ++ \"{Z}\",\n", .{list.items.len-1, it});
257306 } else {
258 try w.print(" \".{}/{}\",\n", .{mod.clean_path, it});
307 // try w.print(" ,\n", .{mod.clean_path, it});
259308 }
260309 }
261310 for (mod.deps) |d| {
......@@ -271,9 +320,9 @@ fn print_csrc_dirs_to(w: fs.File.Writer, mod: u.Module, list: *std.ArrayList([]c
271320 try list.append(mod.clean_path);
272321 for (mod.c_source_files) |it| {
273322 if (!local) {
274 try w.print(" {}comptime get_flags({}), cache ++ \"/{}/{}\"{},\n", .{"[_][]const u8{", list.items.len-1, mod.clean_path, it, "}"});
323 try w.print(" {}_ids[{}], cache ++ _paths[{}] ++ \"{}\"{},\n", .{"[_][]const u8{", list.items.len-1, list.items.len-1, it, "}"});
275324 } else {
276 try w.print(" {}comptime get_flags({}), \".{}/{}\"{},\n", .{"[_][]const u8{", list.items.len-1, mod.clean_path, it, "}"});
325 try w.print(" {}\"{}\", \".{}/{}\"{},\n", .{"[_][]const u8{", mod.clean_path, mod.clean_path, it, "}"});
277326 }
278327 }
279328 for (mod.deps) |d| {
......@@ -291,7 +340,7 @@ fn print_csrc_flags_to(w: fs.File.Writer, mod: u.Module, list: *std.ArrayList([]
291340 try w.print(" pub const @\"{}\" = {};\n", .{"", "&[_][]const u8{}"});
292341 }
293342 else {
294 try w.print(" pub const @\"{}\" = {}", .{mod.clean_path, "&[_][]const u8{"});
343 try w.print(" pub const @\"{}\" = {}", .{mod.id, "&[_][]const u8{"});
295344 for (mod.c_source_flags) |it| {
296345 try w.print("\"{Z}\",", .{it});
297346 }
src/common.zig+4
......@@ -24,6 +24,7 @@ pub fn collect_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {
2424 .system_lib => {
2525 if (d.is_for_this()) try moduledeps.append(u.Module{
2626 .is_sys_lib = true,
27 .id = d.id,
2728 .name = d.path,
2829 .only_os = d.only_os,
2930 .except_os = d.except_os,
......@@ -40,6 +41,7 @@ pub fn collect_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {
4041 error.FileNotFound => {
4142 if (d.c_include_dirs.len > 0 or d.c_source_files.len > 0) {
4243 var mod_from = try u.Module.from(d);
44 mod_from.id = try u.random_string(48);
4345 mod_from.clean_path = u.trim_prefix(moddir, dir)[1..];
4446 if (mod_from.is_for_this()) try moduledeps.append(mod_from);
4547 }
......@@ -49,6 +51,7 @@ pub fn collect_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {
4951 };
5052 dd.clean_path = u.trim_prefix(moddir, dir)[1..];
5153
54 if (dd.id.len == 0) dd.id = try u.random_string(48);
5255 if (d.name.len > 0) dd.name = d.name;
5356 if (d.main.len > 0) dd.main = d.main;
5457 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 {
6366 }
6467 return u.Module{
6568 .is_sys_lib = false,
69 .id = m.id,
6670 .name = m.name,
6771 .main = m.main,
6872 .c_include_dirs = m.c_include_dirs,
src/util/dep.zig+1
......@@ -13,6 +13,7 @@ pub const Dep = struct {
1313 type: u.DepType,
1414 path: []const u8,
1515
16 id: []const u8,
1617 name: []const u8,
1718 main: []const u8,
1819 version: []const u8,
src/util/modfile.zig+4
......@@ -14,6 +14,7 @@ pub const ModFile = struct {
1414 const Self = @This();
1515
1616 alloc: *std.mem.Allocator,
17 id: []const u8,
1718 name: []const u8,
1819 main: []const u8,
1920 c_include_dirs: [][]const u8,
......@@ -29,6 +30,7 @@ pub const ModFile = struct {
2930 const input = try file.reader().readAllAlloc(alloc, mb);
3031 const doc = try yaml.parse(alloc, input);
3132
33 const id = doc.mapping.get_string("id");
3234 const name = doc.mapping.get("name").?.string;
3335 const main = doc.mapping.get("main").?.string;
3436
......@@ -43,6 +45,7 @@ pub const ModFile = struct {
4345 try dep_list.append(u.Dep{
4446 .type = dep_type,
4547 .path = path,
48 .id = "",
4649 .name = item.mapping.get_string("name"),
4750 .main = item.mapping.get_string("main"),
4851 .version = item.mapping.get_string("version"),
......@@ -58,6 +61,7 @@ pub const ModFile = struct {
5861
5962 return Self{
6063 .alloc = alloc,
64 .id = if (id.len == 0) try u.random_string(48) else id,
6165 .name = name,
6266 .main = main,
6367 .c_include_dirs = try doc.mapping.get_string_array(alloc, "c_include_dirs"),
src/util/module.zig+2
......@@ -9,6 +9,7 @@ const u = @import("index.zig");
99
1010pub const Module = struct {
1111 is_sys_lib: bool,
12 id: []const u8,
1213 name: []const u8,
1314 main: []const u8,
1415 c_include_dirs: [][]const u8,
......@@ -23,6 +24,7 @@ pub const Module = struct {
2324 pub fn from(dep: u.Dep) !Module {
2425 return Module{
2526 .is_sys_lib = false,
27 .id = dep.id,
2628 .name = dep.name,
2729 .main = dep.main,
2830 .c_include_dirs = dep.c_include_dirs,