3 """Python module for generating .ninja files.
5 Note that this is emphatically not a required piece of Ninja; it's
6 just a helpful utility for build-file-generation systems that already
13 def escape_path(word):
14 return word.replace('$ ', '$$ ').replace(' ', '$ ').replace(':', '$:')
17 def __init__(self, output, width=78):
22 self.output.write('\n')
24 def comment(self, text):
25 for line in textwrap.wrap(text, self.width - 2, break_long_words=False,
26 break_on_hyphens=False):
27 self.output.write('# ' + line + '\n')
29 def variable(self, key, value, indent=0):
32 if isinstance(value, list):
33 value = ' '.join(filter(None, value)) # Filter out empty strings.
34 self._line('%s = %s' % (key, value), indent)
36 def pool(self, name, depth):
37 self._line('pool %s' % name)
38 self.variable('depth', depth, indent=1)
40 def rule(self, name, command, description=None, depfile=None,
41 generator=False, pool=None, restat=False, rspfile=None,
42 rspfile_content=None, deps=None):
43 self._line('rule %s' % name)
44 self.variable('command', command, indent=1)
46 self.variable('description', description, indent=1)
48 self.variable('depfile', depfile, indent=1)
50 self.variable('generator', '1', indent=1)
52 self.variable('pool', pool, indent=1)
54 self.variable('restat', '1', indent=1)
56 self.variable('rspfile', rspfile, indent=1)
58 self.variable('rspfile_content', rspfile_content, indent=1)
60 self.variable('deps', deps, indent=1)
62 def build(self, outputs, rule, inputs=None, implicit=None, order_only=None,
63 variables=None, implicit_outputs=None, pool=None):
64 outputs = as_list(outputs)
65 out_outputs = [escape_path(x) for x in outputs]
66 all_inputs = [escape_path(x) for x in as_list(inputs)]
69 implicit = [escape_path(x) for x in as_list(implicit)]
70 all_inputs.append('|')
71 all_inputs.extend(implicit)
73 order_only = [escape_path(x) for x in as_list(order_only)]
74 all_inputs.append('||')
75 all_inputs.extend(order_only)
77 implicit_outputs = [escape_path(x)
78 for x in as_list(implicit_outputs)]
79 out_outputs.append('|')
80 out_outputs.extend(implicit_outputs)
82 self._line('build %s: %s' % (' '.join(out_outputs),
83 ' '.join([rule] + all_inputs)))
85 self._line(' pool = %s' % pool)
88 if isinstance(variables, dict):
89 iterator = iter(variables.items())
91 iterator = iter(variables)
93 for key, val in iterator:
94 self.variable(key, val, indent=1)
98 def include(self, path):
99 self._line('include %s' % path)
101 def subninja(self, path):
102 self._line('subninja %s' % path)
104 def default(self, paths):
105 self._line('default %s' % ' '.join(as_list(paths)))
107 def _count_dollars_before_index(self, s, i):
108 """Returns the number of '$' characters right in front of s[i]."""
111 while dollar_index > 0 and s[dollar_index] == '$':
116 def _line(self, text, indent=0):
117 """Write 'text' word-wrapped at self.width characters."""
118 leading_space = ' ' * indent
119 while len(leading_space) + len(text) > self.width:
120 # The text is too wide; wrap if possible.
122 # Find the rightmost space that would obey our width constraint and
123 # that's not an escaped space.
124 available_space = self.width - len(leading_space) - len(' $')
125 space = available_space
127 space = text.rfind(' ', 0, space)
129 self._count_dollars_before_index(text, space) % 2 == 0):
133 # No such space; just use the first unescaped space we can find.
134 space = available_space - 1
136 space = text.find(' ', space + 1)
138 self._count_dollars_before_index(text, space) % 2 == 0):
141 # Give up on breaking.
144 self.output.write(leading_space + text[0:space] + ' $\n')
145 text = text[space+1:]
147 # Subsequent lines are continuations, so indent them.
148 leading_space = ' ' * (indent+2)
150 self.output.write(leading_space + text + '\n')
159 if isinstance(input, list):
165 """Escape a string such that it can be embedded into a Ninja file without
166 further interpretation."""
167 assert '\n' not in string, 'Ninja syntax does not allow newlines'
168 # We only have one special metacharacter: '$'.
169 return string.replace('$', '$$')
172 def expand(string, vars, local_vars={}):
173 """Expand a string containing $vars as Ninja would.
175 Note: doesn't handle the full Ninja variable syntax, but it's enough
176 to make configure.py's use of it work.
182 return local_vars.get(var, vars.get(var, ''))
183 return re.sub(r'\$(\$|\w*)', exp, string)