-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlib.rs
More file actions
419 lines (379 loc) · 16.9 KB
/
lib.rs
File metadata and controls
419 lines (379 loc) · 16.9 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
use colored::*;
use std::io::{self, Write};
pub fn info(msg: &str) {
println!("{} {}", "[xtask]".green().bold(), msg);
}
pub fn error(msg: &str) {
println!("{} {}", "[xtask]".red().bold(), msg);
}
pub fn warning(msg: &str) {
println!("{} {}", "[xtask]".yellow().bold(), msg);
}
pub fn install_env(env: &str) {
match env {
"Python" => {
info("Checking if Python is already installed...");
println!();
// Check if python or python3 is installed
let check_installed = if cfg!(target_os = "windows") {
std::process::Command::new("python")
.arg("--version")
.stdout(std::process::Stdio::null())
.status()
.map(|s| s.success())
.unwrap_or(false)
} else {
std::process::Command::new("python3")
.arg("--version")
.stdout(std::process::Stdio::null())
.status()
.map(|s| s.success())
.unwrap_or(false)
};
if check_installed {
info("Python is already installed. No need to reinstall.");
return;
}
info("Python not detected. Installing Python environment...");
#[cfg(target_os = "windows")]
let output = std::process::Command::new("powershell")
.args(["-Command", "winget install -e --id Python.Python.3.11"])
.status();
#[cfg(not(target_os = "windows"))]
let output = {
// Detect package manager
let has_cmd = |cmd: &str| {
std::process::Command::new("sh")
.arg("-c")
.arg(format!("command -v {}", cmd))
.stdout(std::process::Stdio::null())
.status()
.map(|s| s.success())
.unwrap_or(false)
};
if has_cmd("apt-get") {
std::process::Command::new("sh")
.arg("-c")
.arg("sudo apt-get update && sudo apt-get install -y python3")
.status()
} else if has_cmd("dnf") {
std::process::Command::new("sh")
.arg("-c")
.arg("sudo dnf install -y python3")
.status()
} else if has_cmd("yum") {
std::process::Command::new("sh")
.arg("-c")
.arg("sudo yum install -y python3")
.status()
} else if has_cmd("pacman") {
std::process::Command::new("sh")
.arg("-c")
.arg("sudo pacman -Sy --noconfirm python")
.status()
} else if has_cmd("brew") {
std::process::Command::new("sh")
.arg("-c")
.arg("brew install python")
.status()
} else {
error(
"No supported package manager found (apt-get, dnf, yum, pacman). Please install Python manually.",
);
return;
}
};
match output {
Ok(status) if status.success() => info("Python installation completed!"),
Ok(status) => error(&format!(
"Python installation failed, exit code: {:?}",
status.code()
)),
Err(e) => error(&format!(
"Error occurred while running install command: {e}"
)),
}
}
"xmake" => {
info("Checking if xmake is already installed...");
println!();
// Check if xmake is installed
let check_installed = std::process::Command::new("xmake")
.arg("--version")
.stdout(std::process::Stdio::null())
.status()
.map(|s| s.success())
.unwrap_or(false);
if check_installed {
info("xmake is already installed. No need to reinstall.");
return;
}
info("xmake not detected. Installing xmake environment...");
#[cfg(target_os = "windows")]
let output = std::process::Command::new("powershell")
.args([
"-Command",
"winget install -e --id Xmake-io.Xmake --source winget",
])
.status();
#[cfg(not(target_os = "windows"))]
let output = std::process::Command::new("sh")
.arg("-c")
.arg("curl -fsSL https://xmake.io/shget.text | bash")
.status();
match output {
Ok(status) if status.success() => info("xmake installation completed!"),
Ok(status) => error(&format!(
"xmake installation failed, exit code: {:?}",
status.code()
)),
Err(e) => error(&format!(
"Error occurred while running install command: {e}"
)),
}
}
"CUDA" => {
info("Checking if CUDA Toolkit is already installed...");
println!();
// Check if cuda toolkit is installed
let check_installed = std::process::Command::new("nvcc")
.arg("--version")
.stdout(std::process::Stdio::null())
.status()
.map(|s| s.success())
.unwrap_or(false);
if check_installed {
info("CUDA Toolkit is already installed. No need to reinstall.");
return;
}
// nvidia-smi, get the highest CUDA version supported by the driver
let has_nvidia_smi = std::process::Command::new("nvidia-smi")
.stdout(std::process::Stdio::null())
.status()
.map(|s| s.success())
.unwrap_or(false);
if has_nvidia_smi {
let output = std::process::Command::new("nvidia-smi")
.output()
.expect("Failed to execute nvidia-smi");
let smi_info = String::from_utf8_lossy(&output.stdout);
for line in smi_info.lines() {
if line.contains("CUDA Version") {
info(&format!("Detected by nvidia-smi: {line}"));
}
if let Some(idx) = line.find("CUDA Version:") {
// extract the CUDA version number
let version_str = line[idx + "CUDA Version:".len()..]
.split_whitespace()
.next()
.unwrap_or("");
info(&format!(
"The highest CUDA version supported by your driver is {version_str}"
));
warning(
"You can also visit https://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html to find the CUDA version compatible with your GPU driver.",
);
}
}
info(
"Please make sure to install a CUDA Toolkit version compatible with your driver.",
);
} else {
error(
"nvidia-smi not found. Please make sure you have an NVIDIA GPU and drivers installed.",
);
return;
}
println!();
warning(
"Please visit https://developer.nvidia.com/cuda-toolkit-archive to select and download the appropriate CUDA version for your driver.",
);
}
"OpenCL" => {
warning(
"The current automatic installation script only supports OpenCL installation for Intel CPU on Windows or Ubuntu systems.",
);
warning("type 'y' to continue, or any other key to exit.");
let mut input = String::new();
io::stdout().flush().unwrap();
io::stdin()
.read_line(&mut input)
.expect("Failed to read input");
if input.trim().to_lowercase() != "y" {
info("Exiting OpenCL installation.");
return;
}
info("Checking if OpenCL is already installed...");
println!();
// Check if OpenCL is installed
#[cfg(target_os = "windows")]
{
let clinfo_path = std::path::Path::new("clinfo.exe");
if !clinfo_path.exists() {
info("Downloading clinfo tool...");
let download_status = std::process::Command::new("curl")
.args(["-o", "clinfo.exe", "https://github.com/ahoylabs/clinfo/releases/download/master-d2baa06/clinfo.exe"])
.status();
if let Err(e) = download_status {
error(&format!("Failed to download clinfo: {}", e));
warning("You may need to enable proxy.");
warning(
"You can also manually download from https://github.com/ahoylabs/clinfo/releases/download/master-d2baa06/clinfo.exe",
);
return;
}
}
let output = std::process::Command::new("clinfo.exe")
.output()
.expect("Failed to execute clinfo.exe");
let clinfo_output = String::from_utf8_lossy(&output.stdout);
if let Some(line) = clinfo_output
.lines()
.find(|line| line.contains("Number of platforms"))
{
if let Some(number) = line.split_whitespace().last() {
if number == "0" {
info("OpenCL is not installed.");
} else {
info(&format!(
"OpenCL is installed. Number of platforms: {}",
number
));
return;
}
} else {
error("Failed to parse the number of platforms.");
return;
}
} else {
error("Failed to find 'Number of platforms' in clinfo output.");
return;
}
}
#[cfg(not(target_os = "windows"))]
{
let has_cmd = |cmd: &str| {
std::process::Command::new("sh")
.arg("-c")
.arg(format!("command -v {}", cmd))
.stdout(std::process::Stdio::null())
.status()
.map(|s| s.success())
.unwrap_or(false)
};
if !has_cmd("clinfo") {
if has_cmd("apt") {
info("Installing clinfo tool...");
let install_status = std::process::Command::new("sh")
.arg("-c")
.arg("sudo apt update && sudo apt install clinfo -y")
.status();
if let Err(e) = install_status {
error(&format!("Failed to install clinfo: {}", e));
return;
}
} else {
error("Unsupported package manager. Please install clinfo manually.");
return;
}
}
let output = std::process::Command::new("clinfo")
.output()
.expect("Failed to execute clinfo");
let clinfo_output = String::from_utf8_lossy(&output.stdout);
if let Some(line) = clinfo_output
.lines()
.find(|line| line.contains("Number of platforms"))
{
if let Some(number) = line.split_whitespace().last() {
if number == "0" {
info("OpenCL is not installed.");
} else {
info(&format!(
"OpenCL is installed. Number of platforms: {}",
number
));
return;
}
} else {
error("Failed to parse the number of platforms.");
return;
}
} else {
error("Failed to find 'Number of platforms' in clinfo output.");
return;
}
}
info("OpenCL not detected. Installing OpenCL environment...");
#[cfg(target_os = "windows")]
{
let download_status = std::process::Command::new("curl")
.args(["-o", "w_opencl_runtime_p_2025.1.0.972.exe", "https://registrationcenter-download.intel.com/akdlm/IRC_NAS/b6dccdb7-b503-41ea-bd4b-a78e9c2d8dd6/w_opencl_runtime_p_2025.1.0.972.exe"])
.status();
if let Err(e) = download_status {
error(&format!(
"Failed to download w_opencl_runtime_p_2025.1.0.972: {}",
e
));
warning("You may need to enable proxy.");
warning(
"You can also manually download from https://registrationcenter-download.intel.com/akdlm/IRC_NAS/b6dccdb7-b503-41ea-bd4b-a78e9c2d8dd6/w_opencl_runtime_p_2025.1.0.972.exe",
);
return;
}
warning(
"Download completed. Please manually execute 'w_opencl_runtime_p_2025.1.0.972.exe' to install OpenCL for Intel CPU.",
);
}
#[cfg(not(target_os = "windows"))]
{
let has_cmd = |cmd: &str| {
std::process::Command::new("sh")
.arg("-c")
.arg(format!("command -v {}", cmd))
.stdout(std::process::Stdio::null())
.status()
.map(|s| s.success())
.unwrap_or(false)
};
if has_cmd("apt") {
info("Installing opencl-headers...");
let install_status = std::process::Command::new("sh")
.arg("-c")
.arg("sudo apt update && sudo apt install opencl-headers ocl-icd-opencl-dev -y")
.status();
if let Err(e) = install_status {
error(&format!("Failed to install OpenCL: {}", e));
return;
}
info("Installing Intel OpenCL runtime...");
let setup_status = std::process::Command::new("sh")
.arg("-c")
.arg(
"wget -O- https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB \
| gpg --dearmor | sudo tee /usr/share/keyrings/oneapi-archive-keyring.gpg > /dev/null && \
echo \"deb [signed-by=/usr/share/keyrings/oneapi-archive-keyring.gpg] https://apt.repos.intel.com/oneapi all main\" | sudo tee /etc/apt/sources.list.d/oneAPI.list && \
sudo apt update"
)
.status();
if let Err(e) = setup_status {
error(&format!("Failed to set up Intel OpenCL repository: {}", e));
return;
}
warning(
"Intel OpenCL runtime installation requires a proxy and may take time.",
);
warning(
"Please manually execute the following command after enabling the proxy:",
);
warning("sudo apt install -y intel-oneapi-runtime-opencl");
} else {
error("Unsupported package manager. Please install OpenCL manually.");
}
}
}
_ => error(&format!(
"Automatic installation for this environment is not supported: {env}"
)),
}
}