╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ generate_linux_syscalls.zig ──────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
196 // 32k should be enough for now. 197 const buf = try gpa.alloc(u8, 1 << 15); 198 defer gpa.free(buf); 199 200 // Fetch the kernel version from the Makefile variables. 201 const version = blk: { 202 const head = try linux_dir.readFile(io, "Makefile", buf[0..128]); 203 var lines = mem.tokenizeScalar(u8, head, '\n'); 204 _ = lines.next(); // Skip SPDX identifier 205 206 var ver = mem.zeroes(std.SemanticVersion); 207 inline for (.{ "major", "minor", "patch" }, .{ "VERSION", "PATCHLEVEL", "SUBLEVEL" }) |field, make_var| { 208 const line = lines.next() orelse @panic("Bad line"); 209 const offset = (make_var ++ " = ").len; 210 @field(ver, field) = try std.fmt.parseInt(usize, line[offset..], 10); 211 } 212 213 break :blk ver; 214 }; 215 216 try Io.Writer.print(stdout, 217 \\// This file is automatically generated by tools/generate_linux_syscalls.zig 218 \\// This list current as of kernel: {f} 219 \\ 220 \\ 221 , .{version}); 222 223 for (architectures, 0..) |arch, i| { 224 const table = try linux_dir.readFile(io, switch (arch.table) { 225 .generic => "scripts/syscall.tbl", 226 .specific => |f| f, 227 }, buf); 228 229 try Io.Writer.print(stdout, "pub const {s} = enum(usize) {{\n", .{arch.@"var"}); 230 if (arch.header) |h| 231 try Io.Writer.writeAll(stdout, h); 232 233 var lines = mem.tokenizeScalar(u8, table, '\n'); 234 while (lines.next()) |line| { 235 if (line[0] == '#') continue; 236 if (arch.get(line)) |res| { 237 const name, const num = res; 238 const final_name = stdlib_renames.get(name) orelse name; 239 try Io.Writer.print(stdout, " {f} = {d},\n", .{ std.zig.fmtId(final_name), num }); 240 } 241 } 242 243 if (arch.footer) |f| 244 try Io.Writer.writeAll(stdout, f); 245 try Io.Writer.writeAll(stdout, "};\n"); 246 if (i != architectures.len - 1) 247 try Io.Writer.writeByte(stdout, '\n'); 248 } 249 250 try Io.Writer.flush(stdout); 251 } 252 253 fn usage(w: *std.Io.Writer, arg0: []const u8) std.Io.Writer.Error!void { 254 try w.print( 255 \\Usage: {s} /path/to/linux 256 \\Alternative Usage: zig run /path/to/git/zig/tools/generate_linux_syscalls.zig -- /path/to/linux 257 \\ 258 \\Generates the list of Linux syscalls for each supported cpu arch, using the Linux development tree. 259 \\Prints to stdout Zig code which you can use to replace the file lib/std/os/linux/syscalls.zig. 260 \\ 261 , .{arg0}); 262 } 263