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 {...@@ -21,7 +21,7 @@ pub fn do(cachepath: string, dir: std.fs.Dir) !void {
21 var options = common.CollectOptions{21 var options = common.CollectOptions{
22 .log = true,22 .log = true,
23 .update = false,23 .update = false,
24 .lock = try common.parse_lockfile(dir),24 .lock = try common.parse_lockfile(gpa, dir),
25 };25 };
26 const top_module = try common.collect_deps_deep(cachepath, dir, &options);26 const top_module = try common.collect_deps_deep(cachepath, dir, &options);
2727
src/common.zig+46-46
...@@ -1,7 +1,6 @@...@@ -1,7 +1,6 @@
1const std = @import("std");1const std = @import("std");
2const string = []const u8;2const string = []const u8;
3const builtin = @import("builtin");3const builtin = @import("builtin");
4const gpa = std.heap.c_allocator;
54
6const ansi = @import("ansi");5const ansi = @import("ansi");
76
...@@ -20,7 +19,7 @@ pub const CollectOptions = struct {...@@ -20,7 +19,7 @@ pub const CollectOptions = struct {
20 log: bool,19 log: bool,
21 update: bool,20 update: bool,
22 lock: ?[]const [4]string = null,21 lock: ?[]const [4]string = null,
23 alloc: *std.mem.Allocator = gpa,22 alloc: *std.mem.Allocator = std.heap.c_allocator,
24 already_fetched: *std.ArrayList(string) = undefined,23 already_fetched: *std.ArrayList(string) = undefined,
2524
26 pub fn init(self: *CollectOptions) !void {25 pub fn init(self: *CollectOptions) !void {
...@@ -30,13 +29,13 @@ pub const CollectOptions = struct {...@@ -30,13 +29,13 @@ pub const CollectOptions = struct {
30};29};
3130
32pub fn collect_deps_deep(cachepath: string, mdir: std.fs.Dir, options: *CollectOptions) !zigmod.Module {31pub 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);
34 try options.init();33 try options.init();
35 var moduledeps = std.ArrayList(zigmod.Module).init(gpa);34 var moduledeps = std.ArrayList(zigmod.Module).init(options.alloc);
36 defer moduledeps.deinit();35 defer moduledeps.deinit();
37 if (m.root_files.len > 0) {36 if (m.root_files.len > 0) {
38 try std.fs.cwd().makePath(".zigmod/deps/files");37 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));
40 }39 }
41 try moduledeps.append(try collect_deps(cachepath, mdir, options));40 try moduledeps.append(try collect_deps(cachepath, mdir, options));
42 for (m.devdeps) |*d| {41 for (m.devdeps) |*d| {
...@@ -45,7 +44,7 @@ pub fn collect_deps_deep(cachepath: string, mdir: std.fs.Dir, options: *CollectO...@@ -45,7 +44,7 @@ pub fn collect_deps_deep(cachepath: string, mdir: std.fs.Dir, options: *CollectO
45 }44 }
46 }45 }
47 return zigmod.Module{46 return zigmod.Module{
48 .alloc = gpa,47 .alloc = options.alloc,
49 .is_sys_lib = false,48 .is_sys_lib = false,
50 .id = "root",49 .id = "root",
51 .name = "root",50 .name = "root",
...@@ -58,12 +57,12 @@ pub fn collect_deps_deep(cachepath: string, mdir: std.fs.Dir, options: *CollectO...@@ -58,12 +57,12 @@ pub fn collect_deps_deep(cachepath: string, mdir: std.fs.Dir, options: *CollectO
58}57}
5958
60pub fn collect_deps(cachepath: string, mdir: std.fs.Dir, options: *CollectOptions) anyerror!zigmod.Module {59pub fn collect_deps(cachepath: string, mdir: std.fs.Dir, options: *CollectOptions) anyerror!zigmod.Module {
61 const m = try zigmod.ModFile.from_dir(gpa, mdir);60 const m = try zigmod.ModFile.from_dir(options.alloc, mdir);
62 var moduledeps = std.ArrayList(zigmod.Module).init(gpa);61 var moduledeps = std.ArrayList(zigmod.Module).init(options.alloc);
63 defer moduledeps.deinit();62 defer moduledeps.deinit();
64 if (m.files.len > 0) {63 if (m.files.len > 0) {
65 try std.fs.cwd().makePath(".zigmod/deps/files");64 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));
67 }66 }
68 for (m.deps) |*d| {67 for (m.deps) |*d| {
69 if (try get_module_from_dep(d, cachepath, options)) |founddep| {68 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...@@ -71,7 +70,7 @@ pub fn collect_deps(cachepath: string, mdir: std.fs.Dir, options: *CollectOption
71 }70 }
72 }71 }
73 return zigmod.Module{72 return zigmod.Module{
74 .alloc = gpa,73 .alloc = options.alloc,
75 .is_sys_lib = false,74 .is_sys_lib = false,
76 .id = m.id,75 .id = m.id,
77 .name = m.name,76 .name = m.name,
...@@ -97,8 +96,8 @@ pub fn collect_pkgs(mod: zigmod.Module, list: *std.ArrayList(zigmod.Module)) any...@@ -97,8 +96,8 @@ pub fn collect_pkgs(mod: zigmod.Module, list: *std.ArrayList(zigmod.Module)) any
97}96}
9897
99pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) !string {98pub 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() });99 const p = try std.fs.path.join(options.alloc, &.{ cachepath, try d.clean_path() });
101 const pv = try std.fs.path.join(gpa, &.{ cachepath, try d.clean_path_v() });100 const pv = try std.fs.path.join(options.alloc, &.{ cachepath, try d.clean_path_v() });
102101
103 const nocache = d.type == .local or d.type == .system_lib;102 const nocache = d.type == .local or d.type == .system_lib;
104 if (!nocache and u.list_contains(options.already_fetched.items, p)) return p;103 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) !...@@ -136,13 +135,13 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) !
136 if (try u.does_folder_exist(pv)) {135 if (try u.does_folder_exist(pv)) {
137 if (vers.id == .branch) {136 if (vers.id == .branch) {
138 if (options.update) {137 if (options.update) {
139 try d.type.update(gpa, pv, d.path);138 try d.type.update(options.alloc, pv, d.path);
140 }139 }
141 }140 }
142 return pv;141 return pv;
143 }142 }
144 try d.type.pull(gpa, d.path, pv);143 try d.type.pull(options.alloc, d.path, pv);
145 if ((try u.run_cmd(gpa, pv, &.{ "git", "checkout", vers.string })) > 0) {144 if ((try u.run_cmd(options.alloc, pv, &.{ "git", "checkout", vers.string })) > 0) {
146 u.fail("fetch: git: {s}: {s} {s} does not exist", .{ d.path, @tagName(vers.id), vers.string });145 u.fail("fetch: git: {s}: {s} {s} does not exist", .{ d.path, @tagName(vers.id), vers.string });
147 }146 }
148 if (builtin.os.tag != .windows and vers.id != .branch) {147 if (builtin.os.tag != .windows and vers.id != .branch) {
...@@ -152,20 +151,20 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) !...@@ -152,20 +151,20 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) !
152 return pv;151 return pv;
153 }152 }
154 if (!try u.does_folder_exist(p)) {153 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);
156 } else {155 } else {
157 if (options.update) {156 if (options.update) {
158 try d.type.update(gpa, p, d.path);157 try d.type.update(options.alloc, p, d.path);
159 }158 }
160 }159 }
161 return p;160 return p;
162 },161 },
163 .hg => {162 .hg => {
164 if (!try u.does_folder_exist(p)) {163 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);
166 } else {165 } else {
167 if (options.update) {166 if (options.update) {
168 try d.type.update(gpa, p, d.path);167 try d.type.update(options.alloc, p, d.path);
169 }168 }
170 }169 }
171 return p;170 return p;
...@@ -174,14 +173,14 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) !...@@ -174,14 +173,14 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) !
174 if (try u.does_folder_exist(pv)) {173 if (try u.does_folder_exist(pv)) {
175 return pv;174 return pv;
176 }175 }
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, "/"));
178 if (d.version.len > 0) {177 if (d.version.len > 0) {
179 if (try u.does_folder_exist(pv)) {178 if (try u.does_folder_exist(pv)) {
180 return pv;179 return pv;
181 }180 }
182 const file_path = try std.fs.path.join(gpa, &.{ pv, file_name });181 const file_path = try std.fs.path.join(options.alloc, &.{ pv, file_name });
183 try d.type.pull(gpa, d.path, pv);182 try d.type.pull(options.alloc, d.path, pv);
184 if (try u.validate_hash(gpa, d.version, file_path)) {183 if (try u.validate_hash(options.alloc, d.version, file_path)) {
185 try std.fs.cwd().deleteFile(file_path);184 try std.fs.cwd().deleteFile(file_path);
186 return pv;185 return pv;
187 }186 }
...@@ -192,8 +191,8 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) !...@@ -192,8 +191,8 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) !
192 if (try u.does_folder_exist(p)) {191 if (try u.does_folder_exist(p)) {
193 try std.fs.cwd().deleteTree(p);192 try std.fs.cwd().deleteTree(p);
194 }193 }
195 const file_path = try std.fs.path.join(gpa, &.{ p, file_name });194 const file_path = try std.fs.path.join(options.alloc, &.{ p, file_name });
196 try d.type.pull(gpa, d.path, p);195 try d.type.pull(options.alloc, d.path, p);
197 try std.fs.deleteFileAbsolute(file_path);196 try std.fs.deleteFileAbsolute(file_path);
198 return p;197 return p;
199 },198 },
...@@ -220,9 +219,9 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO...@@ -220,9 +219,9 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO
220 switch (d.type) {219 switch (d.type) {
221 .system_lib => {220 .system_lib => {
222 return zigmod.Module{221 return zigmod.Module{
223 .alloc = gpa,222 .alloc = options.alloc,
224 .is_sys_lib = true,223 .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),
226 .name = d.path,225 .name = d.path,
227 .only_os = d.only_os,226 .only_os = d.only_os,
228 .except_os = d.except_os,227 .except_os = d.except_os,
...@@ -237,21 +236,21 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO...@@ -237,21 +236,21 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO
237 var dd = try collect_deps(cachepath, moddir, options) catch |e| switch (e) {236 var dd = try collect_deps(cachepath, moddir, options) catch |e| switch (e) {
238 error.FileNotFound => {237 error.FileNotFound => {
239 if (d.main.len > 0 or d.c_include_dirs.len > 0 or d.c_source_files.len > 0) {238 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);
241 if (d.type != .local) mod_from.clean_path = u.trim_prefix(modpath, cachepath)[1..];240 if (d.type != .local) mod_from.clean_path = u.trim_prefix(modpath, cachepath)[1..];
242 if (mod_from.is_for_this()) return mod_from;241 if (mod_from.is_for_this()) return mod_from;
243 return null;242 return null;
244 }243 }
245 const moddirO = try std.fs.cwd().openDir(modpath, .{});244 const moddirO = try std.fs.cwd().openDir(modpath, .{});
246 const tryname = try u.detect_pkgname(gpa, "", modpath);245 const tryname = try u.detect_pkgname(options.alloc, "", modpath);
247 const trymain = u.detct_mainfile(gpa, "", moddirO, tryname) catch |err| switch (err) {246 const trymain = u.detct_mainfile(options.alloc, "", moddirO, tryname) catch |err| switch (err) {
248 error.CantFindMain => null,247 error.CantFindMain => null,
249 else => return err,248 else => return err,
250 };249 };
251 if (trymain) |_| {250 if (trymain) |_| {
252 d.*.name = tryname;251 d.*.name = tryname;
253 d.*.main = trymain.?;252 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);
255 if (d.type != .local) mod_from.clean_path = u.trim_prefix(modpath, cachepath)[1..];254 if (d.type != .local) mod_from.clean_path = u.trim_prefix(modpath, cachepath)[1..];
256 if (mod_from.is_for_this()) return mod_from;255 if (mod_from.is_for_this()) return mod_from;
257 return null;256 return null;
...@@ -263,7 +262,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO...@@ -263,7 +262,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO
263 dd.dep = d.*;262 dd.dep = d.*;
264 const save = dd;263 const save = dd;
265 if (d.type != .local) dd.clean_path = u.trim_prefix(modpath, cachepath)[1..];264 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);
267 if (d.name.len > 0) dd.name = d.name;266 if (d.name.len > 0) dd.name = d.name;
268 if (d.main.len > 0) dd.main = d.main;267 if (d.main.len > 0) dd.main = d.main;
269 if (d.c_include_dirs.len > 0) dd.c_include_dirs = d.c_include_dirs;268 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...@@ -271,7 +270,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO
271 if (d.c_source_files.len > 0) dd.c_source_files = d.c_source_files;270 if (d.c_source_files.len > 0) dd.c_source_files = d.c_source_files;
272 if (d.only_os.len > 0) dd.only_os = d.only_os;271 if (d.only_os.len > 0) dd.only_os = d.only_os;
273 if (d.except_os.len > 0) dd.except_os = d.except_os;272 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 });
275 if (std.mem.eql(u8, modpath, "files")) dd.clean_path = modpath;274 if (std.mem.eql(u8, modpath, "files")) dd.clean_path = modpath;
276 if (dd.is_for_this()) return dd;275 if (dd.is_for_this()) return dd;
277 return null;276 return null;
...@@ -279,28 +278,28 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO...@@ -279,28 +278,28 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO
279 }278 }
280}279}
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 {
283 const destination = ".zigmod/deps/files";282 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);
287 defer map.deinit();286 defer map.deinit();
288287
289 for (dirs) |dir_path| {288 for (dirs) |dir_path| {
290 const dir = try mdir.openDir(dir_path, .{ .iterate = true });289 const dir = try mdir.openDir(dir_path, .{ .iterate = true });
291 var walker = try dir.walk(gpa);290 var walker = try dir.walk(alloc);
292 defer walker.deinit();291 defer walker.deinit();
293 while (try walker.next()) |p| {292 while (try walker.next()) |p| {
294 if (p.kind == .Directory) {293 if (p.kind == .Directory) {
295 continue;294 continue;
296 }295 }
297 const path = try std.mem.dupe(gpa, u8, p.path);296 const path = try std.mem.dupe(alloc, u8, p.path);
298 try map.put(path, try std.fmt.allocPrint(gpa, "{s}/{s}", .{ dir_path, path }));297 try map.put(path, try std.fmt.allocPrint(alloc, "{s}/{s}", .{ dir_path, path }));
299 }298 }
300 }299 }
301300
302 const cwdpath = try std.fs.cwd().realpathAlloc(gpa, ".");301 const cwdpath = try std.fs.cwd().realpathAlloc(alloc, ".");
303 const mpath = try mdir.realpathAlloc(gpa, ".");302 const mpath = try mdir.realpathAlloc(alloc, ".");
304 var fpath = u.trim_prefix(mpath, cwdpath);303 var fpath = u.trim_prefix(mpath, cwdpath);
305 if (fpath.len == 0) fpath = std.fs.path.sep_str;304 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...@@ -330,7 +329,7 @@ pub fn add_files_package(pkg_name: string, mdir: std.fs.Dir, dirs: []const strin
330 );329 );
331330
332 var d: zigmod.Dep = .{331 var d: zigmod.Dep = .{
333 .alloc = gpa,332 .alloc = alloc,
334 .type = .local,333 .type = .local,
335 .path = "files",334 .path = "files",
336 .id = "",335 .id = "",
...@@ -343,18 +342,19 @@ pub fn add_files_package(pkg_name: string, mdir: std.fs.Dir, dirs: []const strin...@@ -343,18 +342,19 @@ pub fn add_files_package(pkg_name: string, mdir: std.fs.Dir, dirs: []const strin
343 var options = CollectOptions{342 var options = CollectOptions{
344 .log = false,343 .log = false,
345 .update = false,344 .update = false,
345 .alloc = alloc,
346 };346 };
347 return (try get_module_from_dep(&d, destination, &options)).?;347 return (try get_module_from_dep(&d, destination, &options)).?;
348}348}
349349
350pub fn parse_lockfile(dir: std.fs.Dir) ![]const [4]string {350pub fn parse_lockfile(alloc: *std.mem.Allocator, dir: std.fs.Dir) ![]const [4]string {
351 var list = std.ArrayList([4]string).init(gpa);351 var list = std.ArrayList([4]string).init(alloc);
352 const max = std.math.maxInt(usize);352 const max = std.math.maxInt(usize);
353 const f = try dir.openFile("zigmod.lock", .{});353 const f = try dir.openFile("zigmod.lock", .{});
354 const r = f.reader();354 const r = f.reader();
355 var i: usize = 0;355 var i: usize = 0;
356 var v: usize = 1;356 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) {
358 if (i == 0 and std.mem.eql(u8, line, "2")) {358 if (i == 0 and std.mem.eql(u8, line, "2")) {
359 v = 2;359 v = 2;
360 continue;360 continue;
...@@ -372,7 +372,7 @@ pub fn parse_lockfile(dir: std.fs.Dir) ![]const [4]string {...@@ -372,7 +372,7 @@ pub fn parse_lockfile(dir: std.fs.Dir) ![]const [4]string {
372 2 => {372 2 => {
373 var iter = std.mem.split(u8, line, " ");373 var iter = std.mem.split(u8, line, " ");
374 const asdep = zigmod.Dep{374 const asdep = zigmod.Dep{
375 .alloc = gpa,375 .alloc = alloc,
376 .type = std.meta.stringToEnum(zigmod.DepType, iter.next().?).?,376 .type = std.meta.stringToEnum(zigmod.DepType, iter.next().?).?,
377 .path = iter.next().?,377 .path = iter.next().?,
378 .version = iter.next().?,378 .version = iter.next().?,