blob: 9e80b4b753666d24a272998a243c1785ebdf1c3b [file] [log] [blame]
Spandan Dasaacf2372022-05-11 21:46:29 +00001#!/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
17from 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
22class 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 Onorato51497182022-05-12 12:58:10 -070052 def add_phony(self, name, deps):
53 build_action = BuildAction(name, "phony", inputs=deps)
54 self.add_build_action(build_action)
55
Spandan Dasaacf2372022-05-11 21:46:29 +000056 def write(self):
57 for node in self.nodes:
58 for line in node.stream():
59 print(line, file=self.file)