Update to zig 0.12.0 (#2)

This commit is contained in:
Vesim 2024-04-26 23:17:36 +02:00 committed by GitHub
parent 9e4ee57ec2
commit 51c49c38d8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 30 additions and 30 deletions

View File

@ -24,7 +24,7 @@ pub fn build(b: *std.Build) !void {
const optimize = b.standardOptimizeOption(.{}); const optimize = b.standardOptimizeOption(.{});
// Add the gpio module so it can be used by the package manager // Add the gpio module so it can be used by the package manager
var gpio_module = b.createModule(.{ .source_file = .{ .path = "src/index.zig" } }); const gpio_module = b.createModule(.{ .root_source_file = .{ .path = "src/index.zig" } });
try b.modules.put(b.dupe("gpio"), gpio_module); try b.modules.put(b.dupe("gpio"), gpio_module);
// Create a step to build all the examples // Create a step to build all the examples
@ -41,7 +41,7 @@ pub fn build(b: *std.Build) !void {
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
}); });
exe.addModule("gpio", gpio_module); exe.root_module.addImport("gpio", gpio_module);
const build_step = b.addInstallArtifact(exe, .{}); const build_step = b.addInstallArtifact(exe, .{});
step.dependOn(&build_step.step); step.dependOn(&build_step.step);
@ -62,7 +62,7 @@ pub fn build(b: *std.Build) !void {
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
}); });
exe.addModule("gpio", gpio_module); exe.root_module.addImport("gpio", gpio_module);
const build_step = b.addInstallArtifact(exe, .{}); const build_step = b.addInstallArtifact(exe, .{});
step.dependOn(&build_step.step); step.dependOn(&build_step.step);

View File

