authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-10-28 04:06:50 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2021-10-28 04:06:50 -07:00
loga8579cb2f378fb3f773e5907887dae37adce3c93
tree2fb276b273932ecd6dea9bbce53ee9497df4be5e
parent291b6a07014a8e7b144643495c4425f6b472baf5

remove global allocator from common


2 files changed, 47 insertions(+), 47 deletions(-)

src/cmd/ci.zig+1-1
......@@ -21,7 +21,7 @@ pub fn do(cachepath: string, dir: std.fs.Dir) !void {
2121 var options = common.CollectOptions{
2222 .log = true,
2323 .update = false,
24 .lock = try common.parse_lockfile(dir),
24 .lock = try common.parse_lockfile(gpa, dir),
2525 };
2626 const top_module = try common.collect_deps_deep(cachepath, dir, &options);
2727
src/common.zig+46-46
......@@ -1,7 +1,6 @@
11const std = @import("std");
22const string = []const u8;
33const builtin = @import("builtin");
4const gpa = std.heap.c_allocator;
54
65const ansi = @import("ansi");
76
......@@ -20,7 +19,7 @@ pub const CollectOptions = struct {
2019 log: bool,
2120 update: bool,
2221 lock: ?[]const [4]string = null,
23 alloc: *std.mem.Allocator = gpa,
22 alloc: *std.mem.Allocator = std.heap.c_allocator,
2423 already_fetched: *std.ArrayList(string) = undefined,
2524
2625 pub fn init(self: *CollectOptions) !void {
......@@ -30,13 +29,13 @@ pub const CollectOptions = struct {
3029};
3130
3231pub fn collect_deps_deep(cachepath: string, mdir: std.fs.Dir, options: *CollectOptions) !zigmod.Module {
33 const m = try zigmod.ModFile.from_dir(gpa, mdir);
32 const m = try zigmod.ModFile.from_dir(options.alloc, mdir);
3433 try options.init();
35 var moduledeps = std.ArrayList(zigmod.Module).init(gpa);
34 var moduledeps = std.ArrayList(zigmod.Module).init(options.alloc);
3635 defer moduledeps.deinit();
3736 if (m.root_files.len > 0) {
3837 try std.fs.cwd().makePath(".zigmod/deps/files");
39 try moduledeps.append(try add_files_package("root", mdir, m.root_files));
38 try moduledeps.append(try add_files_package(options.alloc, "root", mdir, m.root_files));
4039 }
4140 try moduledeps.append(try collect_deps(cachepath, mdir, options));
4241 for (m.devdeps) |*d| {
......@@ -45,7 +44,7 @@ pub fn collect_deps_deep(cachepath: string, mdir: std.fs.Dir, options: *CollectO
4544 }
4645 }
4746 return zigmod.Module{
48 .alloc = gpa,
47 .alloc = options.alloc,
4948 .is_sys_lib = false,
5049 .id = "root",
5150 .name = "root",
......@@ -58,12 +57,12 @@ pub fn collect_deps_deep(cachepath: string, mdir: std.fs.Dir, options: *CollectO
5857}
5958
6059pub fn collect_deps(cachepath: string, mdir: std.fs.Dir, options: *CollectOptions) anyerror!zigmod.Module {
61 const m = try zigmod.ModFile.from_dir(gpa, mdir);
62 var moduledeps = std.ArrayList(zigmod.Module).init(gpa);
60 const m = try zigmod.ModFile.from_dir(options.alloc, mdir);
61 var moduledeps = std.ArrayList(zigmod.Module).init(options.alloc);
6362 defer moduledeps.deinit();
6463 if (m.files.len > 0) {
6564 try std.fs.cwd().makePath(".zigmod/deps/files");
66 try moduledeps.append(try add_files_package(m.id, mdir, m.files));
65 try moduledeps.append(try add_files_package(options.alloc, m.id, mdir, m.files));
6766 }
6867 for (m.deps) |*d| {
6968 if (try get_module_from_dep(d, cachepath, options)) |founddep| {
......@@ -71,7 +70,7 @@ pub fn collect_deps(cachepath: string, mdir: std.fs.Dir, options: *CollectOption
7170 }
7271 }
7372 return zigmod.Module{
74 .alloc = gpa,
73 .alloc = options.alloc,
7574 .is_sys_lib = false,
7675 .id = m.id,
7776 .name = m.name,
......@@ -97,8 +96,8 @@ pub fn collect_pkgs(mod: zigmod.Module, list: *std.ArrayList(zigmod.Module)) any
9796}
9897
9998pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) !string {
100 const p = try std.fs.path.join(gpa, &.{ cachepath, try d.clean_path() });
101 const pv = try std.fs.path.join(gpa, &.{ cachepath, try d.clean_path_v() });
99 const p = try std.fs.path.join(options.alloc, &.{ cachepath, try d.clean_path() });
100 const pv = try std.fs.path.join(options.alloc, &.{ cachepath, try d.clean_path_v() });
102101
103102 const nocache = d.type == .local or d.type == .system_lib;
104103 if (!nocache and u.list_contains(options.already_fetched.items, p)) return p;
......@@ -136,13 +135,13 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) !
136135 if (try u.does_folder_exist(pv)) {
137136 if (vers.id == .branch) {
138137 if (options.update) {
139 try d.type.update(gpa, pv, d.path);
138 try d.type.update(options.alloc, pv, d.path);
140139 }
141140 }
142141 return pv;
143142 }
144 try d.type.pull(gpa, d.path, pv);
145 if ((try u.run_cmd(gpa, pv, &.{ "git", "checkout", vers.string })) > 0) {
143 try d.type.pull(options.alloc, d.path, pv);
144 if ((try u.run_cmd(options.alloc, pv, &.{ "git", "checkout", vers.string })) > 0) {
146145 u.fail("fetch: git: {s}: {s} {s} does not exist", .{ d.path, @tagName(vers.id), vers.string });
147146 }
148147 if (builtin.os.tag != .windows and vers.id != .branch) {
......@@ -152,20 +151,20 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) !
152151 return pv;
153152 }
154153 if (!try u.does_folder_exist(p)) {
155 try d.type.pull(gpa, d.path, p);
154 try d.type.pull(options.alloc, d.path, p);
156155 } else {
157156 if (options.update) {
158 try d.type.update(gpa, p, d.path);
157 try d.type.update(options.alloc, p, d.path);
159158 }
160159 }
161160 return p;
162161 },
163162 .hg => {
164163 if (!try u.does_folder_exist(p)) {
165 try d.type.pull(gpa, d.path, p);
164 try d.type.pull(options.alloc, d.path, p);
166165 } else {
167166 if (options.update) {
168 try d.type.update(gpa, p, d.path);
167 try d.type.update(options.alloc, p, d.path);
169168 }
170169 }
171170 return p;
......@@ -174,14 +173,14 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) !
174173 if (try u.does_folder_exist(pv)) {
175174 return pv;
176175 }
177 const file_name = try u.last(try u.split(gpa, d.path, "/"));
176 const file_name = try u.last(try u.split(options.alloc, d.path, "/"));
178177 if (d.version.len > 0) {
179178 if (try u.does_folder_exist(pv)) {
180179 return pv;
181180 }
182 const file_path = try std.fs.path.join(gpa, &.{ pv, file_name });
183 try d.type.pull(gpa, d.path, pv);
184 if (try u.validate_hash(gpa, d.version, file_path)) {
181 const file_path = try std.fs.path.join(options.alloc, &.{ pv, file_name });
182 try d.type.pull(options.alloc, d.path, pv);
183 if (try u.validate_hash(options.alloc, d.version, file_path)) {
185184 try std.fs.cwd().deleteFile(file_path);
186185 return pv;
187186 }
......@@ -192,8 +191,8 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) !
192191 if (try u.does_folder_exist(p)) {
193192 try std.fs.cwd().deleteTree(p);
194193 }
195 const file_path = try std.fs.path.join(gpa, &.{ p, file_name });
196 try d.type.pull(gpa, d.path, p);
194 const file_path = try std.fs.path.join(options.alloc, &.{ p, file_name });
195 try d.type.pull(options.alloc, d.path, p);
197196 try std.fs.deleteFileAbsolute(file_path);
198197 return p;
199198 },
......@@ -220,9 +219,9 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO
220219 switch (d.type) {
221220 .system_lib => {
222221 return zigmod.Module{
223 .alloc = gpa,
222 .alloc = options.alloc,
224223 .is_sys_lib = true,
225 .id = try u.do_hash(gpa, std.crypto.hash.sha3.Sha3_384, d.path),
224 .id = try u.do_hash(options.alloc, std.crypto.hash.sha3.Sha3_384, d.path),
226225 .name = d.path,
227226 .only_os = d.only_os,
228227 .except_os = d.except_os,
......@@ -237,21 +236,21 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO
237236 var dd = try collect_deps(cachepath, moddir, options) catch |e| switch (e) {
238237 error.FileNotFound => {
239238 if (d.main.len > 0 or d.c_include_dirs.len > 0 or d.c_source_files.len > 0) {
240 var mod_from = try zigmod.Module.from(gpa, d.*, cachepath, options);
239 var mod_from = try zigmod.Module.from(options.alloc, d.*, cachepath, options);
241240 if (d.type != .local) mod_from.clean_path = u.trim_prefix(modpath, cachepath)[1..];
242241 if (mod_from.is_for_this()) return mod_from;
243242 return null;
244243 }
245244 const moddirO = try std.fs.cwd().openDir(modpath, .{});
246 const tryname = try u.detect_pkgname(gpa, "", modpath);
247 const trymain = u.detct_mainfile(gpa, "", moddirO, tryname) catch |err| switch (err) {
245 const tryname = try u.detect_pkgname(options.alloc, "", modpath);
246 const trymain = u.detct_mainfile(options.alloc, "", moddirO, tryname) catch |err| switch (err) {
248247 error.CantFindMain => null,
249248 else => return err,
250249 };
251250 if (trymain) |_| {
252251 d.*.name = tryname;
253252 d.*.main = trymain.?;
254 var mod_from = try zigmod.Module.from(gpa, d.*, cachepath, options);
253 var mod_from = try zigmod.Module.from(options.alloc, d.*, cachepath, options);
255254 if (d.type != .local) mod_from.clean_path = u.trim_prefix(modpath, cachepath)[1..];
256255 if (mod_from.is_for_this()) return mod_from;
257256 return null;
......@@ -263,7 +262,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO
263262 dd.dep = d.*;
264263 const save = dd;
265264 if (d.type != .local) dd.clean_path = u.trim_prefix(modpath, cachepath)[1..];
266 if (dd.id.len == 0) dd.id = try u.random_string(gpa, 48);
265 if (dd.id.len == 0) dd.id = try u.random_string(options.alloc, 48);
267266 if (d.name.len > 0) dd.name = d.name;
268267 if (d.main.len > 0) dd.main = d.main;
269268 if (d.c_include_dirs.len > 0) dd.c_include_dirs = d.c_include_dirs;
......@@ -271,7 +270,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO
271270 if (d.c_source_files.len > 0) dd.c_source_files = d.c_source_files;
272271 if (d.only_os.len > 0) dd.only_os = d.only_os;
273272 if (d.except_os.len > 0) dd.except_os = d.except_os;
274 if (d.type == .local) dd.main = try std.fs.path.join(gpa, &.{ d.main, save.main });
273 if (d.type == .local) dd.main = try std.fs.path.join(options.alloc, &.{ d.main, save.main });
275274 if (std.mem.eql(u8, modpath, "files")) dd.clean_path = modpath;
276275 if (dd.is_for_this()) return dd;
277276 return null;
......@@ -279,28 +278,28 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO
279278 }
280279}
281280
282pub fn add_files_package(pkg_name: string, mdir: std.fs.Dir, dirs: []const string) !zigmod.Module {
281pub fn add_files_package(alloc: *std.mem.Allocator, pkg_name: string, mdir: std.fs.Dir, dirs: []const string) !zigmod.Module {
283282 const destination = ".zigmod/deps/files";
284 const fname = try std.mem.join(gpa, "", &.{ pkg_name, ".zig" });
283 const fname = try std.mem.join(alloc, "", &.{ pkg_name, ".zig" });
285284
286 const map = &std.StringHashMap(string).init(gpa);
285 const map = &std.StringHashMap(string).init(alloc);
287286 defer map.deinit();
288287
289288 for (dirs) |dir_path| {
290289 const dir = try mdir.openDir(dir_path, .{ .iterate = true });
291 var walker = try dir.walk(gpa);
290 var walker = try dir.walk(alloc);
292291 defer walker.deinit();
293292 while (try walker.next()) |p| {
294293 if (p.kind == .Directory) {
295294 continue;
296295 }
297 const path = try std.mem.dupe(gpa, u8, p.path);
298 try map.put(path, try std.fmt.allocPrint(gpa, "{s}/{s}", .{ dir_path, path }));
296 const path = try std.mem.dupe(alloc, u8, p.path);
297 try map.put(path, try std.fmt.allocPrint(alloc, "{s}/{s}", .{ dir_path, path }));
299298 }
300299 }
301300
302 const cwdpath = try std.fs.cwd().realpathAlloc(gpa, ".");
303 const mpath = try mdir.realpathAlloc(gpa, ".");
301 const cwdpath = try std.fs.cwd().realpathAlloc(alloc, ".");
302 const mpath = try mdir.realpathAlloc(alloc, ".");
304303 var fpath = u.trim_prefix(mpath, cwdpath);
305304 if (fpath.len == 0) fpath = std.fs.path.sep_str;
306305
......@@ -330,7 +329,7 @@ pub fn add_files_package(pkg_name: string, mdir: std.fs.Dir, dirs: []const strin
330329 );
331330
332331 var d: zigmod.Dep = .{
333 .alloc = gpa,
332 .alloc = alloc,
334333 .type = .local,
335334 .path = "files",
336335 .id = "",
......@@ -343,18 +342,19 @@ pub fn add_files_package(pkg_name: string, mdir: std.fs.Dir, dirs: []const strin
343342 var options = CollectOptions{
344343 .log = false,
345344 .update = false,
345 .alloc = alloc,
346346 };
347347 return (try get_module_from_dep(&d, destination, &options)).?;
348348}
349349
350pub fn parse_lockfile(dir: std.fs.Dir) ![]const [4]string {
351 var list = std.ArrayList([4]string).init(gpa);
350pub fn parse_lockfile(alloc: *std.mem.Allocator, dir: std.fs.Dir) ![]const [4]string {
351 var list = std.ArrayList([4]string).init(alloc);
352352 const max = std.math.maxInt(usize);
353353 const f = try dir.openFile("zigmod.lock", .{});
354354 const r = f.reader();
355355 var i: usize = 0;
356356 var v: usize = 1;
357 while (try r.readUntilDelimiterOrEofAlloc(gpa, '\n', max)) |line| : (i += 1) {
357 while (try r.readUntilDelimiterOrEofAlloc(alloc, '\n', max)) |line| : (i += 1) {
358358 if (i == 0 and std.mem.eql(u8, line, "2")) {
359359 v = 2;
360360 continue;
......@@ -372,7 +372,7 @@ pub fn parse_lockfile(dir: std.fs.Dir) ![]const [4]string {
372372 2 => {
373373 var iter = std.mem.split(u8, line, " ");
374374 const asdep = zigmod.Dep{
375 .alloc = gpa,
375 .alloc = alloc,
376376 .type = std.meta.stringToEnum(zigmod.DepType, iter.next().?).?,
377377 .path = iter.next().?,
378378 .version = iter.next().?,