OpenHarmony-3.0 编译构建流程 原创 精华

发布于 2021-11-28 17:46
浏览
6收藏

【本文正在参与优质创作者激励】

前言

OpenHarmony-2.0 编译构建流程

早就打算研究下OH3.0的编译框架了,最近一直在搞移植,总算有点进展了,抽个空来分析下3.0的编译框架。大体看了下和2.0的差别不是特别大,OHOS3.0的打包镜像脚本由原来2.0的build\adapter\build_image.sh 全部修改替换为 build\ohos\images\build_image.py 将打包image镜像的部分制作成build_target,以前是通过shell脚本来调用,现在是通过gn和ninja来调用。主要文件在build\ohos\images这个文件夹下。没有修改的部分就不说了,有需要的可以看我之前的帖子 这篇主要说下不一样的地方,看OH3.0是如何将编译好的文件打包成镜像的。

一、增加编译参数

3.0之后在build\build_scripts\build_common.sh 增加了 build_cmd+=" build_target=images",这句的意思约等于在执行默认编译命令 ./build.sh --product-name Hi3516DV300 会有个默认的参数 --build-target images,具体流程是这样的:

build\build_scripts\build_common.sh => build\core\gn\BUILD.gn => build\ohos\images\BUILD.gn

action_with_pydeps("${_platform}_${_image_name}_image") {
    script = "//build/ohos/images/build_image.py"
    depfile = "$target_gen_dir/$target_name.d"
    deps = [ "//build/ohos/packages:${_platform}_install_modules" ]

    image_config_file =
    "//build/ohos/images/mkimage/${_image_name}_image_conf.txt"
    output_image_file = "$current_platform_dir/images/${_image_name}.img"

    image_input_path = "$current_platform_dir/${_image_name}"
    if (_image_name == "userdata") {
        image_input_path = "$current_platform_dir/data"
    }

    sources = [
        image_config_file,
        system_module_info_list,
        system_modules_list,
    ]
    outputs = [ output_image_file ]
    args = [
        "--depfile",
        rebase_path(depfile, root_build_dir),
        "--image-name",
        _image_name,
        "--input-path",
        rebase_path(image_input_path, root_build_dir),
        "--image-config-file",
        rebase_path(image_config_file, root_build_dir),
        "--output-image",
        rebase_path(output_image_file, root_build_dir),
        "--build-image-tools-path",
        rebase_path(build_image_tools_path, root_build_dir),
    ]
    if (sparse_image) {
        args += [ "--sparse-image" ]
    }
}

通常情况下,gn会使用 action 运行一个脚本来生成一个文件,但是这里使用的是 action_with_pydeps,应该也是内置的目标类型。查看官方手册是这么说明的

Inputs and Depfiles

List all files read (or executed) by an action as inputs.

  • It is not enough to have inputs listed by dependent targets. They must be listed directly by targets that use them, or added by a depfile.
  • Non-system Python imports are inputs! For scripts that import such modules, use action_with_pydeps to ensure all dependent Python files are captured as inputs.

前面还定义了一个image_list,然后使用 foreach 执行 action_with_pydeps,要生成几个img文件,就执行几次action_with_pydeps。

  image_list = [
    "system",
    "vendor",
    "userdata",
    "updater",
  ]
  foreach(_image_name, image_list) {... ...}

二、调用python脚本

既然知道了img镜像是由 build\ohos\images\build_image.py 来创建的,那就来分析下这个python脚本。

    if os.path.exists(args.output_image_path):
        os.remove(args.output_image_path)			# 删除之前生成的镜像文件夹
    if args.image_name == 'userdata':
        _prepare_userdata(args.input_path)			# 准备好 userdata.img 需要的文件
    if os.path.isdir(args.input_path):
        _make_image(args)
        _dep_files = []
        for _root, _, _files in os.walk(args.input_path):
            for _file in _files:
                _dep_files.append(os.path.join(_root, _file))
        build_utils.write_depfile(args.depfile,
                                  args.output_image_path,
                                  _dep_files,
                                  add_pydeps=False)
===================================================================
def _make_image(args):
    if args.image_name == 'system':
        _prepare_root(args.input_path)				# 准备好 system.img 需要的文件
    elif args.image_name == 'updater':
        _prepare_updater(args.input_path)			# 准备好 updater.img 需要的文件
    image_type = "raw"
    if args.sparse_image:
        image_type = "sparse"
    mk_image_args = [
        args.input_path, args.image_config_file, args.output_image_path,
        image_type
    ]
    env_path = "../../build/ohos/images/mkimage"
    if args.build_image_tools_path:
        env_path = '{}:{}'.format(env_path, args.build_image_tools_path)
    os.environ['PATH'] = '{}:{}'.format(env_path, os.environ.get('PATH'))
    mkimages.mk_images(mk_image_args)			# 而真正制作镜像使用的下面的函数
===================================================================
# build\ohos\images\mkimage\mkimages.py
def mk_images(args):
    ... ...
    if "system.img" in device:
        src_dir = build_rootdir(src_dir)
    mkfs_tools, mk_configs = load_config(config_file)
    mk_configs = src_dir + " " + device + " " + mk_configs

    res = run_cmd(mkfs_tools + " " + mk_configs)	# 制作镜像命令使用的是mkfs_tools
===================================================================
    if "ext4" in mk_configs:
        fs_type = "ext4"
        mkfs_tools = "mkextimage.py"				# 而mkfs_tools根据文件系统类型,分别调用对应的python脚本
    elif "f2fs" in mk_configs:
        mkfs_tools = "mkf2fsimage.py"
        fs_type = "f2fs"
===================================================================
# build\ohos\images\mkimage\mkextimage.py # 制作ext4文件系统
def build_run_mke2fs(args):
    .. ...
    blocks = int(int(args.fs_size) / BLOCKSIZE)
    mke2fs_cmd += ("mke2fs " + str(mke2fs_opts) + " -t " + FS_TYPE + " -b "
                   + str(BLOCKSIZE) + " " + args.device + " " + str(blocks))
    res = run_cmd(mke2fs_cmd)		# mke2fs:制作文件系统
===================================================================
def build_run_e2fsdroid(args):
	... ...
    e2fsdroid_cmd += ("e2fsdroid" + e2fsdroid_opts + " -f " +
                      args.src_dir + " -a " + args.mount_point +
                      " " + args.device)
    res = run_cmd(e2fsdroid_cmd)	# e2fsdroid:制作镜像文件  

无论前面执行了什么操作,最终都是为了执行mke2fs、e2fsdroid。

关于这两个命令:

mke2fs:Linux下的命令,用于建立ext文件系统。

e2fsdroid:来自三方库,third_party\e2fsprogs。详情可以参考 http://e2fsprogs.sourceforge.net

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
11
收藏 6
回复
举报
回复
添加资源
添加资源将有机会获得更多曝光,你也可以直接关联已上传资源 去关联
    相关推荐