File: test-depfile.py

package info (click to toggle)
gcc-arm-none-eabi 15%3A14.2.rel1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,099,328 kB
  • sloc: cpp: 3,627,108; ansic: 2,571,498; ada: 834,230; f90: 235,082; makefile: 79,231; asm: 74,984; xml: 51,692; exp: 39,736; sh: 33,298; objc: 15,629; python: 15,069; fortran: 14,429; pascal: 7,003; awk: 5,070; perl: 3,106; ml: 285; lisp: 253; lex: 204; haskell: 135
file content (207 lines) | stat: -rw-r--r-- 5,182 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import json


# Parameters.
ALL_ERRORS = False


def _report_error(msg):
    '''Report an error.'''
    full_msg = 'ERROR: ' + msg
    if ALL_ERRORS:
        print(full_msg)
    else:
        raise RuntimeError(full_msg)


class Token(object):
    pass


class Output(Token):
    def __init__(self, path):
        self.path = path


class Input(Token):
    def __init__(self, path):
        self.path = path


class OrderInput(Token):
    def __init__(self, path):
        self.path = path


class Colon(Token):
    pass


class Append(Token):
    pass


class Variable(Token):
    def __init__(self, name):
        self.name = name


class Word(Token):
    def __init__(self, name):
        self.name = name


def validate_depfile(depfile, expect_input=None):
    '''Validate a depfile contains some information

    Returns `False` if the information is not found.
    '''
    with open(depfile, 'r') as fin:
        depfile_content = fin.read()

    real_lines = []
    join_line = False
    for line in depfile_content.split('\n'):
        # Join the line if needed.
        if join_line:
            line = real_lines.pop() + line

        # Detect line continuations.
        join_line = line.endswith('\\')
        # Strip line continuation characters.
        if join_line:
            line = line[:-1]

        # Add to the real line set.
        real_lines.append(line)

    # Perform tokenization.
    tokenized_lines = []
    for line in real_lines:
        tokenized = []
        join_word = False
        for word in line.split(' '):
            if join_word:
                word = tokenized.pop() + ' ' + word

            # Detect word joins.
            join_word = word.endswith('\\')
            # Strip escape character.
            if join_word:
                word = word[:-1]

            # Detect `:` at the end of a word.
            if word.endswith(':'):
                tokenized.append(word[:-1])
                word = word[-1]
            # Detect `:` at the end of a word.
            if word.endswith(':|'):
                tokenized.append(word[:-2])
                word = word[-2]

            # Add word to the tokenized set.
            tokenized.append(word)

        tokenized_lines.append(tokenized)

    # Parse.
    ast = []
    for line in tokenized_lines:
        kind = None
        for token in line:
            if token == ':':
                kind = 'dependency'
            elif token == '+=':
                kind = 'append'
            elif token == ':|':
                kind = 'order-only'
        if line == ['']:
            kind = 'empty'

        if kind is None:
            _report_error('unknown line kind: %s' % line)

        line_parse = []
        if kind == 'dependency':
            after_colon = False
            for token in line:
                if token == ':':
                    after_colon = True
                elif after_colon:
                    line_parse.append(Input(token))
                else:
                    line_parse.append(Output(token))
        elif kind == 'order-only':
            after_op = False
            for token in line:
                if token == ':|':
                    after_op = True
                elif after_op:
                    line_parse.append(OrderInput(token))
                else:
                    line_parse.append(Output(token))
        elif kind == 'append':
            after_op = False
            for token in line:
                if token == '+=':
                    after_op = True
                elif after_op:
                    line_parse.append(Word(token))
                else:
                    line_parse.append(Variable(token))

        ast.append(line_parse)

    for node in ast:
        for token in node:
            if expect_input is not None:
                # If the input is found, clear the expectation.
                if isinstance(token, Input) and token.path == expect_input:
                    expect_input = None

    result = True
    if expect_input:
        _report_error('missing input: %s' % expect_input)
        result = False

    return result


if __name__ == '__main__':
    import sys

    depfile = None
    have_expect = False
    expect_input = None

    # Parse arguments.
    args = sys.argv[1:]
    while args:
        # Take an argument.
        arg = args.pop(0)

        # Flag to change how errors are reported.
        if arg == '-A' or arg == '--all':
            ALL_ERRORS = True
        # Required arguments.
        elif arg == '-d' or arg == '--depfile':
            depfile = args.pop(0)
        elif arg == '-i' or arg == '--expect-input':
            expect_input = args.pop(0)
            have_expect = True

    # Validate that we have the required arguments.
    if depfile is None:
        raise RuntimeError('missing "depfile" file')
    if have_expect is None:
        raise RuntimeError('missing an "expect" argument')

    # Do the actual work.
    try:
        is_ok = validate_depfile(depfile, expect_input=expect_input)
    except BaseException as e:
        _report_error('exception: %s' % e)

    # Fail if errors are found.
    if not is_ok:
        sys.exit(1)