third_party:waf: update to upstream 2.0.4 release
[samba.git] / third_party / waf / waflib / extras / clang_compilation_database.py
1 #! /usr/bin/env python
2 # encoding: utf-8
3 # WARNING! Do not edit! https://waf.io/book/index.html#_obtaining_the_waf_file
4
5 #!/usr/bin/env python
6 # encoding: utf-8
7 # Christoph Koke, 2013
8
9 """
10 Writes the c and cpp compile commands into build/compile_commands.json
11 see http://clang.llvm.org/docs/JSONCompilationDatabase.html
12
13 Usage:
14
15     def configure(conf):
16         conf.load('compiler_cxx')
17         ...
18         conf.load('clang_compilation_database')
19 """
20
21 import sys, os, json, shlex, pipes
22 from waflib import Logs, TaskGen, Task
23
24 Task.Task.keep_last_cmd = True
25
26 if sys.hexversion >= 0x3030000:
27         quote = shlex.quote
28 else:
29         quote = pipes.quote
30
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"
35         try:
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)
40
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):
44                         clang_db.append(task)
45
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))
50         try:
51                 root = json.load(database_file)
52         except IOError:
53                 root = []
54         clang_db = dict((x['file'], x) for x in root)
55         for task in getattr(ctx, 'clang_compilation_database_tasks', []):
56                 try:
57                         cmd = task.last_cmd
58                 except AttributeError:
59                         continue
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))
64                 entry = {
65                         "directory": directory,
66                         "command": cmd,
67                         "file": filename,
68                 }
69                 clang_db[filename] = entry
70         root = list(clang_db.values())
71         database_file.write(json.dumps(root, indent=2))
72
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:
78                 continue
79
80         t = Task.classes[x]
81
82         def runnable_status(self):
83                 def exec_command(cmd, **kw):
84                         pass
85
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)
90                         self.run()
91                         setattr(self, 'exec_command', getattr(self, 'old_exec_command', None))
92                 return run_status
93
94         setattr(t, 'old_runnable_status', getattr(t, 'runnable_status', None))
95         setattr(t, 'runnable_status', runnable_status)
96