1、环境准备
安装下面这三东西,是为了下载 Harmony 源码
sudo apt install curl
 sudo apt install python3-pip
 sudo apt install git-lfs
安装下面这五个东西,是为了解决编译到最后报错(头铁不信的,你可以试试,等最后再安装)
sudo apt install default-jdk
 sudo apt install mtd-utils
 sudo apt install scons
 sudo apt install gcc-arm-none-eabi
 sudo apt install gcc-arm-linux-gnueabi
sudo apt-get install libxt-dev
sudo apt-get install libx11-dev
sudo apt install xorg-dev
sudo apt-get install liblz4-tool
sudo apt install dwarves
2、安装 repo
 务必按照此步骤安装repo。ubuntu默认提供的repo每次启动都会访问googlge服务器,会被墙。
 此外20.04下需要root身份执行–否则提示没权限。
 curl https://gitee.com/oschina/repo/raw/fork_flow/repo-py3 > /usr/local/bin/repo
 chmod a+x /usr/local/bin/repo
 pip3 install -i Simple index requests
3、下载源码
 [官网版本更新说明}(docs: OpenHarmony documentation | OpenHarmony开发者文档 - Gitee.com)
目前最新为 4.0 版本,内核基于 5.10,老版本 2.x 1.x 已经停止维护,内核基于 4.19
参考官网下载方式
repo init -u OpenHarmony/manifest -b OpenHarmony-4.0-Release --no-repo-verify
 repo sync -c
 repo forall -c ‘git lfs pull’
注意那个 4.0 之前没有 v,以前的版本是有这个 v 的,不然会初始化失败,还有就是注意检查电脑是否断网了
4、下载编译依赖工具包
 源码下载完成后,进入根目录执行 bash build/prebuilts_download.sh
看你网速快慢,我这大概10分钟,下载完成后会多出 openharmony_prebuilts 文件夹
5、编译源码
 ./build.sh --product-name rk3568 --ccache
第一次编译前面阶段 2W 多个文件,后面阶段 6W 多个文件,整体时间看你电脑性能。
编译中间要是出错看提示就行,少啥就自行安装,比如这种
default-jdk is not installed. please install it.
 mtd-utils is not installed. please install it.
 scons is not installed. please install it.
 gcc-arm-none-eabi is not installed. please install it.
 gcc-arm-linux-gnueabi is not installed. please install it.
 Missing dependencies, please check!
 [OHOS INFO] Set cache size limit to 100.0 GB
 [OHOS INFO] generated build prop info to /hom
nce:sync_fence
 All rules passed
 Do NO-Config-Cmds-In-Init rule checking now:
 [WARNING]: local_code_sign is not in start cmd list.  path:/system/etc/init/local_code_sign.cfg
 [WARNING]: intell_voice_service is not in start cmd list.  path:/system/etc/init/intell_voice_service.cfg
   Please refer to: https://gitee.com/openharmony/developtools_integration_verification/tree/master/tools/startup_guard/rules/NO-Config-Cmds-In-Init/README.md
 Do NO-Config-SystemParameter-In-INIT rule checking now:
 Please modify according to README.md
 [OHOS INFO] rk3568 build success
 [OHOS INFO] Cost time:  1:58:23
 =====build  successful=====
 2023-11-29 11:47:19
 ++++++++++++++++++++++++++++++++++++++++
  
---------------------------------------------------------------------------------------------------------------------------------
6、编译错误
1、fatal error: X11/XXXX.h: No such file or directory
[OHOS ERROR] In file included from ../../third_party/flutter/glfw/src/init.c:28:
 [OHOS ERROR] In file included from ../../third_party/flutter/glfw/src/internal.h:169:
 [OHOS ERROR] ../../third_party/flutter/glfw/src/x11_platform.h:39:10: fatal error: 'X11/Xcursor/Xcursor.h' file not found
 [OHOS ERROR] #include <X11/Xcursor/Xcursor.h>
 [OHOS ERROR]          ^~~~~~~~~~~~~~~~~~~~~~~
 [OHOS ERROR] 1 error generated.
 [OHOS ERROR] [7497/20650] CC clang_x64/obj/third_party/flutter/glfw/src/glfw/input.o
 [OHOS ERROR] FAILED: clang_x64/obj/third_party/flutter/glfw/src/glfw/input.o
