diff --git a/docs/index.rst b/docs/index.rst index d5103e30..3f541229 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -35,7 +35,8 @@ .. toctree:: :maxdepth: 1 :caption: 应用层 - + + userland/appdev/index userland/libc/index .. toctree:: diff --git a/docs/userland/appdev/c-cpp-quick-start.md b/docs/userland/appdev/c-cpp-quick-start.md new file mode 100644 index 00000000..9dd40792 --- /dev/null +++ b/docs/userland/appdev/c-cpp-quick-start.md @@ -0,0 +1,15 @@ +# 为DragonOS开发C/C++应用 + +## 编译环境 + +  DragonOS与Linux具有部分二进制兼容性,因此可以使用Linux的musl-gcc进行编译。但是由于DragonOS还不支持动态链接, +因此要增加编译参数`-static` + +比如,您可以使用 +```shell +musl-gcc -static -o hello hello.c +``` +来编译一个hello.c文件。 + +在移植现有程序时,可能需要配置`CFLAGS`和`LDFLAGS`,以及`CPPFLAGS`,以便正确地编译,具体请以实际为准。 + diff --git a/docs/userland/appdev/index.rst b/docs/userland/appdev/index.rst new file mode 100644 index 00000000..dea24d30 --- /dev/null +++ b/docs/userland/appdev/index.rst @@ -0,0 +1,10 @@ +应用程序开发文档 +==================================== + + +.. toctree:: + :maxdepth: 1 + :caption: 目录 + + rust-quick-start + c-cpp-quick-start diff --git a/docs/userland/appdev/rust-quick-start.md b/docs/userland/appdev/rust-quick-start.md new file mode 100644 index 00000000..f3f72638 --- /dev/null +++ b/docs/userland/appdev/rust-quick-start.md @@ -0,0 +1,42 @@ +# Rust应用开发快速入门 + +## 编译环境 + +  DragonOS与Linux具有部分二进制兼容性,因此可以使用Linux的Rust编译器进行编译,但是需要进行一些配置: + +您可以参考DragonOS的`tools/bootstrap.sh`中,`initialize_userland_musl_toolchain()`函数的实现,进行配置。 +或者,只要运行一下bootstrap.sh就可以了。 + +主要是因为DragonOS还不支持动态链接,但是默认的工具链里面,包含了动态链接解释器相关的代码,因此像脚本内那样,进行替换就能运行。 + +## 配置项目 + +### 从模板创建 + +:::{note} +该功能需要dadk 0.1.4及以上版本方能支持 +::: + +1. 使用DragonOS的tools目录下的`bootstrap.sh`脚本初始化环境 +2. 在终端输入`cargo install cargo-generate` +3. 在终端输入 + +```shell +cargo generate --git https://github.com/DragonOS-Community/Rust-App-Template +``` +即可创建项目。如果您的网络较慢,请使用镜像站 +```shell +cargo generate --git https://git.mirrors.dragonos.org/DragonOS-Community/Rust-App-Template +``` + +4. 使用`cargo run`来运行项目 +5. 在DragonOS的`user/dadk/config`目录下,使用`dadk new`命令,创建编译配置,安装到DragonOS的`/`目录下。 +(在dadk的编译命令选项处,请使用Makefile里面的`make install`配置进行编译、安装) +6. 编译DragonOS即可安装 + +### 手动配置 + +如果您需要移植别的库/程序到DragonOS,请参考模板内的配置。 + +由于DragonOS目前不支持动态链接,因此目前需要在RUSTFLAGS里面指定`-C target-feature=+crt-static -C link-arg=-no-pie` +并且需要使用上文提到的工具链`nightly-2023-08-15-x86_64-unknown-linux_dragonos-gnu` diff --git a/tools/bootstrap.sh b/tools/bootstrap.sh index 7952dc8f..b3389fe6 100644 --- a/tools/bootstrap.sh +++ b/tools/bootstrap.sh @@ -44,11 +44,21 @@ install_ubuntu_debian_pkg() gnupg \ lsb-release \ llvm-dev libclang-dev clang gcc-multilib \ - gcc build-essential fdisk dosfstools dnsmasq bridge-utils iptables libssl-dev pkg-config + gcc build-essential fdisk dosfstools dnsmasq bridge-utils iptables libssl-dev pkg-config \ + musl-tools sphinx + + # 如果python3没有安装 + if [ -z "$(which python3)" ]; then + echo "正在安装python3..." + sudo apt install -y python3 python3-pip + fi if [ -z "$(which docker)" ] && [ -n ${dockerInstall} ]; then echo "正在安装docker..." sudo apt install -y docker.io docker-compose + sudo usermod -aG docker $USER + sudo newgrp docker + sudo systemctl restart docker elif [ -z ${dockerInstall} ]; then echo "您传入--no-docker参数生效, 安装docker步骤被跳过." elif [ -n "$(which docker)" ]; then @@ -75,7 +85,7 @@ install_archlinux_pkg() curl wget bridge-utils dnsmasq \ diffutils pkgconf which unzip util-linux dosfstools \ gcc make flex texinfo gmp mpfr qemu-base \ - libmpc libssl-dev + libmpc libssl-dev musl } @@ -161,6 +171,41 @@ rustInstall() { fi } +#################################################################################### +# 初始化DragonOS的musl交叉编译工具链 +# 主要是把musl交叉编译工具链的rcrt1.o替换为crt1.o (因为rust的rcrt1.o会使用动态链接的解释器,但是DragonOS目前尚未把它加载进来) +# +# 为DragonOS开发应用的时候,请使用 `cargo +nightly-2023-08-15-x86_64-unknown-linux-gnu build --target x86_64-unknown-linux-musl` 来编译 +# 这样编译出来的应用将能二进制兼容DragonOS +#################################################################################### +initialize_userland_musl_toolchain() +{ + fork_toolchain_from="nightly-2023-08-15-x86_64-unknown-linux-gnu" + custom_toolchain="nightly-2023-08-15-x86_64-unknown-linux_dragonos-gnu" + custom_toolchain_dir="$(dirname $(rustc --print sysroot))/${custom_toolchain}" + # 如果目录为空 + if [ ! -d "${custom_toolchain_dir}" ]; then + echo "Custom toolchain does not exist, creating..." + rustup toolchain install ${fork_toolchain_from} + rustup component add --toolchain ${fork_toolchain_from} rust-src + rustup target add --toolchain ${fork_toolchain_from} x86_64-unknown-linux-musl + cp -r $(dirname $(rustc --print sysroot))/${fork_toolchain_from} ${custom_toolchain_dir} + self_contained_dir=${custom_toolchain_dir}/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained + cp -f ${self_contained_dir}/crt1.o ${self_contained_dir}/rcrt1.o + else + echo "Custom toolchain already exists." + fi + +} + + +install_python_pkg() +{ + echo "正在安装python依赖项..." + # 安装文档生成工具 + sh -c "cd ../docs && pip3 install -r requirements.txt" +} + ############# 脚本开始 ############## # 读取参数及选项,使用 -help 参数查看详细选项 @@ -221,6 +266,11 @@ fi rustInstall # 安装rust + +# 初始化DragonOS的musl交叉编译工具链 +initialize_userland_musl_toolchain +install_python_pkg + # 安装dadk cargo install dadk || exit 1 diff --git a/user/Makefile b/user/Makefile index 1b600d77..e66ae2df 100644 --- a/user/Makefile +++ b/user/Makefile @@ -14,7 +14,7 @@ current_CFLAGS := $(CFLAGS) DADK_VERSION=$(shell dadk -V | awk 'END {print $$2}') # 最小的DADK版本 -MIN_DADK_VERSION = 0.1.3 +MIN_DADK_VERSION = 0.1.4 DADK_CACHE_DIR = $(ROOT_PATH)/bin/dadk_cache # 旧版的libc安装路径