OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 |
| 3 # This file comes from |
| 4 # https://github.com/martine/ninja/blob/master/misc/ninja_syntax.py |
| 5 # Do not edit! Edit the upstream one instead. |
| 6 |
| 7 """Python module for generating .ninja files. |
| 8 |
| 9 Note that this is emphatically not a required piece of Ninja; it's |
| 10 just a helpful utility for build-file-generation systems that already |
| 11 use Python. |
| 12 """ |
| 13 |
| 14 import textwrap |
| 15 |
| 16 class Writer(object): |
| 17 def __init__(self, output, width=78): |
| 18 self.output = output |
| 19 self.width = width |
| 20 |
| 21 def newline(self): |
| 22 self.output.write('\n') |
| 23 |
| 24 def comment(self, text): |
| 25 for line in textwrap.wrap(text, self.width - 2): |
| 26 self.output.write('# ' + line + '\n') |
| 27 |
| 28 def variable(self, key, value, indent=0): |
| 29 self._line('%s = %s' % (key, value), indent) |
| 30 |
| 31 def rule(self, name, command, description=None, depfile=None): |
| 32 self._line('rule %s' % name) |
| 33 self.variable('command', command, indent=1) |
| 34 if description: |
| 35 self.variable('description', description, indent=1) |
| 36 if depfile: |
| 37 self.variable('depfile', depfile, indent=1) |
| 38 |
| 39 def build(self, outputs, rule, inputs=None, implicit=None, order_only=None, |
| 40 variables=None): |
| 41 outputs = self._as_list(outputs) |
| 42 all_inputs = self._as_list(inputs)[:] |
| 43 |
| 44 if implicit: |
| 45 all_inputs.append('|') |
| 46 all_inputs.extend(self._as_list(implicit)) |
| 47 if order_only: |
| 48 all_inputs.append('||') |
| 49 all_inputs.extend(self._as_list(order_only)) |
| 50 |
| 51 self._line('build %s: %s %s' % (' '.join(outputs), |
| 52 rule, |
| 53 ' '.join(all_inputs))) |
| 54 |
| 55 if variables: |
| 56 for key, val in variables: |
| 57 self.variable(key, val, indent=1) |
| 58 |
| 59 return outputs |
| 60 |
| 61 def _line(self, text, indent=0): |
| 62 """Write 'text' word-wrapped at self.width characters.""" |
| 63 leading_space = ' ' * indent |
| 64 while len(text) > self.width: |
| 65 # The text is too wide; wrap if possible. |
| 66 |
| 67 # Find the rightmost space that would obey our width constraint. |
| 68 available_space = self.width - len(leading_space) - len(' $') |
| 69 space = text.rfind(' ', 0, available_space) |
| 70 if space < 0: |
| 71 # No such space; just use the first space we can find. |
| 72 space = text.find(' ', available_space) |
| 73 if space < 0: |
| 74 # Give up on breaking. |
| 75 break |
| 76 |
| 77 self.output.write(leading_space + text[0:space] + ' $\n') |
| 78 text = text[space+1:] |
| 79 |
| 80 # Subsequent lines are continuations, so indent them. |
| 81 leading_space = ' ' * (indent+2) |
| 82 |
| 83 self.output.write(leading_space + text + '\n') |
| 84 |
| 85 def _as_list(self, input): |
| 86 if input is None: |
| 87 return [] |
| 88 if isinstance(input, list): |
| 89 return input |
| 90 return [input] |
| 91 |
| 92 |
| 93 def escape(string): |
| 94 """Escape a string such that it can be embedded into a Ninja file without |
| 95 further interpretation.""" |
| 96 assert '\n' not in string, 'Ninja syntax does not allow newlines' |
| 97 # We only have one special metacharacter: '$'. |
| 98 return string.replace('$', '$$') |
OLD | NEW |