summaryrefslogtreecommitdiffstats
path: root/build/scripts/configure_file.py
blob: 37d81e4662f8fa4cc3e9833c7c124964dedccf10 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python2.7

from __future__ import print_function
import sys
import os.path
import re

cmakeDef01 = "#cmakedefine01"
cmakeDef = "#cmakedefine"


def replaceLine(line, varDict, define):
    words = line.split()
    if words:
        if words[0] == cmakeDef:
            sPos = line.find(cmakeDef)
            ePos = sPos + len(cmakeDef)
            line = line[:sPos] + define + line[ePos:] + '\n'
        if words[0] == cmakeDef01:
            var = words[1]
            cmakeValue = varDict.get(var)
            if cmakeValue == 'yes':
                val = '1'
            else:
                val = '0'
            sPos = line.find(cmakeDef01)
            ePos = line.find(var) + len(var)
            line = line[:sPos] + define + ' ' + var + ' ' + val + line[ePos + 1 :] + '\n'

    finder = re.compile(".*?(@[a-zA-Z0-9_]+@).*")
    while True:
        re_result = finder.match(line)
        if not re_result:
            return line
        key = re_result.group(1)[1:-1]
        line = line[: re_result.start(1)] + varDict.get(key, '') + line[re_result.end(1) :]


def main(inputPath, outputPath, varDict):
    define = '#define' if os.path.splitext(outputPath)[1] != '.asm' else '%define'
    with open(outputPath, 'w') as output:
        with open(inputPath, 'r') as input:
            for line in input:
                output.write(replaceLine(line, varDict, define))


def usage():
    print("usage: configure_file.py inputPath outputPath key1=value1 ...")
    exit(1)


if __name__ == "__main__":
    if len(sys.argv) < 3:
        usage()
    varDict = {}
    for x in sys.argv[3:]:
        try:
            key, value = str(x).split('=', 1)
            value = value.replace("#BACKSLASH#", "\\\\")
            value = value.replace("#DOUBLE_QUOTE#", '"')
        except Exception:
            continue
        varDict[key] = value

    main(sys.argv[1], sys.argv[2], varDict)