Ubuntu22.04 搭建 OpenHarmony 命令行开发环境

文章目录

简介

在本文中,我们将介绍如何使用命令行工具在你的设备上安装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 系统上编译,这应该是唯一的选择,由用户自己来解决依赖的缺失问题。

安装工具链

  1. 默认情况下,Ubuntu中使用的脚本解释器是dash(终端输入ls -l /bin/sh查看链接文件指向的位置),输入如下指令选择No,需要将其替换为bash

    sudo dpkg-reconfigure dash

  2. 使用如下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

  3. 将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源码

  1. 首先需要下载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

  2. OpenHarmony主干代码获取:

    repo init -u https://gitee.com/openharmony/manifest.git -b master --no-repo-verify
    repo sync -c
    repo forall -c 'git lfs pull'

  3. 在源码根目录下执行prebuilts脚本,安装编译器及二进制工具:

    bash build/prebuilts_download.sh

安装编译工具

  1. 在源码根目录运行如下命令安装hb并更新至最新版本:

    python3 -m pip install --user build/hb

  2. 设置环境变量:

    echo "export PATH=~/.local/bin:$PATH" >> ~/.bashrc
    source ~/.bashrc

  3. 按照官方的说法,在源码目录执行"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'
    ...

  4. 下面是我尝试编译时遇到的问题,找不到Mapping

python 复制代码
[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行进行替换:

python 复制代码
from collections import Mapping      # 原有代码
from collections.abc import Mapping  # 替换后的代码

编译测试

  1. 设置编译路径并选择需要编译的目标

    hb set

  • 输入后系统类型选择 small
  • 目标板选择 qemu_small_system_demo
  1. 编译

    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

相关推荐
独行soc8 小时前
#渗透测试#SRC漏洞挖掘#深入挖掘XSS漏洞02之测试流程
web安全·面试·渗透测试·xss·漏洞挖掘·1024程序员节
XuanRanDev11 小时前
【每日一题】LeetCode - 三数之和
数据结构·算法·leetcode·1024程序员节
鹏大师运维17 小时前
【功能介绍】信创终端系统上各WPS版本的授权差异
linux·wps·授权·麒麟·国产操作系统·1024程序员节·统信uos
亦枫Leonlew18 小时前
微积分复习笔记 Calculus Volume 1 - 4.7 Applied Optimization Problems
笔记·数学·微积分·1024程序员节
小肥象不是小飞象18 小时前
(六千字心得笔记)零基础C语言入门第八课——函数(上)
c语言·开发语言·笔记·1024程序员节
一个通信老学姐1 天前
专业130+总400+武汉理工大学855信号与系统考研经验电子信息与通信工程,真题,大纲,参考书。
考研·信息与通信·信号处理·1024程序员节
力姆泰克1 天前
看电动缸是如何提高农机的自动化水平
大数据·运维·服务器·数据库·人工智能·自动化·1024程序员节
力姆泰克1 天前
力姆泰克电动缸助力农业机械装备,提高农机的自动化水平
大数据·服务器·数据库·人工智能·1024程序员节
程思扬1 天前
为什么Uptime+Kuma本地部署与远程使用是网站监控新选择?
linux·服务器·网络·经验分享·后端·网络协议·1024程序员节
转世成为计算机大神1 天前
网关 Spring Cloud Gateway
java·网络·spring boot·1024程序员节