深入解析 OpenHarmony 构建系统-4-OHOSLoader类

在OpenHarmony操作系统构建过程中,OHOSLoader类扮演着至关重要的角色。这个类负责加载和解析构建配置,生成必要的构建文件,并确保构建过程的顺利进行。本文将深入分析OHOSLoader类的实现细节,揭示其如何管理构建配置,并生成系统所需的各类文件。

类初始化与配置解析

OHOSLoader类的初始化过程涉及到多个配置文件的加载和解析。这些文件包括subsystem_config.jsonplatforms.build等,它们定义了构建过程中所需的子系统和平台配置。

python 复制代码
import os
import json

class LoadInterface:
    # 假设这是一个接口类,定义了一些基础方法和属性
    def __init__(self):
        self.config = None
        self.args_dict = {}

class OHOSLoader(LoadInterface):
    def __init__(self):
        super().__init__()
        # 初始化各种配置路径和参数
        self.source_root_dir = ""  # 源代码根目录
        self.gn_root_out_dir = ""  # GN 构建输出目录
        self.os_level = ""  # 操作系统级别
        self.target_cpu = ""  # 目标 CPU 架构
        self.target_os = ""  # 目标操作系统
        self.config_output_relpath = ""  # 配置输出相对路径
        self.config_output_dir = ""  # 配置输出绝对路径
        self.target_arch = ""  # 目标架构
        self.subsystem_config_file = ""  # 子系统配置文件路径
        self.subsystem_config_overlay_file = ""  # 子系统配置覆盖文件路径
        self.platforms_config_file = ""  # 平台配置文件路径
        self.exclusion_modules_config_file = ""  # 排除模块配置文件路径
        self.example_subsystem_file = ""  # 示例子系统文件路径
        self.build_example = ""  # 是否构建示例
        self.scalable_build = ""  # 是否可伸缩构建
        self.build_platform_name = ""  # 构建平台名称
        self.build_xts = ""  # 是否构建 XTS 测试
        self.ignore_api_check = ""  # 是否忽略 API 检查
        self.load_test_config = ""  # 是否加载测试配置
        self.subsystem_configs = ""  # 子系统配置
        self._subsystem_info = ""  # 子系统信息
        self.skip_partlist_check = ""  # 是否跳过部分列表检查

    def __post_init__(self):
        # 初始化源代码根目录
        self.source_root_dir = self.config.root_path + '/'
        
        # 初始化 GN 构建输出目录
        self.gn_root_out_dir = self.config.out_path if not self.config.out_path.startswith(
            '/') else os.path.relpath(self.config.out_path, self.config.root_path)
        
        # 初始化操作系统级别,默认为 "standard"
        self.os_level = self.config.os_level if self.config.os_level else "standard"
        
        # 初始化目标 CPU 架构,默认为 "arm"
        self.target_cpu = self.config.target_cpu if self.config.target_cpu else "arm"
        
        # 初始化目标操作系统,默认为 "ohos"
        self.target_os = self.config.target_os if self.config.target_os else "ohos"
        
        # 初始化配置输出相对路径
        self.config_output_relpath = os.path.join(
            self.gn_root_out_dir, 'build_configs')
        
        # 初始化配置输出绝对路径
        self.config_output_dir = os.path.join(
            self.source_root_dir, self.config_output_relpath)
        
        # 初始化目标架构
        self.target_arch = '{}_{}'.format(self.target_os, self.target_cpu)
        
        # 初始化子系统配置文件路径
        self.subsystem_config_file = os.path.join(
            self.config.root_path, 'out/preloader', self.config.product, 'subsystem_config.json')
        
        # 初始化平台配置文件路径
        self.platforms_config_file = os.path.join(
            self.config.root_path, 'out/preloader', self.config.product, 'platforms.build')
        
        # 初始化排除模块配置文件路径
        self.exclusion_modules_config_file = os.path.join(
            self.config.root_path, 'out/preloader', self.config.product, 'exclusion_modules.json')
        
        # 初始化示例子系统文件路径
        self.example_subsystem_file = os.path.join(
            self.config.root_path, 'build', 'subsystem_config_example.json')

        # 读取编译标准允许文件
        compile_standard_allow_file = os.path.join(
            self.config.root_path, 'out/preloader', self.config.product, 'compile_standard_whitelist.json')
        compile_standard_allow_info = read_json_file(compile_standard_allow_file)
        bundle_subsystem_allow_list = compile_standard_allow_info.get("bundle_subsystem_error", [])

        # 检查配置参数
        self._check_args()

        # 获取构建示例标志
        self.build_example = self.args_dict.get('build_example')
        if not self.build_example:
            self.example_subsystem_file = ""
        
        # 获取可伸缩构建标志
        self.scalable_build = self.args_dict.get('scalable_build')
        
        # 获取构建平台名称
        self.build_platform_name = self.args_dict.get('build_platform_name')
        
        # 获取构建 XTS 测试标志
        self.build_xts = self.args_dict.get('build_xts')
        
        # 获取忽略 API 检查标志
        self.ignore_api_check = self.args_dict.get('ignore_api_check')
        
        # 获取加载测试配置标志
        self.load_test_config = self.args_dict.get('load_test_config')
        
        # 获取跳过部分列表检查标志
        self.skip_partlist_check = self.args_dict.get('skip_partlist_check')
        
        # 扫描子系统配置
        self.subsystem_configs = subsystem_scan.scan(self.subsystem_config_file,
                                                     self.example_subsystem_file,
                                                     self.source_root_dir)

        # 获取子系统信息
        self._subsystem_info = subsystem_info.get_subsystem_info(
            self.subsystem_config_file,
            self.example_subsystem_file,
            self.source_root_dir,
            self.config_output_relpath,
            self.os_level)
        
        # 获取覆盖组件
        overrided_components = self._override_components()

        # 获取平台信息
        self._platforms_info = platforms_loader.get_platforms_info(
            self.platforms_config_file,
            self.source_root_dir,
            self.gn_root_out_dir,
            self.target_arch,
            self.config_output_relpath,
            self.scalable_build)
        
        # 获取变体工具链
        self.variant_toolchains = self._platforms_info.get(
            'variant_toolchain_info').get('platform_toolchain')
        
        # 获取所有平台
        self._all_platforms = self.variant_toolchains.keys()
        
        # 获取构建平台
        self.build_platforms = self._get_build_platforms()
        
        # 加载 OHOS 构建信息
        self.parts_config_info = load_ohos_build.get_parts_info(
            self.source_root_dir,
            self.config_output_relpath,
            self._subsystem_info,
            self.variant_toolchains,
            self.target_arch,
            self.ignore_api_check,
            self.exclusion_modules_config_file,
            self.load_test_config,
            overrided_components,
            bundle_subsystem_allow_list,
            self.skip_partlist_check,
            self.build_xts)
        
        # 获取部件目标
        self.parts_targets = self.parts_config_info.get('parts_targets')
        
        # 获取伪目标
        self.phony_targets = self.parts_config_info.get('phony_target')
        
        # 获取部件信息
        self.parts_info = self.parts_config_info.get('parts_info')
        
        # 获取所有平台的部件
        self.target_platform_parts = self._get_platforms_all_parts()
        
        # 获取所有平台的存根
        self.target_platform_stubs = self._get_platforms_all_stubs()
        
        # 获取所需构建的部件列表
        self.required_parts_targets_list = self._get_required_build_parts_list()
        
        # 获取所需伪目标
        self.required_phony_targets = self._get_required_phony_targets()
        
        # 获取所需构建的目标
        self.required_parts_targets = self._get_required_build_targets()


    def _override_components(self):
        # 获取覆盖组件信息
        # 这里可以根据需要实现具体的覆盖逻辑
        return {}

    def _get_build_platforms(self):
        # 获取构建平台
        # 这里可以根据需要实现具体的平台获取逻辑
        return []

    def _get_platforms_all_parts(self):
        # 获取所有平台的部件
        # 这里可以根据需要实现具体的部件获取逻辑
        return {}

    def _get_platforms_all_stubs(self):
        # 获取所有平台的存根
        # 这里可以根据需要实现具体的存根获取逻辑
        return {}

    def _get_required_build_parts_list(self):
        # 获取所需构建的部件列表
        # 这里可以根据需要实现具体的部件列表获取逻辑
        return []

    def _get_required_phony_targets(self):
        # 获取所需伪目标
        # 这里可以根据需要实现具体的伪目标获取逻辑
        return []

    def _get_required_build_targets(self):
        # 获取所需构建的目标
        # 这里可以根据需要实现具体的构建目标获取逻辑
        return []

