11457
|
1 |
#!/usr/bin/env python
|
|
2 |
# encoding: utf-8
|
|
3 |
# Christoph Koke, 2013
|
|
4 |
|
|
5 |
"""
|
|
6 |
Writes the c and cpp compile commands into build/compile_commands.json
|
|
7 |
see http://clang.llvm.org/docs/JSONCompilationDatabase.html
|
|
8 |
|
|
9 |
Usage:
|
|
10 |
|
|
11 |
def configure(conf):
|
|
12 |
conf.load('compiler_cxx')
|
|
13 |
...
|
|
14 |
conf.load('clang_compilation_database')
|
|
15 |
"""
|
|
16 |
|
|
17 |
import sys, os, json, shlex, pipes
|
|
18 |
from waflib import Logs, TaskGen
|
|
19 |
from waflib.Tools import c, cxx
|
|
20 |
|
|
21 |
if sys.hexversion >= 0x3030000:
|
|
22 |
quote = shlex.quote
|
|
23 |
else:
|
|
24 |
quote = pipes.quote
|
|
25 |
|
|
26 |
@TaskGen.feature('*')
|
|
27 |
@TaskGen.after_method('process_use')
|
|
28 |
def collect_compilation_db_tasks(self):
|
|
29 |
"Add a compilation database entry for compiled tasks"
|
|
30 |
try:
|
|
31 |
clang_db = self.bld.clang_compilation_database_tasks
|
|
32 |
except AttributeError:
|
|
33 |
clang_db = self.bld.clang_compilation_database_tasks = []
|
|
34 |
self.bld.add_post_fun(write_compilation_database)
|
|
35 |
|
|
36 |
for task in getattr(self, 'compiled_tasks', []):
|
|
37 |
if isinstance(task, (c.c, cxx.cxx)):
|
|
38 |
clang_db.append(task)
|
|
39 |
|
|
40 |
def write_compilation_database(ctx):
|
|
41 |
"Write the clang compilation database as JSON"
|
|
42 |
database_file = ctx.bldnode.make_node('compile_commands.json')
|
|
43 |
Logs.info("Build commands will be stored in %s" % database_file.path_from(ctx.path))
|
|
44 |
try:
|
|
45 |
root = json.load(database_file)
|
|
46 |
except IOError:
|
|
47 |
root = []
|
|
48 |
clang_db = dict((x["file"], x) for x in root)
|
|
49 |
for task in getattr(ctx, 'clang_compilation_database_tasks', []):
|
|
50 |
try:
|
|
51 |
cmd = task.last_cmd
|
|
52 |
except AttributeError:
|
|
53 |
continue
|
|
54 |
directory = getattr(task, 'cwd', ctx.variant_dir)
|
|
55 |
f_node = task.inputs[0]
|
|
56 |
filename = os.path.relpath(f_node.abspath(), directory)
|
|
57 |
cmd = " ".join(map(quote, cmd))
|
|
58 |
entry = {
|
|
59 |
"directory": directory,
|
|
60 |
"command": cmd,
|
|
61 |
"file": filename,
|
|
62 |
}
|
|
63 |
clang_db[filename] = entry
|
|
64 |
root = list(clang_db.values())
|
|
65 |
database_file.write(json.dumps(root, indent=2))
|
|
66 |
|