Do image crop/scale/window in the butler prepare threads.
[dcpomatic.git] / waf-tools / clang_compilation_database.py
1 #!/usr/bin/env python
2 # encoding: utf-8
3 # Christoph Koke, 2013
4
5 """
6 Writes the c and cpp compile commands into build/compile_commands.json
7 see http://clang.llvm.org/docs/JSONCompilationDatabase.html
8
9 Usage:
10
11     def configure(conf):
12         conf.load('compiler_cxx')
13         ...
14         conf.load('clang_compilation_database')
15 """
16
17 import sys, os, json, shlex, pipes
18 from waflib import Logs, TaskGen, Task
19
20 Task.Task.keep_last_cmd = True
21
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"
26         try:
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)
31
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):
35                         clang_db.append(task)
36
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))
41         try:
42                 root = json.load(database_file)
43         except IOError:
44                 root = []
45         clang_db = dict((x['file'], x) for x in root)
46         for task in getattr(ctx, 'clang_compilation_database_tasks', []):
47                 try:
48                         cmd = task.last_cmd
49                 except AttributeError:
50                         continue
51                 directory = getattr(task, 'cwd', ctx.variant_dir)
52                 f_node = task.inputs[0]
53                 filename = os.path.relpath(f_node.abspath(), directory)
54                 entry = {
55                         "directory": directory,
56                         "arguments": cmd,
57                         "file": filename,
58                 }
59                 clang_db[filename] = entry
60         root = list(clang_db.values())
61         database_file.write(json.dumps(root, indent=2))
62
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:
68                 continue
69
70         t = Task.classes[x]
71
72         def runnable_status(self):
73                 def exec_command(cmd, **kw):
74                         pass
75
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)
80                         self.run()
81                         setattr(self, 'exec_command', getattr(self, 'old_exec_command', None))
82                 return run_status
83
84         setattr(t, 'old_runnable_status', getattr(t, 'runnable_status', None))
85         setattr(t, 'runnable_status', runnable_status)