0%

Ubuntu 22.04下载与编译OpenHarmony

前情提要

之前文章简要记录了OpenHarmony的下载与编译流程,但是当时配置时没有把每个步骤记录下来,所以写的不是很详细,刚好因为重装了服务器的系统,可以重新来一遍,决定详细记录一下。

OpenHarmony代码下载与编译简单记录

我的开发环境简介

  • OS: Ubuntu 22.04.4 LTS
  • CPU: AMD EPYC 7402P
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
            .-/+oossssoo+/-.               magicdian@zhcompileserver 
`:+ssssssssssssssssss+:` -------------------------
-+ssssssssssssssssssyyssss+- OS: Ubuntu 22.04.4 LTS x86_64
.ossssssssssssssssssdMMMNysssso. Host: KVM/QEMU (Standard PC (Q35 + ICH9, 2009) pc-q35-8.1)
/ssssssssssshdmmNNmmyNMMMMhssssss/ Kernel: 5.15.0-105-generic
+ssssssssshmydMMMMMMMNddddyssssssss+ Uptime: 1 day, 14 mins
/sssssssshNMMMyhhyyyyhmNMMMNhssssssss/ Packages: 1294 (dpkg), 4 (snap)
.ssssssssdMMMNhsssssssssshNMMMdssssssss. Shell: bash 5.1.16
+sssshhhyNMMNyssssssssssssyNMMMysssssss+ Resolution: 1280x800
ossyNMMMNyMMhsssssssssssssshmmmhssssssso Terminal: /dev/pts/1
ossyNMMMNyMMhsssssssssssssshmmmhssssssso CPU: AMD EPYC 7402P (48) @ 2.794GHz
+sssshhhyNMMNyssssssssssssyNMMMysssssss+ GPU: 00:01.0 Vendor 1234 Device 1111
.ssssssssdMMMNhsssssssssshNMMMdssssssss. Memory: 1108MiB / 64281MiB
/sssssssshNMMMyhhyyyyhdNMMMNhssssssss/
+sssssssssdmydMMMMMMMMddddyssssssss+
/ssssssssssshdmNNNNmyNMMMMhssssss/
.ossssssssssssssssssdMMMNysssso.
-+sssssssssssssssssyyyssss+-
`:+ssssssssssssssssss+:`
.-/+oossssoo+/-.

OpenHarmony开发版我目前还没买,之前对Rockpi接触比较多,决定以RK3568作为Target进行编译。

拉取代码部分我采用repo sync的方式,仓库使用gitee。虽然github上也有openharmony的仓库,但以这个仓库为例,https://github.com/openharmony/build 已经两年多没更新了。

官方推荐使用Ubuntu 18.04系统进行编译,但是我不用新的系统就浑身难受。

Gitee配置

首先就是要注册Gitee账号了,注册完之后需要配置一下SSH密钥

配置SSH密钥

生成密钥

ssh-keygen -t ed25519 -C “Gitee SSH Key”

一直按回车就好了,这样密钥也不会有密码加密,使用时方便点,如果想给密钥加密的话,要注意设置passphrase的地方。

查看公钥并配置

