From 340288cbffa066f5990ba99859156c99e0c857e8 Mon Sep 17 00:00:00 2001 From: LO Date: Sat, 11 Oct 2025 11:54:48 +0800 Subject: [PATCH] Optimizte build.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [维护] 优化build.py 中关于BBMULTICONFIG的功能,为后续切换为BBMULTICONFIGS做准备 --- build.py | 107 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 92 insertions(+), 15 deletions(-) diff --git a/build.py b/build.py index 2260858..70f5ce3 100755 --- a/build.py +++ b/build.py @@ -462,6 +462,9 @@ def initlocal(builddir, configs, mirrors=None, downloads=None, port=None): # Convert list to space-separated string bbmulticonfig_str = ' '.join(configs['BBMULTICONFIG']) local_configs_val += 'BBMULTICONFIG = "%s"\n' % bbmulticonfig_str + elif 'BBMULTICONFIGS' in configs and configs['BBMULTICONFIGS']: + bbmulticonfig_str, _ = process_multiconfigs(configs) + local_configs_val += 'BBMULTICONFIG = "%s"\n' % bbmulticonfig_str sstate_mirrors_var = prepare_sstate_mirrors(mirrors) source_mirrors_content = prepare_source_mirrors(downloads) @@ -558,22 +561,33 @@ def initbblayers(builddir, configs): traceback.print_exc(file=sys.stderr) def bitbake_command(configs, command, targets=None, machines=None): - targets = targets or configs.get('targets', []) - multiconfigs = configs.get('BBMULTICONFIG', []) - - # Validate machines if specified - if machines and multiconfigs: - invalid_machines = [m for m in machines if m not in multiconfigs] - if invalid_machines: - print(f"Error: Machines {invalid_machines} are not in BBMULTICONFIG. " - f"Available machines: {multiconfigs}") - sys.exit(1) - - if multiconfigs: - machines_to_use = machines or multiconfigs - targets = [f'mc:{m}:{t}' for m in machines_to_use for t in targets] + if 'BBMULTICONFIG' in configs and configs['BBMULTICONFIG']: + targets = targets or configs.get('targets', []) + multiconfigs = configs.get('BBMULTICONFIG', []) + + # Validate machines if specified + if machines and multiconfigs: + invalid_machines = [m for m in machines if m not in multiconfigs] + if invalid_machines: + print(f"Error: Machines {invalid_machines} are not in BBMULTICONFIG. " + f"Available machines: {multiconfigs}") + sys.exit(1) + + if multiconfigs: + machines_to_use = machines or multiconfigs + targets = [f'mc:{m}:{t}' for m in machines_to_use for t in targets] + + targets_str = ' '.join(targets) + elif 'BBMULTICONFIGS' in configs and configs['BBMULTICONFIGS']: + _, targets_str = process_multiconfigs( + configs, + machines, + targets + ) + else: + targets = targets or configs.get('targets', []) + targets_str = ' '.join(targets) - targets_str = ' '.join(targets) cmd = ['bitbake'] if command: @@ -582,6 +596,69 @@ def bitbake_command(configs, command, targets=None, machines=None): cmd.append(targets_str) return ' '.join(cmd) +def process_multiconfigs(data, input_machines=None, input_targets=None): + """ + Process BBMULTICONFIGS from JSON file + + Args: + data: Configs + input_machines: Optional list of machines to filter by + input_targets: Optional list of targets corresponding to input_machines + + Returns: + Tuple of (BBMULTICONFIG string, MC output string) + """ + # with open(json_path, 'r') as f: + # data = json.load(f) + + if 'BBMULTICONFIGS' not in data: + raise ValueError("Missing BBMULTICONFIGS in JSON") + + # Build machine-to-target mapping from JSON + json_config_map = {} + for config in data['BBMULTICONFIGS']: + if 'machine' not in config or 'target' not in config: + raise ValueError(f"Missing required field(s) in config: {config}") + + machine = config['machine'] + target = config['target'] + + if not machine or not target: + raise ValueError(f"Empty value found in config: {config}") + + json_config_map[machine] = target + + # Use all machines from JSON if no input provided + if input_machines is None: + input_machines = list(json_config_map.keys()) + + # Validate input machines exist in JSON + invalid_machines = [m for m in input_machines if m not in json_config_map] + if invalid_machines: + raise ValueError(f"Machines not found in BBMULTICONFIGS: {json_config_map.keys()}") + + # Handle targets + if input_targets is None: + # Use default targets from JSON + targets = [json_config_map[m] for m in input_machines] + else: + # Validate target count matches machine count + if len(input_targets) != len(input_machines): + raise ValueError("Number of targets must match number of machines") + # Use input targets, but ensure they are non-empty + targets = [] + for m, t in zip(input_machines, input_targets): + if not t: + raise ValueError(f"Empty target provided for machine {m}") + targets.append(t) + + # Generate output strings + bbmulticonfig_str = ' '.join(input_machines) + mc_strings = [f"mc:{m}:{t}" for m, t in zip(input_machines, targets)] + mc_output = ' '.join(mc_strings) + + return bbmulticonfig_str, mc_output + if __name__ == '__main__': try: -- Gitee