authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2022-01-18 03:21:21 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2022-01-18 03:21:21 -08:00
logbc7b03e55e2acf559a4f7f3496c8aa9163c73541
treec52ee2fc4227b0105b48b522be9dbd551dc0992d
parentf1afc8184425b74ec04cd0c8da29cc4623c04597

cmd/fetch- remove use of global allocator


3 files changed, 34 insertions(+), 35 deletions(-)

src/cmd/aquila/install.zig+1-1
......@@ -60,7 +60,7 @@ pub fn execute(args: [][]u8) !void {
6060
6161 // zigmod ci
6262 const ci = @import("../ci.zig");
63 try ci.do(modpath, moddir);
63 try ci.do(gpa, modpath, moddir);
6464
6565 // zig build
6666 const argv: []const string = &.{
src/cmd/ci.zig+7-7
......@@ -1,6 +1,5 @@
11const std = @import("std");
22const string = []const u8;
3const gpa = std.heap.c_allocator;
43
54const zigmod = @import("../lib.zig");
65const u = @import("./../util/index.zig");
......@@ -12,23 +11,24 @@ const common = @import("./../common.zig");
1211pub fn execute(args: [][]u8) !void {
1312 _ = args;
1413
14 const gpa = std.heap.c_allocator;
1515 const cachepath = try std.fs.path.join(gpa, &.{ ".zigmod", "deps" });
1616 const dir = std.fs.cwd();
17 try do(cachepath, dir);
17 try do(gpa, cachepath, dir);
1818}
1919
20pub fn do(cachepath: string, dir: std.fs.Dir) !void {
20pub fn do(alloc: std.mem.Allocator, cachepath: string, dir: std.fs.Dir) !void {
2121 var options = common.CollectOptions{
2222 .log = true,
2323 .update = false,
24 .lock = try common.parse_lockfile(gpa, dir),
25 .alloc = gpa,
24 .lock = try common.parse_lockfile(alloc, dir),
25 .alloc = alloc,
2626 };
2727 const top_module = try common.collect_deps_deep(cachepath, dir, &options);
2828
29 var list = std.ArrayList(zigmod.Module).init(gpa);
29 var list = std.ArrayList(zigmod.Module).init(alloc);
3030 try common.collect_pkgs(top_module, &list);
3131
3232 const fetch = @import("./fetch.zig");
33 try fetch.create_depszig(cachepath, dir, top_module, &list);
33 try fetch.create_depszig(alloc, cachepath, dir, top_module, &list);
3434}
src/cmd/fetch.zig+26-27
......@@ -1,13 +1,11 @@
11const std = @import("std");
22const string = []const u8;
3const gpa = std.heap.c_allocator;
43
54const zigmod = @import("../lib.zig");
65const u = @import("./../util/index.zig");
76const common = @import("./../common.zig");
87
98const ansi = @import("ansi");
10
119const root = @import("root");
1210const build_options = if (@hasDecl(root, "build_options")) root.build_options else struct {};
1311const bootstrap = if (@hasDecl(build_options, "bootstrap")) build_options.bootstrap else false;
......@@ -17,6 +15,7 @@ const bootstrap = if (@hasDecl(build_options, "bootstrap")) build_options.bootst
1715
1816pub fn execute(args: [][]u8) !void {
1917 //
18 const gpa = std.heap.c_allocator;
2019 const cachepath = try std.fs.path.join(gpa, &.{ ".zigmod", "deps" });
2120 const dir = std.fs.cwd();
2221 const should_update = !(args.len >= 1 and std.mem.eql(u8, args[0], "--no-update"));
......@@ -31,16 +30,16 @@ pub fn execute(args: [][]u8) !void {
3130 var list = std.ArrayList(zigmod.Module).init(gpa);
3231 try common.collect_pkgs(top_module, &list);
3332
34 try create_depszig(cachepath, dir, top_module, &list);
33 try create_depszig(gpa, cachepath, dir, top_module, &list);
3534
3635 if (bootstrap) return;
3736
38 try create_lockfile(&list, cachepath, dir);
37 try create_lockfile(gpa, &list, cachepath, dir);
3938
40 try diff_lockfile();
39 try diff_lockfile(gpa);
4140}
4241
43pub fn create_depszig(cachepath: string, dir: std.fs.Dir, top_module: zigmod.Module, list: *std.ArrayList(zigmod.Module)) !void {
42pub fn create_depszig(alloc: std.mem.Allocator, cachepath: string, dir: std.fs.Dir, top_module: zigmod.Module, list: *std.ArrayList(zigmod.Module)) !void {
4443 const f = try dir.createFile("deps.zig", .{});
4544 defer f.close();
4645
......@@ -98,14 +97,14 @@ pub fn create_depszig(cachepath: string, dir: std.fs.Dir, top_module: zigmod.Mod
9897 try w.writeAll("};\n\n");
9998
10099 try w.writeAll("pub const package_data = struct {\n");
101 var duped = std.ArrayList(zigmod.Module).init(gpa);
100 var duped = std.ArrayList(zigmod.Module).init(alloc);
102101 for (list.items) |mod| {
103102 if (mod.is_sys_lib) {
104103 continue;
105104 }
106105 try duped.append(mod);
107106 }
108 try print_pkg_data_to(w, &duped, &std.ArrayList(zigmod.Module).init(gpa));
107 try print_pkg_data_to(w, &duped, &std.ArrayList(zigmod.Module).init(alloc));
109108 try w.writeAll("};\n\n");
110109
111110 try w.writeAll("pub const packages = ");
......@@ -113,15 +112,15 @@ pub fn create_depszig(cachepath: string, dir: std.fs.Dir, top_module: zigmod.Mod
113112 try w.writeAll(";\n\n");
114113
115114 try w.writeAll("pub const pkgs = ");
116 try print_pkgs(w, top_module);
115 try print_pkgs(alloc, w, top_module);
117116 try w.writeAll(";\n\n");
118117
119118 try w.writeAll("pub const imports = struct {\n");
120 try print_imports(w, top_module, cachepath);
119 try print_imports(alloc, w, top_module, cachepath);
121120 try w.writeAll("};\n");
122121}
123122
124fn create_lockfile(list: *std.ArrayList(zigmod.Module), path: string, dir: std.fs.Dir) !void {
123fn create_lockfile(alloc: std.mem.Allocator, list: *std.ArrayList(zigmod.Module), path: string, dir: std.fs.Dir) !void {
125124 const fl = try dir.createFile("zigmod.lock", .{});
126125 defer fl.close();
127126
......@@ -133,7 +132,7 @@ fn create_lockfile(list: *std.ArrayList(zigmod.Module), path: string, dir: std.f
133132 continue;
134133 }
135134 if (md.type == .system_lib) continue;
136 const mpath = try std.fs.path.join(gpa, &.{ path, m.clean_path });
135 const mpath = try std.fs.path.join(alloc, &.{ path, m.clean_path });
137136 const version = try md.exact_version(mpath);
138137 try wl.print("{s} {s} {s}\n", .{ @tagName(md.type), md.path, version });
139138 }
......@@ -145,25 +144,25 @@ const DiffChange = struct {
145144 to: string,
146145};
147146
148fn diff_lockfile() !void {
147fn diff_lockfile(alloc: std.mem.Allocator) !void {
149148 const max = std.math.maxInt(usize);
150149
151150 if (try u.does_folder_exist(".git")) {
152 const result = try u.run_cmd_raw(gpa, null, &.{ "git", "diff", "zigmod.lock" });
151 const result = try u.run_cmd_raw(alloc, null, &.{ "git", "diff", "zigmod.lock" });
153152 const r = std.io.fixedBufferStream(result.stdout).reader();
154 while (try r.readUntilDelimiterOrEofAlloc(gpa, '\n', max)) |line| {
153 while (try r.readUntilDelimiterOrEofAlloc(alloc, '\n', max)) |line| {
155154 if (std.mem.startsWith(u8, line, "@@")) break;
156155 }
157156
158 var rems = std.ArrayList(string).init(gpa);
159 var adds = std.ArrayList(string).init(gpa);
160 while (try r.readUntilDelimiterOrEofAlloc(gpa, '\n', max)) |line| {
157 var rems = std.ArrayList(string).init(alloc);
158 var adds = std.ArrayList(string).init(alloc);
159 while (try r.readUntilDelimiterOrEofAlloc(alloc, '\n', max)) |line| {
161160 if (line[0] == ' ') continue;
162161 if (line[0] == '-') try rems.append(line[1..]);
163162 if (line[0] == '+') if (line[1] == '2') continue else try adds.append(line[1..]);
164163 }
165164
166 var changes = std.StringHashMap(DiffChange).init(gpa);
165 var changes = std.StringHashMap(DiffChange).init(alloc);
167166
168167 var didbreak = false;
169168 var i: usize = 0;
......@@ -349,7 +348,7 @@ fn contains_all(needles: []zigmod.Module, haystack: []const zigmod.Module) bool
349348 return true;
350349}
351350
352fn print_pkgs(w: std.fs.File.Writer, m: zigmod.Module) !void {
351fn print_pkgs(alloc: std.mem.Allocator, w: std.fs.File.Writer, m: zigmod.Module) !void {
353352 try w.writeAll("struct {\n");
354353 for (m.deps) |d| {
355354 if (d.main.len == 0) {
......@@ -358,13 +357,13 @@ fn print_pkgs(w: std.fs.File.Writer, m: zigmod.Module) !void {
358357 if (d.for_build) {
359358 continue;
360359 }
361 const ident = try zig_name_from_pkg_name(d.name);
360 const ident = try zig_name_from_pkg_name(alloc, d.name);
362361 try w.print(" pub const {s} = package_data._{s};\n", .{ ident, d.id[0..12] });
363362 }
364363 try w.writeAll("}");
365364}
366365
367fn print_imports(w: std.fs.File.Writer, m: zigmod.Module, path: string) !void {
366fn print_imports(alloc: std.mem.Allocator, w: std.fs.File.Writer, m: zigmod.Module, path: string) !void {
368367 for (m.deps) |d| {
369368 if (d.main.len == 0) {
370369 continue;
......@@ -372,15 +371,15 @@ fn print_imports(w: std.fs.File.Writer, m: zigmod.Module, path: string) !void {
372371 if (!d.for_build) {
373372 continue;
374373 }
375 const ident = try zig_name_from_pkg_name(d.name);
374 const ident = try zig_name_from_pkg_name(alloc, d.name);
376375 try w.print(" pub const {s} = @import(\"{}/{}/{s}\");\n", .{ ident, std.zig.fmtEscapes(path), std.zig.fmtEscapes(d.clean_path), d.main });
377376 }
378377}
379378
380fn zig_name_from_pkg_name(name: string) !string {
379fn zig_name_from_pkg_name(alloc: std.mem.Allocator, name: string) !string {
381380 var legal = name;
382 legal = try std.mem.replaceOwned(u8, gpa, legal, "-", "_");
383 legal = try std.mem.replaceOwned(u8, gpa, legal, "/", "_");
384 legal = try std.mem.replaceOwned(u8, gpa, legal, ".", "_");
381 legal = try std.mem.replaceOwned(u8, alloc, legal, "-", "_");
382 legal = try std.mem.replaceOwned(u8, alloc, legal, "/", "_");
383 legal = try std.mem.replaceOwned(u8, alloc, legal, ".", "_");
385384 return legal;
386385}