| # 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. |
| |
| import argparse |
| import os |
| from pathlib import Path |
| |
| |
| def _get_parent_filegroup_name(dir: Path, zephyr_root: Path): |
| parent_path = dir.parent |
| parent_relative_path = parent_path.relative_to(zephyr_root) |
| # E.g. '//arm/nxp' |
| return '//' + str(parent_relative_path).replace('\\', '/') |
| |
| |
| def create_build_files(dts_root, output_root): |
| """Walks the directory tree from the given dts_root downwards and creates |
| BUILD.bazel files. Each directory's filegroup includes the filegroup |
| from its parent directory. |
| """ |
| abs_dts_root = Path(dts_root).resolve() |
| zephyr_root = abs_dts_root.parent |
| if zephyr_root.name != 'zephyr': |
| raise ValueError( |
| f'Error: {abs_dts_root} does not seem to be in a valid Zephyr tree.' |
| ) |
| |
| for root, dirs, files in os.walk(dts_root, topdown=True): |
| current_path = Path(root) |
| |
| # We don't generate anything for zephyr/dts/ root. |
| if current_path == abs_dts_root: |
| continue |
| |
| # We don't generate anything for zephyr/dts/bindings and its child |
| # directories. Those are handled separately, possibly by another |
| # script. |
| if current_path.is_relative_to(abs_dts_root / 'bindings'): |
| continue |
| |
| filegroup_name = current_path.name |
| |
| # --- Construct the srcs attribute value --- |
| srcs_parts = [] |
| if any(f.endswith('.dtsi') for f in files): |
| srcs_parts.append('glob(["*.dtsi"])') |
| |
| if any(f.endswith('.h') for f in files): |
| srcs_parts.append('glob(["*.h"])') |
| |
| # Include parent filegroup, if parent directory is not zephyr/dts/ root. |
| abs_root = current_path.resolve() |
| if abs_root.parent != abs_dts_root: |
| parent_filegroup_name = _get_parent_filegroup_name( |
| abs_root, zephyr_root |
| ) |
| srcs_parts.append(f'["{parent_filegroup_name}"]') |
| |
| if not srcs_parts: |
| srcs_value = '[]' |
| else: |
| srcs_value = ' + '.join(srcs_parts) |
| |
| # --- Generate BUILD.bazel content --- |
| content = f'''# SPDX-License-Identifier: Apache-2.0 |
| |
| # Auto-generated using scripts in zephyr-bazel. |
| |
| package(default_visibility = ["//visibility:public"]) |
| |
| filegroup( |
| name = "{filegroup_name}", |
| srcs = {srcs_value}, |
| ) |
| ''' |
| |
| # --- Write the file --- |
| relative_dir = current_path.relative_to(abs_dts_root) |
| output_dir = Path(output_root) / relative_dir |
| output_dir.mkdir(parents=True, exist_ok=True) |
| output_file = output_dir / 'BUILD.bazel' |
| |
| try: |
| with open(output_file, 'w') as f: |
| f.write(content) |
| print(f'Wrote {output_file}') |
| except IOError as e: |
| print(f'Error: Could not write to {output_file}. {e}') |
| |
| |
| def main(): |
| parser = argparse.ArgumentParser( |
| description='Generate BUILD.bazel files for dts directories.' |
| ) |
| parser.add_argument( |
| '--dts_root', required=True, help='The root directory of the dts files.' |
| ) |
| parser.add_argument( |
| '--output_dir', |
| required=True, |
| help=( |
| 'Output directory to hold converted BUILD.bazel files, ' |
| 'should end in "dts"' |
| ), |
| ) |
| args = parser.parse_args() |
| create_build_files(args.dts_root, args.output_dir) |
| |
| |
| if __name__ == '__main__': |
| main() |