5

如何让OpenHarmony编译速度“狂飙”

 1 year ago
source link: https://www.51cto.com/article/746123.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

如何让OpenHarmony编译速度“狂飙”

作者:离北况归 2023-02-09 15:28:19
OpenHarmony有两种编译方式,一种是通过hb工具编译,一种是通过build.sh脚本编译。
d8fec29631689b0ee1c0584e3450fbbcb7bc32.png

​想了解更多关于开源的内容,请访问:​

​51CTO 开源基础软件社区​

​https://ost.51cto.com​

OpenHarmony有两种编译方式,一种是通过hb工具编译,一种是通过build.sh脚本编译。本文笔者将提升build.sh方式编译速度的方法整理如下:

因为笔者只用build.sh脚本编译,没用过hb工具,好像下面的选项也可以用于hb工具。

在OpenHarmony源码中执行./build.sh --h,会打印出./build.sh中可以添加的所有选项

$ ./build.sh -h
++++++++++++++++++++++++++++++++++++++++
The system shell is bash 4.4.20(1)-release
++++++++++++++++++++++++++++++++++++++++
2023-02-07 12:58:04
-h
Usage: entry.py [options]
Options:
-h, --help show this help message and exit
--source-root-dir=SOURCE_ROOT_DIR
--product-name=PRODUCT_NAME
--device-name=DEVICE_NAME
--target-cpu=TARGET_CPU
--target-os=TARGET_OS
--compile-config=COMPILE_CONFIG
-T BUILD_TARGET, --build-target=BUILD_TARGET
--gn-args=GN_ARGS
--ninja-args=NINJA_ARGS
-v, --verbose
--keep-ninja-going
--sparse-image
--jobs=JOBS
--export-para=EXPORT_PARA
--build-only-gn
--ccache
--fast-rebuild
--disable-package-image
--disable-post-build
--disable-part-of-post-build=DISABLE_PART_OF_POST_BUILD
--log-level=LOG_LEVEL
--device-type=DEVICE_TYPE
--build-variant=BUILD_VARIANT
--share-ccache=SHARE_CCACHE
=====build successful=====

提升OpenHarmony编译速度的选项

build.sh脚本编译rk3568方式命令如下:

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

通过在该命令后添加如下选项提升编译速度。

添加 --disable-post-build 参数

取消Postbuild过程,最后的ninja trace解析、每个子系统(不包括源码中的third_party部分)的rom size统计等动作会没有(每个子系统部件描述文件名称为bundle.json,里面定义了子系统的名称。)

提供支持disable post build参数是怎么做的 https://gitee.com/openharmony/build/issues/I5MT9X。

./build.sh --product-name rk3568 --disable-post-build
如何让OpenHarmony编译速度“狂飙”-开源基础软件社区

添加 --disable-package-image参数

取消最后所有的image镜像文件压缩成tar包的动作

tar包位置 out\rk3568\images.tar.gz

./build.sh --product-name rk3568 --disable-package-image

添加 --ccache 参数

ccache会缓存c/c++编译的编译输出,下一次在编译输入不变的情况下,直接复用缓存的产物。用来缓存编译过的.o文件等

执行sudo apt-get install ccache命令安装ccache

再在 --ccache后添加export CCACHE_NOHASHDIR=“true” 和 export CCACHE_SLOPPINESS=“include_file_ctime” (设置ccache在做hash的时候不hash路径、不检查文件的change time)

./build.sh --product-name rk3568 --ccache export CCACHE_NOHASHDIR="true" 
export CCACHE_SLOPPINESS="include_file_ctime"
如何让OpenHarmony编译速度“狂飙”-开源基础软件社区

添加 --fast-rebuild参数

编译流程主要分为:preloader->loader->gn->ninja这四个过程,添加后直接基于已有out/rk3568/build.ninja直接执行编译链接步骤,跳过前面的产品配置解析和gn解析,在gn相关脚本没有发生改变的前提下使用。

./build.sh --product-name rk3568 --fast-rebuild
如何让OpenHarmony编译速度“狂飙”-开源基础软件社区

添加 --gn-args enable_notice_collection=false参数

notice file的搜集用于产品化的LICENSE生成,取消收集开源notice的过程,在非产品化场景开发态可关闭,提升编译速度,节省编译~7%时间。

OpenHarmony开源软件Notice收集策略说明 https://gitee.com/openharmony/build/blob/master/docs/开源软件Notice收集策。

./build.sh --product-name rk3568 --gn-args enable_notice_collection=false

