aboutsummaryrefslogtreecommitdiffstats
path: root/build/scripts/preprocess.py
blob: 012617fdfc701afd7cc3b9ddb7bca34f658b0572 (plain) (blame)
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
import sys
import os


def load_file(p):
    with open(p, 'r') as f:
        return f.read()


def step(base, data, hh):
    def flt():
        for line in data.split('\n'):
            if line in hh:
                pp = os.path.join(base, hh[line])

                yield '\n\n' + load_file(pp) + '\n\n'

                os.unlink(pp)
            else:
                yield line

    return '\n'.join(flt())


def subst_headers(path, headers):
    hh = dict()

    for h in headers:
        hh['# include "' + h + '"'] = h

    data = load_file(path)
    prev = data

    while True:
        ret = step(os.path.dirname(path), prev, hh)

        if ret == prev:
            break

        prev = ret

    if data != prev:
        with open(path, 'w') as f:
            f.write(prev)


if __name__ == '__main__':
    subst_headers(sys.argv[1], ['stack.hh', 'position.hh', 'location.hh'])