评论

收藏

[HarmonyOS] OpenHarmony-2.0-Canary 编译构建流程

移动开发 移动开发 发布于:2021-07-02 17:25 | 阅读数:302 | 评论:0

  2.0版本的编译发生了很大的变化,由hb命令,变成了build.sh。经过分析发现,build.sh也是调用各种的python脚本来进行构建。这样看来1.1.0用json写配置,用python的 hb 解析,再调用gn、ninja来执行编译构建反而更简单了。估计下个版本还会改,我估计会统一成hb命令。
  所以这次先简单分析下。
  ./build.sh --product-name Hi3516DV300
-x    #回显参数和命令
-v    #回显执行的源码
打开 OpenHarmony\build\build_scripts 下的 build.sh、build_common.sh、build_ohos.sh、build_standard.sh、parse_params.sh
将set -e 改为 set -exv 开启调试模式
build.sh

  加了调试参数,我们来分析下 build.sh 在编译构建中做了什么。
# 各种判断和获取参数
# preloader 一.预加载
${PYTHON3} ${source_root_dir}/build/loader/preloader/preloader.py \
  --product-name ${product_name} \
  --source-root-dir ${source_root_dir} \
  --products-config-dir "productdefine/common/products" \
  --preloader-output-root-dir "out/build_configs"
  
# 设置环境变量
source ${source_root_dir}/out/build_configs/${product_name}/preloader/build.prop
# source /home/bright/OpenHarmony/out/build_configs/Hi3516DV300/preloader/build.prop
# call build 二.编译构建
${source_root_dir}/build/build_scripts/build_${system_type}.sh \
  --product-name ${product_name} \
  --device-name ${device_name} \
  --target-os ${target_os} \
  --target-cpu ${target_cpu} \
  ${build_params}
一.预加载

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

DSC0000.png
DSC0001.png
二.编译构建 build_standard.sh

  这个脚本有2个任务:

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

  • 将编译好的文件打包成镜像。
build/build_scripts/build_standard.sh --product-name Hi3516DV300 --device-name hi3516dv300 --target-os ohos --target-cpu arm
# 换个姿势,方便查看
build_standard.sh 
  --product-name Hi3516DV300 
  --device-name hi3516dv300 
  --target-os ohos 
  --target-cpu arm
  接下来先分析下 build_standard.sh
