From 814eba696608f569764a53b00dc4541c8bd9d1b6 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Tue, 18 Jan 2022 04:19:46 -0800 Subject: [PATCH] add ModFile.min_zig_version prop, fixes #51 --- deps.zig | 6 ++++++ docs/zig.mod.md | 4 ++++ src/cmd/fetch.zig | 10 ++++++++++ src/common.zig | 3 +++ src/util/modfile.zig | 2 ++ src/util/module.zig | 15 +++++++++++++++ zig.mod | 1 + 7 files changed, 41 insertions(+) diff --git a/deps.zig b/deps.zig index 8a23baad23e93d53a3001f5fcddc9dd408a7573a..03f3d736120a1dacbd45388a20cac93acbbbb6d8 100644 --- a/deps.zig +++ b/deps.zig @@ -6,6 +6,7 @@ const string = []const u8; pub const cache = ".zigmod/deps"; pub fn addAllTo(exe: *std.build.LibExeObjStep) void { + checkMinZig(builtin.zig_version, exe); @setEvalBranchQuota(1_000_000); for (packages) |pkg| { exe.addPackage(pkg.pkg.?); @@ -42,6 +43,11 @@ pub const Package = struct { vcpkg: bool = false, }; +fn checkMinZig(current: std.SemanticVersion, exe: *std.build.LibExeObjStep) void { + const min = std.SemanticVersion.parse("0.9.0") catch return; + if (current.order(min).compare(.lt)) @panic(exe.builder.fmt("Your Zig version v{} does not meet the minimum build requirement of v{}", .{current, min})); +} + pub const dirs = struct { pub const _root = ""; pub const _89ujp8gq842x = cache ++ "/../.."; diff --git a/docs/zig.mod.md b/docs/zig.mod.md index 3525880379c9ddc5d8334396baa2146e76de4268..bcb394301260df17114a86d2522047a29457c8ac 100644 --- a/docs/zig.mod.md +++ b/docs/zig.mod.md @@ -50,6 +50,10 @@ Similar to `dependencies` but will only get added to the project if the current - Type: `[]Dep` Similar to `dependencies` but will only get added to the project if the current `zig.mod` is the root module. Exposed in `deps.zig` through the `deps.imports` decl. +### `min_zig_version` +- Type: `string` +Parsed as a `std.SemanticVersion`, this attribute refers to the minimum compatible Zig version for this package/application and will cause `zig build` to panic if violated. + ---- ### Dep Object diff --git a/src/cmd/fetch.zig b/src/cmd/fetch.zig index 1ccd74bbac5a0a3fce051c1563056223d6f384f8..f6088442986010f0d4646e12e5b938260f0d73a2 100644 --- a/src/cmd/fetch.zig +++ b/src/cmd/fetch.zig @@ -53,6 +53,7 @@ pub fn create_depszig(alloc: std.mem.Allocator, cachepath: string, dir: std.fs.D try w.writeAll("\n"); try w.writeAll( \\pub fn addAllTo(exe: *std.build.LibExeObjStep) void { + \\ checkMinZig(builtin.zig_version, exe); \\ @setEvalBranchQuota(1_000_000); \\ for (packages) |pkg| { \\ exe.addPackage(pkg.pkg.?); @@ -92,6 +93,15 @@ pub fn create_depszig(alloc: std.mem.Allocator, cachepath: string, dir: std.fs.D \\ ); + try w.print( + \\fn checkMinZig(current: std.SemanticVersion, exe: *std.build.LibExeObjStep) void {{ + \\ const min = std.SemanticVersion.parse("{}") catch return; + \\ if (current.order(min).compare(.lt)) @panic(exe.builder.fmt("Your Zig version v{{}} does not meet the minimum build requirement of v{{}}", .{{current, min}})); + \\}} + \\ + \\ + , .{top_module.minZigVersion()}); + try w.writeAll("pub const dirs = struct {\n"); try print_dirs(w, list.items); try w.writeAll("};\n\n"); diff --git a/src/common.zig b/src/common.zig index a77d77cbed356deaf509938cfacf70f9f1fa6d8f..55587df1dabae2aa8d05777ec75fb99e5db71965 100644 --- a/src/common.zig +++ b/src/common.zig @@ -59,6 +59,7 @@ pub fn collect_deps_deep(cachepath: string, mdir: std.fs.Dir, options: *CollectO .clean_path = "", .yaml = m.yaml, .dep = null, + .min_zig_version = m.min_zig_version, }; } @@ -89,6 +90,7 @@ pub fn collect_deps(cachepath: string, mdir: std.fs.Dir, options: *CollectOption .clean_path = "../..", .yaml = m.yaml, .dep = null, + .min_zig_version = m.min_zig_version, }; } @@ -239,6 +241,7 @@ pub fn get_module_from_dep(d: *zigmod.Dep, cachepath: string, options: *CollectO .yaml = null, .dep = d.*, .for_build = d.for_build, + .min_zig_version = null, }; }, else => { diff --git a/src/util/modfile.zig b/src/util/modfile.zig index 6a16f4ebbd410dde246cbf8648ff2055407d5a53..6a35f8e21fe3e7f8c8e1c030a7f1749c76493915 100644 --- a/src/util/modfile.zig +++ b/src/util/modfile.zig @@ -27,6 +27,7 @@ pub const ModFile = struct { files: []const string, rootdeps: []zigmod.Dep, builddeps: []zigmod.Dep, + min_zig_version: ?std.SemanticVersion, pub fn init(alloc: std.mem.Allocator, mpath: string) !Self { const file = try std.fs.cwd().openFile(mpath, .{}); @@ -66,6 +67,7 @@ pub const ModFile = struct { .files = try mapping.get_string_array(alloc, "files"), .rootdeps = try dep_list_by_name(alloc, mapping, &.{ "dev_dependencies", "root_dependencies" }, false), .builddeps = try dep_list_by_name(alloc, mapping, &.{ "dev_dependencies", "build_dependencies" }, true), + .min_zig_version = std.SemanticVersion.parse(mapping.get_string("min_zig_version")) catch null, }; } diff --git a/src/util/module.zig b/src/util/module.zig index 562a6fc468bc8182d401cff5910632da9c6a5f05..d0030be655c4cba2f58268bfc2574efa19d3a846 100644 --- a/src/util/module.zig +++ b/src/util/module.zig @@ -26,6 +26,7 @@ pub const Module = struct { clean_path: string, dep: ?zigmod.Dep, for_build: bool = false, + min_zig_version: ?std.SemanticVersion, pub fn from(alloc: std.mem.Allocator, dep: zigmod.Dep, modpath: string, options: *common.CollectOptions) !Module { var moddeps = std.ArrayList(Module).init(alloc); @@ -52,6 +53,7 @@ pub const Module = struct { .yaml = dep.yaml, .dep = dep, .for_build = dep.for_build, + .min_zig_version = null, }; } @@ -134,4 +136,17 @@ pub const Module = struct { pub fn short_id(self: Module) string { return u.slice(u8, self.id, 0, 12); } + + pub fn minZigVersion(self: Module) ?std.SemanticVersion { + var res = self.min_zig_version; + + for (self.deps) |dm| { + if (dm.minZigVersion()) |sv| { + if (res == null or sv.order(res.?).compare(.gt)) { + res = sv; + } + } + } + return res; + } }; diff --git a/zig.mod b/zig.mod index bdb23017fc8eea8363b7d9438307b35c95ad0ef0..e68ca71fea414288613ffe5e2774b940cf387702 100644 --- a/zig.mod +++ b/zig.mod @@ -3,6 +3,7 @@ name: zigmod main: src/lib.zig license: MIT description: A package manager for the Zig programming language. +min_zig_version: 0.9.0 dependencies: - src: git https://github.com/yaml/libyaml tag-0.2.5 id: 8mdbh0zuneb0i3hs5jby5je0heem1i6yxusl7c8y8qx68hqc -- 2.54.0