authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2022-03-07 21:22:58 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2022-03-07 21:22:58 -08:00
log9e628ca44b6817ebda927de8d7974ff98d8f95b8
tree7c899c9c3cdcec321b665f79f27a0efa6fadf23a
parent1a078abb8e9727e98054e1f45bc49aac5a2ce5f8

add framework dep type, fixes #10


9 files changed, 53 insertions(+), 8 deletions(-)

deps.zig+6
......@@ -19,6 +19,11 @@ pub fn addAllTo(exe: *std.build.LibExeObjStep) void {
1919 exe.linkSystemLibrary(item);
2020 llc = true;
2121 }
22 for (pkg.frameworks) |item| {
23 if (!std.Target.current.isDarwin()) @panic(exe.builder.fmt("a dependency is attempting to link to the framework {s}, which is only possible under Darwin", .{item}));
24 exe.linkFramework(item);
25 llc = true;
26 }
2227 inline for (pkg.c_include_dirs) |item| {
2328 exe.addIncludeDir(@field(dirs, decl.name) ++ "/" ++ item);
2429 llc = true;
......@@ -40,6 +45,7 @@ pub const Package = struct {
4045 c_source_files: []const string = &.{},
4146 c_source_flags: []const string = &.{},
4247 system_libs: []const string = &.{},
48 frameworks: []const string = &.{},
4349 vcpkg: bool = false,
4450};
4551
docs/deps.zig.md+1
......@@ -18,6 +18,7 @@ pub const Package = struct {
1818 c_source_files: []const string = &.{},
1919 c_source_flags: []const string = &.{},
2020 system_libs: []const string = &.{},
21 frameworks: []const string = &.{},
2122 vcpkg: bool = false,
2223};
2324```
docs/tutorial.md+3
......@@ -63,6 +63,9 @@ The core of expandability, it is possible to add dependencies to your project. H
6363- Other/System Library
6464 - System libraries are similar to Git dependencies, but instead of `git <url>` it is `system_lib <name>`.
6565
66- Other/Framework
67 - Frameworks are similar to system libraries but are specific to Darwin (macOS, iOS, etc) and are defined with `framework <name>`.
68
6669- Other/HTTP
6770 - Http tarballs are also allowed and follow a similar pattern as Git dependencies but use the `http` type. One thing to note is that it is recomended to add a hash verification after your tarball URL so that zigmod may assert whether or not it has been downloaded already to prevent unnecessary trips to the network. Hash verification versions are placed after the URL and in the form `type-string` such as `sha256-8ff0b79fd9118af7a760f1f6a98cac3e69daed325c8f9f0a581ecb62f797fd64`. They may also be placed in their own `version` key instead of `src`. The available hash algorithms are `blake3`, `sha256`, `sha512`.
6871
docs/zig.mod.md+3
......@@ -77,6 +77,7 @@ This is the base attribute used to reference external code for use in your proje
7777The available `type`s are:
7878- `local`
7979- `system_lib`
80- `framework`
8081- `git`
8182- `hg`
8283- `http`
......@@ -94,6 +95,8 @@ This attribute is used to reference the `type`/`path` combo by a specific revisi
9495Version types available to each Dep type:
9596- `system_lib`
9697 - Not affected by `version`.
98- `framework`
99 - Not affected by `version`.
97100- `git`
98101 - `commit`
99102 - `tag`
src/cmd/fetch.zig+17-2
......@@ -66,6 +66,11 @@ pub fn create_depszig(alloc: std.mem.Allocator, cachepath: string, dir: std.fs.D
6666 \\ exe.linkSystemLibrary(item);
6767 \\ llc = true;
6868 \\ }
69 \\ for (pkg.frameworks) |item| {
70 \\ if (!std.Target.current.isDarwin()) @panic(exe.builder.fmt("a dependency is attempting to link to the framework {s}, which is only possible under Darwin", .{item}));
71 \\ exe.linkFramework(item);
72 \\ llc = true;
73 \\ }
6974 \\ inline for (pkg.c_include_dirs) |item| {
7075 \\ exe.addIncludeDir(@field(dirs, decl.name) ++ "/" ++ item);
7176 \\ llc = true;
......@@ -87,6 +92,7 @@ pub fn create_depszig(alloc: std.mem.Allocator, cachepath: string, dir: std.fs.D
8792 \\ c_source_files: []const string = &.{},
8893 \\ c_source_flags: []const string = &.{},
8994 \\ system_libs: []const string = &.{},
95 \\ frameworks: []const string = &.{},
9096 \\ vcpkg: bool = false,
9197 \\};
9298 \\
......@@ -109,7 +115,7 @@ pub fn create_depszig(alloc: std.mem.Allocator, cachepath: string, dir: std.fs.D
109115 try w.writeAll("pub const package_data = struct {\n");
110116 var duped = std.ArrayList(zigmod.Module).init(alloc);
111117 for (list.items) |mod| {
112 if (mod.is_sys_lib) {
118 if (mod.is_sys_lib or mod.is_framework) {
113119 continue;
114120 }
115121 try duped.append(mod);
......@@ -240,7 +246,7 @@ fn diff_printchange(comptime testt: string, comptime replacement: string, item:
240246
241247fn print_dirs(w: std.fs.File.Writer, list: []const zigmod.Module) !void {
242248 for (list) |mod| {
243 if (mod.is_sys_lib) continue;
249 if (mod.is_sys_lib or mod.is_framework) continue;
244250 if (std.mem.eql(u8, mod.id, "root")) {
245251 try w.print(" pub const _root = \"\";\n", .{});
246252 continue;
......@@ -329,6 +335,15 @@ fn print_pkg_data_to(w: std.fs.File.Writer, notdone: *std.ArrayList(zigmod.Modul
329335 }
330336 try w.writeAll(" },\n");
331337 }
338 if (mod.has_framework_deps()) {
339 try w.writeAll(" .frameworks = &.{");
340 for (mod.deps) |item, j| {
341 if (!item.is_sys_lib) continue;
342 try w.print(" \"{}\"", .{std.zig.fmtEscapes(item.name)});
343 if (j != mod.deps.len - 1) try w.writeAll(",");
344 }
345 try w.writeAll(" },\n");
346 }
332347 if (mod.vcpkg) {
333348 try w.writeAll(" .vcpkg = true,\n");
334349 }
src/cmd/sum.zig+1-1
......@@ -36,7 +36,7 @@ pub fn execute(args: [][]u8) !void {
3636 if (std.mem.eql(u8, m.clean_path, "../..")) {
3737 continue;
3838 }
39 if (m.is_sys_lib) continue;
39 if (m.is_sys_lib or m.is_framework) continue;
4040 const hash = try m.get_hash(cachepath);
4141 try w.print("{s} {s}\n", .{ hash, m.clean_path });
4242 }
src/common.zig+6-3
......@@ -50,6 +50,7 @@ pub fn collect_deps_deep(cachepath: string, mdir: std.fs.Dir, options: *CollectO
5050 return zigmod.Module{
5151 .alloc = options.alloc,
5252 .is_sys_lib = false,
53 .is_framework = false,
5354 .id = "root",
5455 .name = "root",
5556 .main = m.main,
......@@ -79,6 +80,7 @@ pub fn collect_deps(cachepath: string, mdir: std.fs.Dir, options: *CollectOption
7980 return zigmod.Module{
8081 .alloc = options.alloc,
8182 .is_sys_lib = false,
83 .is_framework = false,
8284 .id = m.id,
8385 .name = m.name,
8486 .main = m.main,
......@@ -128,7 +130,7 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) !
128130 }
129131 return d.path;
130132 },
131 .system_lib => {
133 .system_lib, .framework => {
132134 // no op
133135 return "";
134136 },
......@@ -227,10 +229,11 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO
227229 if (!nocache) try options.already_fetched.append(modpath);
228230
229231 switch (d.type) {
230 .system_lib => {
232 .system_lib, .framework => {
231233 return zigmod.Module{
232234 .alloc = options.alloc,
233 .is_sys_lib = true,
235 .is_sys_lib = d.type == .system_lib,
236 .is_framework = d.type == .framework,
234237 .id = try u.do_hash(options.alloc, std.crypto.hash.sha3.Sha3_384, d.path),
235238 .name = d.path,
236239 .only_os = d.only_os,
src/util/dep_type.zig+5-2
......@@ -10,6 +10,7 @@ const u = @import("./index.zig");
1010pub const DepType = enum {
1111 local, // A 'package' derived from files in the same repository.
1212 system_lib, // std.build.LibExeObjStep.linkSystemLibrary
13 framework, // std.build.LibExeObjStep.linkFramework
1314 git, // https://git-scm.com/
1415 hg, // https://www.mercurial-scm.org/
1516 http, // https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol
......@@ -36,6 +37,7 @@ pub const DepType = enum {
3637 switch (self) {
3738 .local => {},
3839 .system_lib => {},
40 .framework => {},
3941 .git => {
4042 u.assert((try u.run_cmd(alloc, null, &.{ "git", "clone", "--recurse-submodules", rpath, dpath })) == 0, "git clone {s} failed", .{rpath});
4143 },
......@@ -56,13 +58,13 @@ pub const DepType = enum {
5658 }
5759 }
5860
59 // zig fmt: on
6061 pub fn update(self: DepType, alloc: std.mem.Allocator, dpath: string, rpath: string) !void {
6162 _ = rpath;
6263
6364 switch (self) {
6465 .local => {},
6566 .system_lib => {},
67 .framework => {},
6668 .git => {
6769 u.assert((try u.run_cmd(alloc, dpath, &.{ "git", "fetch" })) == 0, "git fetch failed", .{});
6870 u.assert((try u.run_cmd(alloc, dpath, &.{ "git", "pull" })) == 0, "git pull failed", .{});
......@@ -76,13 +78,13 @@ pub const DepType = enum {
7678 }
7779 }
7880
79 // zig fmt: on
8081 pub fn exact_version(self: DepType, alloc: std.mem.Allocator, mpath: string) !string {
8182 var mdir = try std.fs.cwd().openDir(mpath, .{});
8283 defer mdir.close();
8384 return switch (self) {
8485 .local => "",
8586 .system_lib => "",
87 .framework => "",
8688 .git => try std.fmt.allocPrint(alloc, "commit-{s}", .{(try u.git_rev_HEAD(alloc, mdir))}),
8789 .hg => "",
8890 .http => "",
......@@ -93,6 +95,7 @@ pub const DepType = enum {
9395 return switch (self) {
9496 .local => true,
9597 .system_lib => true,
98 .framework => true,
9699 .git => false,
97100 .hg => false,
98101 .http => false,
src/util/module.zig+11
......@@ -13,6 +13,7 @@ const common = @import("./../common.zig");
1313pub const Module = struct {
1414 alloc: std.mem.Allocator,
1515 is_sys_lib: bool,
16 is_framework: bool,
1617 id: string,
1718 name: string,
1819 main: string,
......@@ -41,6 +42,7 @@ pub const Module = struct {
4142 return Module{
4243 .alloc = alloc,
4344 .is_sys_lib = false,
45 .is_framework = false,
4446 .id = if (dep.id.len > 0) dep.id else try u.random_string(alloc, 48),
4547 .name = dep.name,
4648 .main = dep.main,
......@@ -125,6 +127,15 @@ pub const Module = struct {
125127 return false;
126128 }
127129
130 pub fn has_framework_deps(self: Module) bool {
131 for (self.deps) |d| {
132 if (d.is_framework) {
133 return true;
134 }
135 }
136 return false;
137 }
138
128139 pub fn short_id(self: Module) string {
129140 return u.slice(u8, self.id, 0, 12);
130141 }