1. 环境配置

1.1. 操作系统及工具链

  1. 操作系统: WSL2 Ubuntu 22.04

  2. 参考 blueos 官方文档进行环境搭建 https://vivoblueos.github.io/book/zh/getting-started.html,注意 Python 版本需要是 3.11 及以上。

  3. 参考官方文档搭建 Rust 工具链 https://vivoblueos.github.io/book/zh/build-rust-toolchain.html

1.2. 遇到的问题

  1. 编译 qemu v10.0.2 时,configure步骤报错:
../meson.build:1027:10: ERROR: Dependency lookup for glib-2.0 with method 'pkgconfig' failed: Invalid version, need 'glib-2.0' ['>=2.66.0'] found '2.64.6'.

解决方法:升级 glib,但由于 glib 不是普通包,所以有些麻烦。这个错误是在 Ubuntu 20.04 上出现的,升级到 Ubuntu 22.04 后问题解决。

  1. 运行ninja -C out/qemu_mps2_an385.release时报错:
error loading target specification: could not find specification for target "thumbv7m-vivo-blueos-newlibeabi"

解决方法:确保蓝河官方的 Rust 工具链正确安装且环境变量配置正确。

  1. 运行ninja -C out/qemu_mps2_an385.release check_all时报错:

    1. 报错 1:
    No such file or directory: 'bindgen'
    
    • 解决方法:由于上述工具链不含 cargo,所以另外安装 rustup,并通过 cargo 安装 bindgen。注意 rustup 安装的 rust 工具链不要在环境变量中覆盖蓝河官方的工具链。
    1. 报错 2:
    AttributeError: module 'asyncio' has no attribute 'timeout'
    
    • 解决方法:重新安装 Python3.11 及以上版本,并重新编译安装 qemu v10.0.2。
    1. 报错 3: check_all 测试未通过
    [smoltcp Tcp Socket Test]: Enter poll device loop
    [smoltcp Tcp Socket Test]: Socket listening
    [smoltcp Tcp Socket Test]: Socket connecting
    [smoltcp Tcp Socket Test]: iface resuming Check Timeout
    ninja: build stopped: subcommand failed.
    
    • WSL2 网络问题,暂时没有解决,暂时先忽略该错误。
  2. WSL2 网络问题 使用 repo 同步代码时,WSL2 经常出现网络问题。配置 http_proxy等环境变量未必能解决问题。解决方法如下: 1. Windows 端 Clash 客户端开启 TUN 模式 2. repo init时加上--no-clone-bundle参数 3. 更新 DNS。/etc/systemd/resolved.conf/etc/resolv.conf

2. Docker 配置

根据上述步骤,我整理了一个 Dockerfile,可以快速搭建蓝河操作系统开发环境。

2.1. dockerfile

FROM ubuntu:22.04

ENV SRC_DIR=/opt/src
ENV DESTDIR=/opt/blueos-toolchain

RUN mkdir -p ${SRC_DIR} ${DESTDIR}/usr/local

