| 1 |
import json |
| 2 |
|
| 3 |
|
| 4 |
# Parameters. |
| 5 |
ALL_ERRORS = False |
| 6 |
|
| 7 |
|
| 8 |
def _report_error(msg): |
| 9 |
'''Report an error.''' |
| 10 |
full_msg = 'ERROR: ' + msg |
| 11 |
if ALL_ERRORS: |
| 12 |
print(full_msg) |
| 13 |
else: |
| 14 |
raise RuntimeError(full_msg) |
| 15 |
|
| 16 |
|
| 17 |
class Token(object): |
| 18 |
pass |
| 19 |
|
| 20 |
|
| 21 |
class Output(Token): |
| 22 |
def __init__(self, path): |
| 23 |
self.path = path |
| 24 |
|
| 25 |
|
| 26 |
class Input(Token): |
| 27 |
def __init__(self, path): |
| 28 |
self.path = path |
| 29 |
|
| 30 |
|
| 31 |
class OrderInput(Token): |
| 32 |
def __init__(self, path): |
| 33 |
self.path = path |
| 34 |
|
| 35 |
|
| 36 |
class Colon(Token): |
| 37 |
pass |
| 38 |
|
| 39 |
|
| 40 |
class Append(Token): |
| 41 |
pass |
| 42 |
|
| 43 |
|
| 44 |
class Variable(Token): |
| 45 |
def __init__(self, name): |
| 46 |
self.name = name |
| 47 |
|
| 48 |
|
| 49 |
class Word(Token): |
| 50 |
def __init__(self, name): |
| 51 |
self.name = name |
| 52 |
|
| 53 |
|
| 54 |
def validate_depfile(depfile, expect_input=None): |
| 55 |
'''Validate a depfile contains some information |
| 56 |
|
| 57 |
Returns `False` if the information is not found. |
| 58 |
''' |
| 59 |
with open(depfile, 'r') as fin: |
| 60 |
depfile_content = fin.read() |
| 61 |
|
| 62 |
real_lines = [] |
| 63 |
join_line = False |
| 64 |
for line in depfile_content.split('\n'): |
| 65 |
# Join the line if needed. |
| 66 |
if join_line: |
| 67 |
line = real_lines.pop() + line |
| 68 |
|
| 69 |
# Detect line continuations. |
| 70 |
join_line = line.endswith('\\') |
| 71 |
# Strip line continuation characters. |
| 72 |
if join_line: |
| 73 |
line = line[:-1] |
| 74 |
|
| 75 |
# Add to the real line set. |
| 76 |
real_lines.append(line) |
| 77 |
|
| 78 |
# Perform tokenization. |
| 79 |
tokenized_lines = [] |
| 80 |
for line in real_lines: |
| 81 |
tokenized = [] |
| 82 |
join_word = False |
| 83 |
for word in line.split(' '): |
| 84 |
if join_word: |
| 85 |
word = tokenized.pop() + ' ' + word |
| 86 |
|
| 87 |
# Detect word joins. |
| 88 |
join_word = word.endswith('\\') |
| 89 |
# Strip escape character. |
| 90 |
if join_word: |
| 91 |
word = word[:-1] |
| 92 |
|
| 93 |
# Detect `:` at the end of a word. |
| 94 |
if word.endswith(':'): |
| 95 |
tokenized.append(word[:-1]) |
| 96 |
word = word[-1] |
| 97 |
# Detect `:` at the end of a word. |
| 98 |
if word.endswith(':|'): |
| 99 |
tokenized.append(word[:-2]) |
| 100 |
word = word[-2] |
| 101 |
|
| 102 |
# Add word to the tokenized set. |
| 103 |
tokenized.append(word) |
| 104 |
|
| 105 |
tokenized_lines.append(tokenized) |
| 106 |
|
| 107 |
# Parse. |
| 108 |
ast = [] |
| 109 |
for line in tokenized_lines: |
| 110 |
kind = None |
| 111 |
for token in line: |
| 112 |
if token == ':': |
| 113 |
kind = 'dependency' |
| 114 |
elif token == '+=': |
| 115 |
kind = 'append' |
| 116 |
elif token == ':|': |
| 117 |
kind = 'order-only' |
| 118 |
if line == ['']: |
| 119 |
kind = 'empty' |
| 120 |
|
| 121 |
if kind is None: |
| 122 |
_report_error('unknown line kind: %s' % line) |
| 123 |
|
| 124 |
line_parse = [] |
| 125 |
if kind == 'dependency': |
| 126 |
after_colon = False |
| 127 |
for token in line: |
| 128 |
if token == ':': |
| 129 |
after_colon = True |
| 130 |
elif after_colon: |
| 131 |
line_parse.append(Input(token)) |
| 132 |
else: |
| 133 |
line_parse.append(Output(token)) |
| 134 |
elif kind == 'order-only': |
| 135 |
after_op = False |
| 136 |
for token in line: |
| 137 |
if token == ':|': |
| 138 |
after_op = True |
| 139 |
elif after_op: |
| 140 |
line_parse.append(OrderInput(token)) |
| 141 |
else: |
| 142 |
line_parse.append(Output(token)) |
| 143 |
elif kind == 'append': |
| 144 |
after_op = False |
| 145 |
for token in line: |
| 146 |
if token == '+=': |
| 147 |
after_op = True |
| 148 |
elif after_op: |
| 149 |
line_parse.append(Word(token)) |
| 150 |
else: |
| 151 |
line_parse.append(Variable(token)) |
| 152 |
|
| 153 |
ast.append(line_parse) |
| 154 |
|
| 155 |
for node in ast: |
| 156 |
for token in node: |
| 157 |
if expect_input is not None: |
| 158 |
# If the input is found, clear the expectation. |
| 159 |
if isinstance(token, Input) and token.path == expect_input: |
| 160 |
expect_input = None |
| 161 |
|
| 162 |
result = True |
| 163 |
if expect_input: |
| 164 |
_report_error('missing input: %s' % expect_input) |
| 165 |
result = False |
| 166 |
|
| 167 |
return result |
| 168 |
|
| 169 |
|
| 170 |
if __name__ == '__main__': |
| 171 |
import sys |
| 172 |
|
| 173 |
depfile = None |
| 174 |
have_expect = False |
| 175 |
expect_input = None |
| 176 |
|
| 177 |
# Parse arguments. |
| 178 |
args = sys.argv[1:] |
| 179 |
while args: |
| 180 |
# Take an argument. |
| 181 |
arg = args.pop(0) |
| 182 |
|
| 183 |
# Flag to change how errors are reported. |
| 184 |
if arg == '-A' or arg == '--all': |
| 185 |
ALL_ERRORS = True |
| 186 |
# Required arguments. |
| 187 |
elif arg == '-d' or arg == '--depfile': |
| 188 |
depfile = args.pop(0) |
| 189 |
elif arg == '-i' or arg == '--expect-input': |
| 190 |
expect_input = args.pop(0) |
| 191 |
have_expect = True |
| 192 |
|
| 193 |
# Validate that we have the required arguments. |
| 194 |
if depfile is None: |
| 195 |
raise RuntimeError('missing "depfile" file') |
| 196 |
if have_expect is None: |
| 197 |
raise RuntimeError('missing an "expect" argument') |
| 198 |
|
| 199 |
# Do the actual work. |
| 200 |
try: |
| 201 |
is_ok = validate_depfile(depfile, expect_input=expect_input) |
| 202 |
except BaseException as e: |
| 203 |
_report_error('exception: %s' % e) |
| 204 |
|
| 205 |
# Fail if errors are found. |
| 206 |
if not is_ok: |
| 207 |
sys.exit(1) |