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 {...@@ -60,7 +60,7 @@ pub fn execute(args: [][]u8) !void {
6060
61 // zigmod ci61 // zigmod ci
62 const ci = @import("../ci.zig");62 const ci = @import("../ci.zig");
63 try ci.do(modpath, moddir);63 try ci.do(gpa, modpath, moddir);
6464
65 // zig build65 // zig build
66 const argv: []const string = &.{66 const argv: []const string = &.{
src/cmd/ci.zig+7-7
...@@ -1,6 +1,5 @@...@@ -1,6 +1,5 @@
1const std = @import("std");1const std = @import("std");
2const string = []const u8;2const string = []const u8;
3const gpa = std.heap.c_allocator;
43
5const zigmod = @import("../lib.zig");4const zigmod = @import("../lib.zig");
6const u = @import("./../util/index.zig");5const u = @import("./../util/index.zig");
...@@ -12,23 +11,24 @@ const common = @import("./../common.zig");...@@ -12,23 +11,24 @@ const common = @import("./../common.zig");
12pub fn execute(args: [][]u8) !void {11pub fn execute(args: [][]u8) !void {
13 _ = args;12 _ = args;
1413
14 const gpa = std.heap.c_allocator;
15 const cachepath = try std.fs.path.join(gpa, &.{ ".zigmod", "deps" });15 const cachepath = try std.fs.path.join(gpa, &.{ ".zigmod", "deps" });
16 const dir = std.fs.cwd();16 const dir = std.fs.cwd();
17 try do(cachepath, dir);17 try do(gpa, cachepath, dir);
18}18}
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 {
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(gpa, dir),24 .lock = try common.parse_lockfile(alloc, dir),
25 .alloc = gpa,25 .alloc = alloc,
26 };26 };
27 const top_module = try common.collect_deps_deep(cachepath, dir, &options);27 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);
30 try common.collect_pkgs(top_module, &list);30 try common.collect_pkgs(top_module, &list);
3131
32 const fetch = @import("./fetch.zig");32 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);
34}34}
src/cmd/fetch.zig+26-27
...@@ -1,13 +1,11 @@...@@ -1,13 +1,11 @@
1const std = @import("std");1const std = @import("std");
2const string = []const u8;2const string = []const u8;
3const gpa = std.heap.c_allocator;
43
5const zigmod = @import("../lib.zig");4const zigmod = @import("../lib.zig");
6const u = @import("./../util/index.zig");5const u = @import("./../util/index.zig");
7const common = @import("./../common.zig");6const common = @import("./../common.zig");
87
9const ansi = @import("ansi");8const ansi = @import("ansi");
10
11const root = @import("root");9const root = @import("root");
12const build_options = if (@hasDecl(root, "build_options")) root.build_options else struct {};10const build_options = if (@hasDecl(root, "build_options")) root.build_options else struct {};
13const bootstrap = if (@hasDecl(build_options, "bootstrap")) build_options.bootstrap else false;11const 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...@@ -17,6 +15,7 @@ const bootstrap = if (@hasDecl(build_options, "bootstrap")) build_options.bootst
1715
18pub fn execute(args: [][]u8) !void {16pub fn execute(args: [][]u8) !void {
19 //17 //
18 const gpa = std.heap.c_allocator;
20 const cachepath = try std.fs.path.join(gpa, &.{ ".zigmod", "deps" });19 const cachepath = try std.fs.path.join(gpa, &.{ ".zigmod", "deps" });
21 const dir = std.fs.cwd();20 const dir = std.fs.cwd();
22 const should_update = !(args.len >= 1 and std.mem.eql(u8, args[0], "--no-update"));21 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 {...@@ -31,16 +30,16 @@ pub fn execute(args: [][]u8) !void {
31 var list = std.ArrayList(zigmod.Module).init(gpa);30 var list = std.ArrayList(zigmod.Module).init(gpa);
32 try common.collect_pkgs(top_module, &list);31 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
36 if (bootstrap) return;35 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);
41}40}
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 {
44 const f = try dir.createFile("deps.zig", .{});43 const f = try dir.createFile("deps.zig", .{});
45 defer f.close();44 defer f.close();
4645
...@@ -98,14 +97,14 @@ pub fn create_depszig(cachepath: string, dir: std.fs.Dir, top_module: zigmod.Mod...@@ -98,14 +97,14 @@ pub fn create_depszig(cachepath: string, dir: std.fs.Dir, top_module: zigmod.Mod
98 try w.writeAll("};\n\n");97 try w.writeAll("};\n\n");
9998
100 try w.writeAll("pub const package_data = struct {\n");99 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);
102 for (list.items) |mod| {101 for (list.items) |mod| {
103 if (mod.is_sys_lib) {102 if (mod.is_sys_lib) {
104 continue;103 continue;
105 }104 }
106 try duped.append(mod);105 try duped.append(mod);
107 }106 }
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));
109 try w.writeAll("};\n\n");108 try w.writeAll("};\n\n");
110109
111 try w.writeAll("pub const packages = ");110 try w.writeAll("pub const packages = ");
...@@ -113,15 +112,15 @@ pub fn create_depszig(cachepath: string, dir: std.fs.Dir, top_module: zigmod.Mod...@@ -113,15 +112,15 @@ pub fn create_depszig(cachepath: string, dir: std.fs.Dir, top_module: zigmod.Mod
113 try w.writeAll(";\n\n");112 try w.writeAll(";\n\n");
114113
115 try w.writeAll("pub const pkgs = ");114 try w.writeAll("pub const pkgs = ");
116 try print_pkgs(w, top_module);115 try print_pkgs(alloc, w, top_module);
117 try w.writeAll(";\n\n");116 try w.writeAll(";\n\n");
118117
119 try w.writeAll("pub const imports = struct {\n");118 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);
121 try w.writeAll("};\n");120 try w.writeAll("};\n");
122}121}
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 {
125 const fl = try dir.createFile("zigmod.lock", .{});124 const fl = try dir.createFile("zigmod.lock", .{});
126 defer fl.close();125 defer fl.close();
127126
...@@ -133,7 +132,7 @@ fn create_lockfile(list: *std.ArrayList(zigmod.Module), path: string, dir: std.f...@@ -133,7 +132,7 @@ fn create_lockfile(list: *std.ArrayList(zigmod.Module), path: string, dir: std.f
133 continue;132 continue;
134 }133 }
135 if (md.type == .system_lib) continue;134 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 });
137 const version = try md.exact_version(mpath);136 const version = try md.exact_version(mpath);
138 try wl.print("{s} {s} {s}\n", .{ @tagName(md.type), md.path, version });137 try wl.print("{s} {s} {s}\n", .{ @tagName(md.type), md.path, version });
139 }138 }
...@@ -145,25 +144,25 @@ const DiffChange = struct {...@@ -145,25 +144,25 @@ const DiffChange = struct {
145 to: string,144 to: string,
146};145};
147146
148fn diff_lockfile() !void {147fn diff_lockfile(alloc: std.mem.Allocator) !void {
149 const max = std.math.maxInt(usize);148 const max = std.math.maxInt(usize);
150149
151 if (try u.does_folder_exist(".git")) {150 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" });
153 const r = std.io.fixedBufferStream(result.stdout).reader();152 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| {
155 if (std.mem.startsWith(u8, line, "@@")) break;154 if (std.mem.startsWith(u8, line, "@@")) break;
156 }155 }
157156
158 var rems = std.ArrayList(string).init(gpa);157 var rems = std.ArrayList(string).init(alloc);
159 var adds = std.ArrayList(string).init(gpa);158 var adds = std.ArrayList(string).init(alloc);
160 while (try r.readUntilDelimiterOrEofAlloc(gpa, '\n', max)) |line| {159 while (try r.readUntilDelimiterOrEofAlloc(alloc, '\n', max)) |line| {
161 if (line[0] == ' ') continue;160 if (line[0] == ' ') continue;
162 if (line[0] == '-') try rems.append(line[1..]);161 if (line[0] == '-') try rems.append(line[1..]);
163 if (line[0] == '+') if (line[1] == '2') continue else try adds.append(line[1..]);162 if (line[0] == '+') if (line[1] == '2') continue else try adds.append(line[1..]);
164 }163 }
165164
166 var changes = std.StringHashMap(DiffChange).init(gpa);165 var changes = std.StringHashMap(DiffChange).init(alloc);
167166
168 var didbreak = false;167 var didbreak = false;
169 var i: usize = 0;168 var i: usize = 0;
...@@ -349,7 +348,7 @@ fn contains_all(needles: []zigmod.Module, haystack: []const zigmod.Module) bool...@@ -349,7 +348,7 @@ fn contains_all(needles: []zigmod.Module, haystack: []const zigmod.Module) bool
349 return true;348 return true;
350}349}
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 {
353 try w.writeAll("struct {\n");352 try w.writeAll("struct {\n");
354 for (m.deps) |d| {353 for (m.deps) |d| {
355 if (d.main.len == 0) {354 if (d.main.len == 0) {
...@@ -358,13 +357,13 @@ fn print_pkgs(w: std.fs.File.Writer, m: zigmod.Module) !void {...@@ -358,13 +357,13 @@ fn print_pkgs(w: std.fs.File.Writer, m: zigmod.Module) !void {
358 if (d.for_build) {357 if (d.for_build) {
359 continue;358 continue;
360 }359 }
361 const ident = try zig_name_from_pkg_name(d.name);360 const ident = try zig_name_from_pkg_name(alloc, d.name);
362 try w.print(" pub const {s} = package_data._{s};\n", .{ ident, d.id[0..12] });361 try w.print(" pub const {s} = package_data._{s};\n", .{ ident, d.id[0..12] });
363 }362 }
364 try w.writeAll("}");363 try w.writeAll("}");
365}364}
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 {
368 for (m.deps) |d| {367 for (m.deps) |d| {
369 if (d.main.len == 0) {368 if (d.main.len == 0) {
370 continue;369 continue;
...@@ -372,15 +371,15 @@ fn print_imports(w: std.fs.File.Writer, m: zigmod.Module, path: string) !void {...@@ -372,15 +371,15 @@ fn print_imports(w: std.fs.File.Writer, m: zigmod.Module, path: string) !void {
372 if (!d.for_build) {371 if (!d.for_build) {
373 continue;372 continue;
374 }373 }
375 const ident = try zig_name_from_pkg_name(d.name);374 const ident = try zig_name_from_pkg_name(alloc, d.name);
376 try w.print(" pub const {s} = @import(\"{}/{}/{s}\");\n", .{ ident, std.zig.fmtEscapes(path), std.zig.fmtEscapes(d.clean_path), d.main });375 try w.print(" pub const {s} = @import(\"{}/{}/{s}\");\n", .{ ident, std.zig.fmtEscapes(path), std.zig.fmtEscapes(d.clean_path), d.main });
377 }376 }
378}377}
379378
380fn zig_name_from_pkg_name(name: string) !string {379fn zig_name_from_pkg_name(alloc: std.mem.Allocator, name: string) !string {
381 var legal = name;380 var legal = name;
382 legal = try std.mem.replaceOwned(u8, gpa, legal, "-", "_");381 legal = try std.mem.replaceOwned(u8, alloc, legal, "-", "_");
383 legal = try std.mem.replaceOwned(u8, gpa, legal, "/", "_");382 legal = try std.mem.replaceOwned(u8, alloc, legal, "/", "_");
384 legal = try std.mem.replaceOwned(u8, gpa, legal, ".", "_");383 legal = try std.mem.replaceOwned(u8, alloc, legal, ".", "_");
385 return legal;384 return legal;
386}385}