OpenHarmony-2.0-Canary 编译构建流程

系统
文章由鸿蒙社区产出,想要了解更多内容请前往:51CTO和华为官方战略合作共建的鸿蒙技术社区https://harmonyos.51cto.com

[[407354]]

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

OpenHarmony-2.0-Canary

2.0版本的编译发生了很大的变化,由hb命令,变成了build.sh。经过分析发现,build.sh也是调用各种的python脚本来进行构建。这样看来1.1.0用json写配置,用python的 hb 解析,再调用gn、ninja来执行编译构建反而更简单了。估计下个版本还会改,我估计会统一成hb命令。

所以这次先简单分析下。

  1. -x      #回显参数和命令 
  2. -v      #回显执行的源码 
  3. 打开 OpenHarmony\build\build_scripts 下的 build.sh、build_common.sh、build_ohos.sh、build_standard.sh、parse_params.sh 
  4. set -e 改为 set -exv 开启调试模式 

build.sh

加了调试参数,我们来分析下 build.sh 在编译构建中做了什么。

  1. # 各种判断和获取参数 
  2.  
  3. # preloader 一.预加载 
  4. ${PYTHON3} ${source_root_dir}/build/loader/preloader/preloader.py \ 
  5.   --product-name ${product_name} \ 
  6.   --source-root-dir ${source_root_dir} \ 
  7.   --products-config-dir "productdefine/common/products" \ 
  8.   --preloader-output-root-dir "out/build_configs" 
  9.    
  10. # 设置环境变量 
  11. source ${source_root_dir}/out/build_configs/${product_name}/preloader/build.prop 
  12. # source /home/bright/OpenHarmony/out/build_configs/Hi3516DV300/preloader/build.prop 
  13.  
  14. # call build 二.编译构建 
  15. ${source_root_dir}/build/build_scripts/build_${system_type}.sh \ 
  16.   --product-name ${product_name} \ 
  17.   --device-name ${device_name} \ 
  18.   --target-os ${target_os} \ 
  19.   --target-cpu ${target_cpu} \ 
  20.   ${build_params} 

一、预加载

debug的方法,用单步调试来看看这个 preloader.py 的作用。

调试参数:

  1.     // 使用 IntelliSense 了解相关属性。  
  2.     // 悬停以查看现有属性的描述。 
  3.     // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 
  4.     "version""0.2.0"
  5.     "configurations": [ 
  6.         { 
  7.             "name""preloader.py"
  8.             "type""python"
  9.             "request""launch"
  10.             "program""./build/loader/preloader/preloader.py"
  11.             "console""integratedTerminal"
  12.             "args": [ 
  13.                 "--product-name","Hi3516DV300"
  14.                 "--source-root-dir",""
  15.                 "--products-config-dir","productdefine/common/products"
  16.                 "--preloader-output-root-dir","out/build_configs" 
  17.             ], 
  18.             "stopOnEntry"true 
  19.         } 
  20.     ] 

调用了2个python文件,Z:\OpenHarmony\build\loader\preloader\preloader.py 和 Z:\OpenHarmony\build\scripts\util\file_utils.py

这个函数有4个参数。

  1. python3 build/loader/preloader/preloader.py \ 
  2.     --product-name Hi3516DV300 \ 
  3.     --source-root-dir "" \ 
  4.     --products-config-dir productdefine/common/products \ 
  5.     --preloader-output-root-dir out/build_configs 
  6.  
  7. Z:\OpenHarmony\build\loader\preloader\preloader.py:180 
  8. # 建议断点打在这,跳过import 
  9. parser = argparse.ArgumentParser() 

