authorgravatar for hello@nektro.netMeghan <hello@nektro.net> 2020-11-29 16:33:40 -08:00
committergravatar for hello@nektro.netMeghan <hello@nektro.net> 2020-11-29 16:33:40 -08:00
log7a2ee7e11cbe1b982d5a9f916d3da50619ae68bb
tree9bfdea4fe6bd07189318335db16143df13391960
parentc46ba2b2421043f61b7eccce131aa4b1fc75d264

add the system_lib dep type


6 files changed, 81 insertions(+), 1 deletions(-)

README.md+2-1
......@@ -53,6 +53,7 @@ zigmod add <type> <path>
5353-->
5454
5555#### Available types
56- `system_lib`
5657- `git`
5758- `hg`
5859
......@@ -102,7 +103,7 @@ pub fn build(b: *Builder) void {
102103#### Dep object
103104| Name | Type | Note | Description |
104105|------|------|------|-------------|
105| `type` | `string` | required, enum | One of `git`, `hg` |
106| `type` | `string` | required, enum | One of `system_lib`, `git`, `hg` |
106107| `path` | `string` | required | URL/path to this dependency. depends on the type |
107108| `version` | `string` | only on some types | pin this dependency at a specific version |
108109| `only_os` | `string` | | comma separated list of OS names to add this Dep to |
src/cmd_fetch.zig+47
......@@ -40,6 +40,9 @@ pub fn execute(args: [][]u8) !void {
4040 \\ inline for (c_source_files) |fpath| {
4141 \\ exe.addCSourceFile(fpath[1], @field(c_source_flags, fpath[0]));
4242 \\ }
43 \\ for (system_libs) |lib| {
44 \\ exe.linkSystemLibrary(lib);
45 \\ }
4346 \\}
4447 \\
4548 \\fn get_flags(comptime index: usize) []const u8 {
......@@ -62,6 +65,11 @@ pub fn execute(args: [][]u8) !void {
6265 try w.print("{}\n", .{"pub const c_source_files = &[_][2][]const u8{"});
6366 try print_csrc_dirs_to(w, top_module, &std.ArrayList([]const u8).init(gpa), true);
6467 try w.print("{};\n", .{"}"});
68 try w.print("\n", .{});
69 try w.print("{}\n", .{"pub const system_libs = &[_][]const u8{"});
70 try print_sys_libs_to(w, top_module, &std.ArrayList([]const u8).init(gpa), &std.ArrayList([]const u8).init(gpa));
71 try w.print("{};\n", .{"}"});
72
6573}
6674
6775fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {
......@@ -74,6 +82,9 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {
7482 u.print("fetch: {}: {}: {}", .{m.name, @tagName(d.type), d.path});
7583 moddir = p;
7684 switch (d.type) {
85 .system_lib => {
86 // no op
87 },
7788 .git => blk: {
7889 if (!try u.does_file_exist(p)) {
7990 _ = try d.type.pull(d.path, p);
......@@ -121,6 +132,20 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {
121132 },
122133 }
123134 switch (d.type) {
135 .system_lib => {
136 if (d.is_for_this()) try moduledeps.append(u.Module{
137 .is_sys_lib = true,
138 .name = d.path,
139 .only_os = d.only_os,
140 .except_os = d.except_os,
141 .main = "",
142 .c_include_dirs = &[_][]const u8{},
143 .c_source_flags = &[_][]const u8{},
144 .c_source_files = &[_][]const u8{},
145 .deps = &[_]u.Module{},
146 .clean_path = "",
147 });
148 },
124149 else => blk: {
125150 var dd = try fetch_deps(dir, try u.concat(&[_][]const u8{moddir, "/zig.mod"})) catch |e| switch (e) {
126151 error.FileNotFound => {
......@@ -148,6 +173,7 @@ fn fetch_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {
148173 }
149174 }
150175 return u.Module{
176 .is_sys_lib = false,
151177 .name = m.name,
152178 .main = m.main,
153179 .c_include_dirs = m.c_include_dirs,
......@@ -196,6 +222,7 @@ fn print_incl_dirs_to(w: fs.File.Writer, mod: u.Module, list: *std.ArrayList([]c
196222 }
197223 }
198224 for (mod.deps) |d| {
225 if (d.is_sys_lib) continue;
199226 try print_incl_dirs_to(w, d, list, false);
200227 }
201228}
......@@ -213,6 +240,7 @@ fn print_csrc_dirs_to(w: fs.File.Writer, mod: u.Module, list: *std.ArrayList([]c
213240 }
214241 }
215242 for (mod.deps) |d| {
243 if (d.is_sys_lib) continue;
216244 try print_csrc_dirs_to(w, d, list, false);
217245 }
218246}
......@@ -233,6 +261,25 @@ fn print_csrc_flags_to(w: fs.File.Writer, mod: u.Module, list: *std.ArrayList([]
233261 try w.print("{};\n", .{"}"});
234262 }
235263 for (mod.deps) |d| {
264 if (d.is_sys_lib) continue;
236265 try print_csrc_flags_to(w, d, list, false);
237266 }
238267}
268
269fn print_sys_libs_to(w: fs.File.Writer, mod: u.Module, list: *std.ArrayList([]const u8), list2: *std.ArrayList([]const u8)) anyerror!void {
270 if (u.list_contains(list.items, mod.clean_path)) {
271 return;
272 }
273 try list.append(mod.clean_path);
274 //
275 for (mod.deps) |d| {
276 if (!d.is_sys_lib) continue;
277 //
278 if (!u.list_contains(list2.items, d.name)) {
279 try w.print(" \"{}\",\n", .{d.name});
280 try list2.append(d.name);
281 }
282 //
283 try print_sys_libs_to(w, d, list, list2);
284 }
285}
src/common.zig+15
......@@ -21,6 +21,20 @@ pub fn collect_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {
2121 moddir = p;
2222 }
2323 switch (d.type) {
24 .system_lib => {
25 if (d.is_for_this()) try moduledeps.append(u.Module{
26 .is_sys_lib = true,
27 .name = d.path,
28 .only_os = d.only_os,
29 .except_os = d.except_os,
30 .main = "",
31 .c_include_dirs = &[_][]const u8{},
32 .c_source_flags = &[_][]const u8{},
33 .c_source_files = &[_][]const u8{},
34 .deps = &[_]u.Module{},
35 .clean_path = "",
36 });
37 },
2438 else => blk: {
2539 var dd = try collect_deps(dir, try u.concat(&[_][]const u8{moddir, "/zig.mod"})) catch |e| switch (e) {
2640 error.FileNotFound => {
......@@ -48,6 +62,7 @@ pub fn collect_deps(dir: []const u8, mpath: []const u8) anyerror!u.Module {
4862 }
4963 }
5064 return u.Module{
65 .is_sys_lib = false,
5166 .name = m.name,
5267 .main = m.main,
5368 .c_include_dirs = m.c_include_dirs,
src/util/dep.zig+12
......@@ -1,5 +1,6 @@
11const std = @import("std");
22const gpa = std.heap.c_allocator;
3const builtin = @import("builtin");
34
45const u = @import("index.zig");
56
......@@ -29,4 +30,15 @@ pub const Dep = struct {
2930 p = try std.fmt.allocPrint(gpa, "{}{}{}", .{@tagName(self.type), "/", p});
3031 return p;
3132 }
33
34 pub fn is_for_this(self: Dep) bool {
35 const os = @tagName(builtin.os.tag);
36 if (self.only_os.len > 0) {
37 return u.list_contains(self.only_os, os);
38 }
39 if (self.except_os.len > 0) {
40 return !u.list_contains(self.except_os, os);
41 }
42 return true;
43 }
3244};
src/util/dep_type.zig+3
......@@ -4,11 +4,13 @@ const u = @import("./index.zig");
44//
55
66pub const DepType = enum {
7 system_lib, // std.build.LibExeObjStep.linkSystemLibrary
78 git, // https://git-scm.com/
89 hg, // https://www.mercurial-scm.org/
910
1011 pub fn pull(self: DepType, rpath: []const u8, dpath: []const u8) !void {
1112 switch (self) {
13 .system_lib => {},
1214 .git => {
1315 _ = try u.run_cmd(null, &[_][]const u8{"git", "clone", rpath, dpath});
1416 },
......@@ -21,6 +23,7 @@ pub const DepType = enum {
2123
2224 pub fn update(self: DepType, dpath: []const u8, rpath: []const u8) !void {
2325 switch (self) {
26 .system_lib => {},
2427 .git => {
2528 _ = try u.run_cmd(dpath, &[_][]const u8{"git", "fetch"});
2629 _ = try u.run_cmd(dpath, &[_][]const u8{"git", "pull"});
src/util/module.zig+2
......@@ -12,6 +12,7 @@ const kb = b * 1024;
1212const mb = kb * 1024;
1313
1414pub const Module = struct {
15 is_sys_lib: bool,
1516 name: []const u8,
1617 main: []const u8,
1718 c_include_dirs: [][]const u8,
......@@ -25,6 +26,7 @@ pub const Module = struct {
2526
2627 pub fn from(dep: u.Dep) !Module {
2728 return Module{
29 .is_sys_lib = false,
2830 .name = dep.name,
2931 .main = dep.main,
3032 .c_include_dirs = dep.c_include_dirs,