| # Copyright 2025 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. |
| """Batch-translates Zephyr's driver/ directory and subsys/ directory to Bazel.""" |
| |
| import argparse |
| from pathlib import Path |
| import sys |
| |
| import driver_subsys_cmake_to_bazel |
| |
| |
| def find_zephyr_root(path: Path) -> Path: |
| """Finds the Zephyr root directory from a subdirectory path.""" |
| for parent_dir in path.parents: |
| if parent_dir.name == 'zephyr': |
| return parent_dir |
| raise ValueError('Input path is not inside zephyr tree') |
| |
| |
| def main(): |
| """Main entry point.""" |
| parser = argparse.ArgumentParser( |
| description=( |
| 'Batch-convert Zephyr driver or subsys CMakeLists.txt files to' |
| ' BUILD.bazel' |
| ) |
| ) |
| parser.add_argument( |
| '--start_dir', |
| required=True, |
| help='Top-level SoC directory to start conversion, e.g. subsys/logging', |
| ) |
| parser.add_argument( |
| '--output_dir', |
| required=True, |
| help='Output directory to hold converted BUILD.bazel files', |
| ) |
| args = parser.parse_args() |
| |
| start_path = Path(args.start_dir) |
| output_path = Path(args.output_dir) |
| |
| try: |
| any_cmake_file = next(start_path.rglob('CMakeLists.txt')) |
| zephyr_root = find_zephyr_root(any_cmake_file) |
| except (StopIteration, ValueError) as e: |
| print( |
| f"Error: No CMakeLists.txt found in '{args.start_dir}' " |
| "or it's not a Zephyr directory.", |
| file=sys.stderr, |
| ) |
| print(f'Details: {e}', file=sys.stderr) |
| sys.exit(1) |
| |
| driver_root = zephyr_root / 'drivers' |
| subsys_root = zephyr_root / 'subsys' |
| |
| if start_path.parent != driver_root and start_path.parent != subsys_root: |
| print( |
| f"Warning: '{start_path}' is not a top-level driver or subsys " |
| 'directory e.g. subsys/logging. Converted Bazel rules may miss ' |
| 'common files in the parent directory.' |
| ) |
| |
| cmake_files = sorted(list(start_path.rglob('CMakeLists.txt'))) |
| |
| for cmake_file in cmake_files: |
| cmake_dir = cmake_file.parent |
| |
| relative_dir = cmake_dir.relative_to(start_path) |
| output_dir = output_path / relative_dir |
| output_dir.mkdir(parents=True, exist_ok=True) |
| output_file = output_dir / 'BUILD.bazel' |
| |
| print(f'Converting {cmake_file} to {output_file}...') |
| |
| with open(cmake_file, 'r', encoding='utf-8') as f: |
| cmake_content = f.read() |
| |
| target_name = cmake_dir.name |
| |
| bazel_content = ( |
| driver_subsys_cmake_to_bazel.convert_cmakelists_to_bazel( |
| cmake_content, |
| target_name, |
| ) |
| ) |
| |
| with open(output_file, 'w', encoding='utf-8') as f: |
| f.write(bazel_content) |
| |
| print('\nConversion complete.') |
| |
| |
| if __name__ == '__main__': |
| main() |