| # Copyright 2026 The Pigweed Authors |
| # |
| # Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| # use this file except in compliance with the License. You may obtain a copy of |
| # the License at |
| # |
| # https://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| # License for the specific language governing permissions and limitations under |
| # the License. |
| """Script to generate BUILD.bazel files for all drivers/subsys in Zephyr.""" |
| |
| import argparse |
| from pathlib import Path |
| import subprocess |
| import sys |
| |
| |
| def convert(input_root: Path, output_root: Path, subdir_name: str) -> bool: |
| """Converts one driver or subsys directory to Bazel. |
| |
| Returns success or not. |
| """ |
| script_to_run = 'convert_driver_subsys.py' |
| start_dir = str(input_root / subdir_name) |
| output_dir = str(output_root / subdir_name) |
| args = ['--start_dir', start_dir, '--output_dir', output_dir] |
| |
| command = [sys.executable, script_to_run] + args |
| |
| try: |
| # Execute the script in a new process |
| # 'check=True' will raise a CalledProcessError if the script exits with a non-zero status |
| subprocess.run( |
| command, |
| check=True, |
| stdout=subprocess.PIPE, |
| stderr=subprocess.PIPE, |
| timeout=2, |
| text=True, |
| ) |
| print(f'Successfully executed {script_to_run} for {subdir_name}') |
| return True |
| |
| except subprocess.CalledProcessError as e: |
| print(f'Error executing {script_to_run}:') |
| print(f'STDOUT: {e.stdout}') |
| print(f'STDERR: {e.stderr}') |
| except subprocess.TimeoutExpired as e: |
| print(f'{script_to_run} timed out for {subdir_name}.') |
| except FileNotFoundError: |
| print(f"Error: The script '{script_to_run}' was not found.") |
| |
| return False |
| |
| |
| def main(): |
| """Main entry point.""" |
| parser = argparse.ArgumentParser( |
| description=( |
| "Batch-convert Zephyr's CMakeLists.txt files in the entire " |
| 'drivers/ and subsys/ directories to BUILD.bazel' |
| ) |
| ) |
| parser.add_argument( |
| '--zephyr_root', |
| required=True, |
| help='Zephyr SOC root to start conversion, should be /path/to/zephyr', |
| ) |
| parser.add_argument( |
| '--output_dir', |
| required=True, |
| help=( |
| 'Output directory to hold converted BUILD.bazel files, e.g.' |
| ' third_party/zephyr' |
| ), |
| ) |
| args = parser.parse_args() |
| |
| zephyr_root = Path(args.zephyr_root) |
| drivers_root = zephyr_root / 'drivers' |
| subsys_root = zephyr_root / 'subsys' |
| |
| if not drivers_root.is_dir(): |
| raise ValueError( |
| f'{args.zephyr_root} does not contain drivers directory' |
| ) |
| |
| if not subsys_root.is_dir(): |
| raise ValueError( |
| f'{args.zephyr_root} does not contain subsys directory' |
| ) |
| |
| driver_types = [] |
| for child in drivers_root.iterdir(): |
| if child.is_dir(): |
| driver_types.append(child.name) |
| |
| subsys_types = [] |
| for child in subsys_root.iterdir(): |
| if child.is_dir(): |
| subsys_types.append(child.name) |
| |
| output_dir = Path(args.output_dir) |
| failed_drivers = [] |
| for driver_type in driver_types: |
| if not convert(drivers_root, output_dir / 'drivers', driver_type): |
| failed_drivers.append(driver_type) |
| |
| failed_subsys = [] |
| for subsys_type in subsys_types: |
| if not convert(subsys_root, output_dir / 'subsys', subsys_type): |
| failed_subsys.append(subsys_type) |
| |
| if failed_drivers: |
| print( |
| 'Failed to convert the following drivers directories, please' |
| ' convert manually using convert_driver_subsys.py:' |
| ) |
| for driver_type in failed_drivers: |
| print(driver_type) |
| |
| if failed_subsys: |
| print( |
| 'Failed to convert the following subsys directories, please convert' |
| ' manually using convert_driver_subsys.py:' |
| ) |
| for subsys_type in failed_subsys: |
| print(subsys_type) |
| |
| |
| if __name__ == '__main__': |
| main() |