Repository navigation
Expand file tree
/
Copy pathsetup.py
More file actions
152 lines (125 loc) · 4.89 KB
/
Copy pathsetup.py
File metadata and controls
152 lines (125 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# /// script
# requires-python = ">=3.11"
# dependencies = []
# ///
"""Download the self-contained build toolchain into ./toolchain (gitignored).
Pulls the original MSVC6 compiler and wibo's msvcrt support DLLs. URLs are
pinned here on purpose — we deliberately do NOT read decompme's values.yaml
at runtime. wibo must be installed separately and on PATH.
uv run setup.py # download anything missing
uv run setup.py --force # re-download everything
"""
from __future__ import annotations
import argparse
import shutil
import subprocess
import sys
import tarfile
import tempfile
import urllib.request
import zipfile
from pathlib import Path
ROOT = Path(__file__).resolve().parent
TOOLCHAIN = ROOT / "toolchain"
# --- Pinned URLs (sourced once from decompme/compilers values.yaml; not fetched at runtime) ---
MSVC6_URL = "https://github.com/OmniBlade/decomp.me/releases/download/msvcwin9x/msvc6.0.tar.gz"
MSVCRT_DLLS_URL = "https://files.decomp.dev/msvcrt_20251015.zip"
def download(url: str, dest: Path) -> None:
dest.parent.mkdir(parents=True, exist_ok=True)
print(f" ↓ {url}")
req = urllib.request.Request(url, headers={"User-Agent": "legoland-setup"})
with urllib.request.urlopen(req) as resp, open(dest, "wb") as out:
shutil.copyfileobj(resp, out)
def extract_into(archive: Path, dest: Path) -> None:
"""Extract archive into dest, flattening a single wrapping top-level dir if present."""
dest.mkdir(parents=True, exist_ok=True)
with tempfile.TemporaryDirectory() as td:
tmp = Path(td)
if archive.name.endswith((".tar.gz", ".tgz")):
with tarfile.open(archive) as tf:
tf.extractall(tmp) # noqa: S202 - trusted pinned source
elif archive.suffix == ".zip":
with zipfile.ZipFile(archive) as zf:
zf.extractall(tmp)
else:
raise ValueError(f"unknown archive type: {archive.name}")
entries = [p for p in tmp.iterdir() if not p.name.startswith(".")]
roots = [tmp / e.name for e in entries]
src = roots[0] if len(roots) == 1 and roots[0].is_dir() else tmp
for item in src.iterdir():
target = dest / item.name
if target.exists():
shutil.rmtree(target) if target.is_dir() else target.unlink()
shutil.move(str(item), str(target))
def step_msvc6(force: bool) -> None:
dest = TOOLCHAIN / "msvc6"
if dest.exists() and not force:
print("msvc6: present, skipping")
return
if force and dest.exists():
shutil.rmtree(dest)
print("msvc6: downloading + extracting")
with tempfile.NamedTemporaryFile(suffix=".tar.gz", delete=False) as tmp:
archive = Path(tmp.name)
try:
download(MSVC6_URL, archive)
extract_into(archive, dest)
finally:
archive.unlink(missing_ok=True)
def step_dlls(force: bool) -> None:
dest = TOOLCHAIN / "dlls"
if dest.exists() and not force:
print("dlls: present, skipping")
return
if force and dest.exists():
shutil.rmtree(dest)
print("dlls: downloading + extracting")
with tempfile.NamedTemporaryFile(suffix=".zip", delete=False) as tmp:
archive = Path(tmp.name)
try:
download(MSVCRT_DLLS_URL, archive)
extract_into(archive, dest)
finally:
archive.unlink(missing_ok=True)
def run(cmd: list[str], cwd: Path | None = None) -> None:
print(" $ " + " ".join(cmd))
subprocess.run(cmd, cwd=cwd, check=True)
def check_wibo() -> None:
"""Verify wibo is on PATH."""
wibo = shutil.which("wibo")
if wibo is None:
sys.exit("wibo: not found on PATH")
result = subprocess.run([wibo, "--version"], capture_output=True, text=True)
version_str = result.stdout.strip().split()[1] if result.stdout.strip() else "unknown"
print(f"wibo: {version_str} ({wibo})")
def write_clangd() -> None:
"""Write .clangd so the editor can parse the MSVC6 headers (machine-local path)."""
inc = TOOLCHAIN / "msvc6" / "Include"
(ROOT / ".clangd").write_text(
"CompileFlags:\n"
" Compiler: clang\n"
" Add:\n"
" - --target=i686-pc-windows-msvc\n"
" - -fms-extensions\n"
" - -fms-compatibility\n"
" - -fms-compatibility-version=12.00\n"
" - -fdeclspec\n"
" - -isystem\n"
f" - {inc}\n"
)
def main() -> None:
ap = argparse.ArgumentParser(description=__doc__)
ap.add_argument("--force", action="store_true", help="re-download everything")
args = ap.parse_args()
TOOLCHAIN.mkdir(exist_ok=True)
check_wibo()
step_msvc6(args.force)
step_dlls(args.force)
write_clangd()
print("\nToolchain ready under ./toolchain")
for sub in ("msvc6", "dlls"):
p = TOOLCHAIN / sub
n = sum(1 for _ in p.rglob("*")) if p.exists() else 0
print(f" {sub:8} {n} files")
if __name__ == "__main__":
main()