Ubuntu22.04 搭建 OpenHarmony 命令行开发环境
文章目录
- 简介
- 安装工具链
- 获取gitee源码
- 安装编译工具
- 编译测试
简介
在本文中,我们将介绍如何使用命令行工具在你的设备上安装OpenHarmony操作系统。OpenHarmony是一个开源的、面向物联网(IoT)设备的操作系统,它提供了一套全面的开发框架,使得开发者可以更容易地创建和部署IoT应用。
在本次安装中,使用的电脑是基于Ubuntu22的物理机器,按照官方文档的说明,优先推荐的方式是使用 DevEco Studio 进行开发,这种方式使用Windows 做前端,Linux 虚拟机作为编译后端,对于Linux 不熟悉的新手来说比较友好,与此同时也限制了使用的环境,例如 DevEco Device Tool 目前无法在Ubuntu22 上安装成功,因为 Ubuntu22 中默认的 Python 版本是3.10,Python版本不匹配,我尝试修改 DevEco Device Tool 中的部分文件并跳过完整性检查,依然无法安装。
相比使用GUI的方式,我更习惯使用命令行进行编译,简单高效,且灵活度更高,Linux有很多的发行版本,Ubuntu 在国内普通用户使用的最多的,同时也是最受欢迎的版本,但并不是每一个人都会使用Ubuntu,想要再 Ubuntu22 或者其他Linux 系统上编译,这应该是唯一的选择,由用户自己来解决依赖的缺失问题。
安装工具链
- 默认情况下,Ubuntu中使用的脚本解释器是dash(终端输入ls -l /bin/sh查看链接文件指向的位置),输入如下指令选择No,需要将其替换为bash
sudo dpkg-reconfigure dash
- 使用如下apt-get命令安装后续操作所需的库和工具:
sudo apt-get update && sudo apt-get install binutils binutils-dev git git-lfs gnupg flex bison gperf build-essential zip curl zlib1g-dev gcc-multilib g++-multilib gcc-arm-linux-gnueabi libc6-dev-i386 libc6-dev-amd64 lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z1-dev ccache libgl1-mesa-dev libxml2-utils xsltproc unzip m4 bc gnutls-bin python3.10 python3-pip ruby genext2fs device-tree-compiler make libffi-dev e2fsprogs pkg-config perl openssl libssl-dev libelf-dev libdwarf-dev u-boot-tools mtd-utils cpio doxygen liblz4-tool openjdk-8-jre gcc g++ texinfo dosfstools mtools default-jre default-jdk libncurses5 apt-utils wget scons python3.10-distutils tar rsync git-core libxml2-dev lib32z-dev grsync xxd libglib2.0-dev libpixman-1-dev kmod jfsutils reiserfsprogs xfsprogs squashfs-tools pcmciautils quota ppp libtinfo-dev libtinfo5 libncurses5-dev libncursesw5 libstdc++6 gcc-arm-none-eabi vim ssh locales libxinerama-dev libxcursor-dev libxrandr-dev libxi-dev git git-lfs
- 将Python3.10设置为Python与Python3的默认版本
which python3.10 # 查看python3.10的位置
sudo update-alternatives --install /usr/bin/python python {Python 3.10 路径} 1
sudo update-alternatives --install /usr/bin/python3 python3 {Python 3.10 路径} 1
获取gitee源码
- 首先需要下载gitee的repo工具:
curl https://gitee.com/oschina/repo/raw/fork_flow/repo-py3 -o repo
chmod a+x repo
sudo mv repo /usr/bin/
pip3 install -i https://repo.huaweicloud.com/repository/pypi/simple requests
- OpenHarmony主干代码获取:
repo init -u https://gitee.com/openharmony/manifest.git -b master --no-repo-verify
repo sync -c
repo forall -c 'git lfs pull'
- 在源码根目录下执行prebuilts脚本,安装编译器及二进制工具:
bash build/prebuilts_download.sh
安装编译工具
- 在源码根目录运行如下命令安装hb并更新至最新版本:
python3 -m pip install --user build/hb
- 设置环境变量:
echo "export PATH=~/.local/bin:\$PATH" >> ~/.bashrc
source ~/.bashrc
- 按照官方的说法,在源码目录执行"hb help",界面打印以下信息即表示安装成功:
[OHOS INFO] ----------------------------------------------------------------------------------------------------
[OHOS INFO] usage: hb build [option]
[OHOS INFO]
[OHOS INFO] options:
[OHOS INFO] -h, --help show this help message and exit
[OHOS INFO] --target-cpu {arm,arm64,x86_64,x64}
[OHOS INFO] Default:''. Help:Specifies the desired cpu architecture for the build, each may support different cpu architectures, run 'hb set --all' to list product all supported cpu architectures
[OHOS INFO] --target-os {android,ios}
[OHOS INFO] Default:''. Help:Specifies the desired os type for the build, each may support different os type, run 'hb set --all' to list product all supported os type
[OHOS INFO] -p PRODUCT_NAME, --product-name PRODUCT_NAME
[OHOS INFO] Default:''. Help:Build a specified product. You could use this option like this: 1.'hb build --product-name rk3568@hihope' 2.'hb build --product-name rk3568'
...
- 下面是我尝试编译时遇到的问题,找不到Mapping
[OHOS ERROR] Traceback (most recent call last):
[OHOS ERROR] File "/home/lhq/software/OpenHarmony/build/hb/containers/status.py", line 47, in wrapper
[OHOS ERROR] return func(*args, **kwargs)
[OHOS ERROR] File "/home/lhq/software/OpenHarmony/build/hb/main.py", line 112, in main
[OHOS ERROR] module = main._init_set_module()
[OHOS ERROR] File "/home/lhq/software/OpenHarmony/build/hb/main.py", line 83, in _init_set_module
[OHOS ERROR] from services.menu import Menu
[OHOS ERROR] File "/home/lhq/software/OpenHarmony/build/hb/services/menu.py", line 25, in <module>
[OHOS ERROR] from prompt_toolkit.shortcuts import run_application
[OHOS ERROR] File "/home/lhq/.local/lib/python3.10/site-packages/prompt_toolkit/__init__.py", line 16, in <module>
[OHOS ERROR] from .interface import CommandLineInterface
[OHOS ERROR] File "/home/lhq/.local/lib/python3.10/site-packages/prompt_toolkit/interface.py", line 19, in <module>
[OHOS ERROR] from .application import Application, AbortAction
[OHOS ERROR] File "/home/lhq/.local/lib/python3.10/site-packages/prompt_toolkit/application.py", line 8, in <module>
[OHOS ERROR] from .key_binding.bindings.basic import load_basic_bindings
[OHOS ERROR] File "/home/lhq/.local/lib/python3.10/site-packages/prompt_toolkit/key_binding/bindings/basic.py", line 9, in <module>
[OHOS ERROR] from prompt_toolkit.renderer import HeightIsUnknownError
[OHOS ERROR] File "/home/lhq/.local/lib/python3.10/site-packages/prompt_toolkit/renderer.py", line 11, in <module>
[OHOS ERROR] from prompt_toolkit.styles import Style
[OHOS ERROR] File "/home/lhq/.local/lib/python3.10/site-packages/prompt_toolkit/styles/__init__.py", line 8, in <module>
[OHOS ERROR] from .from_dict import *
[OHOS ERROR] File "/home/lhq/.local/lib/python3.10/site-packages/prompt_toolkit/styles/from_dict.py", line 9, in <module>
[OHOS ERROR] from collections import Mapping
[OHOS ERROR] ImportError: cannot import name 'Mapping' from 'collections' (/usr/lib/python3.10/collections/__init__.py)
[OHOS ERROR]
[OHOS ERROR] Code: 0000
[OHOS ERROR]
[OHOS ERROR] Reason: cannot import name 'Mapping' from 'collections' (/usr/lib/python3.10/collections/__init__.py)
[OHOS ERROR]
[OHOS ERROR] Solution: no solution
[OHOS ERROR]
这是由于Python3.10中collections中已经不再包含Mapping,需要将 /home/lhq/.local/lib/python3.10/site-packages/prompt_toolkit/styles/from_dict.py 中的 第9行进行替换:
from collections import Mapping # 原有代码
from collections.abc import Mapping # 替换后的代码
编译测试
- 设置编译路径并选择需要编译的目标
hb set
- 输入后系统类型选择 small
- 目标板选择 qemu_small_system_demo
- 编译
hb build
- 输出如下信息即代表编译成功
[OHOS INFO] --------------------------------------------
[OHOS INFO] ccache summary:
[OHOS INFO] ccache version: 4.5.1
[OHOS INFO] cache hit (direct): 18
[OHOS INFO] cache hit (preprocessed): 24
[OHOS INFO] cache miss: 18
[OHOS INFO] hit rate: 70.00%
[OHOS INFO] miss rate: 30.00%
[OHOS INFO] Cache size (GB): 4.96 / 100.00 (4.96 %)
[OHOS INFO] ---------------------------------------------
[OHOS INFO] c targets overlap rate statistics
[OHOS INFO] subsystem files NO. percentage builds NO. percentage overlap rate
[OHOS INFO] arkui 178 3.9% 178 3.9% 1.00
[OHOS INFO] communication 295 6.5% 295 6.5% 1.00
[OHOS INFO] graphic 34 0.8% 34 0.8% 1.00
[OHOS INFO] hdf 232 5.1% 232 5.1% 1.00
[OHOS INFO] hiviewdfx 14 0.3% 14 0.3% 1.00
[OHOS INFO] kernel 2169 48.0% 2169 48.0% 1.00
[OHOS INFO] powermgr 1 0.0% 1 0.0% 1.00
[OHOS INFO] security 308 6.8% 308 6.8% 1.00
[OHOS INFO] startup 113 2.5% 113 2.5% 1.00
[OHOS INFO] systemabilitymgr 22 0.5% 22 0.5% 1.00
[OHOS INFO] thirdparty 1119 24.7% 1119 24.7% 1.00
[OHOS INFO] window 18 0.4% 18 0.4% 1.00
[OHOS INFO]
[OHOS INFO] c overall build overlap rate: 1.00
[OHOS INFO]
[OHOS INFO]
[OHOS INFO] qemu_small_system_demo build success
[OHOS INFO] Cost time: 0:00:16