diff --git a/src/common.zig b/src/common.zig index e3b166ffae0c451b9e3b4658eabf736b3f7d5c15..e60c7872fdbfa841c54a6594b3adb0aec812676b 100644 --- a/src/common.zig +++ b/src/common.zig @@ -45,6 +45,7 @@ pub fn collect_deps_deep(cachepath: string, mdir: std.fs.Dir, options: *CollectO } } return zigmod.Module{ + .alloc = gpa, .is_sys_lib = false, .id = "root", .name = "root", @@ -70,6 +71,7 @@ pub fn collect_deps(cachepath: string, mdir: std.fs.Dir, options: *CollectOption } } return zigmod.Module{ + .alloc = gpa, .is_sys_lib = false, .id = m.id, .name = m.name, @@ -218,6 +220,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO switch (d.type) { .system_lib => { return zigmod.Module{ + .alloc = gpa, .is_sys_lib = true, .id = try u.do_hash(std.crypto.hash.sha3.Sha3_384, d.path), .name = d.path, @@ -234,7 +237,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO var dd = try collect_deps(cachepath, moddir, options) catch |e| switch (e) { 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 zigmod.Module.from(d.*, cachepath, options); + var mod_from = try zigmod.Module.from(gpa, d.*, cachepath, options); if (d.type != .local) mod_from.clean_path = u.trim_prefix(modpath, cachepath)[1..]; if (mod_from.is_for_this()) return mod_from; return null; @@ -248,7 +251,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO if (trymain) |_| { d.*.name = tryname; d.*.main = trymain.?; - var mod_from = try zigmod.Module.from(d.*, cachepath, options); + var mod_from = try zigmod.Module.from(gpa, d.*, cachepath, options); if (d.type != .local) mod_from.clean_path = u.trim_prefix(modpath, cachepath)[1..]; if (mod_from.is_for_this()) return mod_from; return null; @@ -327,6 +330,7 @@ pub fn add_files_package(pkg_name: string, mdir: std.fs.Dir, dirs: []const strin ); var d: zigmod.Dep = .{ + .alloc = gpa, .type = .local, .path = "files", .id = "", @@ -368,6 +372,7 @@ pub fn parse_lockfile(dir: std.fs.Dir) ![]const [4]string { 2 => { var iter = std.mem.split(u8, line, " "); const asdep = zigmod.Dep{ + .alloc = gpa, .type = std.meta.stringToEnum(zigmod.DepType, iter.next().?).?, .path = iter.next().?, .version = iter.next().?, diff --git a/src/util/dep.zig b/src/util/dep.zig index 5a73900f6970d7a993e562920fab8737ca02975c..f10f62ee804f4c0bf0e0ad05948b850d688d23aa 100644 --- a/src/util/dep.zig +++ b/src/util/dep.zig @@ -1,6 +1,5 @@ const std = @import("std"); const string = []const u8; -const gpa = std.heap.c_allocator; const builtin = std.builtin; const zigmod = @import("../lib.zig"); @@ -13,9 +12,9 @@ const yaml = @import("./yaml.zig"); pub const Dep = struct { const Self = @This(); + alloc: *std.mem.Allocator, type: zigmod.DepType, path: string, - id: string, name: string, main: string, @@ -37,16 +36,16 @@ pub const Dep = struct { p = u.trim_prefix(p, "https://"); p = u.trim_prefix(p, "git://"); p = u.trim_suffix(p, ".git"); - p = try std.mem.join(gpa, "/", &.{ @tagName(self.type), p }); + p = try std.mem.join(self.alloc, "/", &.{ @tagName(self.type), p }); return p; } pub fn clean_path_v(self: Dep) !string { if (self.type == .http and self.version.len > 0) { const i = std.mem.indexOf(u8, self.version, "-").?; - return std.mem.join(gpa, "/", &.{ "v", try self.clean_path(), self.version[i + 1 .. 15] }); + return std.mem.join(self.alloc, "/", &.{ "v", try self.clean_path(), self.version[i + 1 .. 15] }); } - return std.mem.join(gpa, "/", &.{ "v", try self.clean_path(), self.version }); + return std.mem.join(self.alloc, "/", &.{ "v", try self.clean_path(), self.version }); } pub fn is_for_this(self: Dep) bool { @@ -62,13 +61,13 @@ pub const Dep = struct { pub fn exact_version(self: Dep, dpath: string) !string { if (self.version.len == 0) { - return try self.type.exact_version(dpath); + return try self.type.exact_version(self.alloc, dpath); } return switch (self.type) { .git => blk: { const vers = try u.parse_split(zigmod.DepType.GitVersion, "-").do(self.version); if (vers.id.frozen()) break :blk self.version; - break :blk try self.type.exact_version(dpath); + break :blk try self.type.exact_version(self.alloc, dpath); }, else => self.version, }; diff --git a/src/util/dep_type.zig b/src/util/dep_type.zig index 4ff949f4d37a673e5bf6a6152520e9095bee085f..d9dc51d2e05e2a0a011ffdf630dbbe77edb788d9 100644 --- a/src/util/dep_type.zig +++ b/src/util/dep_type.zig @@ -1,6 +1,5 @@ const std = @import("std"); const string = []const u8; -const gpa = std.heap.c_allocator; const u = @import("./index.zig"); @@ -78,13 +77,13 @@ pub const DepType = enum { } // zig fmt: on - pub fn exact_version(self: DepType, mpath: string) !string { + pub fn exact_version(self: DepType, alloc: *std.mem.Allocator, mpath: string) !string { var mdir = try std.fs.cwd().openDir(mpath, .{}); defer mdir.close(); return switch (self) { .local => "", .system_lib => "", - .git => try std.fmt.allocPrint(gpa, "commit-{s}", .{(try u.git_rev_HEAD(gpa, mdir))}), + .git => try std.fmt.allocPrint(alloc, "commit-{s}", .{(try u.git_rev_HEAD(alloc, mdir))}), .hg => "", .http => "", }; diff --git a/src/util/modfile.zig b/src/util/modfile.zig index 7862a31a9ac99b3faa5ca910166e6d44ab025cc9..6961648f633887c24e5acc6ffa7f3b0568f89993 100644 --- a/src/util/modfile.zig +++ b/src/util/modfile.zig @@ -108,6 +108,7 @@ pub const ModFile = struct { } try dep_list.append(zigmod.Dep{ + .alloc = alloc, .type = dep_type, .path = path, .id = item.mapping.get_string("id"), diff --git a/src/util/module.zig b/src/util/module.zig index a1571a36225f7119cf23c88440188e9cc36fa20b..053ca36798b14dcc2a4e1f341b7aa38d88adcfdb 100644 --- a/src/util/module.zig +++ b/src/util/module.zig @@ -1,6 +1,5 @@ const std = @import("std"); const string = []const u8; -const gpa = std.heap.c_allocator; const builtin = @import("builtin"); const zigmod = @import("../lib.zig"); @@ -12,6 +11,7 @@ const common = @import("./../common.zig"); // pub const Module = struct { + alloc: *std.mem.Allocator, is_sys_lib: bool, id: string, name: string, @@ -26,15 +26,17 @@ pub const Module = struct { clean_path: string, dep: ?zigmod.Dep, - pub fn from(dep: zigmod.Dep, dir: string, options: *common.CollectOptions) !Module { - var moddeps = std.ArrayList(Module).init(gpa); + pub fn from(alloc: *std.mem.Allocator, dep: zigmod.Dep, dir: string, options: *common.CollectOptions) !Module { + var moddeps = std.ArrayList(Module).init(alloc); defer moddeps.deinit(); + for (dep.deps) |*d| { if (try common.get_module_from_dep(d, dir, options)) |founddep| { try moddeps.append(founddep); } } return Module{ + .alloc = alloc, .is_sys_lib = false, .id = if (dep.id.len > 0) dep.id else try u.random_string(48), .name = dep.name, @@ -56,9 +58,9 @@ pub const Module = struct { } pub fn get_hash(self: Module, cdpath: string) !string { - const file_list_1 = try u.file_list(try std.mem.concat(gpa, u8, &.{ cdpath, "/", self.clean_path })); + const file_list_1 = try u.file_list(try std.mem.concat(self.alloc, u8, &.{ cdpath, "/", self.clean_path })); - var file_list_2 = std.ArrayList(string).init(gpa); + var file_list_2 = std.ArrayList(string).init(self.alloc); defer file_list_2.deinit(); for (file_list_1) |item| { const _a = u.trim_prefix(item, cdpath); @@ -76,15 +78,15 @@ pub const Module = struct { const h = &std.crypto.hash.Blake3.init(.{}); for (file_list_2.items) |item| { - const abs_path = try std.fs.path.join(gpa, &.{ cdpath, self.clean_path, item }); + const abs_path = try std.fs.path.join(self.alloc, &.{ cdpath, self.clean_path, item }); const file = try std.fs.cwd().openFile(abs_path, .{}); defer file.close(); - const input = try file.reader().readAllAlloc(gpa, u.mb * 100); + const input = try file.reader().readAllAlloc(self.alloc, u.mb * 100); h.update(input); } var out: [32]u8 = undefined; h.final(&out); - const hex = try std.fmt.allocPrint(gpa, "blake3-{x}", .{std.fmt.fmtSliceHexLower(out[0..])}); + const hex = try std.fmt.allocPrint(self.alloc, "blake3-{x}", .{std.fmt.fmtSliceHexLower(out[0..])}); return hex; }