From 06a9ac758c3c201625cbc17d3ccc0f8eea19cdf5 Mon Sep 17 00:00:00 2001 From: Uko Kokņevičs Date: Sun, 2 Apr 2023 22:28:09 +0300 Subject: Update to latest zig --- build.zig | 51 +++++++++++++++++++++++++++++++++------------------ libs/clap | 2 +- src/Installation.zig | 4 ++-- src/install.zig | 4 ++-- src/list.zig | 13 ++++++------- src/main.zig | 14 ++++---------- src/remove.zig | 4 ++-- src/subcommand.zig | 6 +++--- src/switch.zig | 2 +- src/update.zig | 2 +- 10 files changed, 55 insertions(+), 47 deletions(-) diff --git a/build.zig b/build.zig index 934e826..8a080d1 100644 --- a/build.zig +++ b/build.zig @@ -1,25 +1,38 @@ const builtin = @import("builtin"); const std = @import("std"); -const Builder = std.build.Builder; +const Build = std.Build; const SemanticVersion = std.SemanticVersion; +const Version = std.builtin.Version; -pub fn build(b: *Builder) void { +pub fn build(b: *Build) void { const target = b.standardTargetOptions(.{}); - const mode = b.standardReleaseOptions(); + const optimize = b.standardOptimizeOption(.{}); + + const semanticVersion = getSemanticVersion(b); + const version = Version{ + .major = @intCast(u32, semanticVersion.major), + .minor = @intCast(u32, semanticVersion.minor), + .patch = @intCast(u32, semanticVersion.patch), + }; const config = b.addOptions(); - config.addOption(SemanticVersion, "version", getVersion(b)); + config.addOption(SemanticVersion, "version", semanticVersion); - const exe = b.addExecutable("zup", "src/main.zig"); - exe.setTarget(target); - exe.setBuildMode(mode); + const exe = b.addExecutable(.{ + .name = "zup", + .version = version, + .root_source_file = .{ .path = "src/main.zig" }, + .target = target, + .optimize = optimize, + }); exe.addOptions("zup-config", config); - exe.addPackagePath("clap", "libs/clap/clap.zig"); - exe.addPackagePath("curl", "libs/curl/curl.zig"); - exe.addPackagePath("libarchive", "libs/libarchive/libarchive.zig"); - exe.addPackagePath("xdg", "libs/xdg/xdg.zig"); - exe.addPackagePath("zup", "src/main.zig"); + exe.addModule("clap", b.createModule(.{ .source_file = .{ .path = "libs/clap/clap.zig" }})); + exe.addModule("curl", b.createModule(.{ .source_file = .{ .path = "libs/curl/curl.zig" }})); + exe.addModule("libarchive", b.createModule(.{ + .source_file = .{ .path = "libs/libarchive/libarchive.zig" }, + })); + exe.addModule("xdg", b.createModule(.{ .source_file = .{ .path = "libs/xdg/xdg.zig" }})); exe.linkLibC(); exe.linkSystemLibrary("libarchive"); exe.linkSystemLibrary("libcurl"); @@ -34,9 +47,11 @@ pub fn build(b: *Builder) void { const run_step = b.step("run", "Run the app"); run_step.dependOn(&run_cmd.step); - const exe_tests = b.addTest("src/main.zig"); - exe_tests.setTarget(target); - exe_tests.setBuildMode(mode); + const exe_tests = b.addTest(.{ + .root_source_file = .{ .path = "src/main.zig" }, + .target = target, + .optimize = optimize, + }); const test_step = b.step("test", "Run unit tests"); test_step.dependOn(&exe_tests.step); @@ -44,15 +59,15 @@ pub fn build(b: *Builder) void { const default_version = SemanticVersion.parse("0.2.1") catch unreachable; -fn getVersion(b: *Builder) SemanticVersion { +fn getSemanticVersion(b: *Build) SemanticVersion { var out_code: u8 = undefined; const untrimmed = b.execAllowFail( - &.{ "git", "-C", b.build_root, "describe", "--tags" }, + &.{ "git", "-C", b.build_root.path.?, "describe", "--tags" }, &out_code, .Ignore, ) catch return default_version; - const git_desc = std.mem.trim(u8, untrimmed, &std.ascii.spaces); + const git_desc = std.mem.trim(u8, untrimmed, &std.ascii.whitespace); // Turn something like 0.0.1-1-g85f815d into 0.0.1-1+g85f815d const ver_str = switch (std.mem.count(u8, git_desc, "-")) { 0 => git_desc, diff --git a/libs/clap b/libs/clap index dbc6b8e..ab69ef2 160000 --- a/libs/clap +++ b/libs/clap @@ -1 +1 @@ -Subproject commit dbc6b8e54ad753b0605feaeecc8e79dba3572ed3 +Subproject commit ab69ef2db44b6c4b7f00283d52d38fbe71d16c42 diff --git a/src/Installation.zig b/src/Installation.zig index 4f7a48d..3ff65ea 100644 --- a/src/Installation.zig +++ b/src/Installation.zig @@ -1,7 +1,7 @@ const curl = @import("curl"); const std = @import("std"); const xdg = @import("xdg"); -const zup = @import("zup"); +const zup = @import("root"); const Allocator = std.mem.Allocator; const Config = zup.Config; @@ -109,7 +109,7 @@ pub fn getInstalledList(allocator: Allocator) !Installations { continue; } - const trimmed_ver_str = std.mem.trim(u8, res.stdout, &std.ascii.spaces); + const trimmed_ver_str = std.mem.trim(u8, res.stdout, &std.ascii.whitespace); const version = try SemanticVersion.parse(trimmed_ver_str); const name = try allocator.dupe(u8, item.name); diff --git a/src/install.zig b/src/install.zig index 27db4bb..2e3efab 100644 --- a/src/install.zig +++ b/src/install.zig @@ -2,7 +2,7 @@ const libarchive = @import("libarchive"); const curl = @import("curl"); const std = @import("std"); const xdg = @import("xdg"); -const zup = @import("zup"); +const zup = @import("root"); const Allocator = std.mem.Allocator; const Config = zup.Config; @@ -24,7 +24,7 @@ pub fn main(comptime Result: type, config: Config, res: Result) !void { var available = try Installation.getAvailableList(config); defer Installation.deinitMap(allocator, &available); - return perform(allocator, res.positionals[0], res.args.force, available); + return perform(allocator, res.positionals[0], res.args.force != 0, available); } pub fn perform(allocator: Allocator, name: []const u8, force: bool, available: Installations) !void { diff --git a/src/list.zig b/src/list.zig index bf95c2c..0a0fee9 100644 --- a/src/list.zig +++ b/src/list.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const zup = @import("zup"); +const zup = @import("root"); const Allocator = std.mem.Allocator; const ArrayList = std.ArrayList; @@ -20,11 +20,11 @@ pub const max_args = 0; pub fn main(comptime Result: type, config: Config, res: Result) !void { const allocator = config.allocator; - var list_active = res.args.active; - var list_available = res.args.available; - var list_installed = res.args.installed; + var list_active = res.args.active != 0; + var list_available = res.args.available != 0; + var list_installed = res.args.installed != 0; - if (res.args.all) { + if (res.args.all != 0) { list_active = true; list_available = true; list_installed = true; @@ -36,8 +36,7 @@ pub fn main(comptime Result: type, config: Config, res: Result) !void { if (list_active) { const active = try Installation.getActiveName(allocator); defer if (active) |s| allocator.free(s); - // TODO: zig-bug should I really need to do this? - try printActive(if (active) |a| a else null); + try printActive(active); } if (list_installed) { diff --git a/src/main.zig b/src/main.zig index 197a224..93356d6 100644 --- a/src/main.zig +++ b/src/main.zig @@ -73,25 +73,19 @@ fn dispatch( comptime default_fn: anytype, args: anytype, ) !void { - // TODO: This can still be improved, currently we're looping through all possible values, it could be somehow made - // into a switch. - const cmd_enum = CommandMap.get(cmd) orelse return @call(.{}, default_fn, args); + // TODO: This can still be improved, currently we're looping through all + // possible values, it could be somehow made into a switch. + const cmd_enum = CommandMap.get(cmd) orelse return @call(.auto, default_fn, args); const commands = @typeInfo(Command).Enum.fields; inline for (commands) |command| { if (@enumToInt(cmd_enum) == command.value) { - // TODO: zig-bug it cries about modifying a constant if - // I just write `return @call(.{}, fun, args);` const fun = @field(@field(@This(), command.name), fn_name); - return call(fun, args); + return @call(.auto, fun, args); } } unreachable; } -inline fn call(fun: anytype, args: anytype) !void { - return @call(.{}, fun, args); -} - fn unknownCmd(cmd: []const u8, _: Config, _: *ArgIterator) !void { std.log.err("Unknown subcommand: {s}", .{cmd}); return error.ArgError; diff --git a/src/remove.zig b/src/remove.zig index f795df7..2d868fb 100644 --- a/src/remove.zig +++ b/src/remove.zig @@ -1,6 +1,6 @@ const std = @import("std"); const xdg = @import("xdg"); -const zup = @import("zup"); +const zup = @import("root"); const Config = zup.Config; const Installation = zup.Installation; @@ -25,7 +25,7 @@ pub fn main(comptime Result: type, config: Config, res: Result) !void { const is_active = try Installation.isActive(allocator, name); - if (is_active and !res.args.force) { + if (is_active and res.args.force == 0) { std.log.err("{s} is the active installation, not removing without --force!", .{name}); return error.CantRemove; } diff --git a/src/subcommand.zig b/src/subcommand.zig index d750e94..09f1b4f 100644 --- a/src/subcommand.zig +++ b/src/subcommand.zig @@ -1,6 +1,6 @@ const clap = @import("clap"); const std = @import("std"); -const zup = @import("zup"); +const zup = @import("root"); const ArgIterator = std.process.ArgIterator; const Config = zup.Config; @@ -49,11 +49,11 @@ pub fn SubCommand(comptime template: type) type { }; defer res.deinit(); - if (res.args.help) { + if (res.args.help != 0) { return help(name); } - if (res.args.version) { + if (res.args.version != 0) { return zup.printVersion(); } diff --git a/src/switch.zig b/src/switch.zig index e5d243f..f90f7d7 100644 --- a/src/switch.zig +++ b/src/switch.zig @@ -1,6 +1,6 @@ const std = @import("std"); const xdg = @import("xdg"); -const zup = @import("zup"); +const zup = @import("root"); const Config = zup.Config; const Installation = zup.Installation; diff --git a/src/update.zig b/src/update.zig index 3451d90..55e6847 100644 --- a/src/update.zig +++ b/src/update.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const zup = @import("zup"); +const zup = @import("root"); const Config = zup.Config; const Installation = zup.Installation; -- cgit v1.2.3