From 9e628ca44b6817ebda927de8d7974ff98d8f95b8 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Mon, 7 Mar 2022 21:22:58 -0800 Subject: [PATCH] add framework dep type, fixes #10 --- deps.zig | 6 ++++++ docs/deps.zig.md | 1 + docs/tutorial.md | 3 +++ docs/zig.mod.md | 3 +++ src/cmd/fetch.zig | 19 +++++++++++++++++-- src/cmd/sum.zig | 2 +- src/common.zig | 9 ++++++--- src/util/dep_type.zig | 7 +++++-- src/util/module.zig | 11 +++++++++++ 9 files changed, 53 insertions(+), 8 deletions(-) diff --git a/deps.zig b/deps.zig index 2c98ba804478791d57dd53819d8f857e193acb04..8da0a884c74b7ebc44fc3fcd8699c1d1621d0175 100644 --- a/deps.zig +++ b/deps.zig @@ -19,6 +19,11 @@ pub fn addAllTo(exe: *std.build.LibExeObjStep) void { exe.linkSystemLibrary(item); llc = true; } + for (pkg.frameworks) |item| { + 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})); + exe.linkFramework(item); + llc = true; + } inline for (pkg.c_include_dirs) |item| { exe.addIncludeDir(@field(dirs, decl.name) ++ "/" ++ item); llc = true; @@ -40,6 +45,7 @@ pub const Package = struct { c_source_files: []const string = &.{}, c_source_flags: []const string = &.{}, system_libs: []const string = &.{}, + frameworks: []const string = &.{}, vcpkg: bool = false, }; diff --git a/docs/deps.zig.md b/docs/deps.zig.md index 28bd67e24eee5fa2426a570429171f25136ce172..effbd1e969b815dd0845382790c4e0608a6deaeb 100644 --- a/docs/deps.zig.md +++ b/docs/deps.zig.md @@ -18,6 +18,7 @@ pub const Package = struct { c_source_files: []const string = &.{}, c_source_flags: []const string = &.{}, system_libs: []const string = &.{}, + frameworks: []const string = &.{}, vcpkg: bool = false, }; ``` diff --git a/docs/tutorial.md b/docs/tutorial.md index 1f533e2fd335134ccc65e805bf28b87cbeef985b..6c482c38fd03a0d62032cd4c214cf49902358c0f 100644 --- a/docs/tutorial.md +++ b/docs/tutorial.md @@ -63,6 +63,9 @@ The core of expandability, it is possible to add dependencies to your project. H - Other/System Library - System libraries are similar to Git dependencies, but instead of `git ` it is `system_lib `. +- Other/Framework + - Frameworks are similar to system libraries but are specific to Darwin (macOS, iOS, etc) and are defined with `framework `. + - Other/HTTP - 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`. diff --git a/docs/zig.mod.md b/docs/zig.mod.md index a8cdd8dee907c04f7af5e1b2af767b686a7d4131..dcf6c1c92490b56391e47b3be2e65858b6eb116c 100644 --- a/docs/zig.mod.md +++ b/docs/zig.mod.md @@ -77,6 +77,7 @@ This is the base attribute used to reference external code for use in your proje The available `type`s are: - `local` - `system_lib` +- `framework` - `git` - `hg` - `http` @@ -94,6 +95,8 @@ This attribute is used to reference the `type`/`path` combo by a specific revisi Version types available to each Dep type: - `system_lib` - Not affected by `version`. +- `framework` + - Not affected by `version`. - `git` - `commit` - `tag` diff --git a/src/cmd/fetch.zig b/src/cmd/fetch.zig index 8f300d126327aa42c0726b183f34755b0c2c0649..1c11d063d4f457d12609fdd3e7ccc334e434625f 100644 --- a/src/cmd/fetch.zig +++ b/src/cmd/fetch.zig @@ -66,6 +66,11 @@ pub fn create_depszig(alloc: std.mem.Allocator, cachepath: string, dir: std.fs.D \\ exe.linkSystemLibrary(item); \\ llc = true; \\ } + \\ for (pkg.frameworks) |item| { + \\ 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})); + \\ exe.linkFramework(item); + \\ llc = true; + \\ } \\ inline for (pkg.c_include_dirs) |item| { \\ exe.addIncludeDir(@field(dirs, decl.name) ++ "/" ++ item); \\ llc = true; @@ -87,6 +92,7 @@ pub fn create_depszig(alloc: std.mem.Allocator, cachepath: string, dir: std.fs.D \\ c_source_files: []const string = &.{}, \\ c_source_flags: []const string = &.{}, \\ system_libs: []const string = &.{}, + \\ frameworks: []const string = &.{}, \\ vcpkg: bool = false, \\}; \\ @@ -109,7 +115,7 @@ pub fn create_depszig(alloc: std.mem.Allocator, cachepath: string, dir: std.fs.D try w.writeAll("pub const package_data = struct {\n"); var duped = std.ArrayList(zigmod.Module).init(alloc); for (list.items) |mod| { - if (mod.is_sys_lib) { + if (mod.is_sys_lib or mod.is_framework) { continue; } try duped.append(mod); @@ -240,7 +246,7 @@ fn diff_printchange(comptime testt: string, comptime replacement: string, item: fn print_dirs(w: std.fs.File.Writer, list: []const zigmod.Module) !void { for (list) |mod| { - if (mod.is_sys_lib) continue; + if (mod.is_sys_lib or mod.is_framework) continue; if (std.mem.eql(u8, mod.id, "root")) { try w.print(" pub const _root = \"\";\n", .{}); continue; @@ -329,6 +335,15 @@ fn print_pkg_data_to(w: std.fs.File.Writer, notdone: *std.ArrayList(zigmod.Modul } try w.writeAll(" },\n"); } + if (mod.has_framework_deps()) { + try w.writeAll(" .frameworks = &.{"); + for (mod.deps) |item, j| { + if (!item.is_sys_lib) continue; + try w.print(" \"{}\"", .{std.zig.fmtEscapes(item.name)}); + if (j != mod.deps.len - 1) try w.writeAll(","); + } + try w.writeAll(" },\n"); + } if (mod.vcpkg) { try w.writeAll(" .vcpkg = true,\n"); } diff --git a/src/cmd/sum.zig b/src/cmd/sum.zig index eb5ebf1d65faff02cb9e00649e51c7310fb8acb6..a45df8a2a51a073a070224434a8cf8158b6579bd 100644 --- a/src/cmd/sum.zig +++ b/src/cmd/sum.zig @@ -36,7 +36,7 @@ pub fn execute(args: [][]u8) !void { if (std.mem.eql(u8, m.clean_path, "../..")) { continue; } - if (m.is_sys_lib) continue; + if (m.is_sys_lib or m.is_framework) continue; const hash = try m.get_hash(cachepath); try w.print("{s} {s}\n", .{ hash, m.clean_path }); } diff --git a/src/common.zig b/src/common.zig index 0472779470e8a17ec9503dacdf6b8ad74fa983cb..f055fdac778fed296460432b867aca941fe02327 100644 --- a/src/common.zig +++ b/src/common.zig @@ -50,6 +50,7 @@ pub fn collect_deps_deep(cachepath: string, mdir: std.fs.Dir, options: *CollectO return zigmod.Module{ .alloc = options.alloc, .is_sys_lib = false, + .is_framework = false, .id = "root", .name = "root", .main = m.main, @@ -79,6 +80,7 @@ pub fn collect_deps(cachepath: string, mdir: std.fs.Dir, options: *CollectOption return zigmod.Module{ .alloc = options.alloc, .is_sys_lib = false, + .is_framework = false, .id = m.id, .name = m.name, .main = m.main, @@ -128,7 +130,7 @@ pub fn get_modpath(cachepath: string, d: zigmod.Dep, options: *CollectOptions) ! } return d.path; }, - .system_lib => { + .system_lib, .framework => { // no op return ""; }, @@ -227,10 +229,11 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO if (!nocache) try options.already_fetched.append(modpath); switch (d.type) { - .system_lib => { + .system_lib, .framework => { return zigmod.Module{ .alloc = options.alloc, - .is_sys_lib = true, + .is_sys_lib = d.type == .system_lib, + .is_framework = d.type == .framework, .id = try u.do_hash(options.alloc, std.crypto.hash.sha3.Sha3_384, d.path), .name = d.path, .only_os = d.only_os, diff --git a/src/util/dep_type.zig b/src/util/dep_type.zig index 60699a51c288eb7b0acba50f8a0c6c54fac49918..960ff2a2300304295b584b9913825f6727ef8a8e 100644 --- a/src/util/dep_type.zig +++ b/src/util/dep_type.zig @@ -10,6 +10,7 @@ const u = @import("./index.zig"); pub const DepType = enum { local, // A 'package' derived from files in the same repository. system_lib, // std.build.LibExeObjStep.linkSystemLibrary + framework, // std.build.LibExeObjStep.linkFramework git, // https://git-scm.com/ hg, // https://www.mercurial-scm.org/ http, // https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol @@ -36,6 +37,7 @@ pub const DepType = enum { switch (self) { .local => {}, .system_lib => {}, + .framework => {}, .git => { u.assert((try u.run_cmd(alloc, null, &.{ "git", "clone", "--recurse-submodules", rpath, dpath })) == 0, "git clone {s} failed", .{rpath}); }, @@ -56,13 +58,13 @@ pub const DepType = enum { } } - // zig fmt: on pub fn update(self: DepType, alloc: std.mem.Allocator, dpath: string, rpath: string) !void { _ = rpath; switch (self) { .local => {}, .system_lib => {}, + .framework => {}, .git => { u.assert((try u.run_cmd(alloc, dpath, &.{ "git", "fetch" })) == 0, "git fetch failed", .{}); u.assert((try u.run_cmd(alloc, dpath, &.{ "git", "pull" })) == 0, "git pull failed", .{}); @@ -76,13 +78,13 @@ pub const DepType = enum { } } - // zig fmt: on pub fn exact_version(self: DepType, alloc: std.mem.Allocator, mpath: string) !string { var mdir = try std.fs.cwd().openDir(mpath, .{}); defer mdir.close(); return switch (self) { .local => "", .system_lib => "", + .framework => "", .git => try std.fmt.allocPrint(alloc, "commit-{s}", .{(try u.git_rev_HEAD(alloc, mdir))}), .hg => "", .http => "", @@ -93,6 +95,7 @@ pub const DepType = enum { return switch (self) { .local => true, .system_lib => true, + .framework => true, .git => false, .hg => false, .http => false, diff --git a/src/util/module.zig b/src/util/module.zig index c051a3967fb4da2ddad6f96c58d57beabfd43357..a3890466d0bbdd042436eaac2720a6395d33cc25 100644 --- a/src/util/module.zig +++ b/src/util/module.zig @@ -13,6 +13,7 @@ const common = @import("./../common.zig"); pub const Module = struct { alloc: std.mem.Allocator, is_sys_lib: bool, + is_framework: bool, id: string, name: string, main: string, @@ -41,6 +42,7 @@ pub const Module = struct { return Module{ .alloc = alloc, .is_sys_lib = false, + .is_framework = false, .id = if (dep.id.len > 0) dep.id else try u.random_string(alloc, 48), .name = dep.name, .main = dep.main, @@ -125,6 +127,15 @@ pub const Module = struct { return false; } + pub fn has_framework_deps(self: Module) bool { + for (self.deps) |d| { + if (d.is_framework) { + return true; + } + } + return false; + } + pub fn short_id(self: Module) string { return u.slice(u8, self.id, 0, 12); } -- 2.54.0