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
|
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Copyright © 2019 Collabora Ltd.
# SPDX-License-Identifier: GPL-3.0-or-later
import json
import os
import subprocess
import tempfile
if __name__ == '__main__':
with tempfile.TemporaryDirectory() as tmpdir:
with open(os.path.join(tmpdir, 'Makefile'), 'w') as writer:
writer.write('''\
hi: hi.c
\t$(CC) -DMAGIC=more_magic -o$@ $<
''')
with open(os.path.join(tmpdir, 'hi.c'), 'w') as writer:
writer.write('''\
#include <stdio.h>
int main (void)
{
puts ("hi, world!");
return 0;
}
''')
# see d/rules
env = dict(os.environ)
if 'PATH' in env:
env['PATH'] = ':'.join(p for p in env['PATH'].split(':') if not p.startswith('/usr/lib/ccache'))
subprocess.check_call(['bear', '--', 'make'], cwd=tmpdir, env=env)
with open(os.path.join(tmpdir, 'compile_commands.json')) as reader:
commands = json.load(reader)
assert len(commands) == 1, commands
command = commands[0]
assert '-DMAGIC=more_magic' in command['arguments'], command
assert command['directory'] == tmpdir, command
assert command['file'] == os.path.join(tmpdir, 'hi.c'), command
|