花粉乐分享平台宣传视频
> 华为资讯 > 华为资讯 > 鸿蒙编程 > OpenHarmony移植案例:build lite源码分析之hb命令__entry__.py
OpenHarmony移植案例:build lite源码分析之hb命令__entry__.py
来源:华为云开发者联盟
2022-12-02 14:22:10
517
管理

本文分享自华为云社区《移植案例与原理 - build lite源码分析 之 hb命令__entry__.py-云社区-华为云》,作者:zhushy 。

hb命令可以通过python pip包管理器进行安装,应该是OpenHarmony Build的缩写,在python包名称是ohos-build。hb作为编译构建子系统提供的命令行,用于编译构建产品、芯片厂商组件或者单个组件。我们来学习hb命令行工具的源码,本文主要分析下文件openharmony/build/lite/hb/__entry__.py。

1、find_top()函数

find_top()函数用于获取OpenHarmony源代码根目录,之前的系列文章分析过。代码也较简单,不再赘述。

def find_top(): cur_dir = os.getcwd() while cur_dir != "/": hb_internal = os.path.join(cur_dir, 'build/lite/hb_internal') if os.path.exists(hb_internal): return cur_dir cur_dir = os.path.dirname(cur_dir) raise Exception("Please call hb utilities inside source root directory")2、get_hb_commands()函数

get_hb_commands()函数用于返回hb命令行工具支持的命令集。hb支持的命令定义在文件’build/lite/hb_internal/hb_command_set.json’中,支持的命令主要为build、set、env、clean和tool。

def get_hb_commands(config_file): if not os.path.exists(config_file): raise Exception('Error: {} not exist, couldnot get hb command set'.format(config_file)) with open(config_file, 'r') as file: config = json.load(file) return config3、main()函数

在main()函数中,首先获取OpenHarmony源代码根目录,然后把路径'build/lite'插入到sys.path系统搜索路径,为后续调用importlib.import_module接口进行动态加载做准备。⑴处定义hb命令行的支持的选项,使用和命令输出hb -h结合起来学习源代码。⑵处获取hb命令行工具支持的命令集合,然后添加到命令行解析参数列表里parser_list。⑶和⑷配置支持的positional arguments(见 hb -h的输出),⑶处动态引入支持的模块,这些对应文件build/lite/hb_internal/hb_internal/XXX/XXX.py,其中XXX的取值为build、set、clean、env和tool。在这几个python文件中,都会有add_options()函数,用于提供具体命令的参数选项,还有个函数exec_command(),执行具体的命令时,会调用这些函数。⑷处的代码会配置刚才描述的add_options()函数和函数exec_command()。

⑸处的语句获取hb命令传入的参数选项,接下来动态加载’hb_internal.common.utils’,获得函数地址,分别用于控制台输出日志、异常处理等。接下来处理hb命令行传入的选项,⑹处如果指定了’-root’|’–root_path’选项时,开发者主动提供OpenHarmony源代码根目录,会执行args[0].root_path = topdir把根目录传入到参数列表里。⑺根据是hb tool还是其他命令,分别调用对应的函数exec_command(),命令行选项不一样时,传入的参数稍有差异,分别是args和args[0]。对于hb tool,args[1]会传递些要传递给gn命令行的参数gn_args。

def main(): try: topdir = find_top() except Exception as ex: return print("hb_error: Please call hb utilities inside source root directory") sys.path.insert(0, os.path.join(topdir, 'build/lite'))⑴ parser = argparse.ArgumentParser(description='OHOS Build System ' f'version {VERSION}') parser.add_argument('-v', '--version', action='version', version=f'[OHOS INFO] hb version {VERSION}') subparsers = parser.add_subparsers() parser_list = []⑵ command_set = get_hb_commands(os.path.join(topdir, 'build/lite/hb_internal/hb_command_set.json')) for key, val in command_set.items(): parser_list.append({'name': key, 'help': val}) for each in parser_list: module_parser = subparsers.add_parser(name=each.get('name'), help=each.get('help'))⑶ module = importlib.import_module('hb_internal.{0}.{0}'.format( each.get('name')))⑷ module.add_options(module_parser) module_parser.set_defaults(parser=module_parser, command=module.exec_command)⑸ args = parser.parse_known_args() module = importlib.import_module('hb_internal.common.utils') hb_error = getattr(module, 'hb_error') hb_warning = getattr(module, 'hb_warning') ohos_exception = getattr(module, 'OHOSException') try:⑹ if args[0].parser.prog == 'hb set' and 'root_path' in vars(args[0]): # Root_path is topdir. args[0].root_path = topdir⑺ if "tool" in args[0].parser.prog: status = args[0].command(args) else: status = args[0].command(args[0]) except KeyboardInterrupt: hb_warning('User Abort') status = -1 except ohos_exception as exception: hb_error(exception.args[0]) status = -1 except Exception as exception: if not hasattr(args[0], 'command'): parser.print_help() else: hb_error(traceback.format_exc()) hb_error(f'Unhandled error: {exception}') status = -1 return status4、参考站点OpenHarmony / build_lite编译构建指导(https://gitee.com/openharmony/docs/blob/master/zh-cn/device-dev/subsystems/subsys-build-all.md)5、小结

本文介绍了build lite 轻量级编译构建系统hb命令的源码,主要分析了_entry__.py文件。因为时间关系,仓促写作,或能力限制,若有失误之处,请各位读者多多指正。遗漏之处,欢迎补充。感谢阅读,有什么问题,请留言。

点击下方,第一时间了解华为云新鲜技术~

华为云博客_大数据博客_AI博客_云计算博客_开发者中心-华为云

花粉社群VIP加油站

1
点赞
赏礼
赏钱
0
收藏
免责声明:本文仅代表作者个人观点,与花粉乐分享无关。其原创性以及文中陈述文字和内容未经本网证实,对本文以及其中全部或者 部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
凡本网注明 “来源:XXX(非花粉乐分享)”的作品,均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对 其真实性负责。
如因作品内容、版权和其它问题需要同本网联系的,请在一周内进行,以便我们及时处理。
QQ:2443165046 邮箱:info@hflfx.com
关于作者
(Array)
文章
455
主题
0
关注
0
粉丝
0
点击领取今天的签到奖励!
签到排行
随手拍
54个圈友 0个话题
华为手机随手拍,记录生活点滴之美好
华为P30pro
51个圈友 0个话题
这里是华为P30pro手机交流圈,欢迎华为P30pro用户进群交流
体验官
60个圈友 2个话题
华为花粉体验官,体验官专属的交流群
登录后查看您创建的圈子
登录后查看您创建的圈子
所有圈子
杭州互联网违法和不良信息举报平台 网络110报警服务 浙ICP备17046585号
1
0
分享
请选择要切换的马甲:

个人中心

每日签到

我的消息

内容搜索