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 {...@@ -49,6 +49,14 @@ pub fn execute(args: [][]u8) !void {
49 \\}49 \\}
50 });50 });
51 try w.print("\n", .{});51 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", .{});
52 try w.print("pub const packages = ", .{});60 try w.print("pub const packages = ", .{});
53 try print_deps(w, dir, top_module, 0, true);61 try print_deps(w, dir, top_module, 0, true);
54 try w.print(";\n", .{});62 try w.print(";\n", .{});
...@@ -162,6 +170,7 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {...@@ -162,6 +170,7 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {
162 .system_lib => {170 .system_lib => {
163 if (d.is_for_this()) try moduledeps.append(u.Module{171 if (d.is_for_this()) try moduledeps.append(u.Module{
164 .is_sys_lib = true,172 .is_sys_lib = true,
173 .id = "",
165 .name = d.path,174 .name = d.path,
166 .only_os = d.only_os,175 .only_os = d.only_os,
167 .except_os = d.except_os,176 .except_os = d.except_os,
...@@ -178,6 +187,7 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {...@@ -178,6 +187,7 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {
178 error.FileNotFound => {187 error.FileNotFound => {
179 if (d.main.len > 0 or d.c_include_dirs.len > 0 or d.c_source_files.len > 0) {188 if (d.main.len > 0 or d.c_include_dirs.len > 0 or d.c_source_files.len > 0) {
180 var mod_from = try u.Module.from(d);189 var mod_from = try u.Module.from(d);
190 mod_from.id = try u.random_string(48);
181 mod_from.clean_path = u.trim_prefix(moddir, dir)[1..];191 mod_from.clean_path = u.trim_prefix(moddir, dir)[1..];
182 if (mod_from.is_for_this()) try moduledeps.append(mod_from);192 if (mod_from.is_for_this()) try moduledeps.append(mod_from);
183 }193 }
...@@ -187,6 +197,7 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {...@@ -187,6 +197,7 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {
187 };197 };
188 dd.clean_path = u.trim_prefix(moddir, dir)[1..];198 dd.clean_path = u.trim_prefix(moddir, dir)[1..];
189199
200 if (dd.id.len == 0) dd.id = try u.random_string(48);
190 if (d.name.len > 0) dd.name = d.name;201 if (d.name.len > 0) dd.name = d.name;
191 if (d.main.len > 0) dd.main = d.main;202 if (d.main.len > 0) dd.main = d.main;
192 if (d.c_include_dirs.len > 0) dd.c_include_dirs = d.c_include_dirs;203 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 {...@@ -201,6 +212,7 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {
201 }212 }
202 return u.Module{213 return u.Module{
203 .is_sys_lib = false,214 .is_sys_lib = false,
215 .id = m.id,
204 .name = m.name,216 .name = m.name,
205 .main = m.main,217 .main = m.main,
206 .c_include_dirs = m.c_include_dirs,218 .c_include_dirs = m.c_include_dirs,
...@@ -213,6 +225,43 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {...@@ -213,6 +225,43 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {
213 };225 };
214}226}
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
216fn print_deps(w: fs.File.Writer, dir: []const u8, m: u.Module, tabs: i32, array: bool) anyerror!void {265fn print_deps(w: fs.File.Writer, dir: []const u8, m: u.Module, tabs: i32, array: bool) anyerror!void {
217 if (m.deps.len == 0 and tabs > 0) {266 if (m.deps.len == 0 and tabs > 0) {
218 try w.print("null", .{});267 try w.print("null", .{});
...@@ -253,9 +302,9 @@ fn print_incl_dirs_to(w: fs.File.Writer, mod: u.Module, list: *std.ArrayList([]c...@@ -253,9 +302,9 @@ fn print_incl_dirs_to(w: fs.File.Writer, mod: u.Module, list: *std.ArrayList([]c
253 try list.append(mod.clean_path);302 try list.append(mod.clean_path);
254 for (mod.c_include_dirs) |it| {303 for (mod.c_include_dirs) |it| {
255 if (!local) {304 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});
257 } else {306 } else {
258 try w.print(" \".{}/{}\",\n", .{mod.clean_path, it});307 // try w.print(" ,\n", .{mod.clean_path, it});
259 }308 }
260 }309 }
261 for (mod.deps) |d| {310 for (mod.deps) |d| {
...@@ -271,9 +320,9 @@ fn print_csrc_dirs_to(w: fs.File.Writer, mod: u.Module, list: *std.ArrayList([]c...@@ -271,9 +320,9 @@ fn print_csrc_dirs_to(w: fs.File.Writer, mod: u.Module, list: *std.ArrayList([]c
271 try list.append(mod.clean_path);320 try list.append(mod.clean_path);
272 for (mod.c_source_files) |it| {321 for (mod.c_source_files) |it| {
273 if (!local) {322 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, "}"});
275 } else {324 } 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, "}"});
277 }326 }
278 }327 }
279 for (mod.deps) |d| {328 for (mod.deps) |d| {
...@@ -291,7 +340,7 @@ fn print_csrc_flags_to(w: fs.File.Writer, mod: u.Module, list: *std.ArrayList([]...@@ -291,7 +340,7 @@ fn print_csrc_flags_to(w: fs.File.Writer, mod: u.Module, list: *std.ArrayList([]
291 try w.print(" pub const @\"{}\" = {};\n", .{"", "&[_][]const u8{}"});340 try w.print(" pub const @\"{}\" = {};\n", .{"", "&[_][]const u8{}"});
292 }341 }
293 else {342 else {
294 try w.print(" pub const @\"{}\" = {}", .{mod.clean_path, "&[_][]const u8{"});343 try w.print(" pub const @\"{}\" = {}", .{mod.id, "&[_][]const u8{"});
295 for (mod.c_source_flags) |it| {344 for (mod.c_source_flags) |it| {
296 try w.print("\"{Z}\",", .{it});345 try w.print("\"{Z}\",", .{it});
297 }346 }
src/common.zig+4
...@@ -24,6 +24,7 @@ pub fn collect_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {...@@ -24,6 +24,7 @@ pub fn collect_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {
24 .system_lib => {24 .system_lib => {
25 if (d.is_for_this()) try moduledeps.append(u.Module{25 if (d.is_for_this()) try moduledeps.append(u.Module{
26 .is_sys_lib = true,26 .is_sys_lib = true,
27 .id = d.id,
27 .name = d.path,28 .name = d.path,
28 .only_os = d.only_os,29 .only_os = d.only_os,
29 .except_os = d.except_os,30 .except_os = d.except_os,
...@@ -40,6 +41,7 @@ pub fn collect_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {...@@ -40,6 +41,7 @@ pub fn collect_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {
40 error.FileNotFound => {41 error.FileNotFound => {
41 if (d.c_include_dirs.len > 0 or d.c_source_files.len > 0) {42 if (d.c_include_dirs.len > 0 or d.c_source_files.len > 0) {
42 var mod_from = try u.Module.from(d);43 var mod_from = try u.Module.from(d);
44 mod_from.id = try u.random_string(48);
43 mod_from.clean_path = u.trim_prefix(moddir, dir)[1..];45 mod_from.clean_path = u.trim_prefix(moddir, dir)[1..];
44 if (mod_from.is_for_this()) try moduledeps.append(mod_from);46 if (mod_from.is_for_this()) try moduledeps.append(mod_from);
45 }47 }
...@@ -49,6 +51,7 @@ pub fn collect_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {...@@ -49,6 +51,7 @@ pub fn collect_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {
49 };51 };
50 dd.clean_path = u.trim_prefix(moddir, dir)[1..];52 dd.clean_path = u.trim_prefix(moddir, dir)[1..];
5153
54 if (dd.id.len == 0) dd.id = try u.random_string(48);
52 if (d.name.len > 0) dd.name = d.name;55 if (d.name.len > 0) dd.name = d.name;
53 if (d.main.len > 0) dd.main = d.main;56 if (d.main.len > 0) dd.main = d.main;
54 if (d.c_include_dirs.len > 0) dd.c_include_dirs = d.c_include_dirs;57 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 {...@@ -63,6 +66,7 @@ pub fn collect_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {
63 }66 }
64 return u.Module{67 return u.Module{
65 .is_sys_lib = false,68 .is_sys_lib = false,
69 .id = m.id,
66 .name = m.name,70 .name = m.name,
67 .main = m.main,71 .main = m.main,
68 .c_include_dirs = m.c_include_dirs,72 .c_include_dirs = m.c_include_dirs,
src/util/dep.zig+1
...@@ -13,6 +13,7 @@ pub const Dep = struct {...@@ -13,6 +13,7 @@ pub const Dep = struct {
13 type: u.DepType,13 type: u.DepType,
14 path: []const u8,14 path: []const u8,
1515
16 id: []const u8,
16 name: []const u8,17 name: []const u8,
17 main: []const u8,18 main: []const u8,
18 version: []const u8,19 version: []const u8,
src/util/modfile.zig+4
...@@ -14,6 +14,7 @@ pub const ModFile = struct {...@@ -14,6 +14,7 @@ pub const ModFile = struct {
14 const Self = @This();14 const Self = @This();
1515
16 alloc: *std.mem.Allocator,16 alloc: *std.mem.Allocator,
17 id: []const u8,
17 name: []const u8,18 name: []const u8,
18 main: []const u8,19 main: []const u8,
19 c_include_dirs: [][]const u8,20 c_include_dirs: [][]const u8,
...@@ -29,6 +30,7 @@ pub const ModFile = struct {...@@ -29,6 +30,7 @@ pub const ModFile = struct {
29 const input = try file.reader().readAllAlloc(alloc, mb);30 const input = try file.reader().readAllAlloc(alloc, mb);
30 const doc = try yaml.parse(alloc, input);31 const doc = try yaml.parse(alloc, input);
3132
33 const id = doc.mapping.get_string("id");
32 const name = doc.mapping.get("name").?.string;34 const name = doc.mapping.get("name").?.string;
33 const main = doc.mapping.get("main").?.string;35 const main = doc.mapping.get("main").?.string;
3436
...@@ -43,6 +45,7 @@ pub const ModFile = struct {...@@ -43,6 +45,7 @@ pub const ModFile = struct {
43 try dep_list.append(u.Dep{45 try dep_list.append(u.Dep{
44 .type = dep_type,46 .type = dep_type,
45 .path = path,47 .path = path,
48 .id = "",
46 .name = item.mapping.get_string("name"),49 .name = item.mapping.get_string("name"),
47 .main = item.mapping.get_string("main"),50 .main = item.mapping.get_string("main"),
48 .version = item.mapping.get_string("version"),51 .version = item.mapping.get_string("version"),
...@@ -58,6 +61,7 @@ pub const ModFile = struct {...@@ -58,6 +61,7 @@ pub const ModFile = struct {
5861
59 return Self{62 return Self{
60 .alloc = alloc,63 .alloc = alloc,
64 .id = if (id.len == 0) try u.random_string(48) else id,
61 .name = name,65 .name = name,
62 .main = main,66 .main = main,
63 .c_include_dirs = try doc.mapping.get_string_array(alloc, "c_include_dirs"),67 .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");...@@ -9,6 +9,7 @@ const u = @import("index.zig");
99
10pub const Module = struct {10pub const Module = struct {
11 is_sys_lib: bool,11 is_sys_lib: bool,
12 id: []const u8,
12 name: []const u8,13 name: []const u8,
13 main: []const u8,14 main: []const u8,
14 c_include_dirs: [][]const u8,15 c_include_dirs: [][]const u8,
...@@ -23,6 +24,7 @@ pub const Module = struct {...@@ -23,6 +24,7 @@ pub const Module = struct {
23 pub fn from(dep: u.Dep) !Module {24 pub fn from(dep: u.Dep) !Module {
24 return Module{25 return Module{
25 .is_sys_lib = false,26 .is_sys_lib = false,
27 .id = dep.id,
26 .name = dep.name,28 .name = dep.name,
27 .main = dep.main,29 .main = dep.main,
28 .c_include_dirs = dep.c_include_dirs,30 .c_include_dirs = dep.c_include_dirs,