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
19 from waflib.Tools import c, cxx
21 if sys.hexversion >= 0x3030000:
27 @TaskGen.after_method('process_use')
28 def collect_compilation_db_tasks(self):
29 "Add a compilation database entry for compiled tasks"
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)
36 for task in getattr(self, 'compiled_tasks', []):
37 if isinstance(task, (c.c, cxx.cxx)):
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))
45 root = json.load(database_file)
48 clang_db = dict((x["file"], x) for x in root)
49 for task in getattr(ctx, 'clang_compilation_database_tasks', []):
52 except AttributeError:
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))
59 "directory": directory,
63 clang_db[filename] = entry
64 root = list(clang_db.values())
65 database_file.write(json.dumps(root, indent=2))