cutout.py revision a4e54154
1import argparse
2import subprocess
3import json
4import os
5import re
6
7if __name__== '__main__':
8    parser = argparse.ArgumentParser()
9    parser.add_argument('input')
10    parser.add_argument('output')
11    parser.add_argument('buildroot')
12
13    args = parser.parse_known_args()
14
15    # c_args might contain things that are essential for crosscompilation, but
16    # are not included in cc.cmd_array(), so we have to look them up ourselves
17    host_cargs = []
18    buildroot = args[0].buildroot
19    with open(os.path.join(buildroot, 'meson-info', 'intro-buildoptions.json')) as json_file:
20        bopts = json.load(json_file)
21        for opt in bopts:
22            if opt['name'] == 'c_args' and opt['section'] == 'compiler' and opt['machine'] == 'host':
23                host_cargs = opt['value']
24                break
25
26    cpp = args[1]
27    ret = subprocess.run(cpp + host_cargs + [args[0].input], stdout=subprocess.PIPE, check=True)
28
29    stdout = ret.stdout.decode('utf8')
30
31    with open(args[0].output, 'w') as out:
32        write = True
33        for l in stdout.split('\n'):
34            l = l.strip('\r')
35            if l.startswith('CUT_OUT_BEGIN'):
36                write = False
37
38            if write and l:
39                stripped = re.sub('^\s+', '', l)
40                stripped = re.sub('\s*,\s*', ',', stripped)
41                if not stripped.isspace() and stripped:
42                    out.write('%s\n' % stripped)
43
44            if l.startswith('CUT_OUT_END'):
45                write = True
46