3 # WARNING! Do not edit! https://waf.io/book/index.html#_obtaining_the_waf_file
10 Writes the c and cpp compile commands into build/compile_commands.json
11 see http://clang.llvm.org/docs/JSONCompilationDatabase.html
16 conf.load('compiler_cxx')
18 conf.load('clang_compilation_database')
21 import sys, os, json, shlex, pipes
22 from waflib import Logs, TaskGen, Task
24 Task.Task.keep_last_cmd = True
26 if sys.hexversion >= 0x3030000:
31 @TaskGen.feature('c', 'cxx')
32 @TaskGen.after_method('process_use')
33 def collect_compilation_db_tasks(self):
34 "Add a compilation database entry for compiled tasks"
36 clang_db = self.bld.clang_compilation_database_tasks
37 except AttributeError:
38 clang_db = self.bld.clang_compilation_database_tasks = []
39 self.bld.add_post_fun(write_compilation_database)
41 tup = tuple(y for y in [Task.classes.get(x) for x in ('c', 'cxx')] if y)
42 for task in getattr(self, 'compiled_tasks', []):
43 if isinstance(task, tup):
46 def write_compilation_database(ctx):
47 "Write the clang compilation database as JSON"
48 database_file = ctx.bldnode.make_node('compile_commands.json')
49 Logs.info('Build commands will be stored in %s', database_file.path_from(ctx.path))
51 root = json.load(database_file)
54 clang_db = dict((x['file'], x) for x in root)
55 for task in getattr(ctx, 'clang_compilation_database_tasks', []):
58 except AttributeError:
60 directory = getattr(task, 'cwd', ctx.variant_dir)
61 f_node = task.inputs[0]
62 filename = os.path.relpath(f_node.abspath(), directory)
63 cmd = " ".join(map(quote, cmd))
65 "directory": directory,
69 clang_db[filename] = entry
70 root = list(clang_db.values())
71 database_file.write(json.dumps(root, indent=2))
73 # Override the runnable_status function to do a dummy/dry run when the file doesn't need to be compiled.
74 # This will make sure compile_commands.json is always fully up to date.
75 # Previously you could end up with a partial compile_commands.json if the build failed.
76 for x in ('c', 'cxx'):
77 if x not in Task.classes:
82 def runnable_status(self):
83 def exec_command(cmd, **kw):
86 run_status = self.old_runnable_status()
87 if run_status == Task.SKIP_ME:
88 setattr(self, 'old_exec_command', getattr(self, 'exec_command', None))
89 setattr(self, 'exec_command', exec_command)
91 setattr(self, 'exec_command', getattr(self, 'old_exec_command', None))
94 setattr(t, 'old_runnable_status', getattr(t, 'runnable_status', None))
95 setattr(t, 'runnable_status', runnable_status)