添加 --build-only-gn 参数

重新执行Preloader、loader、gn,不进行最后的编译动作。

编译流程主要分为:preloader->loader->gn->ninja这四个过程,标准系统的编译构建过程请参考https://ost.51cto.com/posts/13594。

添加 --build-target 参数

该参数用于指定编译模块

如何找模块的名字:

  • 相关仓下BUILD.gn中关注group、ohos_shared_library、ohos_executable等关键字。
  • ./build.sh --product-name 产品名 --build-target 模块名 --build-only-gn生成build.ninja,然后去该文件中查找相关模块名。

添加 --gn-args enable_lto_O0=true参数

在链接的时候会减弱优化的等级,建议在只考虑编译是否成功的时候使用(会影响最后的so的性能和rom大小)

添加 --gn-args archive_ndk=false参数

编译sdk的时候不执行输出压缩包的动作

添加 export NO_DEVTOOL=1 参数

取消webpack打包过程中生成sourcemap的动作

添加 --gn-args skip_generate_module_list_file=true参数

跳过为test 生成记录文件的过程,节省gn解析的过程,只要不跑tdd测试用例,这个参数都可以加上,编译tdd用例也没关系

添加 -T packages --gn-args skip_gen_module_info=true参数

在不编译image的时候:-T packages --gn-args skip_gen_module_info=true,去掉gn阶段module info的生成

./build.sh --product-name rk3568 --build-target 模块名 -T packages --gn-args 
skip_gen_module_info=true

添加 --gn-args load_test_config=false参数

在不编译test用例的时候加上 --gn-args load_test_config=false,来去掉gn阶段test相关编译目标的解析。

以上参数可叠加使用

例如全量编译,笔者使用下面这条命令编译速度提升了120%:

./build.sh --product-name rk3568 --disable-post-build --disable-package-image 
--gn-args enable_notice_collection=false --gn-args load_test_config=false

添加 --fast-rebuild参数 方式等效于执行 ninja -C

首先用./build.sh全量编译,然后在源码下执行ninja -C out/rk3568 moduleb_lib(编译对象模块)

# 例如编译wukong部件的二进制可执行文件wukong
# 将gn和ninja可执行文件添加到PATH环境变量的方法(临时改变,只能在当前的终端窗口中有效)
export 
PATH=$PATH:/home/jiajiahao/ohos3.2beta4/sources/prebuilts/build-tools/linux-x86/bin
# 然后在源码目录下执行如下语句
ninja -C out/rk3568 wukong
如何让OpenHarmony编译速度“狂飙”-开源基础软件社区
# 例如编译ace_napi部件的动态库libace_napi.z.so
# 将gn和ninja可执行文件添加到PATH环境变量的方法(临时改变,只能在当前的终端窗口中有效)
export PATH=$PATH:/你自己的源码路径/sources/prebuilts/build-tools/linux-x86/bin
# 然后在源码目录下执行如下语句
ninja -C out/rk3568 ace_napi
如何让OpenHarmony编译速度“狂飙”-开源基础软件社区

将gn和ninja可执行文件添加到PATH环境变量的方法

将gn和ninja可执行文件添加到PATH环境变量的方法(临时改变,只能在当前的终端窗口中有效)。

# 
找到读者你自己的OpenHarmony源码目录下的gn和ninja可执行文件绝对路径,在源码下/prebuilts/build-tools/linux-x86/bin
export 
PATH=$PATH:/home/xxx/xxx/sources/prebuilts/build-tools/linux-x86/bin

例如笔者的gn和ninja可执行文件绝对路径。

export 
PATH=$PATH:/home/jiajiahao/ohos3.2beta4/sources/prebuilts/build-tools/linux-x86/bin

notice file是否收集的编译选项–gn-args enable_notice_collection=false是如何支持的。

相关PR https://gitee.com/openharmony/build/pulls/772/files

指定编译期间的日志级别

在OpenHarmony的build.sh里通过–log-level可以指定编译期间的日志级别,三个级别可选:debug, info和error,默认值是info

./build.sh --product-name rk3568 --ccache --log-level=debug

本地打开ninja trace: 解压out/rk3568/build.trace.gz,将build.trace拖到chrome的trace链接chrome://tracing/打开即可。

​想了解更多关于开源的内容,请访问:​

​51CTO 开源基础软件社区​

​https://ost.51cto.com​

责任编辑:jianghua 来源: 51CTO 开源基础软件社区

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK