-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.py
More file actions
103 lines (89 loc) · 2.74 KB
/
Copy pathbuild.py
File metadata and controls
103 lines (89 loc) · 2.74 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
#!/usr/bin/env python3
"""Self-compile asmpython for Windows and Linux."""
import os
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).parent.resolve()
SRC = ROOT / "asmpython" / "__main__.py"
OUT_WIN = ROOT / "build" / "asmpython.exe"
OUT_LIN = ROOT / "build" / "asmpython-linux"
def base_env() -> dict:
env = os.environ.copy()
env["PYTHONPATH"] = str(ROOT) + os.pathsep + env.get("PYTHONPATH", "")
return env
def run_windows() -> None:
print(f"Self-hosting (windows): compiling asmpython -> {OUT_WIN}")
r = subprocess.run(
[
sys.executable,
"-m",
"asmpython",
str(SRC),
"-o",
str(OUT_WIN),
"--target",
"windows",
"--nasm",
"nasm",
"--gcc",
"gcc",
],
env=base_env(),
)
if r.returncode != 0:
print("\nSelf-host build FAILED (windows).")
sys.exit(1)
print(f"Self-host build OK: {OUT_WIN}\n")
def run_linux() -> None:
print(f"Self-hosting (linux): compiling asmpython -> {OUT_LIN}")
# Convert Windows paths to WSL paths. Returns None when WSL is
# unavailable or the conversion fails so callers can skip gracefully.
def wsl_path(p: Path) -> str | None:
try:
# WSL 1 drops backslashes from subprocess args; use forward slashes.
arg = str(p).replace("\\", "/")
r = subprocess.run(
["wsl", "wslpath", "-u", arg],
capture_output=True, text=True,
)
val = r.stdout.strip()
return val if (r.returncode == 0 and val) else None
except (FileNotFoundError, OSError, ValueError):
return None
wsl_src = wsl_path(SRC)
if wsl_src is None:
print("WSL not available or wslpath failed — skipping Linux self-host build.\n")
return
wsl_out = wsl_path(OUT_LIN)
wsl_root = wsl_path(ROOT)
if not wsl_out or not wsl_root:
print("WSL path conversion failed — skipping Linux self-host build.\n")
return
r = subprocess.run([
"wsl",
"env",
f"PYTHONPATH={wsl_root}",
"python3",
"-m",
"asmpython",
wsl_src,
"-o",
wsl_out,
"--target",
"linux",
"--nasm",
"/usr/bin/nasm",
"--gcc",
"/usr/bin/gcc",
])
if r.returncode != 0:
print("\nSelf-host build FAILED (linux).")
sys.exit(1)
print(f"Self-host build OK: {OUT_LIN}\n")
OUT_WIN.parent.mkdir(exist_ok=True)
run_windows()
run_linux()
# Check if we are in a batch script
if not os.getenv("ASMPYTHON_BUILD_BATCH") == "1" and sys.stdin.isatty():
input("Press Enter to exit . . .")