| # Copyright 2021 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. |
| """Calls to build code.""" |
| |
| from recipe_engine import recipe_api |
| |
| |
| class BuildApi(recipe_api.RecipeApi): |
| """Calls to build code.""" |
| |
| CAS_DIGEST_PROPERTY_NAME = 'cas_build_digest' |
| |
| def __init__(self, props, *args, **kwargs): |
| super(BuildApi, self).__init__(*args, **kwargs) |
| # Make these attributes public so recipes can override them. |
| self.gn_args = list(props.gn_args) |
| self.ninja_targets = list(props.ninja_targets) |
| self.dir = None |
| |
| def initialize(self): |
| self.dir = self.m.path['start_dir'].join('build') |
| |
| def gn_gen(self, checkout_dir): |
| cmd = ['gn', 'gen'] |
| |
| for gn_arg in self.gn_args: |
| cmd.append('--args={}'.format(gn_arg)) |
| |
| # Infrequently needed but harmless to always add this. |
| cmd.append('--export-compile-commands') |
| |
| cmd.append(self.dir) |
| |
| with self.m.context(cwd=checkout_dir): |
| self.m.step('gn gen', cmd) |
| |
| def get_gn_args(self, checkout_root=None, test_data=None): |
| context_kwargs = {'cwd': checkout_root} if checkout_root else {} |
| with self.m.context(**context_kwargs): |
| cmd = ['gn', 'args', self.dir, '--list', '--json'] |
| args = self.m.step( |
| 'all gn args', |
| cmd, |
| stdout=self.m.json.output(), |
| step_test_data=lambda: self.m.json.test_api.output_stream( |
| test_data or [] |
| ), |
| ).stdout |
| return {x['name']: x for x in args or ()} |
| |
| def ninja(self): |
| cmd = ['ninja', '-C', self.dir] |
| cmd.extend(self.ninja_targets) |
| |
| self.m.step('ninja', cmd) |
| |
| def __call__(self, checkout_dir): |
| self.gn_gen(checkout_dir) |
| self.ninja() |
| |
| def archive_to_cas(self): |
| # TODO(pwbug/389) Only archive necessary files. |
| with self.m.step.nest('archive to cas') as pres: |
| digest = self.m.cas.archive('archive', self.dir, self.dir) |
| pres.properties[self.CAS_DIGEST_PROPERTY_NAME] = digest |
| |
| def download_from_cas(self, digest): |
| return self.m.cas.download('download from cas', digest, self.dir) |