Repository navigation
Expand file tree
/
Copy pathinstall_deps.sh
More file actions
675 lines (610 loc) · 21.7 KB
/
Copy pathinstall_deps.sh
File metadata and controls
675 lines (610 loc) · 21.7 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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
#!/bin/bash
# -----------------------------------------------------------------------------------------------------------
# Copyright (c) 2025 Huawei Technologies Co., Ltd.
# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
# CANN Open Software License Agreement Version 2.0 (the "License").
# Please refer to the License for details. You may not use this file except in compliance with the License.
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
# See LICENSE in the root of the software repository for the full text of the License.
# -----------------------------------------------------------------------------------------------------------
# ============================================================================
set -euo pipefail
run_command() {
local cmd="$*"
local output
local exit_code
echo "Executing command: $cmd"
if output=$("$@" 2>&1); then
return 0
else
exit_code=$?
echo -e "\nCommand execution failed!"
echo -e "\nFailed command: $cmd"
echo -e "\nError output: $output"
echo -e "\nExit code: $exit_code"
exit $exit_code
fi
}
#* 如果有 sudo 则使用 sudo
if command -v sudo &>/dev/null && [ "$EUID" -ne 0 ]; then
try_sudo="sudo"
else
try_sudo=""
fi
version_ge() {
# Version comparison, format: xx.xx.xx
IFS='.' read -r -a curr_arr <<< "$1"
IFS='.' read -r -a req_arr <<< "$2"
for ((i=0; i<${#req_arr[@]}; i++)); do
curr=${curr_arr[i]:-0}
req=${req_arr[i]}
if (( curr > req )); then
return 0
elif (( curr < req )); then
return 1
fi
done
return 0
}
detect_os() {
# OS detection, supports debian (uses apt), rhel (uses dnf or yum), openEuler(uses dnf), macos
if [[ "$(uname -s)" == "Linux" ]]; then
if [[ -f /etc/debian_version ]]; then
OS="debian"
PKG_MANAGER="apt"
elif [[ -f /etc/redhat-release ]]; then
OS="rhel"
if command -v dnf &> /dev/null; then
PKG_MANAGER="dnf"
else
PKG_MANAGER="yum"
fi
elif [[ -f /etc/euleros-release ]] || [[ -f /etc/openEuler-release ]]; then
OS="euler"
if command -v dnf &> /dev/null; then
PKG_MANAGER="dnf"
else
PKG_MANAGER="yum"
fi
else
echo "Unsupported OS type, please install manually"
exit 1
fi
elif [[ "$(uname -s)" == "Darwin" ]]; then
OS="macos"
if ! command -v brew &> /dev/null; then
echo "Please install Homebrew first"
exit 1
fi
PKG_MANAGER="brew"
else
echo "Unsupported OS type"
exit 1
fi
#EulerOS(uses dnf) version >= 2.10
if [[ -f /etc/euleros-release ]]; then
euler_version=$(grep '^VERSION_ID=' /etc/os-release | cut -d'"' -f2)
if version_ge "2.10" "$euler_version"; then
echo "Unsupported EulerOS $euler_version (<2.10) type"
exit 1
fi
fi
}
# 帮助信息函数
show_help() {
cat << EOF
用法: bash $0 [选项]
选项说明:
-url "镜像地址 信任主机" 自定义 PyPI 镜像源(必须用引号包裹,地址与主机名用空格分隔)
-h 显示此帮助信息并退出
使用示例:
1. 使用默认镜像源(清华 → 华为自动切换)
bash $0
2. 使用自定义镜像源
bash $0 -url "https://repo.huaweicloud.com/repository/pypi/simple repo.huaweicloud.com"
3. 查看帮助
bash $0 -h
EOF
}
# 解析命令行参数
CUSTOM_MIRROR=()
while [[ $# -gt 0 ]]; do
case "$1" in
-url)
if [[ -n "$2" ]]; then
IFS=' ' read -ra CUSTOM_MIRROR <<< "$2"
shift 2
else
echo "错误:-url 参数必须跟随 镜像地址 和 信任host" >&2
exit 1
fi
;;
-h)
show_help
exit 0
;;
*)
echo "无效参数:$1" >&2
show_help
exit 1
;;
esac
done
# 1. 统一的依赖包列表
COMMON_PKGS=(
"numpy>=1.21.6,<2.0"
"sympy>=1.10.1"
"psutil>=5.9"
"scipy>=1.7.3"
"cloudpickle"
"ml-dtypes<0.6.0"
"tornado"
"absl-py"
"decorator>=5.1.0"
"attrs"
"jinja2"
"mpmath"
"packaging>=26.0"
"setuptools>=59.0.0"
)
# 2. 统一的 pip 公共参数
PIP_COMMON_ARGS=(
"--no-deps"
"--timeout=60"
)
# 3. 镜像源配置
MIRROR_TSINGHUA=("https://pypi.tuna.tsinghua.edu.cn/simple" "pypi.tuna.tsinghua.edu.cn")
MIRROR_HUAWEI=("https://repo.huaweicloud.com/repository/pypi/simple" "repo.huaweicloud.com")
# ==================== 逻辑区(勿动) ====================
# 封装安装函数:接收 镜像URL 和 信任Host 作为参数
install_with_mirror() {
local mirror_url=$1
local trusted_host=$2
local mirror_name=$3
echo "尝试使用 ${mirror_name} 安装..."
pip3 install "${COMMON_PKGS[@]}" \
-i "${mirror_url}" \
--trusted-host "${trusted_host}" \
"${PIP_COMMON_ARGS[@]}"
return $?
}
install_python_deps() {
echo -e "\n==== Installing CANN 8.5 required Python packages ===="
if python3 -c "import numpy" &>/dev/null; then
# Get current version
local current_ver
current_ver=$(python3 -c "import numpy; print(numpy.__version__)" 2>/dev/null)
if [ -n "$current_ver" ]; then
echo "numpy $current_ver already installed, skipping pinned version."
echo "To install the required Python dependencies, run 'pip3 install -r requirements.txt' in a virtual environment from the repository root."
return
fi
fi
# ============== 优先使用自定义镜像源 ==============
if [ ${#CUSTOM_MIRROR[@]} -ge 2 ]; then
local custom_url=${CUSTOM_MIRROR[0]}
local custom_host=${CUSTOM_MIRROR[1]}
echo -e "\n使用命令行传入的自定义镜像源安装..."
if install_with_mirror "${custom_url}" "${custom_host}" "自定义镜像源"; then
echo -e "\n✅ CANN Python 依赖安装完成(自定义镜像源)"
return
else
echo -e "\033[31m[错误] 自定义镜像源安装失败!\033[0m"
exit 1
fi
fi
# Let pip handle redundancy — it's safe and idempotent
# 1. 尝试清华镜像
if install_with_mirror "${MIRROR_TSINGHUA[@]}" "清华镜像源"; then
echo -e "\n✅ CANN Python 依赖安装完成(清华镜像)"
return
fi
echo -e "\033[33m[警告] 清华镜像安装失败,自动切换华为云镜像...\033[0m"
# 2. 尝试华为云镜像
if install_with_mirror "${MIRROR_HUAWEI[@]}" "华为云镜像源"; then
echo -e "\n✅ CANN Python 依赖安装完成(华为云镜像)"
return
fi
# 3. 全部失败
echo -e "\033[31m[错误] 所有镜像源均安装失败!请检查网络或手动安装\033[0m"
}
install_python() {
# Python version >= 3.7.0
echo -e "\n==== Checking Python ===="
local req_ver="3.7.0"
local curr_ver=""
if command -v python3 &> /dev/null; then
curr_ver=$(python3 --version 2>&1 | awk '{print $2}')
echo "Current Python version: $curr_ver"
if version_ge "$curr_ver" "$req_ver"; then
echo "Python version meets requirements"
return
fi
fi
echo "Installing Python..."
case "$OS" in
debian)
run_command $try_sudo $PKG_MANAGER update
run_command $try_sudo $PKG_MANAGER install -y python3 python3-pip python3-dev
;;
rhel)
if grep -q "release 7" /etc/redhat-release; then
run_command $try_sudo $PKG_MANAGER install -y centos-release-scl
run_command $try_sudo $PKG_MANAGER install -y rh-python38 rh-python38-python-devel
run_command source /opt/rh/rh-python38/enable
echo "Need to execute 'source /opt/rh/rh-python38/enable' to activate python3.8"
else
run_command $try_sudo $PKG_MANAGER install -y python3 python3-pip python3-devel
fi
;;
euler)
run_command $try_sudo $PKG_MANAGER install -y python3 python3-pip python3-devel
;;
macos)
run_command brew install python@3.11
echo 'export PATH="/usr/local/opt/python@3.11/bin:$PATH"' >> ~/.zshrc
run_command source ~/.zshrc
;;
esac
if command -v python3 &> /dev/null; then
curr_ver=$(python3 --version 2>&1 | awk '{print $2}')
if version_ge "$curr_ver" "$req_ver"; then
echo "Python installed successfully ($curr_ver)"
else
echo "Python version still doesn't meet requirements, please install manually"
exit 1
fi
else
echo "Python installation failed"
exit 1
fi
}
gcc_version() {
if [[ "$debian_13" == true ]]; then
local version
if version=$("$1" -dumpfullversion -dumpversion 2>/dev/null) && [[ -n "$version" ]]; then
echo "$version"
else
"$1" -dumpversion
fi
else
"$1" --version | head -n1 | awk '{print $3}'
fi
}
install_gcc() {
# GCC version >= 7.3.0
echo -e "\n==== Checking GCC ===="
local req_ver="7.3.0"
local gcc_curr_ver=""
local gxx_curr_ver=""
local debian_13=false
if [[ "$OS" == "debian" ]] && grep -Eq '^ID="?debian"?$' /etc/os-release && \
grep -Eq '^VERSION_ID="?13(\.[0-9]+)*"?$' /etc/os-release; then
debian_13=true
fi
if command -v gcc &> /dev/null; then
gcc_curr_ver=$(gcc_version gcc)
else
gcc_curr_ver="0.0.0"
fi
if command -v g++ &> /dev/null; then
gxx_curr_ver=$(gcc_version g++)
else
gxx_curr_ver="0.0.0"
fi
echo "Current GCC version: $gcc_curr_ver G++ version: $gxx_curr_ver"
if version_ge "$gcc_curr_ver" "$req_ver"; then
if version_ge "$gxx_curr_ver" "$req_ver"; then
echo "GCC ($gcc_curr_ver) and G++ ($gxx_curr_ver) version meets requirements."
return
fi
fi
echo "Installing GCC and G++..."
case "$OS" in
debian)
run_command $try_sudo $PKG_MANAGER update
if [[ "$debian_13" == true ]]; then
run_command $try_sudo $PKG_MANAGER install -y gcc g++
else
run_command $try_sudo $PKG_MANAGER install -y gcc-9 g++-9
run_command $try_sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90 \
--slave /usr/bin/g++ g++ /usr/bin/g++-9
fi
;;
rhel)
if grep -q "release 7" /etc/redhat-release; then
run_command $try_sudo $PKG_MANAGER install -y centos-release-scl
run_command $try_sudo $PKG_MANAGER install -y devtoolset-9-gcc devtoolset-9-gcc-c++
run_command source /opt/rh/devtoolset-9/enable
echo "Need to execute 'source /opt/rh/devtoolset-9/enable' to activate GCC9"
else
run_command $try_sudo $PKG_MANAGER install -y gcc gcc-c++
fi
;;
euler)
run_command $try_sudo $PKG_MANAGER install -y gcc gcc-c++
;;
macos)
if ! xcode-select -p &> /dev/null; then
xcode-select --install
fi
run_command brew install gcc@11
echo 'export CC=/usr/local/bin/gcc-11' >> ~/.zshrc
echo 'export CXX=/usr/local/bin/g++-11' >> ~/.zshrc
run_command source ~/.zshrc
;;
esac
if command -v gcc &> /dev/null && command -v g++ &> /dev/null; then
gcc_curr_ver=$(gcc_version gcc)
gxx_curr_ver=$(gcc_version g++)
if version_ge "$gcc_curr_ver" "$req_ver" && version_ge "$gxx_curr_ver" "$req_ver"; then
echo "GCC ($gcc_curr_ver) and G++ ($gxx_curr_ver) installed successfully."
else
echo "GCC version or G++ version still doesn't meet requirements, please install manually."
exit 1
fi
else
echo "GCC or G++ installation failed"
exit 1
fi
}
install_patch() {
echo -e "\n==== Checking and Installing patch ===="
# Check if 'patch' is already available in PATH
if command -v patch &> /dev/null; then
echo "patch is already installed."
return 0
fi
echo "patch not found. Installing..."
case "$OS" in
debian)
run_command $try_sudo "$PKG_MANAGER" update
run_command $try_sudo "$PKG_MANAGER" install -y patch
;;
rhel)
if grep -q "release 7" /etc/redhat-release 2>/dev/null; then
run_command $try_sudo "$PKG_MANAGER" install -y patch
else
run_command $try_sudo "$PKG_MANAGER" install -y patch
fi
;;
euler)
run_command $try_sudo "$PKG_MANAGER" install -y patch
;;
macos)
echo "macOS usually ships with 'patch'. If missing, install Xcode Command Line Tools:"
echo " xcode-select --install"
if ! xcode-select -p &> /dev/null; then
echo "ERROR: Xcode Command Line Tools not installed. Please run 'xcode-select --install' manually."
exit 1
else
echo "WARNING: 'patch' is missing despite Xcode CLI tools being present. Please investigate."
exit 1
fi
;;
*)
echo "Unsupported OS: $OS. Cannot auto-install patch."
exit 1
;;
esac
if command -v patch &> /dev/null; then
echo "patch installed successfully."
else
echo "ERROR: Failed to install patch. Please install it manually."
exit 1
fi
}
install_cmake() {
# CMake version >= 3.18.4
echo -e "\n==== Checking CMake ===="
local req_ver="3.18.4"
local curr_ver=""
if command -v cmake &> /dev/null; then
curr_ver=$(cmake --version | awk '/^cmake/ {print $3}')
echo "Current CMake version: $curr_ver"
if version_ge "$curr_ver" "$req_ver"; then
echo "CMake meets requirements"
return
fi
fi
echo "Installing CMake..."
case "$OS" in
debian)
if grep -q "Ubuntu 18.04" /etc/os-release; then
run_command wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc 2>/dev/null | gpg --dearmor - | $try_sudo tee /usr/share/keyrings/kitware-archive-keyring.gpg >/dev/null
run_command echo 'deb [signed-by=/usr/share/keyrings/kitware-archive-keyring.gpg] https://apt.kitware.com/ubuntu/ bionic main' | sudo tee /etc/apt/sources.list.d/kitware.list >/dev/null
run_command $try_sudo apt update
run_command $try_sudo apt install -y cmake make
else
run_command $try_sudo $PKG_MANAGER update
run_command $try_sudo $PKG_MANAGER install -y cmake make
fi
;;
rhel)
if grep -q "release 7" /etc/redhat-release; then
run_command $try_sudo $PKG_MANAGER install -y epel-release
run_command $try_sudo $PKG_MANAGER install -y cmake3 make
run_command $try_sudo ln -s /usr/bin/cmake3 /usr/bin/cmake
else
run_command $try_sudo $PKG_MANAGER install -y cmake make
fi
;;
euler)
run_command $try_sudo $PKG_MANAGER install -y cmake make
;;
macos)
run_command brew install cmake
;;
esac
if command -v cmake &> /dev/null; then
curr_ver=$(cmake --version | awk '/^cmake/ {print $3}')
if version_ge "$curr_ver" "$req_ver"; then
echo "CMake installed successfully ($curr_ver)"
else
echo "CMake version still doesn't meet requirements, please install manually"
exit 1
fi
else
echo "CMake installation failed"
exit 1
fi
}
install_pigz() {
# pigz version >= 2.4
echo -e "\n==== Checking pigz ===="
local req_ver="2.4"
local curr_ver=""
if command -v pigz &> /dev/null; then
curr_ver=$(pigz --version 2>&1 | awk '{print $2}')
echo "Current pigz version: $curr_ver"
if version_ge "$curr_ver" "$req_ver"; then
echo "pigz meets requirements"
return
fi
fi
read -p "Install pigz? [Y/n] " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Skipping pigz installation"
return
fi
echo "Installing pigz..."
case "$OS" in
debian|rhel|euler)
run_command $try_sudo $PKG_MANAGER install -y pigz
;;
macos)
run_command brew install pigz
;;
esac
if command -v pigz &> /dev/null; then
curr_ver=$(pigz --version 2>&1 | awk '{print $2}')
echo "pigz installed successfully ($curr_ver)"
else
echo "pigz installation failed, can be ignored"
fi
}
install_dos2unix() {
echo -e "\n==== Checking dos2unix ===="
if command -v dos2unix &> /dev/null; then
echo "dos2unix has been installed"
return
fi
echo "Installing dos2unix..."
case "$OS" in
debian|rhel|euler)
run_command $try_sudo $PKG_MANAGER install -y dos2unix
;;
macos)
run_command brew install dos2unix
;;
esac
if command -v dos2unix &> /dev/null; then
echo "dos2unix installed successfully"
else
echo "dos2unix installation failed"
exit 1
fi
}
install_googletest() {
# Recommended googletest version: release-1.11.0
echo -e "\n==== Checking googletest ===="
local req_ver="1.11.0"
local curr_ver=""
local gtest_src_dir="/usr/src/gtest"
if command -v pkg-config &> /dev/null && pkg-config --exists gtest; then
curr_ver=$(pkg-config --modversion gtest)
echo "Current googletest version: $curr_ver"
if version_ge "$curr_ver" "$req_ver"; then
echo "googletest meets requirements"
return
fi
fi
read -p "Install googletest? [Y/n] " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Skipping googletest installation"
return
fi
if ! command -v pkg-config &> /dev/null; then
echo "Installing pkg-config for googletest detection..."
case "$OS" in
debian)
run_command $try_sudo $PKG_MANAGER update
run_command $try_sudo $PKG_MANAGER install -y pkg-config
;;
rhel)
run_command $try_sudo $PKG_MANAGER install -y pkgconfig
;;
euler)
run_command $try_sudo $PKG_MANAGER install -y pkgconf-pkg-config
;;
macos)
run_command brew install pkgconf
;;
esac
if ! command -v pkg-config &> /dev/null; then
echo "pkg-config installation failed"
exit 1
fi
fi
echo "Installing googletest..."
case "$OS" in
debian)
# Install libgtest-dev
run_command $try_sudo $PKG_MANAGER install -y libgtest-dev
# Check if gtest source directory exists
if [ ! -d "$gtest_src_dir" ]; then
echo "googletest source directory not found: $gtest_src_dir"
echo "Attempting to reinstall libgtest-dev..."
run_command $try_sudo $PKG_MANAGER purge -y libgtest-dev
run_command $try_sudo $PKG_MANAGER install -y libgtest-dev
# Check directory again
if [ ! -d "$gtest_src_dir" ]; then
echo "Still cannot find $gtest_src_dir, please install manually:"
echo "1. Download source: wget https://github.com/google/googletest/archive/refs/tags/release-1.11.0.tar.gz"
echo "2. Extract and compile: tar -zxf release-1.11.0.tar.gz && cd googletest-release-1.11.0 && cmake . && make && sudo make install"
exit 1
fi
fi
# Force cmake execution in gtest source directory (even if cd fails)
echo "Entering $gtest_src_dir to compile..."
run_command $try_sudo cmake -S "$gtest_src_dir" -B "$gtest_src_dir/build"
run_command $try_sudo make -C "$gtest_src_dir/build"
run_command $try_sudo cp "$gtest_src_dir/build/lib/"*.a /usr/lib
;;
rhel|euler)
run_command $try_sudo $PKG_MANAGER install -y gtest gtest-devel
;;
macos)
run_command brew install googletest
echo 'export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH"'
;;
esac
if pkg-config --exists gtest; then
curr_ver=$(pkg-config --modversion gtest)
echo "googletest installed successfully ($curr_ver)"
else
echo "googletest installation failed"
exit 1
fi
}
main() {
echo "===================================================="
echo "Starting project dependency installation"
echo "===================================================="
detect_os
install_python
install_python_deps
install_gcc
install_patch
install_cmake
install_pigz
install_dos2unix
install_googletest
echo -e "===================================================="
echo "All dependencies installed successfully!"
echo "===================================================="
}
main "$@"