全部操作主要就是def _run(args):这一个函数

  1. Z:\OpenHarmony\build\loader\preloader\preloader.py:133 
  2. def _run(args): 
  3.     # 忽略不重要的步骤,直接看核心部分 
  4.      
  5.     # 生成 out/build_configs/Hi3516DV300/preloader/parts.json 
  6.     write_json_file(parts_info_file, parts_config_info) 
  7.  
  8.     # 获取 OpenHarmony/build/loader/preloader/platforms.template 这个模板 
  9.     platforms_template_file = _get_platform_template_file(args.source_root_dir) 
  10.      
  11.     # 复制 OpenHarmony\out\build_configs\standard_system\platforms.build 
  12.     _copy_platforms_config(platforms_template_file, parts_info_file, 
  13.                            platform_config_output_path) 
  14.  
  15.     _build_info_list = [] 
  16.     build_info_file = os.path.join(product_info_output_path, 'build.prop'
  17.     for k, v in build_configs.items(): 
  18.         _build_info_list.append('{}={}'.format(k, v)) 
  19.      
  20.     # 生成 out/build_configs/Hi3516DV300/preloader/build.prop 
  21.     write_file(build_info_file, '\n'.join(_build_info_list)) 

简单来说就是通过产品形态配置仓调取3个json文件+1个platforms.template,生成3个配置文件

产品形态配置仓调取3个json文件,具体可以参考:https://gitee.com/openharmony/productdefine_common

配置文件:

  1. 配置文件: 
  2. productdefine/common 
  3. ├── base                         # 不同系统类型的最小系统,开发者不用配置,系统提供 
  4. │   └── standard_system.json 
  5. ├── device                       # device配置,配置文件按照device name命名 
  6. │   └── hi3516dv300.json 
  7. └── products                     # 产品配置,配置文件按照product name命名 
  8.     └── Hi3516DV300.json 
  9.  
  10. 输出文件: 
  11. ├── Hi3516DV300 
  12. │   └── preloader 
  13. │       ├── build.prop 
  14. │       └── parts.json 
  15. └── standard_system 
  16.     └── platforms.build 

 来个更直观的对比图吧。

二、编译构建 build_standard.sh

这个脚本有2个任务:

准备好参数执行gn、ninja构建命令。

将编译好的文件打包成镜像。

  1. build/build_scripts/build_standard.sh --product-name Hi3516DV300 --device-name hi3516dv300 --target-os ohos --target-cpu arm 
  2. # 换个姿势,方便查看 
  3. build_standard.sh  
  4.     --product-name Hi3516DV300  
  5.     --device-name hi3516dv300  
  6.     --target-os ohos  
  7.     --target-cpu arm 

接下来先分析下 build_standard.sh

  1. Z:\OpenHarmony\build\build_scripts\build_standard.sh 
  2.  
  3. # 忽略不重要的步骤,直接看核心部分 
  4.  
  5. function main() { 
  6.   # build ohos 
  7.   do_make_ohos  # 这个函数在 OpenHarmony\build\build_scripts\build_common.sh 文件中。 
  8.   # 而最终这个函数会生成,下面这条构建脚本,主要的编译工作都是由 build_ohos.sh 来完成的。 
  9. + build/build_scripts/build_ohos.sh device_type=Hi3516DV300 target_os=ohos target_cpu=arm gn_args=is_standard_system=true   
  10.  
  11.   ... ... 
  12.  
  13.   # build images 
  14.   build/adapter/images/build_image.sh --device-name ${device_name} \ 
  15.     --ohos-build-out-dir ${ohos_build_root_dir}/packages/phone 
  16.   # 第二部分重要工作是生成镜像。参考:2.【生成镜像】 
  17. + build/adapter/images/build_image.sh --device-name hi3516dv300 --ohos-build-out-dir /home/bright/OpenHarmony/out/ohos-arm-release/packages/phone 

1.build_ohos.sh【编译构建】

这个脚本的结构就简单了

  1. 参数初始化
  2. 执行编译构建gn、ninjia
  3. 统计分析结果
  1. build/build_scripts/build_ohos.sh  
  2.     device_type=Hi3516DV300  
  3.     target_os=ohos  
  4.     target_cpu=arm  
  5.     gn_args=is_standard_system=true 
  6.  
  7. # 忽略不重要的步骤,直接看核心部分 
  8.  
  9. main() 
  10.     source ${BUILD_SCRIPT_DIR}/pre_process.sh 
  11.     pre_process "$@" # 再执行a.参数初始化 
  12.  
  13.     source ${BUILD_SCRIPT_DIR}/make_main.sh 
  14.     do_make "$@" # 再执行b.Gn和ninja的命令都是在这个脚本执行 
  15.  
  16.     source ${BUILD_SCRIPT_DIR}/post_process.sh 
  17.     post_process "$@" # 再执行c.构建完成之后进行的收尾工作 
  18.      
  19. gn_args=is_standard_system=true 
  20.     exit $RET 
  21.  
  22. main "$@" 
  23. ================================================================================================================================ 
  24. 【参数设置】 
  25. + BIN_PATH=/home/bright/OpenHarmony/build/build_scripts 
  26. + BASE_HOME=/home/bright/OpenHarmony 
  27. + BUILD_SCRIPT_DIR=/home/bright/OpenHarmony/build/core/build_scripts 
  28.  
  29. 【脚本执行】 
  30. + pre_process device_type=Hi3516DV300 target_os=ohos target_cpu=arm gn_args=is_standard_system=true 
  31. + main device_type=Hi3516DV300 target_os=ohos target_cpu=arm gn_args=is_standard_system=true 
  32. + post_process device_type=Hi3516DV300 target_os=ohos target_cpu=arm  

a.pre_process.sh

对各种参数进行初始化

  1. pre_process() 
  2.     echo "pre_process" 
  3.     case $(uname -s) in 
  4.         Darwin) 
  5.             HOST_DIR="darwin-x86" 
  6.             HOST_OS="mac" 
  7.             ;; 
  8.         Linux) 
  9.             HOST_DIR="linux-x86" 
  10.             HOST_OS="linux" 
  11.             ;; 
  12.         *) 
  13.             echo "Unsupported host platform: $(uname -s)" 
  14.             RET=1 
  15.             exit $RET 
  16.     esac 
  17.  
  18.     export PATH=${BASE_HOME}/prebuilts/python/${HOST_DIR}/3.8.5/bin:${BASE_HOME}/prebuilts/build-tools/${HOST_DIR}/bin:$PATH 
  19.     python --version 
  20.  
  21.     # 初始化了4个脚本,对参数进行初始化,这块先不展开了,下个版本不改再来分析 
  22.     source ${BUILD_SCRIPT_DIR}/init_parameters.sh # 给参数赋值 
  23.     source ${BUILD_SCRIPT_DIR}/parse_cmdline.sh 
  24.     source ${BUILD_SCRIPT_DIR}/common_fun.sh 
  25.     source ${BUILD_SCRIPT_DIR}/trap_ctrlc.sh 
  26.  
  27.     init_parameter "$@" # 这个脚本没啥好说的,就是参数赋值 
  28.     parse_cmdline "$@"  # 解析命令行 
  29.     # Trap SIGINT 
  30.     trap "trap_ctrlc" 2 
  31.  
  32.     if [ "${PYCACHE_ENABLE}" == true ];then 
  33.         source ${BUILD_SCRIPT_DIR}/set_pycache.sh 
  34.         set_pycache 
  35.     fi 
  36. ================================================================================================================================ 
  37. 【生成环境变量】 
  38. + export PATH=/home/bright/OpenHarmony/prebuilts/python/linux-x86/3.8.5/bin:/home/bright/OpenHarmony/prebuilts/build-tools/linux-x86/bin:                                                                                                /home/bright/.local/bin:/home/bright/OpenHarmony/prebuilts/build-tools/common/nodejs/node-v12.18.4-linux-x64/bin:/home/bright/.local/bin:                                                                                                /home/bright/.local/bin:/home/bright/developtools:/home/bright/developtools/xpack-openocd-0.10.0-15/bin:/home/bright/developtools/gcc-lin                                                                                                aro-arm-linux-gnueabihf-4.9-2014.07_linux/bin:/home/bright/developtools/gcc_riscv32/bin:/home/bright/developtools/hc-gen:/home/bright/dev                                                                                                eloptools/llvm/bin:/home/bright/developtools/ninja:/home/bright/developtools/gn:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/                                                                                                bin:/usr/games:/usr/local/games:/snap/bin 
  39. 【执行脚本】 
  40. + init_parameter device_type=Hi3516DV300 target_os=ohos target_cpu=arm gn_args=is_standard_system=true 
  41. + parse_cmdline device_type=Hi3516DV300 target_os=ohos target_cpu=arm gn_args=is_standard_system=true 
  42. + trap trap_ctrlc 2 # trap运行 trap_ctrlc 脚本。TODO 

