2 # https://github.com/martine/ninja/blob/master/misc/ninja_syntax.py
3 # Do not edit! Edit the upstream one instead.
5 """Python module for generating .ninja files.
7 Note that this is emphatically not a required piece of Ninja; it's
8 just a helpful utility for build-file-generation systems that already
15 def escape_path(word):
16 return word.replace('$ ','$$ ').replace(' ','$ ').replace(':', '$:')
19 def __init__(self, output, width=78):
24 self.output.write('\n')
26 def comment(self, text):
27 for line in textwrap.wrap(text, self.width - 2):
28 self.output.write('# ' + line + '\n')
30 def variable(self, key, value, indent=0):
33 if isinstance(value, list):
34 value = ' '.join(filter(None, value)) # Filter out empty strings.
35 self._line('%s = %s' % (key, value), indent)
37 def pool(self, name, depth):
38 self._line('pool %s' % name)
39 self.variable('depth', depth, indent=1)
41 def rule(self, name, command, description=None, depfile=None,
42 generator=False, pool=None, restat=False, rspfile=None,
43 rspfile_content=None, deps=None):
44 self._line('rule %s' % name)
45 self.variable('command', command, indent=1)
47 self.variable('description', description, indent=1)
49 self.variable('depfile', depfile, indent=1)
51 self.variable('generator', '1', indent=1)
53 self.variable('pool', pool, indent=1)
55 self.variable('restat', '1', indent=1)
57 self.variable('rspfile', rspfile, indent=1)
59 self.variable('rspfile_content', rspfile_content, indent=1)
61 self.variable('deps', deps, indent=1)
63 def build(self, outputs, rule, inputs=None, implicit=None, order_only=None,
65 outputs = self._as_list(outputs)
66 all_inputs = self._as_list(inputs)[:]
67 out_outputs = list(map(escape_path, outputs))
68 all_inputs = list(map(escape_path, all_inputs))
71 implicit = map(escape_path, self._as_list(implicit))
72 all_inputs.append('|')
73 all_inputs.extend(implicit)
75 order_only = map(escape_path, self._as_list(order_only))
76 all_inputs.append('||')
77 all_inputs.extend(order_only)
79 self._line('build %s: %s' % (' '.join(out_outputs),
80 ' '.join([rule] + all_inputs)))
83 if isinstance(variables, dict):
84 iterator = iter(variables.items())
86 iterator = iter(variables)
88 for key, val in iterator:
89 self.variable(key, val, indent=1)
93 def include(self, path):
94 self._line('include %s' % path)
96 def subninja(self, path):
97 self._line('subninja %s' % path)
99 def default(self, paths):
100 self._line('default %s' % ' '.join(self._as_list(paths)))
102 def _count_dollars_before_index(self, s, i):
103 """Returns the number of '$' characters right in front of s[i]."""
106 while dollar_index > 0 and s[dollar_index] == '$':
111 def _line(self, text, indent=0):
112 """Write 'text' word-wrapped at self.width characters."""
113 leading_space = ' ' * indent
114 while len(leading_space) + len(text) > self.width:
115 # The text is too wide; wrap if possible.
117 # Find the rightmost space that would obey our width constraint and
118 # that's not an escaped space.
119 available_space = self.width - len(leading_space) - len(' $')
120 space = available_space
122 space = text.rfind(' ', 0, space)
124 self._count_dollars_before_index(text, space) % 2 == 0:
128 # No such space; just use the first unescaped space we can find.
129 space = available_space - 1
131 space = text.find(' ', space + 1)
133 self._count_dollars_before_index(text, space) % 2 == 0:
136 # Give up on breaking.
139 self.output.write(leading_space + text[0:space] + ' $\n')
140 text = text[space+1:]
142 # Subsequent lines are continuations, so indent them.
143 leading_space = ' ' * (indent+2)
145 self.output.write(leading_space + text + '\n')
147 def _as_list(self, input):
150 if isinstance(input, list):
156 """Escape a string such that it can be embedded into a Ninja file without
157 further interpretation."""
158 assert '\n' not in string, 'Ninja syntax does not allow newlines'
159 # We only have one special metacharacter: '$'.
160 return string.replace('$', '$$')