def read_json_file(file_path):
    # 读取 JSON 文件
    with open(file_path, 'r') as file:
        return json.load(file)

__post_init__方法中,类成员变量被进一步初始化和配置。这包括设置源根目录、输出目录、目标CPU架构等。此外,还进行了一些必要的检查,如配置文件的存在性和有效性。

构建参数检查

构建参数的正确性是构建成功的关键。OHOSLoader类提供了多个方法来检查构建参数,包括子系统配置文件、平台配置文件等。

python 复制代码
@throw_exception
def _check_args(self):
    LogUtil.hb_info("Checking all build args...")
    # 检查子系统配置文件
    if not read_json_file(self.subsystem_config_file):
        self.subsystem_config_file = os.path.join(
            self.source_root_dir, 'build/subsystem_config.json')
    if not read_json_file(self.subsystem_config_file):
        raise OHOSException("Cannot get the content from platform config file, \
                        please check whether the corresponding file('out/preloader/{}/subsystem_config.json' or \
                        'build/subsystem_config.json') is written correctly.".format(self.config.product), "2001")

生成系统能力文件

系统能力文件(SystemCapability.json)是OpenHarmony构建过程中的一个重要组成部分,它定义了系统支持的各种能力。

python 复制代码
@throw_exception
def _generate_syscap_files(self):
    pre_syscap_info_path = os.path.dirname(self.platforms_config_file)
    system_path = os.path.join(self.source_root_dir, os.path.join(
        os.path.dirname(self.platforms_config_file), "system/"))
    syscap_product_dict = read_json_file(
        os.path.join(pre_syscap_info_path, "syscap.json"))
    syscap_info_list = self.parts_config_info.get('syscap_info')
    target_syscap_with_part_name_list = []
    target_syscap_list = []
    target_syscap_for_init_list = []
    all_syscap_list = []
    for syscap in syscap_info_list:
        if syscap['component'] not in self.required_parts_targets_list:
            continue
        if 'syscap' not in syscap or syscap['syscap'] is None \
                or len(syscap['syscap']) == 0 or syscap['syscap'] == [""]:
            continue
        for syscap_string in syscap['syscap']:
            all_syscap_list.append(syscap_string.split('=')[0].strip())

