Files
astromaddie--HaloCE-VR/tools/android_posix_stubs.py
T
2026-09-26 16:16:00 +12:00

64 lines
2.2 KiB
Python

#!/usr/bin/env python3
"""Generate the Android guest's side of port/linux/src/posix.h.
The posix_*.c helpers run in the Android host (they are the platform
layer's only contact with the C library's file system and sockets). Their
interface is 32-bit clean, so the guest can call them directly, except for
errno: the host's is not the guest's. Each generated wrapper calls the host
function through an import named hostposix_<name> and copies the host's
errno into the guest's afterwards.
Usage: android_posix_stubs.py posix.h output.c imports.list
"""
import re
import sys
def main():
header, output, imports = sys.argv[1:4]
text = open(header, encoding="utf-8").read()
text = re.sub(r"/\*.*?\*/", "", text, flags=re.S)
prototypes = re.findall(r"^([A-Za-z_][\w \*]*?)\b(posix_\w+)\s*\(([^;]*?)\);", text, re.M | re.S)
out = [
"/* generated by tools/android_posix_stubs.py from port/linux/src/posix.h; do not edit */",
"#include <errno.h>",
'#include "posix.h"',
"",
"int host_errno(void);",
"",
]
names = []
for ret, name, params in prototypes:
ret = " ".join(ret.split())
params = " ".join(params.split())
args = []
if params != "void":
for param in params.split(","):
m = re.search(r"(\w+)\s*$", param.strip())
args.append(m.group(1))
host_name = "hostposix_" + name[len("posix_"):]
names.append(host_name)
out.append(f"{ret} {host_name}({params});")
out.append(f"{ret} {name}({params})")
out.append("{")
call = f"{host_name}({', '.join(args)})"
if ret == "void":
out.append(f"\t{call};")
out.append("\terrno = host_errno();")
else:
out.append(f"\t{ret} host_result = {call};")
out.append("")
out.append("\terrno = host_errno();")
out.append("\treturn host_result;")
out.append("}")
out.append("")
with open(output, "w", encoding="utf-8") as f:
f.write("\n".join(out))
with open(imports, "w", encoding="utf-8") as f:
f.write("\n".join(names) + "\n")
if __name__ == "__main__":
main()