首页 > 行业资讯 > 正文

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

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

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

$./build.sh-h ++++++++++++++++++++++++++++++++++++++++ Thesystemshellisbash4.4.20(1)-release ++++++++++++++++++++++++++++++++++++++++ 2023-02-0712:58:04 -h Usage:entry.py[options] Options: -h,–helpshowthishelpmessageandexit –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 -TBUILD_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 =====buildsuccessful=====

提升OpenHarmony 编译速度的选项

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

./build.sh–product-namerk3568–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-namerk3568–disable-post-build

如何让OpenHarmony编译速度“狂飙”-openharmony1.1

②添加 –disable-package-image 参数

取消最后所有的 image 镜像文件压缩成 tar 包的动作,tar 包位置 out k3568images.tar.gz。

./build.sh–product-namerk3568–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-namerk3568–ccacheexportCCACHE_NOHASHDIR=”true”exportCCACHE_SLOPPINESS=”include_file_ctime” 如何让OpenHarmony编译速度“狂飙”-openharmony1.11

④添加 –fast-rebuild 参数

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

./build.sh–product-namerk3568–fast-rebuild 如何让OpenHarmony编译速度“狂飙”-openharmony1.12

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

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

OpenHarmony 开源软件 Notice 收集策略说明:

https://gitee.com/openharmony/build/blob/master/docs/%E5%BC%80%E6%BA%90%E8%BD%AF%E4%BB%B6Notice%E6%94%B6%E9%9B%86%E7%AD%96%E7%95%A5%E8%AF%B4%E6%98%8E.md ./build.sh–product-namerk3568–gn-argsenable_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 的时候:

-Tpackages–gn-argsskip_gen_module_info=true 去掉 gn阶段 module info 的生成。 ./build.sh–product-namerk3568–build-target模块名-Tpackages–gn-argsskip_gen_module_info=true

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

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

以上参数可叠加使用,例如全量编译,笔者使用下面这条命令编译速度提升了 120%:

./build.sh–product-namerk3568–disable-post-build–disable-package-image–gn-argsenable_notice_collection=false–gn-argsload_test_config=false

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

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

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

如何让OpenHarmony编译速度“狂飙”-openharmony1.14

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

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

exportPATH=$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-namerk3568–ccache–log-level=debug

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

审核编辑:汤梓红

猜你喜欢