feat: Compile on latest zig 0.12.0-dev.3496+a2df84d0f

While doing this also
- Remove benchmarking
- Migrate to page_allocator
This commit is contained in:
Kalle Carlbark 2024-03-31 14:46:01 +02:00
parent 7674e7d0d8
commit 38d5108309
No known key found for this signature in database
3 changed files with 17 additions and 83 deletions

View file

@ -1,70 +1,31 @@
const std = @import("std"); 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 { 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(.{}); 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 optimize = b.standardOptimizeOption(.{});
const clap_dep = b.dependency("clap", .{ .target = target, .optimize = optimize }); const clap_dep = b.dependency("clap", .{ .target = target, .optimize = optimize });
const clap = clap_dep.module("clap"); 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(); var opt = b.addOptions();
opt.addOption([]const u8, "version", "0.0.2"); opt.addOption([]const u8, "version", "0.0.2");
const exe = b.addExecutable(.{ const exe = b.addExecutable(.{
.name = "b64", .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" }, .root_source_file = .{ .path = "src/main.zig" },
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
}); });
exe.addModule("build_info", opt.createModule()); exe.root_module.addImport("build_info", opt.createModule());
exe.addModule("clap", clap); exe.root_module.addImport("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`).
b.installArtifact(exe); 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); 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()); 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| { if (b.args) |args| {
run_cmd.addArgs(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"); const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step); 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(.{ const unit_tests = b.addTest(.{
.root_source_file = .{ .path = "src/main.zig" }, .root_source_file = .{ .path = "src/main.zig" },
.target = target, .target = target,
@ -73,9 +34,6 @@ pub fn build(b: *std.Build) void {
const run_unit_tests = b.addRunArtifact(unit_tests); 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"); const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_unit_tests.step); test_step.dependOn(&run_unit_tests.step);
} }

View file

@ -1,14 +1,11 @@
.{ .{
.name = "b64", .name = "b64",
.version = "0.0.2", .version = "0.0.2",
.dependencies = .{ .paths = .{""},
.clap = .{ .dependencies = .{
.url = "https://github.com/Hejsil/zig-clap/archive/refs/tags/0.7.0.tar.gz", .clap = .{
.hash = "1220f48518ce22882e102255ed3bcdb7aeeb4891f50b2cdd3bd74b5b2e24d3149ba2" .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"
}
}
} }

View file

@ -1,7 +1,6 @@
const std = @import("std"); const std = @import("std");
const clap = @import("clap"); const clap = @import("clap");
const build_info = @import("build_info"); const build_info = @import("build_info");
const benchmark = @import("benchmark");
const base64 = std.base64; const base64 = std.base64;
const debug = std.debug; const debug = std.debug;
const io = std.io; const io = std.io;
@ -9,18 +8,19 @@ const io = std.io;
pub fn main() !void { pub fn main() !void {
const params = comptime clap.parseParamsComptime( const params = comptime clap.parseParamsComptime(
\\-h, --help Display this help and exit. \\-h, --help Display this help and exit.
\\-b, --benchmark Benchmark b64
\\-d, --decode Decode base64 to string. \\-d, --decode Decode base64 to string.
\\-e, --encode Encode string to base64. \\-e, --encode Encode string to base64.
\\-v, --version Display version. \\-v, --version Display version.
\\<str> String to encode or decode. \\<str> String to encode or decode.
); );
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit(); defer _ = gpa.deinit();
const allocator = gpa.allocator(); const allocator = std.heap.page_allocator;
var diag = clap.Diagnostic{}; var diag = clap.Diagnostic{};
var res = clap.parse(clap.Help, &params, clap.parsers.default, .{ var res = clap.parse(clap.Help, &params, clap.parsers.default, .{
.allocator = allocator,
.diagnostic = &diag, .diagnostic = &diag,
}) catch |err| { }) catch |err| {
// Report useful error and exit // Report useful error and exit
@ -39,13 +39,6 @@ pub fn main() !void {
return; 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) { if (res.args.version != 0) {
version(); 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 { fn decode(allocator: std.mem.Allocator, b64string: []const u8) ![]u8 {
const decoder = std.base64.standard.Decoder; const decoder = std.base64.standard.Decoder;
var decodeSize = decoder.calcSizeForSlice(b64string) catch |err| { const decodeSize = decoder.calcSizeForSlice(b64string) catch |err| {
return err; return err;
}; };
@ -163,29 +156,15 @@ fn decode(allocator: std.mem.Allocator, b64string: []const u8) ![]u8 {
return err; return err;
}; };
var decodedString = try allocator.dupe(u8, decoded); const decodedString = try allocator.dupe(u8, decoded);
return decodedString; 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" { test "base64_decode() returns decoded string" {
const test_allocator = std.testing.allocator; const test_allocator = std.testing.allocator;
const base64String = "aGVqIGxrc2pkbGthanNkIGxha3NqZGxramFzZA=="; const base64String = "aGVqIGxrc2pkbGthanNkIGxha3NqZGxramFzZA==";
var expectedString = "hej lksjdlkajsd laksjdlkjasd"; const expectedString = "hej lksjdlkajsd laksjdlkjasd";
const decodedString = try decode(test_allocator, base64String); const decodedString = try decode(test_allocator, base64String);
defer test_allocator.free(decodedString); defer test_allocator.free(decodedString);
@ -196,7 +175,7 @@ test "base64_decode() returns decoded string" {
test "base64_encode() returns encoded string" { test "base64_encode() returns encoded string" {
const test_allocator = std.testing.allocator; const test_allocator = std.testing.allocator;
const expectedEncodedString = "aGVqIGxrc2pkbGthanNkIGxha3NqZGxramFzZA=="; const expectedEncodedString = "aGVqIGxrc2pkbGthanNkIGxha3NqZGxramFzZA==";
var string = "hej lksjdlkajsd laksjdlkjasd"; const string = "hej lksjdlkajsd laksjdlkjasd";
const encodedString = try encode(test_allocator, string); const encodedString = try encode(test_allocator, string);
defer test_allocator.free(encodedString); defer test_allocator.free(encodedString);