-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
369 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,20 +5,17 @@ | |
<meta charset="utf-8" /> | ||
<meta name="generator" content="pandoc" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" /> | ||
<title>OpenHarmony 3.2 编译</title> | ||
<link rel="stylesheet" href="https://www.qin-juan-ge-zhu.top/common/CSS/pandoc.css"> | ||
<script type="text/javascript" src="https://www.qin-juan-ge-zhu.top/common/js/myhighlight.js"></script> | ||
<script type="text/javascript" src="https://www.qin-juan-ge-zhu.top/common/script4code.js"></script> | ||
<title>OHOS全量编译记录</title> | ||
<link rel="stylesheet" href="https://test.qin-juan-ge-zhu.top/common/CSS/pandoc.css"> | ||
<script type="text/javascript" src="https://test.qin-juan-ge-zhu.top/common/js/myhighlight.js"></script> | ||
<script type="text/javascript" src="https://test.qin-juan-ge-zhu.top/common/script4code.js"></script> | ||
</head> | ||
|
||
<body> | ||
<div class="pandoc"> | ||
<div class="main"> | ||
<header id="title-block-header"> | ||
<p class="title">OpenHarmony 3.2 编译</p> | ||
</header> | ||
<p class="title">OHOS 全量编译记录</p> | ||
<!-- 鸿蒙开发环境搭建 --> | ||
<p>[toc]</p> | ||
<blockquote> | ||
<p>本次编译环境搭建参考了以下博客:</p> | ||
<ul> | ||
|
@@ -39,7 +36,8 @@ <h1 id="环境与依赖">环境与依赖</h1> | |
<h2 id="硬件环境">硬件环境</h2> | ||
<p>编译过程使用的是 | ||
<code>Ubuntu</code>,经测试,<code>Ubuntu 22.04 LTS</code>/<code>Ubuntu 20.04 LTS</code>/<code>Ubuntu 18.04 LTS</code> | ||
均可用。</p> | ||
均可用。 | ||
</p> | ||
<p>网上的大多数博客里没有对编译的硬件限制作出说明,现根据我们的情况,给出一个大概的范围:</p> | ||
<ul> | ||
<li><strong>内存必须在 10G 以上</strong>,我使用的是 13G,编译成功(8G 时编译失败了,下文会说明)</li> | ||
|
@@ -109,39 +107,45 @@ <h2 id="gitee-帐户">gitee 帐户</h2> | |
<p>此时,我们就可以通过 ssh 的方式与 gitee 进行交互、也可以免密提交代码了。</p> | ||
<h1 id="系统源码编译">系统源码编译</h1> | ||
<h2 id="配置-repo-工具">配置 repo 工具</h2> | ||
<p><strong>注意:前两条命令需要以 root 身份执行!!!</strong></p> | ||
<pre><code># 以下两条命令需要以root身份执行 | ||
sudo -s | ||
curl -s https://gitee.com/oschina/repo/raw/fork_flow/repo-py3 > /usr/local/bin/repo | ||
chmod a+x /usr/local/bin/repo | ||
exit | ||
<pre><code>curl -s https://gitee.com/oschina/repo/raw/fork_flow/repo-py3 > ~/repo | ||
chmod a+x ~/repo | ||
sudo mv ~/repo /usr/local/bin/repo | ||
sudo chown root:root /usr/local/bin/repo | ||
|
||
# 这条普通身份也可以 | ||
pip3 install -i https://repo.huaweicloud.com/repository/pypi/simple requests</code></pre> | ||
<p>之所以前两条命令需要以 root 身份而不能是 sudo,是因为<code>/usr/local/bin</code>是一个只有 root 用户才有写权限的目录,而 sudo 命令虽然是以 root | ||
<p>需要注意的是,不能直接使用 sudo 搭配管道,这是因为<code>/usr/local/bin</code>是一个只有 root 用户才有写权限的目录,而 sudo 命令虽然是以 root | ||
身份执行,但<strong>重定向时候 sudo 用的也是当前用户身份</strong>,权限不足,自然报错。(<strong>如果是管道,那么 sudo | ||
也是只对当前命令有效,而不是对后续的整个管道有效。</strong>)</p> | ||
<h2 id="获取源码">获取源码</h2> | ||
<p>这里需要注意的是,一般情况下最好使用带有 v 和 Release 的版本。不带 Release 不是发布版,会随时更新代码,容易编译出错;带 Release 的也有两种 tag,是带 v 和不带 v | ||
的区别,如<code>OpenHarmony-v4.0-Release</code>和<code>OpenHarmony-4.0-Release</code>。二者的区别主要在于,不带 v | ||
的是官方维护的稳定版,也会更新代码,厂商的补丁一般只针对带 v 的使用。</p> | ||
<p>通过 repo + https/ssh 下载:</p> | ||
<pre><code># 通过http下载,这里需要注意你需要的ohos的版本 | ||
repo init -u https://gitee.com/openharmony/manifest.git -b master --no-repo-verify | ||
# 如果需要的是特定分支,-b后边改成对应分支名 | ||
<pre><code># 如果需要的是特定分支,-b后边改成对应分支名 | ||
# repo init -u https://gitee.com/openharmony/manifest.git -b master --no-repo-verify | ||
# 如果是tag,-b后的参数比较复杂,要在网页上提前确定好需要的tag名字, | ||
# 如下载的是tag为OpenHarmony-v3.2-Release的版本,命令如下: | ||
# repo init -u https://gitee.com/openharmony/manifest -b refs/tags/OpenHarmony-v3.2-Release --no-repo-verify | ||
# 除使用https外,也可以通过ssh下载 | ||
# repo init -u [email protected]:openharmony/manifest.git -b master --no-repo-verify | ||
|
||
repo init -u https://gitee.com/openharmony/manifest -b refs/tags/OpenHarmony-v4.0-Release --no-repo-verify | ||
repo sync -c | ||
repo forall -c 'git lfs pull'</code></pre> | ||
<h2 id="prebuilts-与编译">prebuilts 与编译</h2> | ||
<h2 id="补丁与编译">补丁与编译</h2> | ||
<p>首先,打上厂商的补丁。下载对应版本补丁后,将补丁文件放到源码根目录下,执行:</p> | ||
<pre><code>unzip purple_pi_oh_patch.zip | ||
cd purple_pi_oh_patch | ||
./ido_patch.sh</code></pre> | ||
<p>看到<code>patch complete</code>字样,说明补丁成功。接下来进行 prebuilts 和编译:</p> | ||
<pre><code># 先在源码根目录下执行脚本,安装编译器及二进制工具 | ||
bash build/prebuilts_download.sh | ||
# 再执行如下命令进行版本编译 | ||
|
||
# 编译 | ||
# 注意:默认编译的时候,目标cpu是32位,即使为64位cpu也无法使用64位功能 | ||
sudo ./build.sh --product-name rk3568 --ccache | ||
# 如果是64位cpu,需要加上--target-cpu=arm64 | ||
sudo ./build.sh --product-name rk3568 --ccache --target-cpu=arm64</code></pre> | ||
# 编译rk3568时 | ||
# sudo ./build.sh --product-name rk3568 --ccache --target-cpu=arm64 | ||
sudo ./build.sh --product-name purple_pi_oh --ccache --no-prebuilt-sdk --target-cpu=arm64</code></pre> | ||
<h2 id="编译完成">编译完成</h2> | ||
<p>编译所生成的文件都归档在 out 目录下,结果镜像输出在源码根目录下的<code>out/rk3568/packages/phone/images</code>目录下。</p> | ||
<p>自此源码编译成功,即可进行镜像烧录。</p> | ||
|
@@ -181,14 +185,75 @@ <h2 id="设备磁盘空间不足">设备磁盘空间不足</h2> | |
sudo mount -o remount -rw / | ||
sudo mount -o remount -rw /var/snap/firefox/common/host-hunspell</code></pre> | ||
<p>而后在 gparted 中点击<code>gparted->刷新设备</code>,即可进行分区扩展。</p> | ||
<h2 id="ninja-编译报错-code-4000">Ninja 编译报错 <code>Code 4000</code></h2> | ||
<p>编译 rk3568 过程中可能会出现以下报错:</p> | ||
<pre><code>[OHOS ERROR] Code: 4000 | ||
[OHOS ERROR] Reason: ninja phase failed</code></pre> | ||
<p>详细的报错信息已经丢失,将就着看吧。反正别的办法都不好使的时候试试这个。</p> | ||
<p>此时执行以下内容即可:</p> | ||
<pre><code>rm -rf out | ||
sed -i 's/CONFIG_DEBUG_INFO_BTF=y/# CONFIG_DEBUG_INFO_BTF=y/g' kernel/linux/config/linux-5.10/rk3568/arch/arm64_defconfig</code></pre> | ||
<p>而后重新编译。</p> | ||
<h2 id="另一个code-4000">另一个<code>Code 4000</code></h2> | ||
<p>编译时报错如下:</p> | ||
<pre><code>ninja: build stopped: subcommand failed. | ||
[91m[OHOS ERROR][0m Traceback (most recent call last): | ||
[91m[OHOS ERROR][0m File "/home/player/Desktop/ohos/src/build/hb/services/ninja.py", line 49, in _execute_ninja_cmd | ||
[91m[OHOS ERROR][0m SystemUtil.exec_command( | ||
[91m[OHOS ERROR][0m File "/home/player/Desktop/ohos/src/build/hb/util/system_util.py", line 63, in exec_command | ||
[91m[OHOS ERROR][0m raise OHOSException( | ||
[91m[OHOS ERROR][0m exceptions.ohos_exception.OHOSException: Please check build log in /home/player/Desktop/ohos/src/out/purple_pi_oh/build.log | ||
[91m[OHOS ERROR][0m | ||
[91m[OHOS ERROR][0m During handling of the above exception, another exception occurred: | ||
[91m[OHOS ERROR][0m | ||
[91m[OHOS ERROR][0m Traceback (most recent call last): | ||
[91m[OHOS ERROR][0m File "/home/player/Desktop/ohos/src/build/hb/containers/status.py", line 47, in wrapper | ||
[91m[OHOS ERROR][0m return func(*args, **kwargs) | ||
[91m[OHOS ERROR][0m File "/home/player/Desktop/ohos/src/build/hb/modules/ohos_build_module.py", line 67, in run | ||
[91m[OHOS ERROR][0m raise exception | ||
[91m[OHOS ERROR][0m File "/home/player/Desktop/ohos/src/build/hb/modules/ohos_build_module.py", line 65, in run | ||
[91m[OHOS ERROR][0m super().run() | ||
[91m[OHOS ERROR][0m File "/home/player/Desktop/ohos/src/build/hb/modules/interface/build_module_interface.py", line 72, in run | ||
[91m[OHOS ERROR][0m raise exception | ||
[91m[OHOS ERROR][0m File "/home/player/Desktop/ohos/src/build/hb/modules/interface/build_module_interface.py", line 70, in run | ||
[91m[OHOS ERROR][0m self._target_compilation() | ||
[91m[OHOS ERROR][0m File "/home/player/Desktop/ohos/src/build/hb/modules/ohos_build_module.py", line 103, in _target_compilation | ||
[91m[OHOS ERROR][0m self.target_compiler.run() | ||
[91m[OHOS ERROR][0m File "/home/player/Desktop/ohos/src/build/hb/services/ninja.py", line 38, in run | ||
[91m[OHOS ERROR][0m self._execute_ninja_cmd() | ||
[91m[OHOS ERROR][0m File "/home/player/Desktop/ohos/src/build/hb/services/ninja.py", line 52, in _execute_ninja_cmd | ||
[91m[OHOS ERROR][0m raise OHOSException('ninja phase failed', '4000') | ||
[91m[OHOS ERROR][0m exceptions.ohos_exception.OHOSException: ninja phase failed | ||
[91m[OHOS ERROR][0m | ||
[91m[OHOS ERROR][0m Code: 4000 | ||
[91m[OHOS ERROR][0m | ||
[91m[OHOS ERROR][0m Reason: ninja phase failed | ||
[91m[OHOS ERROR][0m | ||
[91m[OHOS ERROR][0m Solution: Please check the compile log at out/{compiling product}/build.log, If you could analyze build logs. | ||
[91m[OHOS ERROR][0m Or you can try the following steps to solve this problem: | ||
[91m[OHOS ERROR][0m 1. cd to OHOS root path | ||
[91m[OHOS ERROR][0m 2. run 'hb clean --all' or 'rm -rf out build/resources/args/*.json'. | ||
[91m[OHOS ERROR][0m 3. repo sync | ||
[91m[OHOS ERROR][0m 4. repo forall -c 'git lfs pull' | ||
[91m[OHOS ERROR][0m 5. bash build/prebuilts_download.sh | ||
[91m[OHOS ERROR][0m 6. rebuild your product or component | ||
[91m[OHOS ERROR][0m | ||
[91m[OHOS ERROR][0m If you still cannot solve this problem, you could post this problem on: | ||
[91m[OHOS ERROR][0m https://gitee.com/openharmony/build/issues | ||
[91m[OHOS ERROR][0m</code></pre> | ||
<p>在报错信息之上,还有很多很多行,大致意思是正在对某些文件或仓库拉补丁,输出了补丁的 commit 信息。报错中最后提示的解决办法其实是无效的,我全量删除源码、甚至重装虚拟机,都还会遇到这个问题。</p> | ||
<p>仔细分析报错信息,发现报错的根本原因是在 python 中<strong>处理一个 Exception 时,又抛出了另一个 | ||
Exception,导致程序异常终止</strong>。上网搜索可知,这种问题的一般原因是 python | ||
同时拉取或爬取大量网页内容,但都失败了,导致同时出现两个异常,程序崩溃。具体到我们这里,问题就是拉补丁失败了,我检查了一下我的网络环境,发现我挂着梯子,梯子质量不是那么好。尝试将梯子关掉后,再次编译,问题解决。 | ||
</p> | ||
<h2 id="一个奇怪的报错gn-failed">一个奇怪的报错——<code>GN Failed</code></h2> | ||
<p>这是一个很奇怪的报错,我们尚未找到真正的原因所在,只知道如何暂时地绕过去。</p> | ||
<h3 id="问题描述与复现">问题描述与复现</h3> | ||
<p>问题复现方法为在编译过程中,执行以下命令:</p> | ||
<pre><code>./build.sh --product-name rk3568 --ccache</code></pre> | ||
<p>该命令是指导书中写的编译命令,但执行时候会出错;<strong>与上文所述的经确认无误的编译命令不同在于没加<code>sudo</code></strong>。</p> | ||
<p>如果按照指导书的命令来,不出意外编译将会报错: <img src="https://www.qin-juan-ge-zhu.top/images/code/ohos_gn_fail_1.png" /> | ||
<img src="https://www.qin-juan-ge-zhu.top/images/code/ohos_gn_fail_2.png" /></p> | ||
<p>如果按照指导书的命令来,不出意外编译将会报错: <img src="https://www.qin-juan-ge-zhu.top/images/code/ohos_gn_fail_1.png" /> <img | ||
src="https://www.qin-juan-ge-zhu.top/images/code/ohos_gn_fail_2.png" /></p> | ||
<p><strong>以下是该问题的具体描述:</strong></p> | ||
<p><code>build.log</code>如下:</p> | ||
<pre><code>Set cache size limit to 100.0 GB | ||
|
@@ -311,6 +376,109 @@ <h1 id="ohos-的-ndk">ohos 的 NDK</h1> | |
<p>将编译出来的 NDK 的 zip 解压到你想要的目录下,然后将该目录添加到环境变量中,即可使用。</p> | ||
<p>注意,NDK 包提供的交叉编译工具是 cmake 和 ninja,编译器是 clang 和 clang++,并没有我们熟悉的 gcc/g++和 make。除此之外,NDK | ||
还未我们提供编译所需的全套服务,如编译工具链配置文件<code>ohos.toolchain.cmake</code>、头文件、库文件等。快说,谢谢 ohos~</p> | ||
<p>为了更方便地使用 NDK,鄙人不才,写了两个脚本,分别用于 cmake 编译和单文件编译:</p> | ||
<pre><code>#!/bin/bash | ||
|
||
####################################################################### | ||
# File Name : compile.sh | ||
# Encoding : utf-8 | ||
# Author : We-unite | ||
# Email : [email protected] | ||
# Created Time : 2024-11-18 15:19:15 | ||
####################################################################### | ||
|
||
set -e | ||
|
||
if [ $UID -eq 0 ]; then | ||
echo "Please do not run this script as root" | ||
exit 1 | ||
fi | ||
|
||
if [ $# -ne 2 ]; then | ||
echo "Usage: $0 <static|shared> <v7|v8>" | ||
exit 1 | ||
fi | ||
|
||
if [ $2 == "v8" ]; then | ||
arch=arm64-v8a | ||
elif [ $2 == "v7" ]; then | ||
arch=armeabi-v7a | ||
else | ||
echo "Invalid architecture: $2" | ||
exit 1 | ||
fi | ||
|
||
link=$1 # static or shared | ||
native_path=~/app/native | ||
|
||
export PATH=$native_path/build-tools/cmake/bin:$PATH | ||
|
||
# 使用cmake编译,编译生成的文件运行在rk3568上 | ||
cmake -B build -D OHOS_STL=c++_$link -D OHOS_ARCH=$arch \ | ||
-D OHOS_PLATFORM=OHOS \ | ||
-D CMAKE_TOOLCHAIN_FILE=$(find $native_path -name ohos.toolchain.cmake) | ||
cmake --build build</code></pre> | ||
<pre><code>#!/bin/bash | ||
|
||
####################################################################### | ||
# File Name : compile-tiny.sh | ||
# Encoding : utf-8 | ||
# Author : We-unite | ||
# Email : [email protected] | ||
# Created Time : 2024-11-16 13:06:58 | ||
####################################################################### | ||
|
||
set -e | ||
# 如果是root,报错 | ||
if [ $(id -u) -eq 0 ]; then | ||
echo "Do not run as root" | ||
exit 1 | ||
fi | ||
|
||
if [ $# -ne 2 ]; then | ||
echo "Usage: $0 <src file> [armv8-a|armv7-a]" | ||
exit 1 | ||
fi | ||
|
||
native=~/app/native | ||
file=$1 | ||
targetFile=${file%.*} | ||
arch=$2 | ||
|
||
case $arch in | ||
armv8-a) | ||
compiler=$native/llvm/bin/aarch64-unknown-linux-ohos | ||
targetPlatform=aarch64-linux-ohos | ||
;; | ||
armv7-a) | ||
compiler=$native/llvm/bin/armv7-unknown-linux-ohos | ||
targetPlatform=arm-linux-ohos | ||
;; | ||
*) | ||
echo "Unsupported arch" | ||
exit 1 | ||
;; | ||
esac | ||
|
||
case ${file##*.} in | ||
c) | ||
compiler=$compiler-clang | ||
;; | ||
cpp) | ||
compiler=$compiler-clang++ | ||
;; | ||
*) | ||
echo "Unsupported file type" | ||
exit 1 | ||
;; | ||
esac | ||
|
||
export CPATH= | ||
|
||
$compiler -o $targetFile $file -Wall \ | ||
--target=$targetPlatform \ | ||
--sysroot=$native/sysroot \ | ||
-march=$arch -mfloat-abi=softfp</code></pre> | ||
<h1 id="完结撒花">完结撒花</h1> | ||
<p>本次鸿蒙开发环境的搭建过程可谓一波三折,总结几个最大的坑点,或许可以作为编译的经验罢:</p> | ||
<ul> | ||
|
@@ -323,8 +491,8 @@ <h1 id="完结撒花">完结撒花</h1> | |
</li> | ||
</ul> | ||
<p>不说了,抓紧攒点钱开学升级电脑配置要紧……磁盘快炸了……</p> | ||
<p class="time">2024.2.12,甲辰年正月初三<br>2024.4.24改</p> | ||
<script src="https://www.qin-juan-ge-zhu.top/common/js/comment.js"></script> | ||
<p class="time">2024.2.12,甲辰年正月初三<br>2024.4.24改<br>2024.11.19改</p> | ||
<script src="https://test.qin-juan-ge-zhu.top/common/js/comment.js"></script> | ||
</div> | ||
</div> | ||
</body> | ||
|
Oops, something went wrong.