diff --git a/hb/__init__.py b/hb/__init__.py index 0f1e2188a546805c80052c0e340c25e848b1d4ca..51acd25a0f966734fa23b65563132528fad7b498 100755 --- a/hb/__init__.py +++ b/hb/__init__.py @@ -41,4 +41,4 @@ CONFIG_STRUCT = { "product_path": None, "device_path": None } -VERSION = '0.3.5' +VERSION = '0.3.7' diff --git a/hb/build/build.py b/hb/build/build.py index ebb9f509c96d6512af4d37bb626a70fc5ef6e2cc..859e2bccc7bc08e80db2a09076fe6c2c87d4036f 100755 --- a/hb/build/build.py +++ b/hb/build/build.py @@ -20,12 +20,11 @@ from collections import defaultdict from hb.build.build_process import Build from hb.set.set import set_product -from hb.common.device import Device def add_options(parser): parser.add_argument('component', help='name of the component', nargs='*', - default=[]) + default=None) parser.add_argument('-b', '--build_type', help='release or debug version', nargs=1, default=['debug']) parser.add_argument('-c', '--compiler', help='specify compiler', @@ -52,11 +51,12 @@ def add_options(parser): def exec_command(args): - build = Build() - cmd_args = defaultdict(list) + if len(args.product): + product, company = args.product[0].split('@') + set_product(product_name=product, company=company) - if len(args.component): - build.target = args.component[0] + build = Build(args.component) + cmd_args = defaultdict(list) build.register_args('ohos_build_type', args.build_type[0]) @@ -73,12 +73,6 @@ def exec_command(args): build.register_args('tee_enable', 'true', quota=False) build.config.fs_attr.add('tee_enable') - if len(args.product): - product, company = args.product[0].split('@') - set_product(product_name=product, company=company) - - build.compiler = Device.get_compiler(build.config.device_path) - if args.ndk: build.register_args('ohos_build_ndk', 'true', quota=False) diff --git a/hb/build/build_process.py b/hb/build/build_process.py index 4c7d434b0bd2c17f0667b3ed10904117552f4930..e7b70f8a1ffb3e298683d55fe814b5a03a774839 100755 --- a/hb/build/build_process.py +++ b/hb/build/build_process.py @@ -33,7 +33,7 @@ from hb.build.fs_process import Packer class Build(): - def __init__(self): + def __init__(self, component=None): self.config = Config() # Get gn args ready @@ -42,12 +42,19 @@ class Build(): self._compiler = None self._test = None + self.target = component + self.check_in_device() + @property def target(self): return self._target @target.setter def target(self, component): + if component is None or not len(component): + return + component = component[0] + cts = CTS() cts.init_from_json() for subsystem_cls in cts: @@ -103,7 +110,6 @@ class Build(): self.config.fs_attr = None def build(self, full_compile, ninja=True, cmd_args=None): - self.check_in_device() cmd_list = self.get_cmd(full_compile, ninja) if cmd_args is None: @@ -111,6 +117,7 @@ class Build(): for exec_cmd in cmd_list: exec_cmd(cmd_args) + hb_info(f'{os.path.basename(self.config.out_path)} build success') return 0 def get_cmd(self, full_compile, ninja): @@ -118,20 +125,27 @@ class Build(): self.register_args('ohos_full_compile', 'true', quota=False) return [self.gn_build] + cmd_list = [] + build_ninja = os.path.join(self.config.out_path, 'build.ninja') - packer = Packer() if not os.path.isfile(build_ninja): self.register_args('ohos_full_compile', 'true', quota=False) makedirs(self.config.out_path) - return [self.gn_build, self.ninja_build, packer.fs_make] - if full_compile: + cmd_list = [self.gn_build, self.ninja_build] + elif full_compile: self.register_args('ohos_full_compile', 'true', quota=False) remove_path(self.config.out_path) makedirs(self.config.out_path) - return [self.gn_build, self.ninja_build, packer.fs_make] + cmd_list = [self.gn_build, self.ninja_build] + else: + self.register_args('ohos_full_compile', 'false', quota=False) + cmd_list = [self.ninja_build] - self.register_args('ohos_full_compile', 'false', quota=False) - return [self.ninja_build, packer.fs_make] + if self.config.fs_attr is not None: + packer = Packer() + cmd_list.append(packer.fs_make) + + return cmd_list def gn_build(self, cmd_args): # Clean out path @@ -184,18 +198,22 @@ class Build(): self.config.out_path] + ninja_args exec_command(ninja_cmd, log_path=self.config.log_path, log_filter=True) - hb_info('{} build success'.format( - os.path.basename(self.config.out_path))) - def check_in_device(self): if self._target is None and Device.is_in_device(): # Compile device board device_path, kernel, board = Device.device_menuconfig() - # xxx: build device, no need to set root manually, so set it speculatively. - self.config.root_path = os.path.dirname(os.path.dirname(os.path.dirname(os.getcwd()))) + hb_info(f'{device_path}') + # build device, no need to set root manually, + # so set it speculatively. + self.config.root_path = os.path.abspath(os.path.join(device_path, + os.pardir, + os.pardir, + os.pardir, + os.pardir)) self.config.out_path = os.path.join(self.config.root_path, 'out', board) + self.compiler = Device.get_compiler(device_path) gn_device_path = os.path.dirname(device_path) gn_kernel_path = device_path self.register_args('ohos_build_target', [gn_device_path]) @@ -203,6 +221,7 @@ class Build(): self.register_args('ohos_kernel_type', kernel) else: # Compile product in "hb set" + self.compiler = Device.get_compiler(self.config.device_path) self.register_args('product_path', self.config.product_path) self.register_args('device_path', self.config.device_path) self.register_args('ohos_kernel_type', self.config.kernel) diff --git a/hb/build/fs_process.py b/hb/build/fs_process.py index 760a5db909dca56c85d9e4e9f7375dff2571220a..b691b565a984a63a0092b893fed1717f186e65ae 100644 --- a/hb/build/fs_process.py +++ b/hb/build/fs_process.py @@ -79,30 +79,46 @@ class Packer(): if target_dir == '': continue - source_path = os.path.join(self.config.out_path, source_dir) - target_path = os.path.join(fs_path, target_dir) - if source_dir == '' or not os.path.isdir(source_path): - makedirs(target_path, exist_ok=exist_ok, with_rm=with_rm) + source_path = self.fs_dirs_replace(source_dir, + self.config.out_path) + target_path = self.fs_dirs_replace(target_dir, fs_path) + + if source_dir == '' or not os.path.exists(source_path): + makedirs(target_path) target_mode_tuple = (target_path, fs_dir.get('dir_mode', 755)) self.chmod_dirs.append(target_mode_tuple) continue self.copy_files(source_path, target_path, fs_dir) + def fs_dirs_replace(self, path, default_path): + source_path, is_changed = self.replace(path) + if not is_changed: + source_path = os.path.join(default_path, path) + return source_path + def copy_files(self, spath, tpath, fs_dir): ignore_files = fs_dir.get('ignore_files', []) dir_mode = fs_dir.get('dir_mode', 755) file_mode = fs_dir.get('file_mode', 555) - for srelpath, sfile in self.list_all_files(spath, ignore_files): - tdirname = srelpath.replace(spath, tpath) - if not os.path.isdir(tdirname): - makedirs(tdirname) - self.chmod_dirs.append((tdirname, dir_mode)) - tfile = os.path.join(tdirname, os.path.basename(sfile)) + def copy_file_process(source_path, target_path): + if not os.path.isdir(target_path): + makedirs(target_path) + self.chmod_dirs.append((target_path, dir_mode)) + tfile = os.path.join(target_path, os.path.basename(source_path)) shutil.copy(sfile, tfile) self.chmod_dirs.append((tfile, file_mode)) + if os.path.isfile(spath): + sfile = spath + copy_file_process(spath, tpath) + return + + for srelpath, sfile in self.list_all_files(spath, ignore_files): + tdirname = srelpath.replace(spath, tpath) + copy_file_process(sfile, tdirname) + def chmod(self, file, mode): mode = int(str(mode), base=8) if os.path.exists(file): @@ -131,15 +147,16 @@ class Packer(): yield relpath, full_path def replace(self, raw_str): + old_str = raw_str for old, new in self.replace_items.items(): raw_str = raw_str.replace(old, new) - return raw_str + return raw_str, old_str != raw_str def fs_link(self): fs_symlink = self.fs_cfg.get('fs_symlink', []) for symlink in fs_symlink: - source = self.replace(symlink.get('source', '')) - link_name = self.replace(symlink.get('link_name', '')) + source, _ = self.replace(symlink.get('source', '')) + link_name, _ = self.replace(symlink.get('link_name', '')) if os.path.exists(link_name): os.remove(link_name) os.symlink(source, link_name) @@ -161,7 +178,7 @@ class Packer(): log_path = self.config.log_path for cmd in fs_make_cmd: - cmd = self.replace(cmd) + cmd, _ = self.replace(cmd) cmd = cmd.split(' ') exec_command(cmd, log_path=log_path) @@ -189,10 +206,11 @@ class Packer(): def fs_make(self, cmd_args): fs_cfg_path = os.path.join(self.config.product_path, 'fs.yml') if not os.path.isfile(fs_cfg_path): - hb_info(f'{fs_cfg_path} not found, stop packing fs') + hb_info(f'{fs_cfg_path} not found, stop packing fs. ' + 'If the product does not need to be packaged, ignore it.') return if self.config.fs_attr is None: - hb_info(f'component compiling, no need to pack fs') + hb_info('component compiling, no need to pack fs') return fs_cfg_list = read_yaml_file(fs_cfg_path)