summaryrefslogtreecommitdiff
path: root/tools/meson-render-jinja2.py
blob: 9a6fc7c31c88eccf02484138d2ab7d2e04b3c0e5 (plain)
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
#!/usr/bin/env python3
# SPDX-License-Identifier: LGPL-2.1-or-later

import ast
import re
import sys

import jinja2

def parse_config_h(filename):
    # Parse config.h file generated by meson.
    ans = {}
    for line in open(filename):
        m = re.match(r'#define\s+(\w+)\s+(.*)', line)
        if not m:
            continue
        a, b = m.groups()
        if b and b[0] in '0123456789"':
            b = ast.literal_eval(b)
        ans[a] = b
    return ans

def render(filename, defines):
    text = open(filename).read()
    template = jinja2.Template(text, trim_blocks=True, undefined=jinja2.StrictUndefined)
    return template.render(defines)

if __name__ == '__main__':
    defines = parse_config_h(sys.argv[1])
    print(render(sys.argv[2], defines))