1 #! /usr/bin/env python3
3 # Invoke sparse based on the contents of compile_commands.json,
4 # also working around several deficiencies in cgcc's command line
13 def cmdline_for_sparse(sparse, cmdline):
14 # Do not include the C compiler executable
17 out = sparse + ['-no-compile']
26 # prevent sparse from treating output files as inputs
27 if x == '-MF' or x == '-MQ' or x == '-o':
30 # cgcc ignores -no-compile if it sees -M or -MM?
31 if x.startswith('-M'):
33 # sparse does not understand these!
34 if x == '-iquote' or x == '-isystem':
41 root_path = os.getenv('MESON_BUILD_ROOT')
43 return s if not root_path else os.path.join(root_path, s)
45 ccjson_path = build_path(sys.argv[1])
46 with open(ccjson_path, 'r') as fd:
47 compile_commands = json.load(fd)
50 sparse_env = os.environ.copy()
51 for cmd in compile_commands:
52 cmdline = shlex.split(cmd['command'])
53 cmd = cmdline_for_sparse(sparse, cmdline)
54 print('REAL_CC=%s' % shlex.quote(cmdline[0]),
55 ' '.join((shlex.quote(x) for x in cmd)))
56 sparse_env['REAL_CC'] = cmdline[0]
57 r = subprocess.run(cmd, env=sparse_env, cwd=root_path)
59 sys.exit(r.returncode)