b.make_main.sh丨gn、ninja

【重点】来看这个脚本,Gn和ninja的命令都是在这个脚本执行

参数: device_type=Hi3516DV300 target_os=ohos target_cpu=arm gn_args=is_standard_system=true

  1. build\core\build_scripts\make_main.sh 
  2.  
  3. do_make() 
  4.     TARGET_OUT_DIR=${BASE_HOME}/${OUT_DIR}/${TARGET_OS}-${TARGET_ARCH}-${BUILD_VARIANT} # 设置输出目录 
  5.      
  6.     # 忽略不重要的步骤,直接看核心部分 
  7.  
  8.     # prepare to save build log 
  9.     LOG_FILE=${TARGET_OUT_DIR}/build.log 
  10.     log_prepare $LOG_FILE # 生成编译构建的log文件,OpenHarmony/out/ohos-arm-release/build.log 
  11.     log "$@" 
  12.  
  13.     BEGIN_TIME=$(date "+%s") # 记录当前时间 
  14.  
  15.     source ${BUILD_SCRIPT_DIR}/get_gn_parameters.sh 
  16.     get_gn_parameters # 这个脚本用来获取gn的参数 
  17.  
  18.     if [ "${SKIP_GN_PARSE}"x = falsex ]; then # 这里执行了gn命令,并将过程写入log文件,参考下面【gn命令】 
  19.         ${BUILD_TOOLS_DIR}/gn gen ${TARGET_OUT_DIR} \ 
  20.             --args="target_os=\"${TARGET_OS}\" target_cpu=\"${TARGET_ARCH}\" is_debug=false \ 
  21.             device_type=\"${DEVICE_TYPE}\" is_component_build=true use_custom_libcxx=true \ 
  22.             ${GN_ARGS} ${TEST_BUILD_PARA_STRING}  ${IS_ASAN} \ 
  23.             release_test_suite=${RELEASE_TEST_SUITE}" 2>&1 | tee -a $log 
  24.  
  25.         # 忽略部分代码,到这里脚本调用了gn,这部分的内容是gn命令产生的,可以在OpenHarmony/out/ohos-arm-release/build.log中找到 
  26.  
  27.     log "Starting Ninja..." 
  28.     NINJA_START_TIME=$(date +%s%N) 
  29.     echo python version: $(python --version) 
  30.     ninja_build_args="--source-root-dir ${BASE_HOME} --root-build-dir ${TARGET_OUT_DIR} \ # 设置ninja的参数 
  31.             --build-target-name ${BUILD_TARGET_NAME}" 
  32.     if [ "${TARGET_PLATFORM}" != "" ];then 
  33.         ninja_build_args="$ninja_build_args --target-platform ${TARGET_PLATFORM}" 
  34.     fi 
  35.     # 在执行ninja命令前,还得调用一个python脚本,参考:【构建目标处理程序】 
  36.     real_build_target=$(python ${BASE_HOME}/build/scripts/build_target_handler.py $ninja_build_args) 
  37.     echo "build_target: "$real_build_target 
  38.  
  39.     if [ "${USE_NARUTO}"x = "truex" ];then 
  40.         ${BUILD_TOOLS_DIR}/naruto -d keepdepfile -p ${BASE_HOME}/.naruto_cache -C ${TARGET_OUT_DIR} ${real_build_target} ${NINJA_ARGS} 2>&1 | tee -a $log 
  41.     else 
  42.         # 整个构建脚本最核心的部分,此时构建脚本全部交由ninja处理,打印编译信息,将结果写入log。参考:【ninja命令】 
  43.         ${BUILD_TOOLS_DIR}/ninja -d keepdepfile -C ${TARGET_OUT_DIR} ${real_build_target} ${NINJA_ARGS} 2>&1 | tee -a $log 
  44.     fi 
  45.  
  46.     # 忽略部分代码 
  47. ================================================================================================================================ 
  48. 【gn命令】 
  49. + /home/bright/OpenHarmony/prebuilts/build-tools/linux-x86/bin/gn gen /home/bright/OpenHarmony/out/ohos-arm-release '--args=target_os="ohos" target_cpu="arm" is_debug=false device_type="Hi3516DV300" is_component_build=true use_custom_libcxx=true is_standard_system=true release_test_suite=false' 
  50.  
  51.  
  52. 【ninja参数】 
  53. + ninja_build_args='--source-root-dir /home/bright/OpenHarmony --root-build-dir /home/bright/OpenHarmony/out/ohos-arm-release --build-target-name packages' 
  54.  
  55. 【构建目标处理程序】 
  56. + python /home/bright/OpenHarmony/build/scripts/build_target_handler.py \ 
  57.     --source-root-dir /home/bright/OpenHarmony \ 
  58.     --root-build-dir /home/bright/OpenHarmony/out/ohos-arm-release \ 
  59.     --build-target-name packages 
  60.      
  61. 【ninja命令】 
  62. + ninja -d keepdepfile -C /home/bright/OpenHarmony/out/ohos-arm-release packages  