Z:\OpenHarmony\build\build_scripts\build_standard.sh
# 忽略不重要的步骤,直接看核心部分
function main() {
  # build ohos
  do_make_ohos  # 这个函数在 OpenHarmony\build\build_scripts\build_common.sh 文件中。
  # 而最终这个函数会生成,下面这条构建脚本,主要的编译工作都是由 build_ohos.sh 来完成的。
+ build/build_scripts/build_ohos.sh device_type=Hi3516DV300 target_os=ohos target_cpu=arm gn_args=is_standard_system=true  
  ... ...
  # build images
  build/adapter/images/build_image.sh --device-name ${device_name} \
  --ohos-build-out-dir ${ohos_build_root_dir}/packages/phone
  # 第二部分重要工作是生成镜像。参考:2.【生成镜像】
+ 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【编译构建】


  这个脚本的结构就简单了

  • 参数初始化
  • 执行编译构建gn、ninjia
  • 统计分析结果
build/build_scripts/build_ohos.sh 
  device_type=Hi3516DV300 
  target_os=ohos 
  target_cpu=arm 
  gn_args=is_standard_system=true
# 忽略不重要的步骤,直接看核心部分
main()
{
  source ${BUILD_SCRIPT_DIR}/pre_process.sh
  pre_process "$@" # 再执行a.参数初始化
  source ${BUILD_SCRIPT_DIR}/make_main.sh
  do_make "$@" # 再执行b.Gn和ninja的命令都是在这个脚本执行
  source ${BUILD_SCRIPT_DIR}/post_process.sh
  post_process "$@" # 再执行c.构建完成之后进行的收尾工作
  
gn_args=is_standard_system=true
  exit $RET
}
main "$@"
================================================================================================================================
【参数设置】
+ BIN_PATH=/home/bright/OpenHarmony/build/build_scripts
+ BASE_HOME=/home/bright/OpenHarmony
+ BUILD_SCRIPT_DIR=/home/bright/OpenHarmony/build/core/build_scripts
【脚本执行】
+ pre_process device_type=Hi3516DV300 target_os=ohos target_cpu=arm gn_args=is_standard_system=true
+ main device_type=Hi3516DV300 target_os=ohos target_cpu=arm gn_args=is_standard_system=true
+ post_process device_type=Hi3516DV300 target_os=ohos target_cpu=arm
a.pre_process.sh
  对各种参数进行初始化
pre_process()
{
  echo "pre_process"
  case $(uname -s) in
    Darwin)
      HOST_DIR="darwin-x86"
      HOST_OS="mac"
      ;;
    Linux)
      HOST_DIR="linux-x86"
      HOST_OS="linux"
      ;;
    *)
      echo "Unsupported host platform: $(uname -s)"
      RET=1
      exit $RET
  esac
  export PATH=${BASE_HOME}/prebuilts/python/${HOST_DIR}/3.8.5/bin:${BASE_HOME}/prebuilts/build-tools/${HOST_DIR}/bin:$PATH
  python --version
  # 初始化了4个脚本,对参数进行初始化,这块先不展开了,下个版本不改再来分析
  source ${BUILD_SCRIPT_DIR}/init_parameters.sh # 给参数赋值
  source ${BUILD_SCRIPT_DIR}/parse_cmdline.sh
  source ${BUILD_SCRIPT_DIR}/common_fun.sh
  source ${BUILD_SCRIPT_DIR}/trap_ctrlc.sh
  init_parameter "$@" # 这个脚本没啥好说的,就是参数赋值
  parse_cmdline "$@"  # 解析命令行
  # Trap SIGINT
  trap "trap_ctrlc" 2
  if [ "${PYCACHE_ENABLE}" == true ];then
    source ${BUILD_SCRIPT_DIR}/set_pycache.sh
    set_pycache
  fi
}
================================================================================================================================
【生成环境变量】
+ 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
【执行脚本】
+ init_parameter device_type=Hi3516DV300 target_os=ohos target_cpu=arm gn_args=is_standard_system=true
+ parse_cmdline device_type=Hi3516DV300 target_os=ohos target_cpu=arm gn_args=is_standard_system=true
+ 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
build\core\build_scripts\make_main.sh
do_make()
{
  TARGET_OUT_DIR=${BASE_HOME}/${OUT_DIR}/${TARGET_OS}-${TARGET_ARCH}-${BUILD_VARIANT} # 设置输出目录
  
  # 忽略不重要的步骤,直接看核心部分
  # prepare to save build log
  LOG_FILE=${TARGET_OUT_DIR}/build.log
  log_prepare $LOG_FILE # 生成编译构建的log文件,OpenHarmony/out/ohos-arm-release/build.log
  log "$@"
  BEGIN_TIME=$(date "+%s") # 记录当前时间
  source ${BUILD_SCRIPT_DIR}/get_gn_parameters.sh
  get_gn_parameters # 这个脚本用来获取gn的参数
  if [ "${SKIP_GN_PARSE}"x = falsex ]; then # 这里执行了gn命令,并将过程写入log文件,参考下面【gn命令】
    ${BUILD_TOOLS_DIR}/gn gen ${TARGET_OUT_DIR} \
      --args="target_os="${TARGET_OS}" target_cpu="${TARGET_ARCH}" is_debug=false \
      device_type="${DEVICE_TYPE}" is_component_build=true use_custom_libcxx=true \
      ${GN_ARGS} ${TEST_BUILD_PARA_STRING}  ${IS_ASAN} \
      release_test_suite=${RELEASE_TEST_SUITE}" 2>&1 | tee -a $log
    # 忽略部分代码,到这里脚本调用了gn,这部分的内容是gn命令产生的,可以在OpenHarmony/out/ohos-arm-release/build.log中找到
  log "Starting Ninja..."
  NINJA_START_TIME=$(date +%s%N)
  echo python version: $(python --version)
  ninja_build_args="--source-root-dir ${BASE_HOME} --root-build-dir ${TARGET_OUT_DIR} \ # 设置ninja的参数
      --build-target-name ${BUILD_TARGET_NAME}"
  if [ "${TARGET_PLATFORM}" != "" ];then
    ninja_build_args="$ninja_build_args --target-platform ${TARGET_PLATFORM}"
  fi
  # 在执行ninja命令前,还得调用一个python脚本,参考:【构建目标处理程序】
  real_build_target=$(python ${BASE_HOME}/build/scripts/build_target_handler.py $ninja_build_args)
  echo "build_target: "$real_build_target
  if [ "${USE_NARUTO}"x = "truex" ];then
    ${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
  else
    # 整个构建脚本最核心的部分,此时构建脚本全部交由ninja处理,打印编译信息,将结果写入log。参考:【ninja命令】
    ${BUILD_TOOLS_DIR}/ninja -d keepdepfile -C ${TARGET_OUT_DIR} ${real_build_target} ${NINJA_ARGS} 2>&1 | tee -a $log
  fi
  # 忽略部分代码
}
================================================================================================================================
【gn命令】
+ /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'

【ninja参数】
+ ninja_build_args='--source-root-dir /home/bright/OpenHarmony --root-build-dir /home/bright/OpenHarmony/out/ohos-arm-release --build-target-name packages'
【构建目标处理程序】
+ python /home/bright/OpenHarmony/build/scripts/build_target_handler.py \
  --source-root-dir /home/bright/OpenHarmony \
  --root-build-dir /home/bright/OpenHarmony/out/ohos-arm-release \
  --build-target-name packages
  
【ninja命令】
+ 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
build\core\build_scripts\post_process.sh
# 忽略统计时间、pycache等部分代码,总结下来就是执行了几个python脚本
【这个python脚本,生成 sorted_action_duration.txt 排序构建各个模块的操作时间】
+ python3 /home/bright/OpenHarmony/build/scripts/ninja2trace.py \
  --ninja-log /home/bright/OpenHarmony/out/ohos-arm-release/.ninja_log \
  --trace-file /home/bright/OpenHarmony/out/ohos-arm-release/build.trace \
  --ninja-start-time 1623461510200574308 \
  --duration-file /home/bright/OpenHarmony/out/ohos-arm-release/sorted_action_duration.txt
【计算C目标重叠率统计】        
+ python3 /home/bright/OpenHarmony/build/ohos/statistics/build_overlap_statistics.py \
  --build-out-dir /home/bright/OpenHarmony/out/ohos-arm-release \
  --subsystem-config-file /home/bright/OpenHarmony/build/subsystem_config.json \
  --root-source-dir /home/bright/OpenHarmony
  
【获取构建警告列表】
+ python3 /home/bright/OpenHarmony/build/scripts/get_warnings.py \
  --build-log-file /home/bright/OpenHarmony/out/ohos-arm-release/build.log \
  --warning-out-file /home/bright/OpenHarmony/out/ohos-arm-release/packages/WarningList.txt
2.build_image.sh【生成镜像】

  这个脚本的主要工作

  • 复制 root 目录
  • 生成镜像
build/adapter/images/build_image.sh --device-name hi3516dv300 \
  --ohos-build-out-dir /home/bright/OpenHarmony/out/ohos-arm-release/packages/phone
  
  ... ...
  
prepare_root # a.复制 root 目录
build_vendro_image # b.生成 vendr.image
build_system_image # c.生成 system.image
build_userdata_image # d.生成 userdata.image
if [[ "${device_name}" == "hi3516dv300" ]]; then
  source ${OHOS_ROOT_PATH}/build/adapter/images/updater/build_updater_image.sh
fi
a.复制 root 目录
function prepare_root() {
  if [[ -d "${ohos_build_out_dir}/images/root" ]]; then
    rm -rf ${ohos_build_out_dir}/images/root
  fi
  cp -arf prebuilts/aosp_prebuilt_libs/minisys/root ${ohos_build_out_dir}/images/
  local dir_list=(acct apex cache config data debug_ramdisk dev mnt oem proc sbin storage sys system vendor)
  pushd ${ohos_build_out_dir}/images/root # 【pushd 把当前工作目录压栈,成为栈顶】
  for _path in ${dir_list[@]}
  do
    if [[ ! -d "${_path}" ]]; then
      mkdir ${_path}  # 【新建 dir_list 里的目录】
    fi
  done
  popd # 【popd 将目录弹出目录栈】
}
================================================================================================================================
【复制root过来】
cp -arf prebuilts/aosp_prebuilt_libs/minisys/root /home/bright/OpenHarmony/out/ohos-arm-release/packages/phone/images/
【然后新建下面列表的文件夹】
(acct apex cache config data debug_ramdisk dev mnt oem proc sbin storage sys system vendor)
b.生成 vendr.image
function build_vendro_image() {
  cp -arf prebuilts/aosp_prebuilt_libs/minisys/vendor ${ohos_build_out_dir}/images/
  if [[ -d "${ohos_build_out_dir}/vendor" ]]; then
    cp -arf ${ohos_build_out_dir}/vendor/* ${ohos_build_out_dir}/images/vendor/
  fi
  # remove img
  rm -rf ${ohos_build_out_dir}/images/vendor.img
  # build system image
  PATH=prebuilts/aosp_prebuilt_libs/host_tools/bin:$PATH prebuilts/aosp_prebuilt_libs/host_tools/releasetools/build_image.py \
    ${ohos_build_out_dir}/images/vendor \
    prebuilts/aosp_prebuilt_libs/minisys/vendor_image_info.txt \
    ${ohos_build_out_dir}/images/vendor.img \
    ${ohos_build_out_dir}/images/system   
  if [[ "${PIPESTATUS[0]}" -ne 0 ]]; then
    echo "\033[31m  build: build vendor image error.\033[0m"
    exit 1
  fi
  echo -e "\033[32m  build vendor image successful.\033[0m"
}
================================================================================================================================
【vendor的来源有两个】
# 1.prebuilts/aosp_prebuilt_libs/minisys/vendor
# 2.out/ohos-arm-release/packages/phone/vendor
+ cp -arf prebuilts/aosp_prebuilt_libs/minisys/vendor /home/bright/OpenHarmony/out/ohos-arm-release/packages/phone/images/
+ cp -arf /home/bright/OpenHarmony/out/ohos-arm-release/packages/phone/vendor/* /home/bright/OpenHarmony/out/ohos-arm-release/packages/phone/images/vendor/
【设置环境变量】
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
【生成镜像】
+ prebuilts/aosp_prebuilt_libs/host_tools/releasetools/build_image.py \
  /home/bright/OpenHarmony/out/ohos-arm-release/packages/phone/images/vendor \
  prebuilts/aosp_prebuilt_libs/minisys/vendor_image_info.txt \
  /home/bright/OpenHarmony/out/ohos-arm-release/packages/phone/images/vendor.img \
  /home/bright/OpenHarmony/out/ohos-arm-release/packages/phone/images/system
c.生成 system.image
function _update_build_prop() {
  local system_build_prop_file=${ohos_build_out_dir}/images/system/build.prop
  local ohos_build_prop_file=${OHOS_ROOT_PATH}/build/adapter/makefiles/ohos_system.prop   
  if [[ -f "${ohos_build_prop_file}" ]] && [[ -f "${system_build_prop_file}" ]]; then
    echo '' >> ${system_build_prop_file}
    cat ${ohos_build_prop_file} >> ${system_build_prop_file}
  fi
}
function build_system_image() {
  if [[ ! -d "${ohos_build_out_dir}/images" ]]; then
    mkdir ${ohos_build_out_dir}/images
  fi
  cp -arf prebuilts/aosp_prebuilt_libs/minisys/system ${ohos_build_out_dir}/images/
  cp -arf ${ohos_build_out_dir}/system/* ${ohos_build_out_dir}/images/system/  
  # update build.prop
  _update_build_prop
  # remove img
  rm -rf ${ohos_build_out_dir}/images/system.img
  # build system image
  PATH=prebuilts/aosp_prebuilt_libs/host_tools/bin:$PATH prebuilts/aosp_prebuilt_libs/host_tools/releasetools/build_image.py \
    ${ohos_build_out_dir}/images/system \
    prebuilts/aosp_prebuilt_libs/minisys/system_image_info.txt \
    ${ohos_build_out_dir}/images/system.img \
    ${ohos_build_out_dir}/images/system    
  if [[ "${PIPESTATUS[0]}" -ne 0 ]]; then
    echo "\033[31m  build: build system image error.\033[0m"
    exit 1
  fi
  echo -e "\033[32m  build system image successful.\033[0m"
}
================================================================================================================================
【local命令用于在函数内定义局部变量】
+ local system_build_prop_file=/home/bright/OpenHarmony/out/ohos-arm-release/packages/phone/images/system/build.prop
+ local ohos_build_prop_file=/build/adapter/makefiles/ohos_system.prop
【system的来源也有2个】
# 1.prebuilts/aosp_prebuilt_libs/minisys/system
# 2.out/ohos-arm-release/packages/phone/system
cp -arf prebuilts/aosp_prebuilt_libs/minisys/system /home/bright/OpenHarmony/out/ohos-arm-release/packages/phone/images/
cp -arf /home/bright/OpenHarmony/out/ohos-arm-release/packages/phone/system/* /home/bright/OpenHarmony/out/ohos-arm-release/packages/phone/images/system/  
【环境变量】
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
【镜像生成】
prebuilts/aosp_prebuilt_libs/host_tools/releasetools/build_image.py \
  /home/bright/OpenHarmony/out/ohos-arm-release/packages/phone/images/system \
  prebuilts/aosp_prebuilt_libs/minisys/system_image_info.txt \
  /home/bright/OpenHarmony/out/ohos-arm-release/packages/phone/images/system.img \
  /home/bright/OpenHarmony/out/ohos-arm-release/packages/phone/images/system
d.生成 userdata.image
function build_userdata_image() {
  if [[ -d "${ohos_build_out_dir}/images/data" ]]; then
    rm -rf ${ohos_build_out_dir}/images/data
  fi
  mkdir ${ohos_build_out_dir}/images/data
  # build userdat image
  PATH=prebuilts/aosp_prebuilt_libs/host_tools/bin:$PATH prebuilts/aosp_prebuilt_libs/host_tools/releasetools/build_image.py \
    ${ohos_build_out_dir}/images/data \
    prebuilts/aosp_prebuilt_libs/minisys/userdata_image_info.txt \
    ${ohos_build_out_dir}/images/userdata.img \
    ${ohos_build_out_dir}/images/system    
  if [[ "${PIPESTATUS[0]}" -ne 0 ]]; then
    echo "\033[31m  build: build userdata image error.\033[0m"
    exit 1
  fi
  echo -e "\033[32m  build userdata image successful.\033[0m"
}
================================================================================================================================
【新建data目录】
out/ohos-arm-release/packages/phone/images/data
【环境变量】
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
【镜像生成】
prebuilts/aosp_prebuilt_libs/host_tools/releasetools/build_image.py \
  /home/bright/OpenHarmony/out/ohos-arm-release/packages/phone/images/data \
  prebuilts/aosp_prebuilt_libs/minisys/userdata_image_info.txt \
  /home/bright/OpenHarmony/out/ohos-arm-release/packages/phone/images/userdata.img \
  /home/bright/OpenHarmony/out/ohos-arm-release/packages/phone/images/system
3.关于build_image.py

  这个python脚本多次被调用,如果下次版本升级还用它,我们再来深挖一下,这次先放他一马。
  prebuilts/aosp_prebuilt_libs/host_tools/releasetools/build_image.py
properties_file
build_image.py [输入目录] [属性文件] [输出文件] [输出录了]
  in_dir = argv[0]
  glob_dict_file = argv[1]
  out_file = argv[2]
  target_out = argv[3]
  往期文章:
鸿蒙编译构建丨hb工具分析
关注下面的标签,发现更多相似文章