| # Copyright 2023 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. |
| """Bazel-related functions.""" |
| |
| import contextlib |
| import dataclasses |
| import pprint |
| from typing import Any, Dict, Optional |
| |
| from PB.recipe_modules.pigweed.bazel.options import Options |
| |
| import attr |
| from recipe_engine import config_types, recipe_api |
| |
| |
| @dataclasses.dataclass |
| class BazelRunner: |
| api: Any |
| checkout_root: config_types.Path |
| options: Options |
| bazel: Optional[config_types.Path] = None |
| |
| def ensure(self) -> config_types.Path: |
| if self.bazel: |
| return self.bazel |
| |
| # read_json in a few lines requires absolute, normalized paths. |
| json_path = self.api.path.abspath( |
| self.checkout_root / self.options.cipd_json_path |
| ) |
| |
| ensure_file = self.api.cipd.EnsureFile() |
| for package in self.api.file.read_json( |
| f'read {self.api.path.basename(json_path)}', |
| json_path, |
| test_data=_JSON_TEST_DATA, |
| )["packages"]: |
| ensure_file.add_package(package['path'], package['tags'][0]) |
| |
| root = self.api.path.mkdtemp() |
| self.api.cipd.ensure(root, ensure_file, name='ensure bazel') |
| self.bazel = root / 'bazel' |
| return self.bazel |
| |
| def run(self, **kwargs): |
| name = ' '.join(['bazel'] + list(self.options.args)) |
| with self.api.context(cwd=self.checkout_root): |
| with self.api.macos_sdk(): |
| return self.api.step( |
| name, [self.ensure(), *self.options.args], **kwargs |
| ) |
| |
| |
| # This is copied from bazel.json in Pigweed, but there's no need to keep it |
| # up-to-date. |
| _JSON_TEST_DATA = { |
| "included_files": [], |
| "packages": [ |
| { |
| "path": "fuchsia/third_party/bazel/${platform}", |
| "platforms": ["linux-amd64", "mac-amd64", "windows-amd64"], |
| "tags": ["version:2@6.3.0.6"], |
| "version_file": ".versions/bazel.cipd_version", |
| }, |
| { |
| "path": "flutter/java/openjdk/${platform}", |
| "platforms": [ |
| "linux-amd64", |
| "mac-amd64", |
| "mac-arm64", |
| "windows-amd64", |
| ], |
| "tags": ["version:17"], |
| }, |
| ], |
| } |
| |
| |
| class BazelApi(recipe_api.RecipeApi): |
| """Bazel utilities.""" |
| |
| def new_runner(self, checkout: Any, options: Options) -> BazelRunner: |
| return BazelRunner(self.m, checkout.root, options) |