Spandan Das | aacf237 | 2022-05-11 21:46:29 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (C) 2022 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | from ninja_syntax import Variable, BuildAction, Rule, Pool, Subninja, Line |
| 18 | |
| 19 | # TODO: Format the output according to a configurable width variable |
| 20 | # This will ensure that the generated content fits on a screen and does not |
| 21 | # require horizontal scrolling |
| 22 | class Writer: |
| 23 | |
| 24 | def __init__(self, file): |
| 25 | self.file = file |
| 26 | self.nodes = [] # type Node |
| 27 | |
| 28 | def add_variable(self, variable: Variable): |
| 29 | self.nodes.append(variable) |
| 30 | |
| 31 | def add_rule(self, rule: Rule): |
| 32 | self.nodes.append(rule) |
| 33 | |
| 34 | def add_build_action(self, build_action: BuildAction): |
| 35 | self.nodes.append(build_action) |
| 36 | |
| 37 | def add_pool(self, pool: Pool): |
| 38 | self.nodes.append(pool) |
| 39 | |
| 40 | def add_comment(self, comment: str): |
| 41 | self.nodes.append(Line(value=f"# {comment}")) |
| 42 | |
| 43 | def add_default(self, default: str): |
| 44 | self.nodes.append(Line(value=f"default {default}")) |
| 45 | |
| 46 | def add_newline(self): |
| 47 | self.nodes.append(Line(value="")) |
| 48 | |
| 49 | def add_subninja(self, subninja: Subninja): |
| 50 | self.nodes.append(subninja) |
| 51 | |
Joe Onorato | 5149718 | 2022-05-12 12:58:10 -0700 | [diff] [blame] | 52 | def add_phony(self, name, deps): |
| 53 | build_action = BuildAction(name, "phony", inputs=deps) |
| 54 | self.add_build_action(build_action) |
| 55 | |
Spandan Das | aacf237 | 2022-05-11 21:46:29 +0000 | [diff] [blame] | 56 | def write(self): |
| 57 | for node in self.nodes: |
| 58 | for line in node.stream(): |
| 59 | print(line, file=self.file) |