# =========================================================
# 1. 系统依赖
# =========================================================
RUN apt update && apt install -y \
    build-essential cmake ninja-build pkg-config libssl-dev \
    curl git wget gdb-multiarch \
    python3.11 python3.11-venv python3-pip \
    meson libglib2.0-dev libslirp-dev \
    flex bison libfdt-dev \
    clang llvm lld \
    gcc-riscv64-unknown-elf \
    python3-kconfiglib python3-tomli \
    ca-certificates xz-utils unzip \
 && ln -sf /usr/bin/python3.11 /usr/bin/python3 \
 && rm -rf /var/lib/apt/lists/*

# =========================================================
# 2. repo & gn
# =========================================================
RUN curl -o /usr/local/bin/repo \
    https://storage.googleapis.com/git-repo-downloads/repo \
 && chmod +x /usr/local/bin/repo

COPY gn-linux-amd64.zip /tmp/gn.zip
RUN unzip /tmp/gn.zip -d /tmp/gn \
 && mv /tmp/gn/gn /usr/local/bin/gn \
 && chmod +x /usr/local/bin/gn \
 && rm -rf /tmp/gn /tmp/gn.zip

# =========================================================
# 3. ARM Toolchains
# =========================================================
WORKDIR ${SRC_DIR}

RUN wget -q https://developer.arm.com/-/media/Files/downloads/gnu/14.3.rel1/binrel/arm-gnu-toolchain-14.3.rel1-x86_64-arm-none-eabi.tar.xz \
 && tar xf arm-gnu-toolchain-14.3.rel1-x86_64-arm-none-eabi.tar.xz -C /opt \
 && rm arm-gnu-toolchain-14.3.rel1-x86_64-arm-none-eabi.tar.xz \
 && wget -q https://developer.arm.com/-/media/Files/downloads/gnu/14.3.rel1/binrel/arm-gnu-toolchain-14.3.rel1-x86_64-aarch64-none-elf.tar.xz \
 && tar xf arm-gnu-toolchain-14.3.rel1-x86_64-aarch64-none-elf.tar.xz -C /opt \
 && rm arm-gnu-toolchain-14.3.rel1-x86_64-aarch64-none-elf.tar.xz

ENV PATH=/opt/arm-gnu-toolchain-14.3.rel1-x86_64-arm-none-eabi/bin:\
/opt/arm-gnu-toolchain-14.3.rel1-x86_64-aarch64-none-elf/bin:${PATH}

# =========================================================
# 4. QEMU
# =========================================================
RUN wget -q https://download.qemu.org/qemu-10.0.2.tar.xz \
 && tar xf qemu-10.0.2.tar.xz \
 && cd qemu-10.0.2 \
 && mkdir build && cd build \
 && ../configure --prefix=/opt/qemu --enable-slirp \
 && make -j$(nproc) \
 && make install \
 && cd / \
 && rm -rf ${SRC_DIR}/qemu-10.0.2* 

ENV PATH=/opt/qemu/bin:${PATH}

# =========================================================
# 5. Rust toolchain
# =========================================================
ENV RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
ENV RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup

RUN curl https://sh.rustup.rs -sSf | sh -s -- -y
ENV PATH=/root/.cargo/bin:${PATH}

RUN cargo install bindgen-cli

# =========================================================
# 6. BlueOS Rust toolchain
# =========================================================
WORKDIR ${SRC_DIR}

RUN git clone https://github.com/vivoblueos/rust.git \
 && git clone https://github.com/vivoblueos/cc-rs.git \
 && git clone https://github.com/vivoblueos/libc.git

WORKDIR ${SRC_DIR}/rust
RUN cp config.blueos.toml config.toml \
 && ./x.py install -i --stage 1 compiler/rustc \
 && ./x.py install -i --stage 1 library/std --target thumbv7m-vivo-blueos-newlibeabi \
 && ./x.py install -i --stage 1 library/std --target aarch64-vivo-blueos-newlib \
 && ./x.py install -i --stage 1 library/std --target riscv64-vivo-blueos \
 && ./x.py install -i --stage 0 rustfmt rust-analyzer clippy \
 && ./x.py install -i --stage 1 library/std --target x86_64-unknown-linux-gnu \
 && cp -rav build/x86_64-unknown-linux-gnu/llvm/bin ${DESTDIR}/usr/local/ \
 && cp -rav build/x86_64-unknown-linux-gnu/llvm/lib ${DESTDIR}/usr/local/ \
 && rm -rf ${SRC_DIR}

ENV PATH=${DESTDIR}/usr/local/bin:${PATH}

WORKDIR /workspace
CMD ["/bin/bash"]

2.2. .dockerignore

.git
.gitignore

build/
out/
target/

*.log
*.tmp

node_modules/
__pycache__/

*.tar
*.tar.gz
*.tar.xz

2.3. 构建及运行

  1. 前往 https://chrome-infra-packages.appspot.com/dl/gn/gn/linux-amd64/+/latest 下载 gn-linux-amd64.zip
  2. 将上述gn-linux-amd64.zip、Dockerfile、.dockerignore放在同一目录下。
  3. 在该目录下使用 docker build -t blueos-dev . 构建,大约需要2-3小时。至少预留20GB的存储空间。网络上确保代理正常,能够访问github等网站。
  4. 运行:docker run -it –rm blueos-dev:latest /bin/bash