Add compile_commands.json for ccls.
authorCarl Hetherington <cth@carlh.net>
Thu, 13 Feb 2020 23:25:31 +0000 (00:25 +0100)
committerCarl Hetherington <cth@carlh.net>
Thu, 13 Feb 2020 23:25:31 +0000 (00:25 +0100)
.gitignore
compile_commands.json [new symlink]
waf-tools/clang_compilation_database.py [new file with mode: 0644]
wscript

index 0298207cb9ee8182bbc4abb75672640150795ec7..71e4cd736916e9f35859e76d6795c637c2f1f0b6 100644 (file)
@@ -7,3 +7,4 @@ src/version.cc
 UnicodeData.txt
 doc/html
 doc/latex
+waf-tools/*.pyc
diff --git a/compile_commands.json b/compile_commands.json
new file mode 120000 (symlink)
index 0000000..25eb4b2
--- /dev/null
@@ -0,0 +1 @@
+build/compile_commands.json
\ No newline at end of file
diff --git a/waf-tools/clang_compilation_database.py b/waf-tools/clang_compilation_database.py
new file mode 100644 (file)
index 0000000..189de1e
--- /dev/null
@@ -0,0 +1,85 @@
+#!/usr/bin/env python
+# encoding: utf-8
+# Christoph Koke, 2013
+
+"""
+Writes the c and cpp compile commands into build/compile_commands.json
+see http://clang.llvm.org/docs/JSONCompilationDatabase.html
+
+Usage:
+
+    def configure(conf):
+        conf.load('compiler_cxx')
+        ...
+        conf.load('clang_compilation_database')
+"""
+
+import sys, os, json, shlex, pipes
+from waflib import Logs, TaskGen, Task
+
+Task.Task.keep_last_cmd = True
+
+@TaskGen.feature('c', 'cxx')
+@TaskGen.after_method('process_use')
+def collect_compilation_db_tasks(self):
+       "Add a compilation database entry for compiled tasks"
+       try:
+               clang_db = self.bld.clang_compilation_database_tasks
+       except AttributeError:
+               clang_db = self.bld.clang_compilation_database_tasks = []
+               self.bld.add_post_fun(write_compilation_database)
+
+       tup = tuple(y for y in [Task.classes.get(x) for x in ('c', 'cxx')] if y)
+       for task in getattr(self, 'compiled_tasks', []):
+               if isinstance(task, tup):
+                       clang_db.append(task)
+
+def write_compilation_database(ctx):
+       "Write the clang compilation database as JSON"
+       database_file = ctx.bldnode.make_node('compile_commands.json')
+       Logs.info('Build commands will be stored in %s' % database_file.path_from(ctx.path))
+       try:
+               root = json.load(database_file)
+       except IOError:
+               root = []
+       clang_db = dict((x['file'], x) for x in root)
+       for task in getattr(ctx, 'clang_compilation_database_tasks', []):
+               try:
+                       cmd = task.last_cmd
+               except AttributeError:
+                       continue
+               directory = getattr(task, 'cwd', ctx.variant_dir)
+               f_node = task.inputs[0]
+               filename = os.path.relpath(f_node.abspath(), directory)
+               entry = {
+                       "directory": directory,
+                       "arguments": cmd,
+                       "file": filename,
+               }
+               clang_db[filename] = entry
+       root = list(clang_db.values())
+       database_file.write(json.dumps(root, indent=2))
+
+# Override the runnable_status function to do a dummy/dry run when the file doesn't need to be compiled.
+# This will make sure compile_commands.json is always fully up to date.
+# Previously you could end up with a partial compile_commands.json if the build failed.
+for x in ('c', 'cxx'):
+       if x not in Task.classes:
+               continue
+
+       t = Task.classes[x]
+
+       def runnable_status(self):
+               def exec_command(cmd, **kw):
+                       pass
+
+               run_status = self.old_runnable_status()
+               if run_status == Task.SKIP_ME:
+                       setattr(self, 'old_exec_command', getattr(self, 'exec_command', None))
+                       setattr(self, 'exec_command', exec_command)
+                       self.run()
+                       setattr(self, 'exec_command', getattr(self, 'old_exec_command', None))
+               return run_status
+
+       setattr(t, 'old_runnable_status', getattr(t, 'runnable_status', None))
+       setattr(t, 'runnable_status', runnable_status)
diff --git a/wscript b/wscript
index 7798f0d9a885dd000e2967b86156087bb509fce9..c5b8019644eab443b7bd1acb5ef7354927dfe0a3 100644 (file)
--- a/wscript
+++ b/wscript
@@ -82,6 +82,7 @@ def options(opt):
 
 def configure(conf):
     conf.load('compiler_cxx')
+    conf.load('clang_compilation_database', tooldir=['waf-tools'])
     conf.env.append_value('CXXFLAGS', ['-Wall', '-Wextra', '-D_FILE_OFFSET_BITS=64', '-D__STDC_FORMAT_MACROS'])
     if conf.options.force_cpp11:
         conf.env.append_value('CXXFLAGS', ['-std=c++11', '-DBOOST_NO_CXX11_SCOPED_ENUMS'])