跳到主要内容

如何在 Ubuntu 上构建 PolyOS Mobile

PolyOS Mobile 的构建环境

PolyOS Mobile 的构建环境主要需要网络下载工具、C/C++ 编译与构建工具、Java JDK、以及一些其他 Linux 工具。

备注

本文的撰写使用 Ubuntu 20.04 进行了测试,应当在 20.04、20.10、21.04、21.10、22.04 等版本上可以正常工作。

文中的 aptapt-get 命令会需要您授予管理员权限。

我们正在寻求帮助改进,使此文档可以适用于更多的版本。

安装依赖

apt-get install bc bsdmainutils rsync ssh xsltproc genext2fs cpio gawk \
libelf-dev dwarves exuberant-ctags \
curl iputils-ping openssl net-tools wget \
mtools tofrodos \
silversearcher-ag \
liblz4-tool pigz unzip zip zlib1g-dev \
autoconf automake cmake m4 make scons texinfo \
build-essential ccache pkg-config \
default-jdk openjdk-8-jdk ca-certificates-java \
perl python python3 ruby tcl \
device-tree-compiler libfdt-dev kmod u-boot-tools \
mtd-utils \
dwarves exuberant-ctags flex bison \
gcc-multilib g++-multilib \
libncurses5-dev lib32ncurses5-dev lib32z1-dev libtool \
git git-lfs patchutils subversion \
mpc libmpc-dev\
gperf \
libexpat-dev libxml2-dev libxml2-utils \
libglib2.0-dev \
libgl1-mesa-dev \
libpixman-1-dev \
libgmp-dev libmpc-dev libmpfr-dev \
libmount-dev \
openssl libssl-dev libssl1.1 \
libtinfo5 \
libx11-dev x11proto-core-dev \
python-crypto python3-crypto python3-xlrd python3-pip python3-yaml
提示

您可以使用位于中国大陆的 Ubuntu 镜像源来提高下载速度,在这里我们使用中国科学院软件所智能软件研究中心维护的 Ubuntu 镜像,当然您也可以选择使用离您地理位置更近的或是您更喜欢的镜像站。

如果您不知道该如何挑选镜像站,您可以访问 MirrorZ 维护的镜像服务列表 获取更合适的镜像服务。

cat <<'EOF' > /etc/apt/sources.list
deb https://mirror.iscas.ac.cn/ubuntu/ focal main restricted universe multiverse
# deb-src https://mirror.iscas.ac.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirror.iscas.ac.cn/ubuntu/ focal-updates main restricted universe multiverse
# deb-src https://mirror.iscas.ac.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirror.iscas.ac.cn/ubuntu/ focal-backports main restricted universe multiverse
# deb-src https://mirror.iscas.ac.cn/ubuntu/ focal-backports main restricted universe multiverse

# deb https://mirror.iscas.ac.cn/ubuntu/ focal-security main restricted universe multiverse
# deb-src https://mirror.iscas.ac.cn/ubuntu/ focal-security main restricted universe multiverse

deb http://security.ubuntu.com/ubuntu/ focal-security main restricted universe multiverse
# deb-src http://security.ubuntu.com/ubuntu/ focal-security main restricted universe multiverse

# deb https://mirror.iscas.ac.cn/ubuntu/ focal-proposed main restricted universe multiverse
# deb-src https://mirror.iscas.ac.cn/ubuntu/ focal-proposed main restricted universe multiverse
EOF

请不要将这个示例修改直接应用于除 20.04(Focal) 以外的版本,如果您不明白该配置的含义或版本区别,您可以访问 MirrorZ 帮助文档 了解该配置。

安装 repo 工具

由于 PolyOS Mobile 源代码分散在多个仓库中,您还需要下载 repo 工具来轻松的获取 PolyOS Mobile 源代码。

mkdir -p ~/.local/bin
curl -L https://gitee.com/oschina/repo/raw/fork_flow/repo-py3 -o ~/.local/bin/repo
chmod a+x ~/.local/bin/repo

如果您有需要,您也可以选择将 ~/.local/bin 加入您的 PATH 环境变量中。

cat << EOF >> ~/.bashrc
export PATH=$HOME/.local/bin:$PATH
EOF
source ~/.bashrc

(可选)Docker 构建环境

如果您使用其它 Linux 操作系统可以启动一个拥有构建环境的容器来构建。

sudo docker run -it --name polyos_mobile_build -v /path/to/polyos-mobile-code:/ployos-mobile swr.cn-south-1.myhuaweicloud.com/openharmony-docker/openharmony-docker:1.0.0
提示

如果您没有 docker,您可以访问 Docker 安装指导 来了解如何安装。

获取预构建的工具

PolyOS Mobile 的编译依赖一些二进制工具,我们已经源代码的项目 build 目录,我们提供了一个 shell 脚本用于下载和配置这些预构建的二进制程序。您只需要简单的执行它:

bash /path/to/polyos-mobile-code/build/prebuilts_download.sh

耐心等待它执行完成后,您将会得到一个包含预构建程序的目录。

编译 PolyOS Mobile 源代码

PolyOS Mobile 提供了脚本来完成构建过程的控制,在下文中我们将使用位于源代码的项目更目录的一个名为 build.shshell 脚本来执行细粒度不同的 PolyOS Mobile 构建。

编译完整的 PolyOS Mobile

我们通过向构建脚本指定 qemu_riscv64_virt_linux_system 为目标,使其构建一个完整的 PolyOS Mobile 镜像。

bash /path/to/polyos-mobile-code/build.sh --product-name qemu_riscv64_virt_linux_system --ccache

这个过程会需要大量时间,时间的长短取决于您的硬件性能。在您编译完成后,镜像保存在 PolyOS Mobile 源代码目录下的 out/riscv64_virt/packages/phone/images 目录中。

提示

--ccache 是用于缓存编译过程的中间产物,便于您在构建中断后恢复。

编译一个 PolyOS Mobile 模块

我们通过向构建脚本传递一个具体的 target 来指定单独编译一个 PolyOS Mobile 的模块。

bash /path/to/polyos-mobile-code/build.sh --product-name qemu_riscv64_virt_linux_system --ccache --build-target {target_name}

受支持的构建参数

您可以使用 --help 参数获取完整的受支持构建参数列表:

bash /path/to/polyos-mobile-code/build.sh --help