1#!/bin/bash
2
3if test -n "$ZSH_VERSION"; then
4  CURRENT_SHELL=zsh
5elif test -n "$BASH_VERSION"; then
6  CURRENT_SHELL=bash
7elif test -n "$KSH_VERSION"; then
8  CURRENT_SHELL=ksh
9elif test -n "$FCEDIT"; then
10  CURRENT_SHELL=ksh
11elif test -n "$PS3"; then
12  CURRENT_SHELL=unknown
13else
14  CURRENT_SHELL=sh
15fi
16
17source "$HOME/.$CURRENT_SHELL"rc
18
19emulator="qemu"
20defpackman="apt-get"
21dockerInstall="true"
22export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
23export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup
24
25banner()
26{
27	echo "|------------------------------------------|"
28	echo "|    Welcome to the DragonOS bootstrap     |"
29	echo "|------------------------------------------|"
30}
31
32# 因为编码原因, 只有在vim打开该文件的时候对齐才是真的对齐
33congratulations()
34{
35	echo "|-----------Congratulations!---------------|"
36	echo "|                                          |"
37	echo "|   你成功安装了DragonOS所需的依赖项!      |"
38    echo "|                                          |"
39    echo "|   请[关闭]当前终端, 并[重新打开]一个终端 |"
40	echo "|   然后通过以下命令运行:                  |"
41	echo "|                                          |"
42	echo "|                make run                  |"
43	echo "|                                          |"
44	echo "|------------------------------------------|"
45}
46
47####################################
48# 当检测到ubuntu或Debian时,执行此函数 #
49# 参数:第一个参数为包管理器            #
50####################################
51install_ubuntu_debian_pkg()
52{
53    echo "检测到 Ubuntu/Debian"
54	echo "正在更新包管理器的列表..."
55	sudo "$1" update
56	echo "正在安装所需的包..."
57    sudo "$1" install -y \
58        ca-certificates \
59        curl wget \
60        unzip \
61        gnupg \
62        lsb-release \
63        llvm-dev libclang-dev clang gcc-multilib \
64        gcc build-essential fdisk dosfstools dnsmasq bridge-utils iptables libssl-dev pkg-config \
65		sphinx
66	# 必须分开安装,否则会出现错误
67	sudo "$1" install -y \
68		gcc-riscv64-unknown-elf gcc-riscv64-linux-gnu gdb-multiarch
69
70	# 如果python3没有安装
71	if [ -z "$(which python3)" ]; then
72		echo "正在安装python3..."
73		sudo apt install -y python3 python3-pip
74	fi
75
76    if [ -z "$(which docker)" ] && [ -n ${dockerInstall} ]; then
77        echo "正在安装docker..."
78        sudo apt install -y docker.io docker-compose
79		sudo groupadd docker
80		sudo usermod -aG docker $USER
81		sudo systemctl restart docker
82    elif [ -z ${dockerInstall} ]; then
83		echo "您传入--no-docker参数生效, 安装docker步骤被跳过."
84	elif [ -n "$(which docker)" ]; then
85        echo "您的计算机上已经安装了docker"
86    fi
87
88    if [ -z "$(which qemu-system-x86_64)" ]; then
89        echo "正在安装QEMU虚拟机..."
90        sudo $1 install -y qemu-system qemu-kvm
91    else
92        echo "QEMU已经在您的电脑上安装!"
93    fi
94
95}
96
97install_archlinux_pkg()
98{
99    pkgman="pacman"
100    echo "检测到 ArchLinux"
101    echo "正在更新包管理器的列表..."
102    sudo "${pkgman}" -Sy
103    echo "正在安装所需的包..."
104    sudo "${pkgman}" -S --needed --noconfirm \
105	curl wget bridge-utils dnsmasq \
106        diffutils pkgconf which unzip util-linux dosfstools \
107        gcc make flex texinfo gmp mpfr qemu-base \
108        libmpc openssl
109
110}
111
112install_osx_pkg()
113{
114    echo "Detected OSX! 暂不支持Mac OSX的一键安装!"
115    exit 1
116}
117
118####################################################################################
119# This function takes care of everything associated to rust, and the version manager
120# That controls it, it can install rustup and uninstall multirust as well as making
121# sure that the correct version of rustc is selected by rustup
122####################################################################################
123rustInstall() {
124	# Check to see if multirust is installed, we don't want it messing with rustup
125	# In the future we can probably remove this but I believe it's good to have for now
126	if [ -e /usr/local/lib/rustlib/uninstall.sh ] ; then
127		echo "您的系统上似乎安装了multirust。"
128		echo "该工具已被维护人员弃用,并将导致问题。"
129		echo "如果您愿意,此脚本可以从系统中删除multirust"
130		printf "卸载 multirust (y/N):"
131		read multirust
132		if echo "$multirust" | grep -iq "^y" ;then
133			sudo /usr/local/lib/rustlib/uninstall.sh
134		else
135			echo "请手动卸载multistrust和任何其他版本的rust,然后重新运行bootstrap.sh"
136			exit
137		fi
138	fi
139	# If rustup is not installed we should offer to install it for them
140	if [ -z "$(which rustup)" ]; then
141		echo "您没有安装rustup,"
142		echo "我们强烈建议使用rustup, 是否要立即安装?"
143		echo "*WARNING* 这将会发起这样的一个命令 'curl | sh' "
144		printf "(y/N): "
145		read rustup
146		if echo "$rustup" | grep -iq "^y" ;then
147			#install rustup
148			curl https://sh.rustup.rs -sSf | sh -s -- --default-toolchain nightly
149			# You have to add the rustup variables to the $PATH
150			echo "export PATH=\"\$HOME/.cargo/bin:\$PATH\"" >> ~/.bashrc
151			# source the variables so that we can execute rustup commands in the current shell
152			source ~/.cargo/env
153			source "$HOME/.cargo/env"
154		else
155			echo "Rustup will not be installed!"
156		fi
157	fi
158	#
159	if [ -z "$(which rustc)" ]; then
160		echo "Rust 还未被安装"
161		echo "请再次运行脚本,接受rustup安装"
162		echo "或通过以下方式手动安装rustc(不推荐):"
163		echo "curl https://sh.rustup.rs -sSf | sh -s -- --default-toolchain nightly"
164		exit
165	else
166        echo "是否为Rust换源为国内镜像源?(Tuna)"
167		echo "如果您在国内,我们推荐您这样做,以提升网络速度。"
168		echo "*WARNING* 这将会替换原有的镜像源设置。"
169		printf "(y/N): "
170		read change_src
171		if echo "$change_src" | grep -iq "^y" ;then
172			touch ~/.cargo/config
173			bash change_rust_src.sh
174		else
175			echo "取消换源,您原有的配置不会被改变。"
176		fi
177        echo "正在安装DragonOS所需的rust组件...首次安装需要一些时间来更新索引,请耐心等待..."
178        cargo install cargo-binutils
179		rustup toolchain install nightly-2023-01-21-x86_64-unknown-linux-gnu
180		rustup toolchain install nightly-2023-08-15-x86_64-unknown-linux-gnu
181		rustup component add rust-src --toolchain nightly-2023-01-21-x86_64-unknown-linux-gnu
182		rustup component add rust-src --toolchain nightly-2023-08-15-x86_64-unknown-linux-gnu
183		rustup target add x86_64-unknown-none --toolchain nightly-2023-01-21-x86_64-unknown-linux-gnu
184		rustup target add x86_64-unknown-none --toolchain nightly-2023-08-15-x86_64-unknown-linux-gnu
185		rustup target add x86_64-unknown-linux-musl --toolchain nightly-2023-08-15-x86_64-unknown-linux-gnu
186
187		rustup toolchain install nightly-2023-01-21-riscv64gc-unknown-linux-gnu --force-non-host
188		rustup toolchain install nightly-2023-08-15-riscv64gc-unknown-linux-gnu --force-non-host
189		rustup target add riscv64gc-unknown-none-elf --toolchain nightly-2023-01-21-riscv64gc-unknown-linux-gnu
190		rustup target add riscv64imac-unknown-none-elf --toolchain nightly-2023-01-21-riscv64gc-unknown-linux-gnu
191		rustup target add riscv64gc-unknown-none-elf --toolchain nightly-2023-08-15-riscv64gc-unknown-linux-gnu
192		rustup target add riscv64imac-unknown-none-elf --toolchain nightly-2023-08-15-riscv64gc-unknown-linux-gnu
193
194		rustup component add rust-src --toolchain nightly-x86_64-unknown-linux-gnu
195		rustup component add rust-src
196        rustup component add llvm-tools-preview
197		rustup default nightly
198
199		echo "Rust已经成功的在您的计算机上安装!请运行 source ~/.cargo/env 以使rust在当前窗口生效!"
200	fi
201}
202
203####################################################################################
204# 初始化DragonOS的musl交叉编译工具链
205# 主要是把musl交叉编译工具链的rcrt1.o替换为crt1.o (因为rust的rcrt1.o会使用动态链接的解释器,但是DragonOS目前尚未把它加载进来)
206#
207# 为DragonOS开发应用的时候,请使用 `cargo +nightly-2023-08-15-x86_64-unknown-linux-gnu build --target x86_64-unknown-linux-musl` 来编译
208# 	这样编译出来的应用将能二进制兼容DragonOS
209####################################################################################
210initialize_userland_musl_toolchain()
211{
212	fork_toolchain_from="nightly-2023-08-15-x86_64-unknown-linux-gnu"
213	custom_toolchain="nightly-2023-08-15-x86_64-unknown-linux_dragonos-gnu"
214	custom_toolchain_dir="$(dirname $(rustc --print sysroot))/${custom_toolchain}"
215	# 如果目录为空
216	if [ ! -d "${custom_toolchain_dir}" ]; then
217		echo "Custom toolchain does not exist, creating..."
218		rustup toolchain install ${fork_toolchain_from}
219		rustup component add --toolchain ${fork_toolchain_from} rust-src
220		rustup target add --toolchain ${fork_toolchain_from} x86_64-unknown-linux-musl
221		cp -r $(dirname $(rustc --print sysroot))/${fork_toolchain_from} ${custom_toolchain_dir}
222		self_contained_dir=${custom_toolchain_dir}/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained
223		cp -f ${self_contained_dir}/crt1.o ${self_contained_dir}/rcrt1.o
224	else
225		echo "Custom toolchain already exists."
226	fi
227
228}
229
230
231install_python_pkg()
232{
233	echo "正在安装python依赖项..."
234	# 安装文档生成工具
235	sh -c "cd ../docs && pip3 install -r requirements.txt"
236}
237
238
239############# 脚本开始 ##############
240# 读取参数及选项,使用 -help 参数查看详细选项
241while true; do
242	if [ -z "$1" ]; then
243		break;
244	fi
245	echo "repeat"
246	case "$1" in
247		"--no-docker")
248			dockerInstall=""
249		;;
250		"--help")
251			echo "--no-docker(not install docker): 该参数表示执行该脚本的过程中不单独安装docker."
252			exit 0
253		;;
254		*)
255			echo "无法识别参数$1, 请传入 --help 参数查看提供的选项."
256		;;
257	esac
258	shift 1
259done
260
261############ 开始执行 ###############
262banner 			# 开始横幅
263
264if [ "Darwin" == "$(uname -s)" ]; then
265	install_osx_pkg "$emulator" || exit 1
266else
267	# Here we will use package managers to determine which operating system the user is using.
268
269	# Suse and derivatives
270	if hash 2>/dev/null zypper; then
271		suse "$emulator" || exit 1
272	# Debian or any derivative of it
273	elif hash 2>/dev/null apt-get; then
274		install_ubuntu_debian_pkg "$defpackman"  || exit 1
275	# Fedora
276	elif hash 2>/dev/null dnf; then
277		fedora "$emulator" || exit 1
278	# Gentoo
279	elif hash 2>/dev/null emerge; then
280		gentoo "$emulator" || exit 1
281	# SolusOS
282	elif hash 2>/dev/null eopkg; then
283		solus "$emulator" || exit 1
284	# Arch linux
285	elif hash 2>/dev/null pacman; then
286		install_archlinux_pkg || exit 1
287	# FreeBSD
288	elif hash 2>/dev/null pkg; then
289		freebsd "$emulator" || exit 1
290	# Unsupported platform
291	else
292    	printf "\e[31;1mFatal error: \e[0;31mUnsupported platform, please open an issue\[0m" || exit 1
293	fi
294fi
295
296# 安装rust
297rustInstall
298
299
300#  初始化DragonOS的musl交叉编译工具链
301initialize_userland_musl_toolchain
302install_python_pkg
303
304# 安装dadk
305cargo install dadk || exit 1
306
307bashpath=$(cd `dirname $0`; pwd)
308
309# 创建磁盘镜像
310bash ${bashpath}/create_hdd_image.sh
311# 编译安装GCC交叉编译工具链
312bash ${bashpath}/build_gcc_toolchain.sh -cs -kb -kg || (echo "GCC交叉编译工具链安装失败" && exit 1)
313# 编译安装musl交叉编译工具链
314bash ${bashpath}/install_musl_gcc.sh || (echo "musl交叉编译工具链安装失败" && exit 1)
315# 编译安装grub
316bash ${bashpath}/grub_auto_install.sh || (echo "grub安装失败" && exit 1)
317
318# 解决kvm权限问题
319USR=$USER
320sudo adduser $USR kvm
321sudo chown $USR /dev/kvm
322
323congratulations
324