c.post_process.sh

构建完成之后进行的收尾工作。

参数:device_type=Hi3516DV300 target_os=ohos target_cpu=arm gn_args=is_standard_system=true

  1. build\core\build_scripts\post_process.sh 
  2.  
  3. # 忽略统计时间、pycache等部分代码,总结下来就是执行了几个python脚本 
  4.  
  5. 【这个python脚本,生成 sorted_action_duration.txt 排序构建各个模块的操作时间】 
  6. + python3 /home/bright/OpenHarmony/build/scripts/ninja2trace.py \ 
  7.     --ninja-log /home/bright/OpenHarmony/out/ohos-arm-release/.ninja_log \ 
  8.     --trace-file /home/bright/OpenHarmony/out/ohos-arm-release/build.trace \ 
  9.     --ninja-start-time 1623461510200574308 \ 
  10.     --duration-file /home/bright/OpenHarmony/out/ohos-arm-release/sorted_action_duration.txt 
  11.  
  12. 【计算C目标重叠率统计】                 
  13. + python3 /home/bright/OpenHarmony/build/ohos/statistics/build_overlap_statistics.py \ 
  14.     --build-out-dir /home/bright/OpenHarmony/out/ohos-arm-release \ 
  15.     --subsystem-config-file /home/bright/OpenHarmony/build/subsystem_config.json \ 
  16.     --root-source-dir /home/bright/OpenHarmony 
  17.      
  18. 【获取构建警告列表】 
  19. + python3 /home/bright/OpenHarmony/build/scripts/get_warnings.py \ 
  20.     --build-log-file /home/bright/OpenHarmony/out/ohos-arm-release/build.log \ 
  21.     --warning-out-file /home/bright/OpenHarmony/out/ohos-arm-release/packages/WarningList.txt 

