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"""
7Clang tools on Windows are still a bit busted. The tooling can't handle
8backslashes in paths, doesn't understand how to read .rsp files, etc. In
9addition, ninja generates compile commands prefixed with the ninja msvc helper,
10which also confuses clang. This script generates a compile DB that should mostly
11work until clang tooling can be improved upstream.
12"""
13
14import os
15import re
16import json
17import shlex
18import subprocess
19import sys
20
21
22_NINJA_MSVC_WRAPPER = re.compile('ninja -t msvc -e .+? -- ')
23_RSP_RE = re.compile(r' (@(.+?\.rsp)) ')
24
25
26def _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
62def 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
82if __name__ == '__main__':
83  sys.exit(main(sys.argv[1:]))
84