OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2016 The Chromium Authors. All rights reserved. | 2 # Copyright 2016 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 import json | 6 import json |
7 import os | 7 import os |
8 import re | |
9 import shlex | |
10 import sys | |
8 import subprocess | 11 import subprocess |
9 | 12 |
10 | 13 |
14 _RSP_RE = re.compile(r' (@(.+?\.rsp)) ') | |
15 _debugging = False | |
16 | |
17 | |
18 def _ProcessEntry(entry): | |
19 """Transforms one entry in the compile database to be clang-tool friendly.""" | |
20 # Escape backslashes to prevent shlex from interpreting them. | |
21 escaped_command = entry['command'].replace('\\', '\\\\') | |
22 split_command = shlex.split(escaped_command) | |
23 # Drop gomacc.exe from the front, if present. | |
24 if split_command[0].endswith('gomacc.exe'): | |
25 split_command = split_command[1:] | |
26 # Insert --driver-mode=cl as the first argument. | |
27 split_command = split_command[:1] + ['--driver-mode=cl'] + split_command[1:] | |
28 entry['command'] = ' '.join(split_command) | |
29 | |
30 # Expand the contents of the response file, if any. | |
31 # http://llvm.org/bugs/show_bug.cgi?id=21634 | |
32 try: | |
33 match = _RSP_RE.search(entry['command']) | |
34 if match: | |
35 rsp_path = os.path.join(entry['directory'], match.group(2)) | |
36 rsp_contents = file(rsp_path).read() | |
37 entry['command'] = ''.join([ | |
38 entry['command'][:match.start(1)], | |
39 rsp_contents, | |
40 entry['command'][match.end(1):]]) | |
41 except IOError: | |
42 if _debugging: | |
43 print 'Couldn\'t read response file for %s' % entry['file'] | |
44 | |
45 return entry | |
46 | |
47 | |
48 def _ProcessCompileDatabaseForWindows(compile_db): | |
49 """Make the compile db generated by ninja on Windows more clang-tool friendly. | |
50 | |
51 Args: | |
52 compile_db: The compile database parsed as a Python dictionary. | |
53 | |
54 Returns: | |
55 A postprocessed compile db that clang tooling can use. | |
56 """ | |
57 if _debugging > 0: | |
58 print 'Read in %d entries from the compile db' % len(compile_db) | |
59 compile_db = [_ProcessEntry(e) for e in compile_db] | |
60 original_length = len(compile_db) | |
61 | |
62 # Filter out NaCl stuff. The clang tooling chokes on them. | |
63 # TODO(dcheng): This doesn't appear to do anything anymore, remove? | |
64 compile_db = [e for e in compile_db if '_nacl.cc.pdb' not in e['command'] | |
65 and '_nacl_win64.cc.pdb' not in e['command']] | |
66 if _debugging > 0: | |
67 print 'Filtered out %d entries...' % (original_length - len(compile_db)) | |
68 | |
69 # TODO(dcheng): Also filter out multiple commands for the same file. Not sure | |
70 # how that happens, but apparently it's an issue on Windows. | |
71 return compile_db | |
72 | |
73 | |
11 def GenerateWithNinja(path): | 74 def GenerateWithNinja(path): |
12 """Generates a compile database using ninja. | 75 """Generates a compile database using ninja. |
13 | 76 |
14 Args: | 77 Args: |
15 path: The build directory to generate a compile database for. | 78 path: The build directory to generate a compile database for. |
16 """ | 79 """ |
17 # TODO(dcheng): Incorporate Windows-specific compile DB munging from | 80 # TODO(dcheng): Ensure that clang is enabled by checking the output of |
18 # https://codereview.chromium.org/718873004 | 81 # gn args --list=is_clang --short |
Nico
2017/04/06 13:30:52
I don't think that does the right thing when clang
dcheng
2017/04/06 18:54:46
Doh, I thought it worked on Linux when I tested it
| |
19 print 'Generating compile database in %s...' % path | 82 |
20 args = ['ninja', '-C', path, '-t', 'compdb', 'cc', 'cxx', 'objc', 'objcxx'] | 83 # First, generate the compile database. |
21 output = subprocess.check_output(args) | 84 json_compile_db = subprocess.check_output([ |
22 with file(os.path.join(path, 'compile_commands.json'), 'w') as f: | 85 'ninja', '-C', path, '-t', 'compdb', 'cc', 'cxx', 'objc', 'objcxx']) |
23 f.write(output) | 86 compile_db = json.loads(json_compile_db) |
87 | |
88 # TODO(dcheng): Ideally this would check target_os... but not sure there's an | |
89 # easy way to do that, and (for now) cross-compiles don't work without custom | |
90 # patches anyway. | |
91 if sys.platform == 'win32': | |
92 compile_db = _ProcessCompileDatabaseForWindows(compile_db) | |
93 | |
94 f = file(os.path.join(path, 'compile_commands.json'), 'w') | |
95 f.write(json.dumps(compile_db, indent=2)) | |
Nico
2017/04/06 13:30:52
with open(os.path.join(path, 'compile_commands.jso
dcheng
2017/04/06 18:54:46
Done.
| |
24 | 96 |
25 | 97 |
26 def Read(path): | 98 def Read(path): |
27 """Reads a compile database into memory. | 99 """Reads a compile database into memory. |
28 | 100 |
29 Args: | 101 Args: |
30 path: Directory that contains the compile database. | 102 path: Directory that contains the compile database. |
31 """ | 103 """ |
32 with open(os.path.join(path, 'compile_commands.json'), 'rb') as db: | 104 with open(os.path.join(path, 'compile_commands.json'), 'rb') as db: |
33 return json.load(db) | 105 return json.load(db) |
OLD | NEW |