2.build_image.sh【生成镜像】

这个脚本的主要工作

  1. 复制 root 目录
  2. 生成镜像
  1. build/adapter/images/build_image.sh --device-name hi3516dv300 \ 
  2.     --ohos-build-out-dir /home/bright/OpenHarmony/out/ohos-arm-release/packages/phone 
  3.      
  4.     ... ... 
  5.      
  6. prepare_root # a.复制 root 目录 
  7. build_vendro_image # b.生成 vendr.image 
  8. build_system_image # c.生成 system.image 
  9. build_userdata_image # d.生成 userdata.image 
  10.  
  11. if [[ "${device_name}" == "hi3516dv300" ]]; then 
  12.     source ${OHOS_ROOT_PATH}/build/adapter/images/updater/build_updater_image.sh 
  13. fi 

a.复制 root 目录

  1. function prepare_root() { 
  2.     if [[ -d "${ohos_build_out_dir}/images/root" ]]; then 
  3.         rm -rf ${ohos_build_out_dir}/images/root 
  4.     fi 
  5.     cp -arf prebuilts/aosp_prebuilt_libs/minisys/root ${ohos_build_out_dir}/images/ 
  6.     local dir_list=(acct apex cache config data debug_ramdisk dev mnt oem proc sbin storage sys system vendor) 
  7.     pushd ${ohos_build_out_dir}/images/root # 【pushd 把当前工作目录压栈,成为栈顶】 
  8.     for _path in ${dir_list[@]} 
  9.     do 
  10.         if [[ ! -d "${_path}" ]]; then 
  11.             mkdir ${_path}  # 【新建 dir_list 里的目录】 
  12.         fi 
  13.     done 
  14.     popd # 【popd 将目录弹出目录栈】 
  15. ================================================================================================================================ 
  16. 【复制root过来】 
  17. cp -arf prebuilts/aosp_prebuilt_libs/minisys/root /home/bright/OpenHarmony/out/ohos-arm-release/packages/phone/images/ 
  18. 【然后新建下面列表的文件夹】 
  19. (acct apex cache config data debug_ramdisk dev mnt oem proc sbin storage sys system vendor) 

b.生成 vendr.image

  1. function build_vendro_image() { 
  2.     cp -arf prebuilts/aosp_prebuilt_libs/minisys/vendor ${ohos_build_out_dir}/images/ 
  3.     if [[ -d "${ohos_build_out_dir}/vendor" ]]; then 
  4.         cp -arf ${ohos_build_out_dir}/vendor/* ${ohos_build_out_dir}/images/vendor/ 
  5.     fi 
  6.     # remove img 
  7.     rm -rf ${ohos_build_out_dir}/images/vendor.img 
  8.     # build system image 
  9.     PATH=prebuilts/aosp_prebuilt_libs/host_tools/bin:$PATH prebuilts/aosp_prebuilt_libs/host_tools/releasetools/build_image.py \ 
  10.         ${ohos_build_out_dir}/images/vendor \ 
  11.         prebuilts/aosp_prebuilt_libs/minisys/vendor_image_info.txt \ 
  12.         ${ohos_build_out_dir}/images/vendor.img \ 
  13.         ${ohos_build_out_dir}/images/system    
  14.     if [[ "${PIPESTATUS[0]}" -ne 0 ]]; then 
  15.         echo "\033[31m  build: build vendor image error.\033[0m" 
  16.         exit 1 
  17.     fi 
  18.     echo -e "\033[32m  build vendor image successful.\033[0m" 
  19. ================================================================================================================================ 
  20. 【vendor的来源有两个】 
  21. # 1.prebuilts/aosp_prebuilt_libs/minisys/vendor 
  22. # 2.out/ohos-arm-release/packages/phone/vendor 
  23. + cp -arf prebuilts/aosp_prebuilt_libs/minisys/vendor /home/bright/OpenHarmony/out/ohos-arm-release/packages/phone/images/ 
  24. + cp -arf /home/bright/OpenHarmony/out/ohos-arm-release/packages/phone/vendor/* /home/bright/OpenHarmony/out/ohos-arm-release/packages/phone/images/vendor/ 
  25. 【设置环境变量】 
  26. PATH=prebuilts/aosp_prebuilt_libs/host_tools/bin:/home/bright/.local/bin:/home/bright/OpenHarmony/prebuilts/build-tools/common/nodejs/node-v12.18.4-linux-x64/bin:/home/bright/.local/bin:/home/bright/.local/bin:/home/bright/developtools:/home/bright/developtools/xpack-openocd-0.10.0-15/bin:/home/bright/developtools/gcc-linaro-arm-linux-gnueabihf-4.9-2014.07_linux/bin:/home/bright/developtools/gcc_riscv32/bin:/home/bright/developtools/hc-gen:/home/bright/developtools/llvm/bin:/home/bright/developtools/ninja:/home/bright/developtools/gn:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin 
  27. 【生成镜像】 
  28. + prebuilts/aosp_prebuilt_libs/host_tools/releasetools/build_image.py \ 
  29.     /home/bright/OpenHarmony/out/ohos-arm-release/packages/phone/images/vendor \ 
  30.     prebuilts/aosp_prebuilt_libs/minisys/vendor_image_info.txt \ 
  31.     /home/bright/OpenHarmony/out/ohos-arm-release/packages/phone/images/vendor.img \ 
  32.     /home/bright/OpenHarmony/out/ohos-arm-release/packages/phone/images/system    

c.生成 system.image

  1. function _update_build_prop() { 
  2.     local system_build_prop_file=${ohos_build_out_dir}/images/system/build.prop 
  3.     local ohos_build_prop_file=${OHOS_ROOT_PATH}/build/adapter/makefiles/ohos_system.prop    
  4.     if [[ -f "${ohos_build_prop_file}" ]] && [[ -f "${system_build_prop_file}" ]]; then 
  5.         echo '' >> ${system_build_prop_file} 
  6.         cat ${ohos_build_prop_file} >> ${system_build_prop_file} 
  7.     fi 
  8.  
  9. function build_system_image() { 
  10.     if [[ ! -d "${ohos_build_out_dir}/images" ]]; then 
  11.         mkdir ${ohos_build_out_dir}/images 
  12.     fi 
  13.     cp -arf prebuilts/aosp_prebuilt_libs/minisys/system ${ohos_build_out_dir}/images/ 
  14.     cp -arf ${ohos_build_out_dir}/system/* ${ohos_build_out_dir}/images/system/     
  15.     # update build.prop 
  16.     _update_build_prop 
  17.     # remove img 
  18.     rm -rf ${ohos_build_out_dir}/images/system.img 
  19.     # build system image 
  20.     PATH=prebuilts/aosp_prebuilt_libs/host_tools/bin:$PATH prebuilts/aosp_prebuilt_libs/host_tools/releasetools/build_image.py \ 
  21.         ${ohos_build_out_dir}/images/system \ 
  22.         prebuilts/aosp_prebuilt_libs/minisys/system_image_info.txt \ 
  23.         ${ohos_build_out_dir}/images/system.img \ 
  24.         ${ohos_build_out_dir}/images/system         
  25.     if [[ "${PIPESTATUS[0]}" -ne 0 ]]; then 
  26.         echo "\033[31m  build: build system image error.\033[0m" 
  27.         exit 1 
  28.     fi 
  29.     echo -e "\033[32m  build system image successful.\033[0m" 
  30. ================================================================================================================================ 
  31. local命令用于在函数内定义局部变量】 
  32. local system_build_prop_file=/home/bright/OpenHarmony/out/ohos-arm-release/packages/phone/images/system/build.prop 
  33. local ohos_build_prop_file=/build/adapter/makefiles/ohos_system.prop 
  34. 【system的来源也有2个】 
  35. # 1.prebuilts/aosp_prebuilt_libs/minisys/system 
  36. # 2.out/ohos-arm-release/packages/phone/system 
  37. cp -arf prebuilts/aosp_prebuilt_libs/minisys/system /home/bright/OpenHarmony/out/ohos-arm-release/packages/phone/images/ 
  38. cp -arf /home/bright/OpenHarmony/out/ohos-arm-release/packages/phone/system/* /home/bright/OpenHarmony/out/ohos-arm-release/packages/phone/images/system/     
  39. 【环境变量】 
  40. PATH=prebuilts/aosp_prebuilt_libs/host_tools/bin:/home/bright/.local/bin:/home/bright/OpenHarmony/prebuilts/build-tools/common/nodejs/node-v12.18.4-linux-x64/bin:/home/bright/.local/bin:/home/bright/.local/bin:/home/bright/developtools:/home/bright/developtools/xpack-openocd-0.10.0-15/bin:/home/bright/developtools/gcc-linaro-arm-linux-gnueabihf-4.9-2014.07_linux/bin:/home/bright/developtools/gcc_riscv32/bin:/home/bright/developtools/hc-gen:/home/bright/developtools/llvm/bin:/home/bright/developtools/ninja:/home/bright/developtools/gn:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin 
  41. 【镜像生成】 
  42. prebuilts/aosp_prebuilt_libs/host_tools/releasetools/build_image.py \ 
  43.     /home/bright/OpenHarmony/out/ohos-arm-release/packages/phone/images/system \ 
  44.     prebuilts/aosp_prebuilt_libs/minisys/system_image_info.txt \ 
  45.     /home/bright/OpenHarmony/out/ohos-arm-release/packages/phone/images/system.img \ 
  46.     /home/bright/OpenHarmony/out/ohos-arm-release/packages/phone/images/system 

d.生成 userdata.image

  1. function build_userdata_image() { 
  2.     if [[ -d "${ohos_build_out_dir}/images/data" ]]; then 
  3.         rm -rf ${ohos_build_out_dir}/images/data 
  4.     fi 
  5.     mkdir ${ohos_build_out_dir}/images/data 
  6.     # build userdat image 
  7.     PATH=prebuilts/aosp_prebuilt_libs/host_tools/bin:$PATH prebuilts/aosp_prebuilt_libs/host_tools/releasetools/build_image.py \ 
  8.         ${ohos_build_out_dir}/images/data \ 
  9.         prebuilts/aosp_prebuilt_libs/minisys/userdata_image_info.txt \ 
  10.         ${ohos_build_out_dir}/images/userdata.img \ 
  11.         ${ohos_build_out_dir}/images/system         
  12.     if [[ "${PIPESTATUS[0]}" -ne 0 ]]; then 
  13.         echo "\033[31m  build: build userdata image error.\033[0m" 
  14.         exit 1 
  15.     fi 
  16.     echo -e "\033[32m  build userdata image successful.\033[0m" 
  17. ================================================================================================================================ 
  18. 【新建data目录】 
  19. out/ohos-arm-release/packages/phone/images/data 
  20. 【环境变量】 
  21. PATH=prebuilts/aosp_prebuilt_libs/host_tools/bin:/home/bright/.local/bin:/home/bright/OpenHarmony/prebuilts/build-tools/common/nodejs/node-v12.18.4-linux-x64/bin:/home/bright/.local/bin:/home/bright/.local/bin:/home/bright/developtools:/home/bright/developtools/xpack-openocd-0.10.0-15/bin:/home/bright/developtools/gcc-linaro-arm-linux-gnueabihf-4.9-2014.07_linux/bin:/home/bright/developtools/gcc_riscv32/bin:/home/bright/developtools/hc-gen:/home/bright/developtools/llvm/bin:/home/bright/developtools/ninja:/home/bright/developtools/gn:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin 
  22. 【镜像生成】 
  23. prebuilts/aosp_prebuilt_libs/host_tools/releasetools/build_image.py \ 
  24.     /home/bright/OpenHarmony/out/ohos-arm-release/packages/phone/images/data \ 
  25.     prebuilts/aosp_prebuilt_libs/minisys/userdata_image_info.txt \ 
  26.     /home/bright/OpenHarmony/out/ohos-arm-release/packages/phone/images/userdata.img \ 
  27.     /home/bright/OpenHarmony/out/ohos-arm-release/packages/phone/images/system     

3.关于build_image.py

这个python脚本多次被调用,如果下次版本升级还用它,我们再来深挖一下,这次先放他一马。

  1. properties_file 
  2. build_image.py [输入目录] [属性文件] [输出文件] [输出录了] 
  3.   in_dir = argv[0] 
  4.   glob_dict_file = argv[1] 
  5.   out_file = argv[2] 
  6.   target_out = argv[3] 

 

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

 

责任编辑:jianghua 来源: 鸿蒙社区
相关推荐

2021-09-16 15:04:28

鸿蒙HarmonyOS应用

2021-07-02 14:16:04

鸿蒙HarmonyOS应用

2021-12-02 10:05:01

鸿蒙HarmonyOS应用

2021-06-11 14:16:21

Windows 10WSL OpenHarmon

2024-07-26 16:39:33

鸿蒙系统开源构建系统

2023-06-12 15:43:44

鸿蒙智能家居开发

2021-10-13 07:18:40

鸿蒙HarmonyOS应用

2022-02-21 14:49:26

OpenHarmon操作系统鸿蒙

2023-08-04 15:00:43

ArkTS语言鸿蒙

2023-07-17 16:13:21

组件模块开发的鸿蒙

2021-07-21 09:58:50

鸿蒙HarmonyOS应用

2021-07-19 15:34:05

鸿蒙HarmonyOS应用

2022-01-11 15:41:18

鸿蒙HarmonyOS应用

2023-07-27 14:38:33

开源鸿蒙

2023-08-11 14:06:58

鸿蒙Windows

2021-07-27 11:54:51

windows系统OpenHarmony

2021-09-08 15:23:51

鸿蒙HarmonyOS应用

2021-08-06 15:09:22

鸿蒙HarmonyOS应用

2023-07-28 15:32:26

鸿蒙操作系统

2022-05-07 16:13:59

DevEcoTool鸿蒙
点赞
收藏

51CTO技术栈公众号