@ -2,7 +2,7 @@ const std = @import("std");
const gpio = @import("gpio"); const gpio = @import("gpio");
pub fn main() !void { pub fn main() !void {
var iter_dir = try std.fs.openIterableDirAbsolute("/dev", .{}); var iter_dir = try std.fs.openDirAbsolute("/dev", .{ .iterate = true });
defer iter_dir.close(); defer iter_dir.close();
const stdout = std.io.getStdOut().writer(); const stdout = std.io.getStdOut().writer();
@ -11,7 +11,7 @@ pub fn main() !void {
while (try iter.next()) |entry| { while (try iter.next()) |entry| {
if (!hasPrefix(entry.name, "gpiochip")) continue; if (!hasPrefix(entry.name, "gpiochip")) continue;
const fl = try iter_dir.dir.openFile(entry.name, .{}); const fl = try iter_dir.openFile(entry.name, .{});
var chip = try gpio.getChipByFd(fl.handle); var chip = try gpio.getChipByFd(fl.handle);
defer chip.close(); // This will close the fd defer chip.close(); // This will close the fd

View File

@ -16,7 +16,7 @@ pub fn main() !void {
return error.InsufficientArguments; return error.InsufficientArguments;
} }
var path: []const u8 = if (hasPrefix(args[1], "gpiochip")) const path: []const u8 = if (hasPrefix(args[1], "gpiochip"))
try std.mem.concat(alloc, u8, &.{ "/dev/", args[1] }) try std.mem.concat(alloc, u8, &.{ "/dev/", args[1] })
else else
try std.mem.concat(alloc, u8, &.{ "/dev/gpiochip", args[1] }); try std.mem.concat(alloc, u8, &.{ "/dev/gpiochip", args[1] });
@ -32,7 +32,7 @@ pub fn main() !void {
// Iterate over each argument starting from the second one // Iterate over each argument starting from the second one
for (args[2..args.len]) |argument| { for (args[2..args.len]) |argument| {
// Parse each argument as an integer and add it to offsets // Parse each argument as an integer and add it to offsets
var offset = try std.fmt.parseUnsigned(u32, argument, 10); const offset = try std.fmt.parseUnsigned(u32, argument, 10);
try offsets.append(offset); try offsets.append(offset);
} }

View File

@ -18,7 +18,7 @@ pub fn main() !void {
// If the argument has the "gpiochip" prefix, // If the argument has the "gpiochip" prefix,
// just use it unchanged. Otherwise, add the prefix. // just use it unchanged. Otherwise, add the prefix.
var filename: []const u8 = if (hasGpiochip) const filename: []const u8 = if (hasGpiochip)
argument argument
else else
try std.mem.concat(alloc, u8, &.{ "gpiochip", argument }); try std.mem.concat(alloc, u8, &.{ "gpiochip", argument });

View File

@ -16,7 +16,7 @@ pub fn main() !void {
return error.InsufficientArguments; return error.InsufficientArguments;
} }
var path: []const u8 = if (hasPrefix(args[1], "gpiochip")) const path: []const u8 = if (hasPrefix(args[1], "gpiochip"))
try std.mem.concat(alloc, u8, &.{ "/dev/", args[1] }) try std.mem.concat(alloc, u8, &.{ "/dev/", args[1] })
else else
try std.mem.concat(alloc, u8, &.{ "/dev/gpiochip", args[1] }); try std.mem.concat(alloc, u8, &.{ "/dev/gpiochip", args[1] });
@ -34,8 +34,8 @@ pub fn main() !void {
// Get the index of the equals sign in the argument // Get the index of the equals sign in the argument
const eqIndex = std.mem.indexOf(u8, argument, "=") orelse return error.InvalidArgument; const eqIndex = std.mem.indexOf(u8, argument, "=") orelse return error.InvalidArgument;
// Parse each argument's offset and value, and add it to the values map // Parse each argument's offset and value, and add it to the values map
var offset = try std.fmt.parseUnsigned(u32, argument[0..eqIndex], 10); const offset = try std.fmt.parseUnsigned(u32, argument[0..eqIndex], 10);
var value = try std.fmt.parseUnsigned(u1, argument[eqIndex + 1 .. argument.len], 10); const value = try std.fmt.parseUnsigned(u1, argument[eqIndex + 1 .. argument.len], 10);
try values.put(offset, value != 0); try values.put(offset, value != 0);
} }

View File

@ -3,19 +3,19 @@ const gpio = @import("index.zig");
/// Opens the file at path and uses the file descriptor to get the gpiochip. /// Opens the file at path and uses the file descriptor to get the gpiochip.
pub fn getChip(path: []const u8) !Chip { pub fn getChip(path: []const u8) !Chip {
var fl = try std.fs.openFileAbsolute(path, .{}); const fl = try std.fs.openFileAbsolute(path, .{});
return try getChipByFd(fl.handle); return try getChipByFd(fl.handle);
} }
/// Same as `getChip` but the `path` parameter is null-terminated. /// Same as `getChip` but the `path` parameter is null-terminated.
pub fn getChipZ(path: [*:0]const u8) !Chip { pub fn getChipZ(path: [*:0]const u8) !Chip {
var fl = try std.fs.openFileAbsoluteZ(path, .{}); const fl = try std.fs.openFileAbsoluteZ(path, .{});
return try getChipByFd(fl.handle); return try getChipByFd(fl.handle);
} }
/// Returns a `chip` with the given file descriptor. /// Returns a `chip` with the given file descriptor.
pub fn getChipByFd(fd: std.os.fd_t) !Chip { pub fn getChipByFd(fd: std.posix.fd_t) !Chip {
var info = try gpio.uapi.getChipInfo(fd); const info = try gpio.uapi.getChipInfo(fd);
return Chip{ return Chip{
.name = info.name, .name = info.name,
.label = info.label, .label = info.label,
@ -35,7 +35,7 @@ pub const Chip = struct {
/// If it isn't set, "zig-gpio" will be used instead. /// If it isn't set, "zig-gpio" will be used instead.
consumer: ?[gpio.uapi.MAX_NAME_SIZE]u8 = null, consumer: ?[gpio.uapi.MAX_NAME_SIZE]u8 = null,
/// The file descriptor of the `gpiochip` device. /// The file descriptor of the `gpiochip` device.
handle: std.os.fd_t, handle: std.posix.fd_t,
// The amount of lines available under this device. // The amount of lines available under this device.
lines: u32, lines: u32,
closed: bool = false, closed: bool = false,
@ -71,7 +71,7 @@ pub const Chip = struct {
/// Requests and returns a single line at the given `offset`, from the given `chip`. /// Requests and returns a single line at the given `offset`, from the given `chip`.
pub fn requestLine(self: Chip, offset: u32, flags: gpio.uapi.LineFlags) !Line { pub fn requestLine(self: Chip, offset: u32, flags: gpio.uapi.LineFlags) !Line {
var l = try self.requestLines(&.{offset}, flags); const l = try self.requestLines(&.{offset}, flags);
return Line{ .lines = l }; return Line{ .lines = l };
} }
@ -108,14 +108,14 @@ pub const Chip = struct {
pub fn close(self: *Chip) void { pub fn close(self: *Chip) void {
if (self.closed) return; if (self.closed) return;
self.closed = true; self.closed = true;
std.os.close(self.handle); std.posix.close(self.handle);
} }
}; };
/// Represents a collection of lines requested from a `chip`. /// Represents a collection of lines requested from a `chip`.
pub const Lines = struct { pub const Lines = struct {
/// The file descriptor of the lines. /// The file descriptor of the lines.
handle: std.os.fd_t, handle: std.posix.fd_t,
/// The amount of lines being controlled. /// The amount of lines being controlled.
num_lines: u32, num_lines: u32,
/// The offsets of the lines being controlled. /// The offsets of the lines being controlled.
@ -254,7 +254,7 @@ pub const Lines = struct {
pub fn close(self: *Lines) void { pub fn close(self: *Lines) void {
if (self.closed) return; if (self.closed) return;
self.closed = true; self.closed = true;
std.os.close(self.handle); std.posix.close(self.handle);
} }
}; };

View File

@ -176,20 +176,20 @@ pub const LineInfoChanged = extern struct {
/// Returns an error based on the given return code /// Returns an error based on the given return code
fn handleErrno(ret: usize) !void { fn handleErrno(ret: usize) !void {
if (ret == 0) return; if (ret == 0) return;
return switch (std.os.errno(ret)) { return switch (std.posix.errno(ret)) {
.BUSY => error.DeviceIsBusy, .BUSY => error.DeviceIsBusy,
.INVAL => error.InvalidArgument, .INVAL => error.InvalidArgument,
.BADF => error.BadFileDescriptor, .BADF => error.BadFileDescriptor,
.NOTTY => error.InappropriateIOCTLForDevice, .NOTTY => error.InappropriateIOCTLForDevice,
.IO => error.IOError, .IO => error.IOError,
.FAULT => unreachable, .FAULT => unreachable,
else => |err| return std.os.unexpectedErrno(err), else => |err| return std.posix.unexpectedErrno(err),
}; };
} }
/// Executes `GPIO_GET_CHIPINFO_IOCTL` on the given fd and returns the resulting /// Executes `GPIO_GET_CHIPINFO_IOCTL` on the given fd and returns the resulting
/// `ChipInfo` value /// `ChipInfo` value
pub fn getChipInfo(fd: std.os.fd_t) !ChipInfo { pub fn getChipInfo(fd: std.posix.fd_t) !ChipInfo {
const req = std.os.linux.IOCTL.IOR(0xB4, 0x01, ChipInfo); const req = std.os.linux.IOCTL.IOR(0xB4, 0x01, ChipInfo);
var info = std.mem.zeroes(ChipInfo); var info = std.mem.zeroes(ChipInfo);
try handleErrno(std.os.linux.ioctl(fd, req, @intFromPtr(&info))); try handleErrno(std.os.linux.ioctl(fd, req, @intFromPtr(&info)));
@ -198,7 +198,7 @@ pub fn getChipInfo(fd: std.os.fd_t) !ChipInfo {
/// Executes `GPIO_V2_GET_LINEINFO_IOCTL` on the given fd and returns the resulting /// Executes `GPIO_V2_GET_LINEINFO_IOCTL` on the given fd and returns the resulting
/// `LineInfo` value /// `LineInfo` value
pub fn getLineInfo(fd: std.os.fd_t, offset: u32) !LineInfo { pub fn getLineInfo(fd: std.posix.fd_t, offset: u32) !LineInfo {
const req = std.os.linux.IOCTL.IOWR(0xB4, 0x05, LineInfo); const req = std.os.linux.IOCTL.IOWR(0xB4, 0x05, LineInfo);
var info = std.mem.zeroes(LineInfo); var info = std.mem.zeroes(LineInfo);
info.offset = offset; info.offset = offset;
@ -208,7 +208,7 @@ pub fn getLineInfo(fd: std.os.fd_t, offset: u32) !LineInfo {
/// Executes `GPIO_V2_GET_LINEINFO_WATCH_IOCTL` on the given fd and returns the resulting /// Executes `GPIO_V2_GET_LINEINFO_WATCH_IOCTL` on the given fd and returns the resulting
/// `LineInfo` value /// `LineInfo` value
pub fn watchLineInfo(fd: std.os.fd_t, offset: u32) !LineInfo { pub fn watchLineInfo(fd: std.posix.fd_t, offset: u32) !LineInfo {
const req = std.os.linux.IOCTL.IOWR(0xB4, 0x06, LineInfo); const req = std.os.linux.IOCTL.IOWR(0xB4, 0x06, LineInfo);
var info = std.mem.zeroes(LineInfo); var info = std.mem.zeroes(LineInfo);
info.offset = offset; info.offset = offset;
@ -217,14 +217,14 @@ pub fn watchLineInfo(fd: std.os.fd_t, offset: u32) !LineInfo {
} }
/// Executes `GPIO_GET_LINEINFO_UNWATCH_IOCTL` on the given fd /// Executes `GPIO_GET_LINEINFO_UNWATCH_IOCTL` on the given fd
pub fn unwatchLineInfo(fd: std.os.fd_t, offset: u32) !void { pub fn unwatchLineInfo(fd: std.posix.fd_t, offset: u32) !void {
const req = std.os.linux.IOCTL.IOWR(0xB4, 0x0C, u32); const req = std.os.linux.IOCTL.IOWR(0xB4, 0x0C, u32);
try handleErrno(std.os.linux.ioctl(fd, req, @intFromPtr(&offset))); try handleErrno(std.os.linux.ioctl(fd, req, @intFromPtr(&offset)));
} }
/// Executes `GPIO_V2_GET_LINE_IOCTL` on the given fd and returns the resulting /// Executes `GPIO_V2_GET_LINE_IOCTL` on the given fd and returns the resulting
/// line descriptor /// line descriptor
pub fn getLine(fd: std.os.fd_t, lr: LineRequest) !std.os.fd_t { pub fn getLine(fd: std.posix.fd_t, lr: LineRequest) !std.posix.fd_t {
const lrp = &lr; const lrp = &lr;
const req = std.os.linux.IOCTL.IOWR(0xB4, 0x07, LineRequest); const req = std.os.linux.IOCTL.IOWR(0xB4, 0x07, LineRequest);
try handleErrno(std.os.linux.ioctl(fd, req, @intFromPtr(lrp))); try handleErrno(std.os.linux.ioctl(fd, req, @intFromPtr(lrp)));
@ -233,7 +233,7 @@ pub fn getLine(fd: std.os.fd_t, lr: LineRequest) !std.os.fd_t {
/// Executes `GPIO_V2_LINE_GET_VALUES_IOCTL` on the given fd with the given mask, /// Executes `GPIO_V2_LINE_GET_VALUES_IOCTL` on the given fd with the given mask,
/// and returns a bitset representing all the line values. /// and returns a bitset representing all the line values.
pub fn getLineValues(fd: std.os.fd_t, mask: LineValueBitset) !LineValueBitset { pub fn getLineValues(fd: std.posix.fd_t, mask: LineValueBitset) !LineValueBitset {
var vals = LineValues{ .mask = mask }; var vals = LineValues{ .mask = mask };
const req = std.os.linux.IOCTL.IOWR(0xB4, 0x0E, LineValues); const req = std.os.linux.IOCTL.IOWR(0xB4, 0x0E, LineValues);
try handleErrno(std.os.linux.ioctl(fd, req, @intFromPtr(&vals))); try handleErrno(std.os.linux.ioctl(fd, req, @intFromPtr(&vals)));
@ -241,13 +241,13 @@ pub fn getLineValues(fd: std.os.fd_t, mask: LineValueBitset) !LineValueBitset {
} }
/// Executes `GPIO_V2_LINE_SET_VALUES_IOCTL` on the given fd /// Executes `GPIO_V2_LINE_SET_VALUES_IOCTL` on the given fd
pub fn setLineValues(fd: std.os.fd_t, lv: LineValues) !void { pub fn setLineValues(fd: std.posix.fd_t, lv: LineValues) !void {
const req = std.os.linux.IOCTL.IOWR(0xB4, 0x0F, LineValues); const req = std.os.linux.IOCTL.IOWR(0xB4, 0x0F, LineValues);
try handleErrno(std.os.linux.ioctl(fd, req, @intFromPtr(&lv))); try handleErrno(std.os.linux.ioctl(fd, req, @intFromPtr(&lv)));
} }
/// Executes `GPIO_V2_LINE_SET_CONFIG_IOCTL` on the given fd /// Executes `GPIO_V2_LINE_SET_CONFIG_IOCTL` on the given fd
pub fn setLineConfig(fd: std.os.fd_t, lc: LineConfig) !void { pub fn setLineConfig(fd: std.posix.fd_t, lc: LineConfig) !void {
const req = std.os.linux.IOCTL.IOWR(0xB4, 0x0D, LineConfig); const req = std.os.linux.IOCTL.IOWR(0xB4, 0x0D, LineConfig);
try handleErrno(std.os.linux.ioctl(fd, req, @intFromPtr(&lc))); try handleErrno(std.os.linux.ioctl(fd, req, @intFromPtr(&lc)));
} }