了解更多开源请访问:开源基础软件社区https://ost.51cto.comhb命令可以通过pythonpip包管理器安装,应该是OpenHarmonyBuild的缩写,python包名为ohos-build。hb作为编译构建子系统提供的命令行,用于编译构建产品、芯片厂商组件或单个组件。下面来学习一下hb命令行工具的源码。本文主要分析文件openharmony/build/lite/hb/__entry__.py。1.find_top()函数find_top()函数用于获取OpenHarmony源码的根目录,在之前的系列文章中有分析。代码也比较简单,这里不再赘述。deffind_top():cur_dir=os.getcwd()whilecur_dir!="/":hb_internal=os.path.join(cur_dir,'build/lite/hb_internal')ifos.path.exists(hb_internal):returncur_dircur_dir=os.path.dirname(cur_dir)raiseException("Pleasecallhbutilitiesinsidesourcerootdirectory")2.get_hb_commands()函数get_hb_commands()函数用于返回hb命令行工具支持的命令集。hb支持的命令定义在文件'build/lite/hb_internal/hb_command_set.json'中,支持的命令主要有build、set、env、clean和tool。defget_hb_commands(config_file):如果不是os.path.exists(config_file):raiseException('Error:{}notexist,couldn'tgethbcommandset'.format(config_file))withopen(config_file,'r')asfile:config=json.load(file)returnconfig3,main()function在main()函数中,首先获取OpenHarmony源码的根目录,然后将路径'build/lite'插入到sys中.path系统搜索路径,为后续调用importlib.import_module接口准备动态加载。⑴定义hb命令行支持的选项,使用命令输出hb-h学习源码。(2)获取hb命令行工具支持的命令集,然后添加到命令行解析参数列表中的parser_list中。⑶和⑷配置支持的位置参数(见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()。(5)处语句获取hb命令传入的参数options,然后动态加载'hb_internal.common.utils'获取函数地址,用于控制台输出日志,异常处理等。接下来处理使用hb命令行传入的选项。如果在⑩中指定了'-root'|'-root_path'选项,开发者会主动提供OpenHarmony源码的根目录,会执行args[0].root_path=topdir把根目录传过去在参数列表中。⑺根据hb工具或其他命令,分别调用相应的函数exec_command()。当命令行选项不同时,传入的参数也略有不同,即args和args[0]。对于hb工具,args[1]会传递一些参数gn_args传递给gn命令行。defmain():try:topdir=find_top()除了Exceptionasex:returnprint("hb_error:Pleasecallhbutilitiesinsidesourcerootdirectory")sys.path.insert(0,os.path.join(topdir,'build/lite'))⑴parser=argparse.ArgumentParser(description='OHOSBuildSystem'f'version{VERSION}')parser.add_argument('-v','--version',action='version',version=f'[OHOSINFO]hbversion{VERSION}')subparsers=parser.add_subparsers()parser_list=[]⑵command_set=get_hb_commands(os.path.join(topdir,'build/lite/hb_internal/hb_command_set.json'))forkey,valincommand_set.items():parser_list.append({'name':key,'help':val})foreachinparser_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()模块=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:⑩ifargs[0].parser.prog=='hbset'and'root_path'invars(args[0]):#Root_pathistopdir.args[0].root_path=topdir⑺如果args[0].parser.prog中的“工具”:status=args[0].command(args)else:status=args[0].command(args[0])除了KeyboardInterrupt:hb_warning('UserAbort')status=-1除了ohos_exception作为异常:hb_error(exception.args[0])status=-1除了Exception作为异常:如果不是hasattr(args[0],'command'):parser.print_help()else:hb_error(traceback.format_exc())hb_error(f'Unhandlederror:{exception}')status=-1return状态4。参考站点OpenHarmony/build_lite编译构建指导五、总结本文介绍了buildlite轻量级编译构建系统的hb命令源码,主要分析了_\entry__.py文件。想了解更多开源信息,请访问:开源基础软件社区https://ost.51cto.com。
