oscall 0.1.0

Low-level cross-platform interface to operating system services: files, memory mapping, networking and more, with no libc on Linux
Documentation
#!/usr/bin/env python3
"""Generate src/sys/linux/nr/*.rs, the system call numbers of each
architecture, from the tables of a Linux kernel source tree. They are public
as `oscall::raw::linux::nr`.

    scripts/gen-syscalls.py /usr/src/linux
"""
import keyword
import pathlib
import sys

# architecture -> (table, ABI columns to keep, offset)
ARCHS = {
    "x86_64": ("arch/x86/entry/syscalls/syscall_64.tbl", {"common", "64"}),
    "x86": ("arch/x86/entry/syscalls/syscall_32.tbl", {"i386"}),
    "arm": ("arch/arm/tools/syscall.tbl", {"common", "eabi"}),
    "aarch64": (
        "scripts/syscall.tbl",
        {"common", "64", "renameat", "rlimit", "memfd_secret"},
    ),
    "riscv64": (
        "scripts/syscall.tbl",
        {"common", "64", "riscv", "rlimit", "memfd_secret"},
    ),
    "riscv32": ("scripts/syscall.tbl", {"common", "32", "riscv", "memfd_secret"}),
}


def main() -> None:
    kernel = pathlib.Path(sys.argv[1])
    out = pathlib.Path(__file__).resolve().parent.parent / "src/sys/linux/nr"
    version = {}
    for line in (kernel / "Makefile").read_text().splitlines()[:6]:
        if " = " in line:
            key, value = line.split(" = ", 1)
            version[key] = value
    release = f"{version['VERSION']}.{version['PATCHLEVEL']}"
    for arch, (table, abis) in ARCHS.items():
        lines = [
            f"//! System call numbers for {arch}, from Linux {release}.",
            "//!",
            "//! Generated by `scripts/gen-syscalls.py`: do not edit.",
            "",
        ]
        seen = set()
        for row in (kernel / table).read_text().splitlines():
            fields = row.split()
            if len(fields) < 3 or not fields[0].isdigit() or fields[1] not in abis:
                continue
            name = fields[2].upper()
            if name in seen:
                continue
            seen.add(name)
            assert not keyword.iskeyword(name)
            lines.append(f"/// `{fields[2]}`.")
            lines.append(f"pub const {name}: usize = {fields[0]};")
        (out / f"{arch}.rs").write_text("\n".join(lines) + "\n")
        print(f"{arch}: {len(seen)} system calls")


if __name__ == "__main__":
    main()