OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 # Autocompletion config for YouCompleteMe in Chromium. | 5 # Autocompletion config for YouCompleteMe in Chromium. |
6 # | 6 # |
7 # USAGE: | 7 # USAGE: |
8 # | 8 # |
9 # 1. Install YCM [https://github.com/Valloric/YouCompleteMe] | 9 # 1. Install YCM [https://github.com/Valloric/YouCompleteMe] |
10 # (Googlers should check out [go/ycm]) | 10 # (Googlers should check out [go/ycm]) |
(...skipping 21 matching lines...) Expand all Loading... |
32 # for everything I've used it for. | 32 # for everything I've used it for. |
33 # | 33 # |
34 # * That whole ninja & clang thing? We could support other configs if someone | 34 # * That whole ninja & clang thing? We could support other configs if someone |
35 # were willing to write the correct commands and a parser. | 35 # were willing to write the correct commands and a parser. |
36 # | 36 # |
37 # * This has only been tested on gPrecise. | 37 # * This has only been tested on gPrecise. |
38 | 38 |
39 | 39 |
40 import os | 40 import os |
41 import os.path | 41 import os.path |
| 42 import re |
42 import subprocess | 43 import subprocess |
43 import sys | 44 import sys |
44 | 45 |
45 | 46 |
| 47 def SystemIncludeDirectoryFlags(): |
| 48 """Determines compile flags to include the system include directories. |
| 49 |
| 50 Use as a workaround for https://github.com/Valloric/YouCompleteMe/issues/303 |
| 51 |
| 52 Returns: |
| 53 (List of Strings) Compile flags to append. |
| 54 """ |
| 55 try: |
| 56 with open(os.devnull, 'rb') as DEVNULL: |
| 57 output = subprocess.check_output(['clang', '-v', '-E', '-x', 'c++', '-'], |
| 58 stdin=DEVNULL, stderr=subprocess.STDOUT) |
| 59 except (FileNotFoundError, subprocess.CalledProcessError): |
| 60 return [] |
| 61 includes_regex = r'#include <\.\.\.> search starts here:\s*' \ |
| 62 r'(.*?)End of search list\.' |
| 63 includes = re.search(includes_regex, output.decode(), re.DOTALL).group(1) |
| 64 flags = [] |
| 65 for path in includes.splitlines(): |
| 66 path = path.strip() |
| 67 if os.path.isdir(path): |
| 68 flags.append('-isystem') |
| 69 flags.append(path) |
| 70 return flags |
| 71 |
| 72 |
| 73 _system_include_flags = SystemIncludeDirectoryFlags() |
| 74 |
46 # Flags from YCM's default config. | 75 # Flags from YCM's default config. |
47 flags = [ | 76 flags = [ |
48 '-DUSE_CLANG_COMPLETER', | 77 '-DUSE_CLANG_COMPLETER', |
49 '-std=c++11', | 78 '-std=c++11', |
50 '-x', | 79 '-x', |
51 'c++', | 80 'c++', |
52 ] | 81 ] |
53 | 82 |
54 | 83 |
55 def PathExists(*args): | 84 def PathExists(*args): |
56 return os.path.exists(os.path.join(*args)) | 85 return os.path.exists(os.path.join(*args)) |
57 | 86 |
58 | 87 |
59 def FindChromeSrcFromFilename(filename): | 88 def FindChromeSrcFromFilename(filename): |
60 """Searches for the root of the Chromium checkout. | 89 """Searches for the root of the Chromium checkout. |
61 | 90 |
62 Simply checks parent directories until it finds .gclient and src/. | 91 Simply checks parent directories until it finds .gclient and src/. |
63 | 92 |
64 Args: | 93 Args: |
65 filename: (String) Path to source file being edited. | 94 filename: (String) Path to source file being edited. |
66 | 95 |
67 Returns: | 96 Returns: |
68 (String) Path of 'src/', or None if unable to find. | 97 (String) Path of 'src/', or None if unable to find. |
69 """ | 98 """ |
70 curdir = os.path.normpath(os.path.dirname(filename)) | 99 curdir = os.path.normpath(os.path.dirname(filename)) |
71 while not (PathExists(curdir, 'src') and PathExists(curdir, 'src', 'DEPS') | 100 while not (os.path.basename(os.path.realpath(curdir)) == 'src' |
72 and (PathExists(curdir, '.gclient') | 101 and PathExists(curdir, 'DEPS') |
73 or PathExists(curdir, 'src', '.git'))): | 102 and (PathExists(curdir, '..', '.gclient') |
| 103 or PathExists(curdir, '.git'))): |
74 nextdir = os.path.normpath(os.path.join(curdir, '..')) | 104 nextdir = os.path.normpath(os.path.join(curdir, '..')) |
75 if nextdir == curdir: | 105 if nextdir == curdir: |
76 return None | 106 return None |
77 curdir = nextdir | 107 curdir = nextdir |
78 return os.path.join(curdir, 'src') | 108 return curdir |
79 | 109 |
80 | 110 |
81 def GetClangCommandFromNinjaForFilename(chrome_root, filename): | 111 def GetClangCommandFromNinjaForFilename(chrome_root, filename): |
82 """Returns the command line to build |filename|. | 112 """Returns the command line to build |filename|. |
83 | 113 |
84 Asks ninja how it would build the source file. If the specified file is a | 114 Asks ninja how it would build the source file. If the specified file is a |
85 header, tries to find its companion source file first. | 115 header, tries to find its companion source file first. |
86 | 116 |
87 Args: | 117 Args: |
88 chrome_root: (String) Path to src/. | 118 chrome_root: (String) Path to src/. |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 filename: (String) Path to source file being edited. | 218 filename: (String) Path to source file being edited. |
189 | 219 |
190 Returns: | 220 Returns: |
191 (Dictionary) | 221 (Dictionary) |
192 'flags': (List of Strings) Command line flags. | 222 'flags': (List of Strings) Command line flags. |
193 'do_cache': (Boolean) True if the result should be cached. | 223 'do_cache': (Boolean) True if the result should be cached. |
194 """ | 224 """ |
195 chrome_root = FindChromeSrcFromFilename(filename) | 225 chrome_root = FindChromeSrcFromFilename(filename) |
196 chrome_flags = GetClangCommandFromNinjaForFilename(chrome_root, | 226 chrome_flags = GetClangCommandFromNinjaForFilename(chrome_root, |
197 filename) | 227 filename) |
198 final_flags = flags + chrome_flags | 228 final_flags = flags + chrome_flags + _system_include_flags |
199 | 229 |
200 return { | 230 return { |
201 'flags': final_flags, | 231 'flags': final_flags, |
202 'do_cache': True | 232 'do_cache': True |
203 } | 233 } |
OLD | NEW |