| # 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 create_build_files(zephyr_root, output_root): |
| """Finds all subdirectories under zephyr_root/samples/ containing a |
| sample.yaml file, and all subdirectories under zephyr_root/tests/ |
| containing a testcase.yaml file, and generates a BUILD.bazel file |
| in the corresponding directory in output_root. |
| """ |
| zephyr_root = Path(zephyr_root).resolve() |
| output_root = Path(output_root).resolve() |
| |
| def process_dir(sub_dir_name, marker_file): |
| target_root = zephyr_root / sub_dir_name |
| if not target_root.exists(): |
| print(f'Warning: {target_root} does not exist.') |
| return |
| |
| for root, _, files in os.walk(target_root): |
| if marker_file in files: |
| current_path = Path(root) |
| relative_path = current_path.relative_to(zephyr_root) |
| output_dir = output_root / relative_path |
| output_dir.mkdir(parents=True, exist_ok=True) |
| output_file = output_dir / 'BUILD.bazel' |
| |
| try: |
| with open(output_file, 'w') as f: |
| f.write("""# SPDX-License-Identifier: Apache-2.0 |
| |
| # Auto-generated from corresponding CMakeLists.txt in Zephyr using scripts |
| # in zephyr-bazel. |
| |
| load("//:cc.bzl", "zephyr_app", "zephyr_cc_library") |
| |
| package(default_visibility = ["//visibility:public"]) |
| |
| zephyr_app( |
| name = package_name().split("/")[-1], |
| deps = [":main"], |
| ) |
| |
| zephyr_cc_library( |
| name = "main", |
| visibility = ["//visibility:private"], |
| srcs = glob(["**/*.c"]) + glob(["**/*.h"], allow_empty=True), |
| ) |
| """) |
| print(f'Wrote {output_file}') |
| except IOError as e: |
| print(f'Error: Could not write to {output_file}. {e}') |
| |
| process_dir('samples', 'sample.yaml') |
| process_dir('tests', 'testcase.yaml') |
| |
| |
| def main(): |
| parser = argparse.ArgumentParser( |
| description='Generate BUILD.bazel files for samples and tests directories.' |
| ) |
| parser.add_argument( |
| '--zephyr_root', required=True, help='The root directory of the Zephyr tree.' |
| ) |
| parser.add_argument( |
| '--output_root', |
| help='The root directory for the generated BUILD.bazel files.', |
| ) |
| args = parser.parse_args() |
| create_build_files(args.zephyr_root, args.output_root) |
| |
| |
| if __name__ == '__main__': |
| main() |