From 38d510830948ac48d1d80e9bd113cc345a73f24f Mon Sep 17 00:00:00 2001 From: Kalle Carlbark Date: Sun, 31 Mar 2024 14:46:01 +0200 Subject: [PATCH] feat: Compile on latest zig 0.12.0-dev.3496+a2df84d0f While doing this also - Remove benchmarking - Migrate to page_allocator --- build.zig | 46 ++-------------------------------------------- build.zig.zon | 19 ++++++++----------- src/main.zig | 35 +++++++---------------------------- 3 files changed, 17 insertions(+), 83 deletions(-) diff --git a/build.zig b/build.zig index 628a425..cf69c34 100644 --- a/build.zig +++ b/build.zig @@ -1,70 +1,31 @@ const std = @import("std"); -// Although this function looks imperative, note that its job is to -// declaratively construct a build graph that will be executed by an external -// runner. pub fn build(b: *std.Build) void { - // Standard target options allows the person running `zig build` to choose - // what target to build for. Here we do not override the defaults, which - // means any target is allowed, and the default is native. Other options - // for restricting supported target set are available. const target = b.standardTargetOptions(.{}); - - // Standard optimization options allow the person running `zig build` to select - // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not - // set a preferred release mode, allowing the user to decide how to optimize. const optimize = b.standardOptimizeOption(.{}); const clap_dep = b.dependency("clap", .{ .target = target, .optimize = optimize }); const clap = clap_dep.module("clap"); - const benchmark_dep = b.dependency("benchmark", .{ .target = target, .optimize = optimize }); - const benchmark = benchmark_dep.module("benchmark"); - var opt = b.addOptions(); opt.addOption([]const u8, "version", "0.0.2"); const exe = b.addExecutable(.{ .name = "b64", - // In this case the main source file is merely a path, however, in more - // complicated build scripts, this could be a generated file. .root_source_file = .{ .path = "src/main.zig" }, .target = target, .optimize = optimize, }); - exe.addModule("build_info", opt.createModule()); - exe.addModule("clap", clap); - exe.addModule("benchmark", benchmark); - - // This declares intent for the executable to be installed into the - // standard location when the user invokes the "install" step (the default - // step when running `zig build`). + exe.root_module.addImport("build_info", opt.createModule()); + exe.root_module.addImport("clap", clap); b.installArtifact(exe); - - // This *creates* a Run step in the build graph, to be executed when another - // step is evaluated that depends on it. The next line below will establish - // such a dependency. const run_cmd = b.addRunArtifact(exe); - - // By making the run step depend on the install step, it will be run from the - // installation directory rather than directly from within the cache directory. - // This is not necessary, however, if the application depends on other installed - // files, this ensures they will be present and in the expected location. run_cmd.step.dependOn(b.getInstallStep()); - - // This allows the user to pass arguments to the application in the build - // command itself, like this: `zig build run -- arg1 arg2 etc` if (b.args) |args| { run_cmd.addArgs(args); } - - // This creates a build step. It will be visible in the `zig build --help` menu, - // and can be selected like this: `zig build run` - // This will evaluate the `run` step rather than the default, which is "install". const run_step = b.step("run", "Run the app"); run_step.dependOn(&run_cmd.step); - // Creates a step for unit testing. This only builds the test executable - // but does not run it. const unit_tests = b.addTest(.{ .root_source_file = .{ .path = "src/main.zig" }, .target = target, @@ -73,9 +34,6 @@ pub fn build(b: *std.Build) void { const run_unit_tests = b.addRunArtifact(unit_tests); - // Similar to creating the run step earlier, this exposes a `test` step to - // the `zig build --help` menu, providing a way for the user to request - // running the unit tests. const test_step = b.step("test", "Run unit tests"); test_step.dependOn(&run_unit_tests.step); } diff --git a/build.zig.zon b/build.zig.zon index 6be491b..d9b61a5 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -1,14 +1,11 @@ .{ - .name = "b64", - .version = "0.0.2", - .dependencies = .{ - .clap = .{ - .url = "https://github.com/Hejsil/zig-clap/archive/refs/tags/0.7.0.tar.gz", - .hash = "1220f48518ce22882e102255ed3bcdb7aeeb4891f50b2cdd3bd74b5b2e24d3149ba2" + .name = "b64", + .version = "0.0.2", + .paths = .{""}, + .dependencies = .{ + .clap = .{ + .url = "https://github.com/Hejsil/zig-clap/archive/master/latest.tar.gz", + .hash = "122014e73fd712190e109950837b97f6143f02d7e2b6986e1db70b6f4aadb5ba6a0d", + }, }, - .benchmark = .{ - .url = "https://github.com/karlseguin/benchmark.zig/archive/master/latest.tar.gz", - .hash = "12201c5d5325df8789a66fb2f014af9e1d4af9edb9ee245bebb4a5435fe01e56d1a2" - } - } } diff --git a/src/main.zig b/src/main.zig index 680dc99..bca28ce 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,7 +1,6 @@ const std = @import("std"); const clap = @import("clap"); const build_info = @import("build_info"); -const benchmark = @import("benchmark"); const base64 = std.base64; const debug = std.debug; const io = std.io; @@ -9,18 +8,19 @@ const io = std.io; pub fn main() !void { const params = comptime clap.parseParamsComptime( \\-h, --help Display this help and exit. - \\-b, --benchmark Benchmark b64 \\-d, --decode Decode base64 to string. \\-e, --encode Encode string to base64. \\-v, --version Display version. \\ String to encode or decode. ); + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer _ = gpa.deinit(); - const allocator = gpa.allocator(); + const allocator = std.heap.page_allocator; var diag = clap.Diagnostic{}; var res = clap.parse(clap.Help, ¶ms, clap.parsers.default, .{ + .allocator = allocator, .diagnostic = &diag, }) catch |err| { // Report useful error and exit @@ -39,13 +39,6 @@ pub fn main() !void { return; } - if (res.args.benchmark != 0) { - (try benchmark.run(benchmark_encode)).print("encode"); - (try benchmark.run(benchmark_decode)).print("decode"); - - return; - } - if (res.args.version != 0) { version(); @@ -149,7 +142,7 @@ fn encode(allocator: std.mem.Allocator, string: []const u8) ![]const u8 { fn decode(allocator: std.mem.Allocator, b64string: []const u8) ![]u8 { const decoder = std.base64.standard.Decoder; - var decodeSize = decoder.calcSizeForSlice(b64string) catch |err| { + const decodeSize = decoder.calcSizeForSlice(b64string) catch |err| { return err; }; @@ -163,29 +156,15 @@ fn decode(allocator: std.mem.Allocator, b64string: []const u8) ![]u8 { return err; }; - var decodedString = try allocator.dupe(u8, decoded); + const decodedString = try allocator.dupe(u8, decoded); return decodedString; } -fn benchmark_encode(allocator: std.mem.Allocator, timer: *std.time.Timer) !void { - const input = "hej lksjdlkajsd laksjdlkjasd"; - timer.reset(); - - std.mem.doNotOptimizeAway(try encode(allocator, input)); -} - -fn benchmark_decode(allocator: std.mem.Allocator, timer: *std.time.Timer) !void { - const input = "aGVqIGxrc2pkbGthanNkIGxha3NqZGxramFzZA=="; - timer.reset(); - - std.mem.doNotOptimizeAway(try decode(allocator, input)); -} - test "base64_decode() returns decoded string" { const test_allocator = std.testing.allocator; const base64String = "aGVqIGxrc2pkbGthanNkIGxha3NqZGxramFzZA=="; - var expectedString = "hej lksjdlkajsd laksjdlkjasd"; + const expectedString = "hej lksjdlkajsd laksjdlkjasd"; const decodedString = try decode(test_allocator, base64String); defer test_allocator.free(decodedString); @@ -196,7 +175,7 @@ test "base64_decode() returns decoded string" { test "base64_encode() returns encoded string" { const test_allocator = std.testing.allocator; const expectedEncodedString = "aGVqIGxrc2pkbGthanNkIGxha3NqZGxramFzZA=="; - var string = "hej lksjdlkajsd laksjdlkjasd"; + const string = "hej lksjdlkajsd laksjdlkjasd"; const encodedString = try encode(test_allocator, string); defer test_allocator.free(encodedString);