OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 """ |
| 7 Clang tools on Windows are still a bit busted. The tooling can't handle |
| 8 backslashes in paths, doesn't understand how to read .rsp files, etc. In |
| 9 addition, ninja generates compile commands prefixed with the ninja msvc helper, |
| 10 which also confuses clang. This script generates a compile DB that should mostly |
| 11 work until clang tooling can be improved upstream. |
| 12 """ |
| 13 |
| 14 import os |
| 15 import re |
| 16 import json |
| 17 import shlex |
| 18 import subprocess |
| 19 import sys |
| 20 |
| 21 |
| 22 _NINJA_MSVC_WRAPPER = re.compile('ninja -t msvc -e .+? -- ') |
| 23 _RSP_RE = re.compile(r' (@(.+?\.rsp)) ') |
| 24 |
| 25 |
| 26 def _ProcessEntry(e): |
| 27 # Strip off the ninja -t msvc wrapper. |
| 28 e['command'] = _NINJA_MSVC_WRAPPER.sub('', e['command']) |
| 29 |
| 30 # Prepend --driver-mode=cl to the command's arguments. |
| 31 # Escape backslashes so shlex doesn't try to interpret them. |
| 32 escaped_command = e['command'].replace('\\', '\\\\') |
| 33 split_command = shlex.split(escaped_command) |
| 34 e['command'] = ' '.join( |
| 35 split_command[:1] + ['--driver-mode=cl'] + split_command[1:]) |
| 36 |
| 37 # Expand the contents of the response file, if any. |
| 38 # http://llvm.org/bugs/show_bug.cgi?id=21634 |
| 39 try: |
| 40 match = _RSP_RE.search(e['command']) |
| 41 rsp_path = os.path.join(e['directory'], match.group(2)) |
| 42 rsp_contents = file(rsp_path).read() |
| 43 e['command'] = ''.join([ |
| 44 e['command'][:match.start(1)], |
| 45 rsp_contents, |
| 46 e['command'][match.end(1):]]) |
| 47 except IOError: |
| 48 pass |
| 49 |
| 50 # TODO(dcheng): This should be implemented in Clang tooling. |
| 51 # http://llvm.org/bugs/show_bug.cgi?id=19687 |
| 52 # Finally, use slashes instead of backslashes to avoid bad escaping by the |
| 53 # tooling. This should really only matter for command, but we do it for all |
| 54 # keys for consistency. |
| 55 e['directory'] = e['directory'].replace('\\', '/') |
| 56 e['command'] = e['command'].replace('\\', '/') |
| 57 e['file'] = e['file'].replace('\\', '/') |
| 58 |
| 59 return e |
| 60 |
| 61 |
| 62 def main(argv): |
| 63 # First, generate the compile database. |
| 64 print 'Generating compile DB with ninja...' |
| 65 compile_db_as_json = subprocess.check_output(shlex.split( |
| 66 'ninja -C out/Debug -t compdb cc cxx objc objcxx')) |
| 67 |
| 68 compile_db = json.loads(compile_db_as_json) |
| 69 print 'Read in %d entries from the compile db' % len(compile_db) |
| 70 compile_db = [_ProcessEntry(e) for e in compile_db] |
| 71 original_length = len(compile_db) |
| 72 |
| 73 # Filter out NaCl stuff. The clang tooling chokes on them. |
| 74 compile_db = [e for e in compile_db if '_nacl.cc.pdb' not in e['command'] |
| 75 and '_nacl_win64.cc.pdb' not in e['command']] |
| 76 print 'Filtered out %d entries...' % (original_length - len(compile_db)) |
| 77 f = file('out/Debug/compile_commands.json', 'w') |
| 78 f.write(json.dumps(compile_db, indent=2)) |
| 79 print 'Done!' |
| 80 |
| 81 |
| 82 if __name__ == '__main__': |
| 83 sys.exit(main(sys.argv[1:])) |
OLD | NEW |