From ed67a52b151d8531bbe81e783d383864ffc914c5 Mon Sep 17 00:00:00 2001 From: Luna Date: Fri, 4 Aug 2023 21:34:17 -0300 Subject: [PATCH 1/3] fixes for some version of zig --- build.zig | 1 - src/lang.zig | 6 +++--- src/printer.zig | 4 ++-- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/build.zig b/build.zig index 520aeb9..ad6f87d 100644 --- a/build.zig +++ b/build.zig @@ -45,7 +45,6 @@ pub fn build(b: *Builder) void { const mode = b.standardReleaseOptions(); const exe = b.addExecutable("scritcher", "src/main.zig"); exe.setBuildMode(mode); - exe.use_stage1 = true; exe.install(); setupLinks(exe); diff --git a/src/lang.zig b/src/lang.zig index 08346ff..ca5fbec 100644 --- a/src/lang.zig +++ b/src/lang.zig @@ -488,7 +488,7 @@ pub const CommandList = struct { const inner_command = cmd_ptr.cast(typ).?; inline for (@typeInfo(typ).Struct.fields) |cmd_field| { - switch (cmd_field.field_type) { + switch (cmd_field.type) { []u8, []const u8 => self.list.allocator.free(@field(inner_command, cmd_field.name)), else => {}, } @@ -576,7 +576,7 @@ pub const Lang = struct { } const arg = maybe_arg.?; - const arg_value = switch (cmd_field.field_type) { + const arg_value = switch (cmd_field.type) { f32 => try std.fmt.parseFloat(f32, arg), u64 => try std.fmt.parseInt(u64, arg, 10), usize => try std.fmt.parseInt(usize, arg, 10), @@ -601,7 +601,7 @@ pub const Lang = struct { } const arg = arg_opt.?; - const argument_value = switch (cmd_field.field_type) { + const argument_value = switch (cmd_field.type) { usize => try std.fmt.parseInt(usize, arg, 10), i32 => try std.fmt.parseInt(i32, arg, 10), f32 => try std.fmt.parseFloat(f32, arg), diff --git a/src/printer.zig b/src/printer.zig index a851602..b3da89b 100644 --- a/src/printer.zig +++ b/src/printer.zig @@ -7,9 +7,9 @@ fn printCommandWithParams(stream: anytype, command: anytype) !void { const Parameters = @TypeOf(command.parameters); try stream.print(" {d} {d}", .{ command.split, command.index }); inline for (@typeInfo(Parameters).Struct.fields) |field| { - if (field.field_type == f32 or field.field_type == f64) { + if (field.type == f32 or field.type == f64) { try stream.print(" {}", .{@field(command.parameters, field.name)}); - } else if (field.field_type == usize or field.field_type == u64) { + } else if (field.type == usize or field.type == u64) { try stream.print(" {d}", .{@field(command.parameters, field.name)}); } else { try stream.print(" {s}", .{@field(command.parameters, field.name)}); From 7cc93c2976208ce6070b6d12843b6e2268061fc0 Mon Sep 17 00:00:00 2001 From: Luna Date: Fri, 4 Aug 2023 21:34:51 -0300 Subject: [PATCH 2/3] zig fmt --- src/custom.zig | 10 +++++----- src/image.zig | 26 +++++++++++++------------- src/lang.zig | 2 +- src/lv2_helpers.zig | 2 +- src/main.zig | 4 ++-- src/plugin.zig | 4 ++-- src/runner.zig | 2 +- 7 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/custom.zig b/src/custom.zig index ffb129c..d942a3b 100644 --- a/src/custom.zig +++ b/src/custom.zig @@ -24,7 +24,7 @@ pub const RandomNoise = struct { if (params.fill_bytes > 0) { var rand_buf = allocator.alloc(f32, params.fill_bytes) catch return null; - for (rand_buf) |_, idx| { + for (rand_buf, 0..) |_, idx| { rand_buf[idx] = r.random().float(f32); } @@ -72,8 +72,8 @@ pub const WildNoise = struct { if (params.fill_bytes > 0) { var rand_buf = allocator.alloc(f32, params.fill_bytes) catch return null; - for (rand_buf) |_, idx| { - rand_buf[idx] = @intToFloat(f32, r.random().int(u1)); + for (rand_buf, 0..) |_, idx| { + rand_buf[idx] = @as(f32, @floatFromInt(r.random().int(u1))); } return WildNoise{ @@ -99,7 +99,7 @@ pub const WildNoise = struct { bufs.out[0] = rand_buf[self.cnt]; self.cnt += 1; } else { - bufs.out[0] = @intToFloat(f32, self.r.random().int(u1)); + bufs.out[0] = @as(f32, @floatFromInt(self.r.random().int(u1))); } } }; @@ -163,7 +163,7 @@ pub const Embed = struct { image.sseek(self.sndfile, 0); - self.buf = try self.allocator.alloc(f32, @intCast(usize, in_fmt.channels)); + self.buf = try self.allocator.alloc(f32, @as(usize, @intCast(in_fmt.channels))); } pub fn deinit(self: *@This()) void { diff --git a/src/image.zig b/src/image.zig index 6a7481c..47be1cd 100644 --- a/src/image.zig +++ b/src/image.zig @@ -63,7 +63,7 @@ pub fn swrite(file: *c.SNDFILE, buf: [*]f32, frames: i64) !void { } pub fn sseek(file: *c.SNDFILE, offset: usize) void { - const offset_i64 = @intCast(i64, offset); + const offset_i64 = @as(i64, @intCast(offset)); const frames = c.sf_seek(file, offset_i64, c.SEEK_SET); const frames_current = c.sf_seek(file, 0, c.SEEK_CUR); std.debug.assert(frames == frames_current); @@ -80,7 +80,7 @@ pub fn temporaryName(allocator: std.mem.Allocator) ![]u8 { var nam = try allocator.alloc(u8, template.len); std.mem.copy(u8, nam, template); - const seed = @truncate(u64, @bitCast(u128, std.time.nanoTimestamp())); + const seed = @as(u64, @truncate(@as(u128, @bitCast(std.time.nanoTimestamp())))); var r = std.rand.DefaultPrng.init(seed); var fill = nam[template_start.len..nam.len]; @@ -88,8 +88,8 @@ pub fn temporaryName(allocator: std.mem.Allocator) ![]u8 { var i: usize = 0; while (i < 100) : (i += 1) { // generate a random uppercase letter, that is, 65 + random number. - for (fill) |_, f_idx| { - var idx = @intCast(u8, r.random().uintLessThan(u5, 24)); + for (fill, 0..) |_, f_idx| { + var idx = @as(u8, @intCast(r.random().uintLessThan(u5, 24))); var letter = @as(u8, 65) + idx; fill[f_idx] = letter; } @@ -151,7 +151,7 @@ pub const Image = struct { .sndfile = sndfile, .path = path, .curpath = path, - .frames = @intCast(usize, in_fmt.frames), + .frames = @as(usize, @intCast(in_fmt.frames)), }; return image; @@ -161,7 +161,7 @@ pub const Image = struct { var in_fmt = mkSfInfo(); // clone sndfile var sndfile = try sopen(self.allocator, self.curpath, c.SFM_READ, &in_fmt); - std.debug.assert(self.frames == @intCast(usize, in_fmt.frames)); + std.debug.assert(self.frames == @as(usize, @intCast(in_fmt.frames))); var image = try self.allocator.create(Image); @@ -173,7 +173,7 @@ pub const Image = struct { .sndfile = sndfile, .path = self.path, .curpath = self.curpath, - .frames = @intCast(usize, in_fmt.frames), + .frames = @as(usize, @intCast(in_fmt.frames)), }; return image; @@ -198,12 +198,12 @@ pub const Image = struct { pub fn read(self: *Image, file_chans: c_int, buf: []f32) bool { const n_read: c.sf_count_t = c.sf_readf_float(self.sndfile, buf.ptr, 1); - const buf_chans = @intCast(c_int, buf.len); + const buf_chans = @as(c_int, @intCast(buf.len)); var i = file_chans - 1; while (i < buf_chans) : (i += 1) { //buf[@intCast(usize, i)] = buf[i % file_chans]; - buf[@intCast(usize, i)] = buf[@intCast(usize, @mod(i, file_chans))]; + buf[@as(usize, @intCast(i))] = buf[@as(usize, @intCast(@mod(i, file_chans)))]; } return n_read == 1; @@ -237,13 +237,13 @@ pub const Image = struct { var view: []f32 = buf[0..buf.len]; if (bytes_until_end < buf.len) { - read_bytes = c.sf_readf_float(self.sndfile, buf.ptr, @intCast(i64, bytes_until_end)); + read_bytes = c.sf_readf_float(self.sndfile, buf.ptr, @as(i64, @intCast(bytes_until_end))); view = buf[0..bytes_until_end]; } else { - read_bytes = c.sf_readf_float(self.sndfile, buf.ptr, @intCast(i64, buf.len)); + read_bytes = c.sf_readf_float(self.sndfile, buf.ptr, @as(i64, @intCast(buf.len))); } - try swrite(out_file, view.ptr, @intCast(i64, view.len)); + try swrite(out_file, view.ptr, @as(i64, @intCast(view.len))); } sseek(self.sndfile, end); @@ -264,7 +264,7 @@ pub const Image = struct { // std.testing.expectEqual(self.frames, @intCast(usize, in_fmt.frames)); self.curpath = path; - self.frames = @intCast(usize, in_fmt.frames); + self.frames = @as(usize, @intCast(in_fmt.frames)); log.debug("\timage: reopened on '{s}' (frames={d}, fmt.frames={d})", .{ self.curpath, diff --git a/src/lang.zig b/src/lang.zig index ca5fbec..9f85d0f 100644 --- a/src/lang.zig +++ b/src/lang.zig @@ -669,7 +669,7 @@ pub const Lang = struct { comptime var lowered_command_name = [_]u8{0} ** struct_name.len; comptime { - for (struct_name) |c, i| { + for (struct_name, 0..) |c, i| { lowered_command_name[i] = std.ascii.toLower(c); } } diff --git a/src/lv2_helpers.zig b/src/lv2_helpers.zig index 82c961d..1d3df64 100644 --- a/src/lv2_helpers.zig +++ b/src/lv2_helpers.zig @@ -69,7 +69,7 @@ pub fn setupPorts(ctx: *plugin.Context) ![]Port { var ports = try ctx.allocator.alloc(Port, n_ports); - for (ports) |_, idx| { + for (ports, 0..) |_, idx| { var port: *Port = &ports[idx]; port.* = Port{ .lilv_port = null, diff --git a/src/main.zig b/src/main.zig index 083b204..7b7b66b 100644 --- a/src/main.zig +++ b/src/main.zig @@ -30,8 +30,8 @@ fn copyCommandToHeap(allocator: std.mem.Allocator, command: langs.Command, compt var heap_cmd = try allocator.create(CommandStruct); @memcpy( - @ptrCast([*]u8, &heap_cmd), - @ptrCast([*]const u8, &casted), + @as([*]u8, @ptrCast(&heap_cmd)), + @as([*]const u8, @ptrCast(&casted)), @sizeOf(CommandStruct), ); diff --git a/src/plugin.zig b/src/plugin.zig index b0fb504..57f8059 100644 --- a/src/plugin.zig +++ b/src/plugin.zig @@ -101,8 +101,8 @@ pub const RunContext = struct { var i: usize = 0; var o: usize = 0; - for (ports) |_, p_idx| { - var p = @intCast(u32, p_idx); + for (ports, 0..) |_, p_idx| { + var p = @as(u32, @intCast(p_idx)); var port: *lv2.Port = &ports[p_idx]; switch (port.ptype) { diff --git a/src/runner.zig b/src/runner.zig index 76f54a0..739a95f 100644 --- a/src/runner.zig +++ b/src/runner.zig @@ -70,7 +70,7 @@ pub const Runner = struct { // ':0' should ALWAYS point to the image. if (self.repl) index += 3 else index += 3; - for (self.args) |arg, idx| { + for (self.args, 0..) |arg, idx| { log.debug("arg{d} = {s}", .{ idx, arg }); } log.debug("fetch arg idx={d}", .{index}); From c73b98a3565a4e39de836ddfb6bb5194f742af3e Mon Sep 17 00:00:00 2001 From: Luna Date: Fri, 4 Aug 2023 21:50:59 -0300 Subject: [PATCH 3/3] fix for zig 0.11 --- build.zig | 38 +++++++++++++++++++++++++------------- src/image.zig | 4 ++-- src/lang.zig | 7 ++++--- src/magick.zig | 4 ++-- src/main.zig | 6 +----- src/plugin.zig | 2 +- src/runner.zig | 4 ++-- 7 files changed, 37 insertions(+), 28 deletions(-) diff --git a/build.zig b/build.zig index ad6f87d..e969b99 100644 --- a/build.zig +++ b/build.zig @@ -12,8 +12,8 @@ fn setupLinks(step: *builds.LibExeObjStep) void { step.linkSystemLibrary("GraphicsMagickWand"); step.linkSystemLibrary("GraphicsMagick"); - step.addIncludePath("/usr/include/GraphicsMagick"); - step.addIncludePath("/usr/include"); + step.addIncludePath(.{ .path = "/usr/include/GraphicsMagick" }); + step.addIncludePath(.{ .path = "/usr/include" }); const possible_lilv_include_dirs = [_][]const u8{ "/usr/include/lilv-0/lilv", @@ -32,7 +32,7 @@ fn setupLinks(step: *builds.LibExeObjStep) void { found_any_lilv = true; std.debug.print("found lilv at '{s}'\n", .{possible_lilv_dir}); - step.addIncludePath(possible_lilv_dir); + step.addIncludePath(.{ .path = possible_lilv_dir }); } if (!found_any_lilv) { @@ -42,22 +42,34 @@ fn setupLinks(step: *builds.LibExeObjStep) void { } pub fn build(b: *Builder) void { - const mode = b.standardReleaseOptions(); - const exe = b.addExecutable("scritcher", "src/main.zig"); - exe.setBuildMode(mode); - exe.install(); + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + const exe = b.addExecutable(.{ + .name = "scritcher", + .root_source_file = .{ .path = "src/main.zig" }, + .target = target, + .optimize = optimize, + }); setupLinks(exe); + b.installArtifact(exe); - const test_obj_step = b.addTest("src/main.zig"); - setupLinks(test_obj_step); + const run_cmd = b.addRunArtifact(exe); - const run_cmd = exe.run(); - run_cmd.step.dependOn(b.getInstallStep()); + if (b.args) |args| { + run_cmd.addArgs(args); + } const run_step = b.step("run", "Run the app"); run_step.dependOn(&run_cmd.step); - const test_step = b.step("test", "Run tests"); - test_step.dependOn(&test_obj_step.step); + const test_step = b.addTest(.{ + .root_source_file = .{ .path = "src/main.zig" }, + .target = target, + .optimize = optimize, + }); + setupLinks(test_step); + const run_unit_tests = b.addRunArtifact(test_step); + const test_cmd = b.step("test", "run unit tests"); + test_cmd.dependOn(&run_unit_tests.step); } diff --git a/src/image.zig b/src/image.zig index 47be1cd..3905ea5 100644 --- a/src/image.zig +++ b/src/image.zig @@ -26,7 +26,7 @@ pub fn sopen( mode: i32, fmt: *c.SF_INFO, ) !*c.SNDFILE { - var cstr_path = try std.cstr.addNullByte(allocator, path); + var cstr_path = try allocator.dupeZ(u8, path); defer allocator.free(cstr_path); var file = c.sf_open(cstr_path.ptr, mode, fmt); @@ -321,7 +321,7 @@ pub const Image = struct { // now, for each param for the plugin, we find its port, and set // the value for the port there. for (params.items) |param| { - var sym_cstr = try std.cstr.addNullByte(self.allocator, param.sym); + var sym_cstr = try self.allocator.dupeZ(u8, param.sym); defer self.allocator.free(sym_cstr); var sym = c.lilv_new_string(ctx.world, sym_cstr.ptr); diff --git a/src/lang.zig b/src/lang.zig index 9f85d0f..b18b0fb 100644 --- a/src/lang.zig +++ b/src/lang.zig @@ -496,7 +496,8 @@ pub const CommandList = struct { } } - self.list.allocator.destroy(cmd_ptr); + //TODO this is ian invalid free + //self.list.allocator.destroy(cmd_ptr); } self.list.deinit(); } @@ -537,7 +538,7 @@ pub const Lang = struct { fn parseCommandArguments( self: *@This(), comptime command_struct: type, - tok_it: *std.mem.SplitIterator(u8), + tok_it: *std.mem.SplitIterator(u8, .sequence), commands: *CommandList, ) !void { // Based on the command struct fields, we can parse the arguments. @@ -638,7 +639,7 @@ pub const Lang = struct { if (std.mem.startsWith(u8, stmt, "#")) continue; // TODO better tokenizer instead of just tokenize(" ")...maybe???? - var tok_it = std.mem.split(u8, stmt, " "); + var tok_it = std.mem.splitSequence(u8, stmt, " "); var cmd_opt = tok_it.next(); if (cmd_opt == null) { diff --git a/src/magick.zig b/src/magick.zig index c6adf3c..e79f513 100644 --- a/src/magick.zig +++ b/src/magick.zig @@ -38,7 +38,7 @@ fn magickLoad(image: *Image) !MagickContext { var mctx = try MagickContext.init(); errdefer mctx.deinit(); - var curpath = try std.cstr.addNullByte(image.allocator, image.curpath); + var curpath = try image.allocator.dupeZ(u8, image.curpath); defer image.allocator.free(curpath); log.debug("loading '{s}'", .{curpath}); @@ -53,7 +53,7 @@ fn magickSave(image: *Image, wand: *mc.MagickWand) !void { const allocator = image.allocator; var tmpnam = try images.temporaryName(allocator); - var c_tmpnam = try std.cstr.addNullByte(allocator, tmpnam); + var c_tmpnam = try allocator.dupeZ(u8, tmpnam); defer allocator.free(c_tmpnam); log.debug("\tmagick: saving to '{s}'..", .{c_tmpnam}); diff --git a/src/main.zig b/src/main.zig index 7b7b66b..7563392 100644 --- a/src/main.zig +++ b/src/main.zig @@ -29,11 +29,7 @@ fn copyCommandToHeap(allocator: std.mem.Allocator, command: langs.Command, compt const casted = command.cast(CommandStruct).?; var heap_cmd = try allocator.create(CommandStruct); - @memcpy( - @as([*]u8, @ptrCast(&heap_cmd)), - @as([*]const u8, @ptrCast(&casted)), - @sizeOf(CommandStruct), - ); + heap_cmd.* = casted.*; return &heap_cmd.base; } diff --git a/src/plugin.zig b/src/plugin.zig index 57f8059..f5f6517 100644 --- a/src/plugin.zig +++ b/src/plugin.zig @@ -131,7 +131,7 @@ pub const RunContext = struct { }; pub fn makeContext(allocator: std.mem.Allocator, plugin_uri: []const u8) !Context { - const cstr_plugin_uri = try std.cstr.addNullByte(allocator, plugin_uri); + const cstr_plugin_uri = try allocator.dupeZ(u8, plugin_uri); defer allocator.free(cstr_plugin_uri); var world: *c.LilvWorld = c.lilv_world_new().?; diff --git a/src/runner.zig b/src/runner.zig index 739a95f..0eea50b 100644 --- a/src/runner.zig +++ b/src/runner.zig @@ -152,7 +152,7 @@ pub const Runner = struct { while (try it.next()) |entry| { switch (entry.kind) { - .File => blk: { + .file => blk: { if (!std.mem.startsWith(u8, entry.name, starts_with)) break :blk {}; // we want to get the N in x_gN.ext @@ -215,7 +215,7 @@ pub const Runner = struct { const rotate_cmd = cmd.cast(lang.Command.Rotate).?; var image = try self.getImage(); - var c_bgfill = try std.cstr.addNullByte(self.allocator, rotate_cmd.bgfill); + var c_bgfill = try self.allocator.dupeZ(u8, rotate_cmd.bgfill); defer self.allocator.free(c_bgfill); try magick.runRotate(image, rotate_cmd.deg, c_bgfill);