mirror of
https://github.com/DragonOS-Community/DragonOS.git
synced 2025-06-21 18:33:23 +00:00
新增rust ffi (#77)
* 引入cargo * 取消对Cargo.lock的跟踪 * 解决vscode报错问题 * new: rust的代码能够调用c语言的printk_color * 1、将原本run.sh的工作拆解,变为几个不同的make命令 2、在docker镜像中编译rust * 更改workflow * update workflow * new: 解决workflow无法通过编译的问题
This commit is contained in:
179
tools/bootstrap.sh
Normal file
179
tools/bootstrap.sh
Normal file
@ -0,0 +1,179 @@
|
||||
emulator="qemu"
|
||||
defpackman="apt-get"
|
||||
|
||||
banner()
|
||||
{
|
||||
echo "|------------------------------------------|"
|
||||
echo "| Welcome to the DragonOS bootstrap |"
|
||||
echo "|------------------------------------------|"
|
||||
}
|
||||
|
||||
congratulations()
|
||||
{
|
||||
echo "|-----------Congratulations!---------------|"
|
||||
echo "| |"
|
||||
echo "| 你成功安装了DragonOS所需的依赖项! |"
|
||||
echo "| 您可以通过以下命令运行它: |"
|
||||
echo "| |"
|
||||
echo "| make run-docker -j 你的cpu核心数 |"
|
||||
echo "| |"
|
||||
echo "|------------------------------------------|"
|
||||
}
|
||||
|
||||
####################################
|
||||
# 当检测到ubuntu或Debian时,执行此函数 #
|
||||
# 参数:第一个参数为包管理器 #
|
||||
####################################
|
||||
install_ubuntu_debian_pkg()
|
||||
{
|
||||
echo "检测到 Ubuntu/Debian"
|
||||
echo "正在更新包管理器的列表..."
|
||||
sudo "$1" update
|
||||
echo "正在安装所需的包..."
|
||||
sudo "$1" install -y \
|
||||
ca-certificates \
|
||||
curl \
|
||||
gnupg \
|
||||
lsb-release \
|
||||
llvm-dev libclang-dev clang gcc-multilib \
|
||||
gcc build-essential fdisk
|
||||
|
||||
if [ -z "$(which docker)" ]; then
|
||||
echo "正在安装docker..."
|
||||
exit 1
|
||||
sudo mkdir -p /etc/apt/keyrings
|
||||
curl -fsSL https://download.docker.com/linux/debian/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
|
||||
echo \
|
||||
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian \
|
||||
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
|
||||
sudo $1 update
|
||||
sudo "$1" install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
|
||||
else
|
||||
echo "您的计算机上已经安装了docker"
|
||||
fi
|
||||
|
||||
if [ -z "$(which qemu-system-x86_64)" ]; then
|
||||
echo "正在安装QEMU虚拟机..."
|
||||
sudo $1 install -y qemu qemu-system qemu-kvm
|
||||
else
|
||||
echo "QEMU已经在您的电脑上安装!"
|
||||
fi
|
||||
|
||||
}
|
||||
|
||||
install_osx_pkg()
|
||||
{
|
||||
echo "Detected OSX! 暂不支持Mac OSX的一键安装!"
|
||||
exit 1
|
||||
}
|
||||
|
||||
####################################################################################
|
||||
# This function takes care of everything associated to rust, and the version manager
|
||||
# That controls it, it can install rustup and uninstall multirust as well as making
|
||||
# sure that the correct version of rustc is selected by rustup
|
||||
####################################################################################
|
||||
rustInstall() {
|
||||
# Check to see if multirust is installed, we don't want it messing with rustup
|
||||
# In the future we can probably remove this but I believe it's good to have for now
|
||||
if [ -e /usr/local/lib/rustlib/uninstall.sh ] ; then
|
||||
echo "您的系统上似乎安装了multirust。"
|
||||
echo "该工具已被维护人员弃用,并将导致问题。"
|
||||
echo "如果您愿意,此脚本可以从系统中删除multirust"
|
||||
printf "卸载 multirust (y/N):"
|
||||
read multirust
|
||||
if echo "$multirust" | grep -iq "^y" ;then
|
||||
sudo /usr/local/lib/rustlib/uninstall.sh
|
||||
else
|
||||
echo "请手动卸载multistrust和任何其他版本的rust,然后重新运行bootstrap.sh"
|
||||
exit
|
||||
fi
|
||||
fi
|
||||
# If rustup is not installed we should offer to install it for them
|
||||
if [ -z "$(which rustup)" ]; then
|
||||
echo "您没有安装rustup,"
|
||||
echo "我们强烈建议使用rustup, 是否要立即安装?"
|
||||
echo "*WARNING* 这将会发起这样的一个命令 'curl | sh' "
|
||||
printf "(y/N): "
|
||||
read rustup
|
||||
if echo "$rustup" | grep -iq "^y" ;then
|
||||
#install rustup
|
||||
curl https://sh.rustup.rs -sSf | sh -s -- --default-toolchain nightly
|
||||
# You have to add the rustup variables to the $PATH
|
||||
echo "export PATH=\"\$HOME/.cargo/bin:\$PATH\"" >> ~/.bashrc
|
||||
# source the variables so that we can execute rustup commands in the current shell
|
||||
source ~/.cargo/env
|
||||
source "$HOME/.cargo/env"
|
||||
else
|
||||
echo "Rustup will not be installed!"
|
||||
fi
|
||||
fi
|
||||
#
|
||||
if [ -z "$(which rustc)" ]; then
|
||||
echo "Rust 还未被安装"
|
||||
echo "请再次运行脚本,接受rustup安装"
|
||||
echo "或通过以下方式手动安装rustc(不推荐):"
|
||||
echo "curl -sSf https://static.rust-lang.org/rustup.sh | sh -s -- --channel=nightly"
|
||||
exit
|
||||
else
|
||||
echo "是否为Rust换源为Gitee镜像源?"
|
||||
echo "如果您在国内,我们推荐您这样做,以提升网络速度。"
|
||||
echo "*WARNING* 这将会替换原有的镜像源设置。"
|
||||
printf "(y/N): "
|
||||
read change_src
|
||||
if echo "$change_src" | grep -iq "^y" ;then
|
||||
touch ~/.cargo/config
|
||||
bash change_rust_src.sh
|
||||
else
|
||||
echo "取消换源,您原有的配置不会被改变。"
|
||||
fi
|
||||
echo "正在安装DragonOS所需的rust组件...首次安装需要一些时间来更新索引,请耐心等待..."
|
||||
cargo install cargo-binutils
|
||||
rustup toolchain install nightly
|
||||
rustup default nightly
|
||||
rustup component add rust-src
|
||||
rustup component add llvm-tools-preview
|
||||
rustup target add x86_64-unknown-none
|
||||
echo "Rust已经成功的在您的计算机上安装!请运行 source ~/.cargo/env 以使rust在当前窗口生效!"
|
||||
fi
|
||||
}
|
||||
|
||||
############ 开始执行 ###############
|
||||
banner
|
||||
rustInstall
|
||||
exit
|
||||
if [ "Darwin" == "$(uname -s)" ]; then
|
||||
install_osx_pkg "$emulator" || exit 1
|
||||
else
|
||||
# Here we will use package managers to determine which operating system the user is using.
|
||||
|
||||
# Suse and derivatives
|
||||
if hash 2>/dev/null zypper; then
|
||||
suse "$emulator" || exit 1
|
||||
# Debian or any derivative of it
|
||||
elif hash 2>/dev/null apt-get; then
|
||||
install_ubuntu_debian_pkg "$defpackman" || exit 1
|
||||
# Fedora
|
||||
elif hash 2>/dev/null dnf; then
|
||||
fedora "$emulator" || exit 1
|
||||
# Gentoo
|
||||
elif hash 2>/dev/null emerge; then
|
||||
gentoo "$emulator" || exit 1
|
||||
# SolusOS
|
||||
elif hash 2>/dev/null eopkg; then
|
||||
solus "$emulator" || exit 1
|
||||
# Arch linux
|
||||
elif hash 2>/dev/null pacman; then
|
||||
archLinux "$emulator" || exit 1
|
||||
# FreeBSD
|
||||
elif hash 2>/dev/null pkg; then
|
||||
freebsd "$emulator" || exit 1
|
||||
# Unsupported platform
|
||||
else
|
||||
printf "\e[31;1mFatal error: \e[0;31mUnsupported platform, please open an issue\[0m" || exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# 创建磁盘镜像
|
||||
sudo bash create_hdd_image.sh
|
||||
|
||||
congratulations
|
@ -1,7 +1,10 @@
|
||||
docker rm -f dragonos-build || echo "No existed container"
|
||||
p=`pwd`
|
||||
cpu_count=$(cat /proc/cpuinfo |grep "processor"|wc -l)
|
||||
docker run --rm --privileged=true --cap-add SYS_ADMIN --cap-add MKNOD -v $p:/data -v /dev:/dev --name dragonos-build -i dragonos/dragonos-dev:v1.0 bash << EOF
|
||||
docker run --rm --privileged=true --cap-add SYS_ADMIN --cap-add MKNOD -v $p:/data -v /dev:/dev -v dragonos-build-cargo:/root/.cargo/registry --name dragonos-build -i dragonos/dragonos-dev:v1.1.0-beta3 bash << EOF
|
||||
source ~/.cargo/env
|
||||
cd /data
|
||||
bash run.sh --current_in_docker
|
||||
# Change rust src
|
||||
bash tools/change_rust_src.sh
|
||||
make all -j $cpu_count && make write_diskimage
|
||||
EOF
|
8
tools/change_rust_src.sh
Normal file
8
tools/change_rust_src.sh
Normal file
@ -0,0 +1,8 @@
|
||||
# 更换Rust镜像源
|
||||
echo -e "[source.crates-io] \n \
|
||||
registry = \"https://github.com/rust-lang/crates.io-index\" \n \
|
||||
\n \
|
||||
replace-with = 'dragonos-gitee' \n \
|
||||
[source.dragonos-gitee] \n \
|
||||
registry = \"https://gitee.com/DragonOS/crates.io-index.git\" \n \
|
||||
" > ~/.cargo/config
|
36
tools/run-qemu.sh
Normal file
36
tools/run-qemu.sh
Normal file
@ -0,0 +1,36 @@
|
||||
# 进行启动前检查
|
||||
flag_can_run=1
|
||||
|
||||
|
||||
allflags=$(qemu-system-x86_64 -cpu help | awk '/flags/ {y=1; getline}; y {print}' | tr ' ' '\n' | grep -Ev "^$" | sed -r 's|^|+|' | tr '\n' ',' | sed -r "s|,$||")
|
||||
|
||||
# 请根据自己的需要,在-d 后方加入所需的trace事件
|
||||
|
||||
# 标准的trace events
|
||||
qemu_trace_std=cpu_reset,guest_errors,trace:check_exception,exec,cpu
|
||||
# 调试usb的trace
|
||||
qemu_trace_usb=trace:usb_xhci_reset,trace:usb_xhci_run,trace:usb_xhci_stop,trace:usb_xhci_irq_msi,trace:usb_xhci_irq_msix,trace:usb_xhci_port_reset,trace:msix_write_config,trace:usb_xhci_irq_msix,trace:usb_xhci_irq_msix_use,trace:usb_xhci_irq_msix_unuse,trace:usb_xhci_irq_msi,trace:usb_xhci_*
|
||||
|
||||
|
||||
qemu_accel=kvm
|
||||
if [ $(uname) == Darwin ]; then
|
||||
qemu_accel=hvf
|
||||
fi
|
||||
|
||||
if [ $flag_can_run -eq 1 ]; then
|
||||
|
||||
qemu-system-x86_64 -d ../bin/disk.img -m 512M -smp 2,cores=2,threads=1,sockets=1 \
|
||||
-boot order=d \
|
||||
-monitor stdio -d ${qemu_trace_std} \
|
||||
-s -S -cpu IvyBridge,apic,x2apic,+fpu,check,${allflags} -rtc clock=host,base=localtime -serial file:../serial_opt.txt \
|
||||
-drive id=disk,file=../bin/disk.img,if=none \
|
||||
-device ahci,id=ahci \
|
||||
-device ide-hd,drive=disk,bus=ahci.0 \
|
||||
-net nic,model=virtio \
|
||||
-usb \
|
||||
-device qemu-xhci,id=xhci,p2=8,p3=4 \
|
||||
-machine accel=${qemu_accel}
|
||||
|
||||
else
|
||||
echo "不满足运行条件"
|
||||
fi
|
79
tools/write_disk_image.sh
Normal file
79
tools/write_disk_image.sh
Normal file
@ -0,0 +1,79 @@
|
||||
ARCH="x86_64"
|
||||
# 内核映像
|
||||
root_folder=$(dirname $(pwd))
|
||||
kernel="${root_folder}/bin/kernel/kernel.elf"
|
||||
boot_folder="${root_folder}/bin/disk_mount/boot"
|
||||
|
||||
echo "开始写入磁盘镜像..."
|
||||
|
||||
|
||||
# toolchain
|
||||
OS=`uname -s`
|
||||
if [ "${OS}" == "Linux" ]; then
|
||||
GRUB_PATH="$(dirname $(which grub-file))"
|
||||
elif [ "${OS}" == "Darwin" ]; then
|
||||
GRUB_PATH="${root_folder}/tools/grub-2.06/build/grub/bin"
|
||||
fi
|
||||
export PATH="${GRUB_PATH}:$PATH"
|
||||
|
||||
# ==============检查文件是否齐全================
|
||||
|
||||
bins[0]=${kernel}
|
||||
|
||||
for file in ${bins[*]};do
|
||||
if [ ! -x $file ]; then
|
||||
echo "$file 不存在!"
|
||||
exit
|
||||
fi
|
||||
done
|
||||
|
||||
# ===============文件检查完毕===================
|
||||
|
||||
# 如果是 i386/x86_64,需要判断是否符合 multiboot2 标准
|
||||
if [ ${ARCH} == "i386" ] || [ ${ARCH} == "x86_64" ]; then
|
||||
if ${GRUB_PATH}/grub-file --is-x86-multiboot2 ${kernel}; then
|
||||
echo Multiboot2 Confirmed!
|
||||
else
|
||||
echo NOT Multiboot2!
|
||||
exit
|
||||
fi
|
||||
fi
|
||||
|
||||
# 拷贝程序到硬盘
|
||||
# 判断是否存在硬盘镜像文件,如果不存在,就创建一个(docker模式下,由于镜像中缺少qemu-img不会创建)
|
||||
if [ ! -f "${root_folder}/bin/disk.img" ]; then
|
||||
echo "创建硬盘镜像文件..."
|
||||
sudo bash ./create_hdd_image.sh
|
||||
fi
|
||||
|
||||
mkdir -p ${root_folder}/bin/disk_mount
|
||||
bash mount_virt_disk.sh || exit 1
|
||||
mkdir -p ${boot_folder}/grub
|
||||
cp ${kernel} ${root_folder}/bin/disk_mount/boot
|
||||
# 拷贝用户程序到磁盘镜像
|
||||
mkdir -p ${root_folder}/bin/disk_mount/bin
|
||||
mkdir -p ${root_folder}/bin/disk_mount/dev
|
||||
|
||||
cp -r ${root_folder}/bin/user/* ${root_folder}/bin/disk_mount/bin
|
||||
touch ${root_folder}/bin/disk_mount/dev/keyboard.dev
|
||||
|
||||
# 设置 grub 相关数据
|
||||
if [ ${ARCH} == "i386" ] || [ ${ARCH} == "x86_64" ]; then
|
||||
|
||||
touch ${root_folder}/bin/disk_mount/boot/grub/grub.cfg
|
||||
cfg_content='set timeout=15
|
||||
set default=0
|
||||
menuentry "DragonOS" {
|
||||
multiboot2 /boot/kernel.elf "KERNEL_ELF"
|
||||
}'
|
||||
echo "echo '${cfg_content}' > ${boot_folder}/grub/grub.cfg" | sh
|
||||
fi
|
||||
|
||||
# rm -rf ${iso_folder}
|
||||
LOOP_DEVICE=$(lsblk | grep disk_mount|sed 's/.*\(loop[0-9]*\)p1.*/\1/1g'|awk 'END{print $0}')
|
||||
echo $LOOP_DEVICE
|
||||
|
||||
grub-install --target=i386-pc --boot-directory=${root_folder}/bin/disk_mount/boot/ /dev/$LOOP_DEVICE
|
||||
|
||||
sync
|
||||
bash umount_virt_disk.sh
|
Reference in New Issue
Block a user