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
3 # found in the LICENSE file.
5 # Autocompletion config for YouCompleteMe in Chromium.
9 # 1. Install YCM [https://github.com/Valloric/YouCompleteMe]
10 # (Googlers should check out [go/ycm])
12 # 2. Point to this config file in your .vimrc:
13 # let g:ycm_global_ycm_extra_conf =
14 # '<chrome_depot>/src/tools/vim/chromium.ycm_extra_conf.py'
21 # * You must use ninja & clang to build Chromium.
23 # * You must have run gyp_chromium and built Chromium recently.
28 # * The purpose of this script is to construct an accurate enough command line
29 # for YCM to pass to clang so it can build and extract the symbols.
31 # * Right now, we only pull the -I and -D flags. That seems to be sufficient
32 # for everything I've used it for.
34 # * That whole ninja & clang thing? We could support other configs if someone
35 # were willing to write the correct commands and a parser.
37 # * This has only been tested on gPrecise.
44 # Flags from YCM's default config.
46 '-DUSE_CLANG_COMPLETER',
53 def PathExists(*args
):
54 return os
.path
.exists(os
.path
.join(*args
))
57 def FindChromeSrcFromFilename(filename
):
58 """Searches for the root of the Chromium checkout.
60 Simply checks parent directories until it finds .gclient and src/.
63 filename: (String) Path to source file being edited.
66 (String) Path of 'src/', or None if unable to find.
68 curdir
= os
.path
.normpath(os
.path
.dirname(filename
))
69 while not (PathExists(curdir
, 'src') and PathExists(curdir
, 'src', 'DEPS')
70 and (PathExists(curdir
, '.gclient')
71 or PathExists(curdir
, 'src', '.git'))):
72 nextdir
= os
.path
.normpath(os
.path
.join(curdir
, '..'))
76 return os
.path
.join(curdir
, 'src')
79 # Largely copied from ninja-build.vim (guess_configuration)
80 def GetNinjaOutputDirectory(chrome_root
):
81 """Returns <chrome_root>/<output_dir>/(Release|Debug).
83 The configuration chosen is the one most recently generated/built. Detects
84 a custom output_dir specified by GYP_GENERATOR_FLAGS."""
87 generator_flags
= os
.getenv('GYP_GENERATOR_FLAGS', '').split(' ')
88 for flag
in generator_flags
:
89 name_value
= flag
.split('=', 1)
90 if len(name_value
) == 2 and name_value
[0] == 'output_dir':
91 output_dir
= name_value
[1]
93 root
= os
.path
.join(chrome_root
, output_dir
)
94 debug_path
= os
.path
.join(root
, 'Debug')
95 release_path
= os
.path
.join(root
, 'Release')
97 def is_release_15s_newer(test_path
):
99 debug_mtime
= os
.path
.getmtime(os
.path
.join(debug_path
, test_path
))
103 rel_mtime
= os
.path
.getmtime(os
.path
.join(release_path
, test_path
))
106 return rel_mtime
- debug_mtime
>= 15
108 if is_release_15s_newer('build.ninja') or is_release_15s_newer('protoc'):
113 def GetClangCommandFromNinjaForFilename(chrome_root
, filename
):
114 """Returns the command line to build |filename|.
116 Asks ninja how it would build the source file. If the specified file is a
117 header, tries to find its companion source file first.
120 chrome_root: (String) Path to src/.
121 filename: (String) Path to source file being edited.
124 (List of Strings) Command line arguments for clang.
129 # Generally, everyone benefits from including Chromium's src/, because all of
130 # Chromium's includes are relative to that.
131 chrome_flags
= ['-I' + os
.path
.join(chrome_root
)]
133 # Default file to get a reasonable approximation of the flags for a Blink
135 blink_root
= os
.path
.join(chrome_root
, 'third_party', 'WebKit')
136 default_blink_file
= os
.path
.join(blink_root
, 'Source', 'core', 'Init.cpp')
138 # Header files can't be built. Instead, try to match a header file to its
139 # corresponding source file.
140 if filename
.endswith('.h'):
141 # Add config.h to Blink headers, which won't have it by default.
142 if filename
.startswith(blink_root
):
143 chrome_flags
.append('-include')
144 chrome_flags
.append(os
.path
.join(blink_root
, 'Source', 'config.h'))
146 alternates
= ['.cc', '.cpp']
147 for alt_extension
in alternates
:
148 alt_name
= filename
[:-2] + alt_extension
149 if os
.path
.exists(alt_name
):
153 if filename
.startswith(blink_root
):
154 # If this is a Blink file, we can at least try to get a reasonable
156 filename
= default_blink_file
158 # If this is a standalone .h file with no source, the best we can do is
159 # try to use the default flags.
162 # Ninja needs the path to the source file from the output build directory.
163 # Cut off the common part and /.
164 subdir_filename
= filename
[len(chrome_root
)+1:]
165 rel_filename
= os
.path
.join('..', '..', subdir_filename
)
167 out_dir
= GetNinjaOutputDirectory(chrome_root
)
169 # Ask ninja how it would build our source file.
170 p
= subprocess
.Popen(['ninja', '-v', '-C', out_dir
, '-t',
171 'commands', rel_filename
+ '^'],
172 stdout
=subprocess
.PIPE
)
173 stdout
, stderr
= p
.communicate()
177 # Ninja might execute several commands to build something. We want the last
180 for line
in reversed(stdout
.split('\n')):
187 # Parse flags that are important for YCM's purposes.
188 for flag
in clang_line
.split(' '):
189 if flag
.startswith('-I'):
190 # Relative paths need to be resolved, because they're relative to the
191 # output dir, not the source.
193 chrome_flags
.append(flag
)
195 abs_path
= os
.path
.normpath(os
.path
.join(out_dir
, flag
[2:]))
196 chrome_flags
.append('-I' + abs_path
)
197 elif flag
.startswith('-std'):
198 chrome_flags
.append(flag
)
199 elif flag
.startswith('-') and flag
[1] in 'DWFfmO':
200 if flag
== '-Wno-deprecated-register' or flag
== '-Wno-header-guard':
201 # These flags causes libclang (3.3) to crash. Remove it until things
204 chrome_flags
.append(flag
)
209 def FlagsForFile(filename
):
210 """This is the main entry point for YCM. Its interface is fixed.
213 filename: (String) Path to source file being edited.
217 'flags': (List of Strings) Command line flags.
218 'do_cache': (Boolean) True if the result should be cached.
220 chrome_root
= FindChromeSrcFromFilename(filename
)
221 chrome_flags
= GetClangCommandFromNinjaForFilename(chrome_root
,
223 final_flags
= flags
+ chrome_flags
226 'flags': final_flags
,