1 import argparse
2 import subprocess
3 import json
4 import os
5 import re
6
7 if __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 cpp_args = [i for i in host_cargs + [args[0].input] if not i.startswith('-g')]
28 ret = subprocess.run(cpp + cpp_args, stdout=subprocess.PIPE, check=True)
29
30 stdout = ret.stdout.decode('utf8')
31
32 with open(args[0].output, 'w') as out:
33 write = True
34 for l in stdout.split('\n'):
35 l = l.strip('\r')
36 if l.startswith('CUT_OUT_BEGIN'):
37 write = False
38
39 if write and l:
40 stripped = re.sub('^\s+', '', l)
41 stripped = re.sub('\s*,\s*', ',', stripped)
42 if not stripped.isspace() and stripped:
43 out.write('%s\n' % stripped)
44
45 if l.startswith('CUT_OUT_END'):
46 write = True