-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInstall.py
More file actions
executable file
·288 lines (236 loc) · 8.22 KB
/
Copy pathInstall.py
File metadata and controls
executable file
·288 lines (236 loc) · 8.22 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env python3
"""Install dependencies for the C compiler."""
import argparse
import os
import shutil
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parent
REQUIREMENTS = ROOT / "requirements.txt"
MIN_PYTHON = (3, 8)
PYTHON_PACKAGES = [
("pycparser", "pycparser"),
("pycparser-fake-libc", "pycparser_fake_libc"),
("matplotlib", "matplotlib"),
]
SYSTEM_TOOLS = [
{
"name": "assembler",
"commands": ("nasm", "yasm"),
"required": True,
"packages": {
"apt": ("nasm",),
"dnf": ("nasm",),
"yum": ("nasm",),
"pacman": ("nasm",),
},
},
{
"name": "linker",
"commands": ("ld.gold", "ld"),
"required": True,
"packages": {
"apt": ("binutils",),
"dnf": ("binutils",),
"yum": ("binutils",),
"pacman": ("binutils",),
},
},
{
"name": "qemu user mode",
"commands": ("qemu-x86_64",),
"required": False,
"packages": {
"apt": ("qemu-user",),
"dnf": ("qemu-user",),
"yum": ("qemu-user",),
"pacman": ("qemu-user",),
},
},
{
"name": "qemu system mode",
"commands": ("qemu-system-x86_64",),
"required": False,
"packages": {
"apt": ("qemu-system-x86",),
"dnf": ("qemu-system-x86",),
"yum": ("qemu-system-x86",),
"pacman": ("qemu-system-x86",),
},
},
]
def check_python_version():
if sys.version_info < MIN_PYTHON:
version = ".".join(str(part) for part in MIN_PYTHON)
print(f"Error: Python {version}+ is required (found {sys.version.split()[0]})", file=sys.stderr)
return False
return True
def command_available(command):
return shutil.which(command) is not None
def detect_package_manager():
for manager, command in (
("pacman", "pacman"),
("apt", "apt-get"),
("dnf", "dnf"),
("yum", "yum"),
):
if command_available(command):
return manager
return None
def run_command(command, check=True):
print(f"$ {' '.join(command)}")
return subprocess.run(command, check=check)
def install_python_packages(break_system_packages=False):
if not REQUIREMENTS.exists():
print(f"Error: requirements file not found: {REQUIREMENTS}", file=sys.stderr)
return False
print("Installing Python dependencies...")
pip_command = [sys.executable, "-m", "pip", "install", "-r", str(REQUIREMENTS)]
if break_system_packages:
pip_command.append("--break-system-packages")
try:
run_command(pip_command)
except subprocess.CalledProcessError:
print("Error: failed to install Python dependencies", file=sys.stderr)
return False
missing = []
for package_name, import_name in PYTHON_PACKAGES:
try:
__import__(import_name)
except ImportError:
missing.append(package_name)
if missing:
print(f"Error: missing Python packages after install: {', '.join(missing)}", file=sys.stderr)
return False
print("Python dependencies installed.")
return True
def tool_status(tool):
for command in tool["commands"]:
if command_available(command):
return True, command
return False, None
def check_system_tools():
missing_required = []
missing_optional = []
print("Checking system tools...")
for tool in SYSTEM_TOOLS:
ok, command = tool_status(tool)
label = tool["name"]
if ok:
print(f" ✓ {label}: {command}")
continue
print(f" ✗ {label}: not found")
if tool["required"]:
missing_required.append(tool)
else:
missing_optional.append(tool)
return missing_required, missing_optional
def install_system_packages(tools, package_manager):
packages = []
for tool in tools:
tool_packages = tool["packages"].get(package_manager, ())
packages.extend(tool_packages)
if not packages:
return False
unique_packages = []
seen = set()
for package in packages:
if package not in seen:
seen.add(package)
unique_packages.append(package)
if package_manager == "apt":
command = ["sudo", "apt-get", "install", "-y", *unique_packages]
elif package_manager == "pacman":
command = ["sudo", "pacman", "-S", "--needed", "--noconfirm", *unique_packages]
else:
command = ["sudo", package_manager, "install", "-y", *unique_packages]
try:
run_command(command)
except subprocess.CalledProcessError:
print("Error: failed to install system packages", file=sys.stderr)
return False
return True
def print_manual_system_instructions(tools, package_manager):
if not tools:
return
print("\nInstall missing system tools manually:")
for tool in tools:
packages = tool["packages"].get(package_manager or "apt", ())
package_list = " ".join(packages)
commands = " or ".join(tool["commands"])
print(f" {tool['name']} ({commands})")
if package_manager == "apt":
print(f" sudo apt-get install {package_list}")
elif package_manager == "pacman":
print(f" sudo pacman -S --needed {package_list}")
elif package_manager in ("dnf", "yum"):
print(f" sudo {package_manager} install {package_list}")
else:
print(f" install packages: {package_list}")
def parse_args():
parser = argparse.ArgumentParser(description="Install dependencies for the C compiler")
parser.add_argument(
"--check-only",
action="store_true",
help="Check dependencies without installing anything",
)
parser.add_argument(
"--system-deps",
action="store_true",
help="Attempt to install missing system tools with the detected package manager",
)
parser.add_argument(
"--skip-python",
action="store_true",
help="Skip installing Python packages",
)
parser.add_argument(
"--break-system-packages",
action="store_true",
help="Pass --break-system-packages to pip (for externally-managed Python environments)",
)
return parser.parse_args()
def main():
args = parse_args()
if not check_python_version():
return 1
os.chdir(ROOT)
if not args.check_only and not args.skip_python:
if not install_python_packages(break_system_packages=args.break_system_packages):
return 1
elif not args.skip_python:
missing = []
for package_name, import_name in PYTHON_PACKAGES:
try:
__import__(import_name)
except ImportError:
missing.append(package_name)
if missing:
print(f"Missing Python packages: {', '.join(missing)}")
return 1
print("Python dependencies OK.")
missing_required, missing_optional = check_system_tools()
package_manager = detect_package_manager()
if args.system_deps and (missing_required or missing_optional):
if package_manager is None:
print("Error: no supported package manager found (pacman, apt-get, dnf, or yum)", file=sys.stderr)
print_manual_system_instructions(missing_required + missing_optional, None)
return 1
print(f"\nInstalling system packages via {package_manager}...")
if not install_system_packages(missing_required + missing_optional, package_manager):
return 1
missing_required, missing_optional = check_system_tools()
if missing_required:
print("\nRequired system tools are still missing.", file=sys.stderr)
print_manual_system_instructions(missing_required, package_manager)
return 1
if missing_optional:
print("\nOptional tools not installed (needed only for QEMU testing).")
print_manual_system_instructions(missing_optional, package_manager)
print("\nInstallation complete.")
print("Compile C code with:")
print(" python3 compiler.py input.c -o output.asm")
return 0
if __name__ == "__main__":
sys.exit(main())