6 Writes the c and cpp compile commands into build/compile_commands.json
7 see http://clang.llvm.org/docs/JSONCompilationDatabase.html
12 conf.load('compiler_cxx')
14 conf.load('clang_compilation_database')
17 import sys, os, json, shlex, pipes
18 from waflib import Logs, TaskGen, Task
20 Task.Task.keep_last_cmd = True
22 @TaskGen.feature('c', 'cxx')
23 @TaskGen.after_method('process_use')
24 def collect_compilation_db_tasks(self):
25 "Add a compilation database entry for compiled tasks"
27 clang_db = self.bld.clang_compilation_database_tasks
28 except AttributeError:
29 clang_db = self.bld.clang_compilation_database_tasks = []
30 self.bld.add_post_fun(write_compilation_database)
32 tup = tuple(y for y in [Task.classes.get(x) for x in ('c', 'cxx')] if y)
33 for task in getattr(self, 'compiled_tasks', []):
34 if isinstance(task, tup):
37 def write_compilation_database(ctx):
38 "Write the clang compilation database as JSON"
39 database_file = ctx.bldnode.make_node('compile_commands.json')
40 Logs.info('Build commands will be stored in %s' % database_file.path_from(ctx.path))
42 root = json.load(database_file)
45 clang_db = dict((x['file'], x) for x in root)
46 for task in getattr(ctx, 'clang_compilation_database_tasks', []):
49 except AttributeError:
51 directory = getattr(task, 'cwd', ctx.variant_dir)
52 f_node = task.inputs[0]
53 filename = os.path.relpath(f_node.abspath(), directory)
55 "directory": directory,
59 clang_db[filename] = entry
60 root = list(clang_db.values())
61 database_file.write(json.dumps(root, indent=2))
63 # Override the runnable_status function to do a dummy/dry run when the file doesn't need to be compiled.
64 # This will make sure compile_commands.json is always fully up to date.
65 # Previously you could end up with a partial compile_commands.json if the build failed.
66 for x in ('c', 'cxx'):
67 if x not in Task.classes:
72 def runnable_status(self):
73 def exec_command(cmd, **kw):
76 run_status = self.old_runnable_status()
77 if run_status == Task.SKIP_ME:
78 setattr(self, 'old_exec_command', getattr(self, 'exec_command', None))
79 setattr(self, 'exec_command', exec_command)
81 setattr(self, 'exec_command', getattr(self, 'old_exec_command', None))
84 setattr(t, 'old_runnable_status', getattr(t, 'runnable_status', None))
85 setattr(t, 'runnable_status', runnable_status)