Files
2026-09-22 18:48:00 +08:00

53 lines
1.3 KiB
Python

"""Download and extract Zig for this operating system."""
import os
import platform
from pathlib import Path
from fetch import ensure_archive, extract_archive
ROOT = Path(__file__).resolve().parents[1]
TOOLS = ROOT / "tools"
VERSION = "0.16.0"
RELEASE = f"https://github.com/ziglang/zig/releases/tag/{VERSION}"
ARCH = {
"amd64": "x86_64",
"x86_64": "x86_64",
"arm64": "aarch64",
"aarch64": "aarch64",
}
OS_FILE = {
"windows": ("windows", "zip", "zig.exe"),
"darwin": ("macos", "tar.xz", "zig"),
"linux": ("linux", "tar.xz", "zig"),
}
def main() -> None:
system = platform.system().lower()
arch = ARCH.get(platform.machine().lower())
spec = OS_FILE.get(system)
if not arch or not spec:
print(RELEASE)
raise SystemExit(2)
os_name, ext, binary = spec
folder = f"zig-{arch}-{os_name}-{VERSION}"
if any(TOOLS.glob(f"zig*/{binary}")):
return
url = f"https://ziglang.org/download/{VERSION}/{folder}.{ext}"
archive = TOOLS / f"{folder}.{ext}"
print(url, flush=True)
ensure_archive(url, archive)
extract_archive(archive, TOOLS)
found = sorted(TOOLS.glob(f"zig*/{binary}"))
if not found:
raise SystemExit(2)
if os.name != "nt":
for path in found:
path.chmod(0o755)
if __name__ == "__main__":
main()