sudo apt-get install libxt-dev
sudo apt-get install libx11-dev
sudo apt install xorg-dev
2、error: use of undeclared identifier 'strlen'
[OHOS ERROR]     idxVal = (idxVal >= projectModels.size()) ? 0 : idxVal;
 [OHOS ERROR]               ~~~~~~ ^  ~~~~~~~~~~~~~~~~~~~~
 [OHOS ERROR] ../../ide/tools/previewer/util/CommandParser.cpp:785:36: warning: comparison of integers of different signs: 'int' and 'std::vector::size_type' (aka 'unsigned long') [-Wsign-compare]
 [OHOS ERROR]     if (enumValue < 0 || enumValue >= projectModels.size()) {
 [OHOS ERROR]                          ~~~~~~~~~ ^  ~~~~~~~~~~~~~~~~~~~~
 [OHOS ERROR] ../../ide/tools/previewer/util/CommandParser.cpp:855:24: error: use of undeclared identifier 'strlen'
 [OHOS ERROR]     size_t argLength = strlen(str);
 [OHOS ERROR]                        ^
 [OHOS ERROR] 2 warnings and 1 error generated.
CommandParser.cpp 添加
#include <string.h>
3、kernel编译报错|/bin/sh: 1: lz4c: not found
  SORTTAB vmlinux
   SYSMAP  System.map
   OBJCOPY arch/arm64/boot/Image
   LZ4     arch/arm64/boot/Image.lz4
 /bin/sh: 1: lz4c: not found
 make[3]: *** [/home/kevin/disk/code/OpenHarmony4.0/out/kernel/src_tmp/linux-5.10/arch/arm64/boot/Makefile:31: arch/arm64/boot/Image.lz4] Error 127
 make[3]: *** Deleting file 'arch/arm64/boot/Image.lz4'
 make[2]: *** [arch/arm64/Makefile:162: Image.lz4] Error 2
 make[1]: *** [arch/arm64/Makefile:208: rk3568-toybrick-x0-linux.img] Error 2
 make[1]: Leaving directory '/home/kevin/disk/code/OpenHarmony4.0/out/kernel/OBJ/linux-5.10'
 make: *** [Makefile:185: __sub-make] Error 2
sudo apt-get install liblz4-tool
4、 error: Cannot resolve BTF IDs for CONFIG_DEBUG_INFO_BTF, please install libelf-dev, libelf-devel or elfutils-libelf-devel
[OHOS ERROR]   WRAP    arch/arm64/include/generated/asm/dma.h
 [OHOS ERROR]   WRAP    arch/arm64/include/generated/asm/emergency-restart.h
 [OHOS ERROR] error: Cannot resolve BTF IDs for CONFIG_DEBUG_INFO_BTF, please install libelf-dev, libelf-devel or elfutils-libelf-devel
 [OHOS ERROR]   WRAP    arch/arm64/include/generated/asm/hw_irq.h
 [OHOS ERROR]   WRAP    arch/arm64/include/generated/asm/irq_regs.h
 [OHOS ERROR]   WRAP    arch/arm64/include/generated/asm/kdebug.h
 [OHOS ERROR]   WRAP    arch/arm64/include/generated/asm/kmap_types.h
 [OHOS ERROR]   WRAP    arch/arm64/include/generated/asm/local.h
 [OHOS ERROR]   WRAP    arch/arm64/include/generated/asm/local64.h
 [OHOS ERROR]   WRAP    arch/arm64/include/generated/asm/mm-arch-hooks.h
 [OHOS ERROR]   WRAP    arch/arm64/include/generated/asm/mmiowb.h
 [OHOS ERROR]   WRAP    arch/arm64/include/generated/asm/msi.h
 [OHOS ERROR] make[2]: *** [/home/kevin/disk/code/OpenHarmony4.0/out/kernel/src_tmp/linux-5.10/Makefile:1295: prepare-resolve_btfids] Error 1
 [OHOS ERROR] make[2]: *** Waiting for unfinished jobs....
 [OHOS ERROR]   WRAP    arch/arm64/include/generated/asm/serial.h
 [OHOS ERROR]   WRAP    arch/arm64/include/generated/asm/switch_to.h
 [OHOS ERROR]   WRAP    arch/arm64/include/generated/asm/trace_clock.h
 [OHOS ERROR]   WRAP    arch/arm64/include/generated/asm/unaligned.h
 [OHOS ERROR]   WRAP    arch/arm64/include/generated/asm/vga.h
 [OHOS ERROR] make[1]: *** [arch/arm64/Makefile:208: rk3568-toybrick-x0-linux.img] Error 2
 [OHOS ERROR] make[1]: Leaving directory '/home/kevin/disk/code/OpenHarmony4.0/out/kernel/OBJ/linux-5.10'
 [OHOS ERROR] make: *** [Makefile:185: __sub-make] Error 2
 [OHOS ERROR] Traceback (most recent call last):
sudo apt install dwarves
修改文件路径:
/OpenHarmony4.0/kernel/linux/config/linux-5.10/arch/arm64/configs/
| k3568_standard_defconfig | 6608 CONFIG_DEBUG_INFO_BTF=y | 
修改为 :
| rk3568_standard_defconfig | 6608 CONFIG_DEBUG_INFO_BTF is not set | 
5、 genext2fs: command not found
 [OHOS ERROR] E/write_index_tbl(1498): try to write index entry(./arch/arm64/boot/dts/rockchip/rk3568-toybrick-x0-linux.dtb)...
 [OHOS ERROR] E/write_index_tbl(1515): mod fdt path:./arch/arm64/boot/dts/rockchip/rk3568-toybrick-x0-linux.dtb -> rk-kernel.dtb...
 [OHOS ERROR] E/write_index_tbl(1498): try to write index entry(logo.bmp)...
 [OHOS ERROR] E/write_index_tbl(1498): try to write index entry(logo_kernel.bmp)...
 [OHOS ERROR]   Image:  resource.img (with rk3568-toybrick-x0-linux.dtb logo.bmp logo_kernel.bmp) is ready
 [OHOS ERROR] make[1]: Leaving directory '/home/kevin/disk/code/OpenHarmony4.0/out/kernel/OBJ/linux-5.10'
 [OHOS ERROR] ~/disk/code/OpenHarmony4.0/out/rk3568
 [OHOS ERROR] blocks = 4096  block_size 16384
 [OHOS ERROR] ../kernel/src_tmp/linux-5.10/make-boot.sh: line 22: genext2fs: command not found
 [OHOS ERROR] Traceback (most recent call last):
sudo apt install genext2fs