生成平台列表和部件信息

OHOSLoader类还负责生成平台列表和部件信息文件,这些信息对于构建过程中的依赖管理和目标配置至关重要。

python 复制代码
@throw_exception
def _generate_platforms_list(self):
    platforms_list_gni_file = os.path.join(self.config_output_dir,
                                           "platforms_list.gni")
    _platforms = set(self.build_platforms)
    _gni_file_content = ['target_platform_list = [', '  "{}"'.format('",\n  "'.join(_platforms)), ']',
                         'kits_platform_list = [', '  "{}",'.format('",\n  "'.join(_platforms))]
    if 'phone' not in self.build_platforms:
        _gni_file_content.append('  "phone"')
    _gni_file_content.append(']')
    write_file(platforms_list_gni_file, '\n'.join(_gni_file_content))
    LogUtil.hb_info("generate platforms list to '{}'".format(
        platforms_list_gni_file))

总结

OHOSLoader类是OpenHarmony构建系统的核心组件之一,它通过加载和解析构建配置文件,生成系统所需的各类文件,确保构建过程的顺利进行。本文详细分析了OHOSLoader类的实现,展示了其在构建过程中的关键作用。通过深入理解这个类的功能,开发者可以更好地掌握OpenHarmony的构建系统,从而更高效地进行系统开发和定制。

相关推荐
weixin_3864689618 小时前
openharmony 6.0编译rk3568过程记录
c语言·c++·git·python·vim·harmonyos·openharmony
长沙红胖子Qt3 天前
关于 Qt5编译工程出现无限循环qmake编译 的解决方法
编译·循环qmake·一直qmake
Car124 天前
在vscode中添加一个tasks.json实现 rt thread的scons编译功能
vscode·json·build·scons
REDcker7 天前
Android HWASan 详解:硬件标记原理、Clang 启用与排障实践
android·linux·debug·编译·clang·asan·hwasan
灵魂学者9 天前
使用 Electron 打包项目构建 .EXE 桌面应用程序(简)
electron·node.js·vue·build·桌面应用程序
阿钱真强道10 天前
18 小凌派 rk2206 鸿蒙 liteos 如何通过修改配置文件,编译不通的案例
华为·鸿蒙·编译·案例·liteos·rk2206
灵魂学者11 天前
使用 pkg 打包 Node.js 项目打包构建 .EXE 桌面应用程序
node.js·build·pkg·.exe
庞轩px19 天前
第七篇:注解与APT深度解析——从@Override到Lombok的底层原理
java·注解·编译·lombok
『昊纸』℃21 天前
Mac上编译C语言的简易方法
c语言·mac·教程·xcode·编译
北风朝向23 天前
Lombok 参数名丢失?只需启用-parameters编译选项即可完美解决
编译·参数·parameters