cat .ssh/id_ed25519.pub
将显示的内容添加到这个页面( https://gitee.com/profile/sshkeys )里的公钥框框中

本地配置ssh config

我将私钥名称由id_ed25519修改为了magicdian_gitee

1
2
3
4
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/magicdian_gitee

测试是否生效

运行ssh -T git@gitee.com命令,应该有如下提示。

1
2
magicdian@MacBook-Pro-14 .ssh % ssh -T git@gitee.com
Hi 经典(@magicdian)! You've successfully authenticated, but GITEE.COM does not provide shell access.

拉取仓库(基于repo方法)

平常做AOSP项目开发用repo习惯了,你也可以选择官方的其他方法,参考这个链接:官方源码拉取教程

安装拉取依赖

安装git-lfs

1
sudo apt-get install -y git-lfs

安装repo工具

1
2
3
4
mkdir ~/bin
curl https://gitee.com/oschina/repo/raw/fork_flow/repo-py3 -o ~/bin/repo
chmod a+x ~/bin/repo
pip3 install -i https://repo.huaweicloud.com/repository/pypi/simple requests

配置bash profile

在.bashrc或.profile里添加

1
export PATH="~/bin:$PATH"

重新开启终端,输入repo命令,有如下提示说明repo配置成功

1
2
magicdian@zhcompileserver:~$ repo
error: repo is not installed. Use "repo init" to install it here.

配置git用户与邮箱

如果不配置的话,运行repo init会报错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
magicdian@zhcompileserver:~/test$ repo init -u git@gitee.com:openharmony/manifest.git -b master --no-repo-verify
Downloading Repo source from https://gitee.com/oschina/repo.git
remote: Enumerating objects: 9008, done.
remote: Total 9008 (delta 0), reused 0 (delta 0), pack-reused 9008
repo: warning: verification of repo code has been disabled;
repo will not be able to verify the integrity of itself.

Downloading manifest from git@gitee.com:openharmony/manifest.git
remote: Enumerating objects: 15856, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 15856 (delta 0), reused 0 (delta 0), pack-reused 15852

Traceback (most recent call last):
File "/home/magicdian/test/.repo/repo/main.py", line 628, in <module>
_Main(sys.argv[1:])
File "/home/magicdian/test/.repo/repo/main.py", line 602, in _Main
result = run()
File "/home/magicdian/test/.repo/repo/main.py", line 595, in <lambda>
run = lambda: repo._Run(name, gopts, argv) or 0
File "/home/magicdian/test/.repo/repo/main.py", line 264, in _Run
result = cmd.Execute(copts, cargs)
File "/home/magicdian/test/.repo/repo/subcmds/init.py", line 531, in Execute
self._ConfigureUser(opt)
File "/home/magicdian/test/.repo/repo/subcmds/init.py", line 390, in _ConfigureUser
name = self._Prompt('Your Name', mp.UserName)
File "/home/magicdian/test/.repo/repo/project.py", line 1030, in UserName
self._LoadUserIdentity()
File "/home/magicdian/test/.repo/repo/project.py", line 1043, in _LoadUserIdentity
u = self.bare_git.var('GIT_COMMITTER_IDENT')
File "/home/magicdian/test/.repo/repo/project.py", line 3584, in runner
raise GitError('%s %s: %s' %
error.GitError: manifests var: Committer identity unknown

*** Please tell me who you are.

Run

git config --global user.email "you@example.com"
git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'magicdian@zhcompileserver.(none)')

官方方法配置git用户与邮箱

1
2
3
git config --global user.name "yourname" (gitee的用户名)
git config --global user.email "your-email-address" (gitee的邮箱地址)
git config --global credential.helper store

指定openharmony仓库使用gitee账户

如果不想配置global设置的用户和邮箱,可以尝试在指定文件夹里使用对应的配置,这里以test目录为例
在用户目录创建.gitconfig.git_gitee_config文件

.gitconfig配置如下

1
2
3
4
5
6
7
[includeIf "gitdir:/home/magicdian/test/"]
path = .git_gitee_config

[core]
editor = vim
[color]
ui = auto

.git_gitee_config配置如下

1
2
3
[user]
name = 你的gitee用户名
email = 你的gitee邮箱

这样一来,在test目录下的所有git项目都会使用.git_gitee_config中的配置,不会影响其他仓库。

初始化repo仓库(以master分支为例)

这里以test目录为例,init过程中会让你检查用户名和邮箱配置是否正确。

命令repo init -u git@gitee.com:openharmony/manifest.git -b master --no-repo-verify 执行效果如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
magicdian@zhcompileserver:~/test$ repo init -u git@gitee.com:openharmony/manifest.git -b master --no-repo-verify
Downloading Repo source from https://gitee.com/oschina/repo.git
remote: Enumerating objects: 9008, done.
remote: Total 9008 (delta 0), reused 0 (delta 0), pack-reused 9008
repo: warning: verification of repo code has been disabled;
repo will not be able to verify the integrity of itself.

Downloading manifest from git@gitee.com:openharmony/manifest.git
remote: Enumerating objects: 15856, done.
remote: Counting objects: 100% (76/76), done.
remote: Compressing objects: 100% (16/16), done.
remote: Total 15856 (delta 39), reused 72 (delta 39), pack-reused 15780

Your Name [你配置的用户名,确认是否正确]:
Your Email [你配置的邮箱,确认是否正确]:

Your identity is: magicdian <你配置的邮箱,确认是否正确>
is this correct [y/N]? y

repo has been initialized in /home/magicdian/test

如果想节省空间,更快拉取项目的话,也可以使用这个命令,这样克隆时使用浅克隆。

1
repo init -u git@gitee.com:openharmony/manifest.git -b master --no-repo-verify --depth=1

拉取OpenHarmony代码

repo拉取部分

官方推荐的拉取命令如下

1
repo sync -c

也可以尝试使用以下命令,使用4线程,并且不同步tags信息。但需要注意的是,如果线程太多,可能会被服务器拒绝,所以根据情自身网络情况来设置,我尝试过8线程,有看到中间有connection reset的情况

1
repo sync -c -j4 --no-tags

平常AOSP项目开发时,我们可能还会使用-d,添加这个选项后就不会强制覆盖本地的修改提交

git-lfs仓库更新

1
repo forall -c 'git lfs pull'

编译代码

安装环境依赖

官方要求

1
sudo apt-get install bison ccache default-jdk flex gcc-arm-linux-gnueabi gcc-arm-none-eabi  genext2fs liblz4-tool libssl-dev libtinfo5 mtd-utils mtools openssl ruby scons unzip u-boot-tools  zip

额外的一些配置

配置python

我这里选择将python默认版本指向python3

1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 1

安装java-wrappers

1
sudo apt-get install -y java-wrappers

如果不安装可能在编译时会有报错:/usr/bin/javac: 4: .: cannot open /usr/lib/java-wrappers/java-wrappers.sh: No such file

安装libglfw3与mesa

1
2
sudo apt-get install libglfw3-dev
sudo apt install mesa-utils

为了解决编译时这个报错,这个报错在网上搜了好多地方都没有提到…折腾了好久才搞定

1
error: 'KHR/khrplatform.h' file not found

修改默认shell环境为bash

Ubuntu默认shell环境是dash,官方建议使用bash获得完整的shell功能

1
2
magicdian@zhcompileserver:~/magicdata/openharmony$ ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Mar 23 2022 /bin/sh -> dash

使用命令 sudo dpkg-reconfigure dash然后选择no,再次检查sh指向

1
2
magicdian@zhcompileserver:~/magicdata/openharmony$ ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Apr 24 14:16 /bin/sh -> bash

下载项目prebuilts

1
sh build/prebuilts_download.sh

赋予build.sh可执行权限

1
sudo chmod +x build.sh

编译(针对RK3568)

使用这个命令编译即可

1
./build.sh --product-name rk3568