[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 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. |
| 4 | |
| 5 | # Autocompletion config for YouCompleteMe in Chromium. |
| 6 | # |
| 7 | # USAGE: |
| 8 | # |
| 9 | # 1. Install YCM [https://github.com/Valloric/YouCompleteMe] |
| 10 | # (Googlers should check out [go/ycm]) |
| 11 | # |
| 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' |
| 15 | # |
| 16 | # 3. Profit |
| 17 | # |
| 18 | # |
| 19 | # Usage notes: |
| 20 | # |
| 21 | # * You must use ninja & clang to build Chromium. |
| 22 | # |
| 23 | # * You must have run gyp_chromium and built Chromium recently. |
| 24 | # |
| 25 | # |
| 26 | # Hacking notes: |
| 27 | # |
| 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. |
| 30 | # |
| 31 | # * Right now, we only pull the -I and -D flags. That seems to be sufficient |
| 32 | # for everything I've used it for. |
| 33 | # |
| 34 | # * That whole ninja & clang thing? We could support other configs if someone |
| 35 | # were willing to write the correct commands and a parser. |
| 36 | # |
| 37 | # * This has only been tested on gPrecise. |
| 38 | |
| 39 | |
| 40 | import os |
dzhioev | 37accc2 | 2014-08-29 23:08:21 | [diff] [blame] | 41 | import os.path |
johnme | 8d6edba | 2015-01-21 14:26:28 | [diff] [blame] | 42 | import re |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 43 | import subprocess |
rouslan | d026bc9 | 2014-10-29 17:54:01 | [diff] [blame] | 44 | import sys |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 45 | |
[email protected] | eea749f | 2013-02-06 19:30:38 | [diff] [blame] | 46 | |
johnme | 8d6edba | 2015-01-21 14:26:28 | [diff] [blame] | 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) |
eroman | cf9eb11 | 2015-01-26 20:46:54 | [diff] [blame] | 59 | except: |
johnme | 8d6edba | 2015-01-21 14:26:28 | [diff] [blame] | 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 | |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 75 | # Flags from YCM's default config. |
| 76 | flags = [ |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 77 | '-DUSE_CLANG_COMPLETER', |
| 78 | '-std=c++11', |
| 79 | '-x', |
| 80 | 'c++', |
| 81 | ] |
| 82 | |
| 83 | |
[email protected] | 1cab7568 | 2013-04-24 19:50:49 | [diff] [blame] | 84 | def PathExists(*args): |
| 85 | return os.path.exists(os.path.join(*args)) |
| 86 | |
| 87 | |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 88 | def FindChromeSrcFromFilename(filename): |
| 89 | """Searches for the root of the Chromium checkout. |
| 90 | |
| 91 | Simply checks parent directories until it finds .gclient and src/. |
| 92 | |
| 93 | Args: |
| 94 | filename: (String) Path to source file being edited. |
| 95 | |
| 96 | Returns: |
| 97 | (String) Path of 'src/', or None if unable to find. |
| 98 | """ |
| 99 | curdir = os.path.normpath(os.path.dirname(filename)) |
johnme | 956993f | 2015-01-21 14:29:43 | [diff] [blame] | 100 | while not (os.path.basename(os.path.realpath(curdir)) == 'src' |
| 101 | and PathExists(curdir, 'DEPS') |
| 102 | and (PathExists(curdir, '..', '.gclient') |
| 103 | or PathExists(curdir, '.git'))): |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 104 | nextdir = os.path.normpath(os.path.join(curdir, '..')) |
| 105 | if nextdir == curdir: |
| 106 | return None |
| 107 | curdir = nextdir |
johnme | 956993f | 2015-01-21 14:29:43 | [diff] [blame] | 108 | return curdir |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 109 | |
| 110 | |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 111 | def GetClangCommandFromNinjaForFilename(chrome_root, filename): |
| 112 | """Returns the command line to build |filename|. |
| 113 | |
[email protected] | eea749f | 2013-02-06 19:30:38 | [diff] [blame] | 114 | Asks ninja how it would build the source file. If the specified file is a |
| 115 | header, tries to find its companion source file first. |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 116 | |
| 117 | Args: |
| 118 | chrome_root: (String) Path to src/. |
| 119 | filename: (String) Path to source file being edited. |
| 120 | |
| 121 | Returns: |
| 122 | (List of Strings) Command line arguments for clang. |
| 123 | """ |
| 124 | if not chrome_root: |
| 125 | return [] |
| 126 | |
[email protected] | eea749f | 2013-02-06 19:30:38 | [diff] [blame] | 127 | # Generally, everyone benefits from including Chromium's src/, because all of |
| 128 | # Chromium's includes are relative to that. |
| 129 | chrome_flags = ['-I' + os.path.join(chrome_root)] |
| 130 | |
[email protected] | 30853fe4 | 2014-08-19 19:43:30 | [diff] [blame] | 131 | # Version of Clang used to compile Chromium can be newer then version of |
| 132 | # libclang that YCM uses for completion. So it's possible that YCM's libclang |
| 133 | # doesn't know about some used warning options, which causes compilation |
| 134 | # warnings (and errors, because of '-Werror'); |
| 135 | chrome_flags.append('-Wno-unknown-warning-option') |
| 136 | |
[email protected] | 3025ebc4 | 2014-07-08 15:37:22 | [diff] [blame] | 137 | # Default file to get a reasonable approximation of the flags for a Blink |
| 138 | # file. |
| 139 | blink_root = os.path.join(chrome_root, 'third_party', 'WebKit') |
| 140 | default_blink_file = os.path.join(blink_root, 'Source', 'core', 'Init.cpp') |
| 141 | |
[email protected] | eea749f | 2013-02-06 19:30:38 | [diff] [blame] | 142 | # Header files can't be built. Instead, try to match a header file to its |
| 143 | # corresponding source file. |
| 144 | if filename.endswith('.h'): |
[email protected] | 3025ebc4 | 2014-07-08 15:37:22 | [diff] [blame] | 145 | # Add config.h to Blink headers, which won't have it by default. |
| 146 | if filename.startswith(blink_root): |
| 147 | chrome_flags.append('-include') |
| 148 | chrome_flags.append(os.path.join(blink_root, 'Source', 'config.h')) |
| 149 | |
[email protected] | eea749f | 2013-02-06 19:30:38 | [diff] [blame] | 150 | alternates = ['.cc', '.cpp'] |
| 151 | for alt_extension in alternates: |
| 152 | alt_name = filename[:-2] + alt_extension |
| 153 | if os.path.exists(alt_name): |
| 154 | filename = alt_name |
| 155 | break |
| 156 | else: |
[email protected] | 3025ebc4 | 2014-07-08 15:37:22 | [diff] [blame] | 157 | if filename.startswith(blink_root): |
| 158 | # If this is a Blink file, we can at least try to get a reasonable |
| 159 | # approximation. |
| 160 | filename = default_blink_file |
| 161 | else: |
| 162 | # If this is a standalone .h file with no source, the best we can do is |
| 163 | # try to use the default flags. |
| 164 | return chrome_flags |
[email protected] | eea749f | 2013-02-06 19:30:38 | [diff] [blame] | 165 | |
rouslan | d026bc9 | 2014-10-29 17:54:01 | [diff] [blame] | 166 | sys.path.append(os.path.join(chrome_root, 'tools', 'vim')) |
| 167 | from ninja_output import GetNinjaOutputDirectory |
dzhioev | 95e4bfd | 2014-09-05 14:39:53 | [diff] [blame] | 168 | out_dir = os.path.realpath(GetNinjaOutputDirectory(chrome_root)) |
[email protected] | b944e06 | 2013-05-08 01:40:55 | [diff] [blame] | 169 | |
dzhioev | 37accc2 | 2014-08-29 23:08:21 | [diff] [blame] | 170 | # Ninja needs the path to the source file relative to the output build |
| 171 | # directory. |
dzhioev | 95e4bfd | 2014-09-05 14:39:53 | [diff] [blame] | 172 | rel_filename = os.path.relpath(os.path.realpath(filename), out_dir) |
dzhioev | 37accc2 | 2014-08-29 23:08:21 | [diff] [blame] | 173 | |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 174 | # Ask ninja how it would build our source file. |
[email protected] | b944e06 | 2013-05-08 01:40:55 | [diff] [blame] | 175 | p = subprocess.Popen(['ninja', '-v', '-C', out_dir, '-t', |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 176 | 'commands', rel_filename + '^'], |
| 177 | stdout=subprocess.PIPE) |
| 178 | stdout, stderr = p.communicate() |
| 179 | if p.returncode: |
[email protected] | eea749f | 2013-02-06 19:30:38 | [diff] [blame] | 180 | return chrome_flags |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 181 | |
| 182 | # Ninja might execute several commands to build something. We want the last |
| 183 | # clang command. |
| 184 | clang_line = None |
| 185 | for line in reversed(stdout.split('\n')): |
[email protected] | eea749f | 2013-02-06 19:30:38 | [diff] [blame] | 186 | if 'clang' in line: |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 187 | clang_line = line |
| 188 | break |
[email protected] | eea749f | 2013-02-06 19:30:38 | [diff] [blame] | 189 | else: |
| 190 | return chrome_flags |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 191 | |
[email protected] | e666f18 | 2014-01-09 12:21:37 | [diff] [blame] | 192 | # Parse flags that are important for YCM's purposes. |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 193 | for flag in clang_line.split(' '): |
| 194 | if flag.startswith('-I'): |
| 195 | # Relative paths need to be resolved, because they're relative to the |
| 196 | # output dir, not the source. |
| 197 | if flag[2] == '/': |
| 198 | chrome_flags.append(flag) |
| 199 | else: |
[email protected] | b944e06 | 2013-05-08 01:40:55 | [diff] [blame] | 200 | abs_path = os.path.normpath(os.path.join(out_dir, flag[2:])) |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 201 | chrome_flags.append('-I' + abs_path) |
[email protected] | e666f18 | 2014-01-09 12:21:37 | [diff] [blame] | 202 | elif flag.startswith('-std'): |
| 203 | chrome_flags.append(flag) |
[email protected] | eea749f | 2013-02-06 19:30:38 | [diff] [blame] | 204 | elif flag.startswith('-') and flag[1] in 'DWFfmO': |
[email protected] | dc569cf9 | 2013-07-12 03:21:12 | [diff] [blame] | 205 | if flag == '-Wno-deprecated-register' or flag == '-Wno-header-guard': |
| 206 | # These flags causes libclang (3.3) to crash. Remove it until things |
[email protected] | 06a4f3e | 2013-07-09 00:16:49 | [diff] [blame] | 207 | # are fixed. |
| 208 | continue |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 209 | chrome_flags.append(flag) |
| 210 | |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 211 | return chrome_flags |
| 212 | |
| 213 | |
| 214 | def FlagsForFile(filename): |
| 215 | """This is the main entry point for YCM. Its interface is fixed. |
| 216 | |
| 217 | Args: |
| 218 | filename: (String) Path to source file being edited. |
| 219 | |
| 220 | Returns: |
| 221 | (Dictionary) |
| 222 | 'flags': (List of Strings) Command line flags. |
| 223 | 'do_cache': (Boolean) True if the result should be cached. |
| 224 | """ |
| 225 | chrome_root = FindChromeSrcFromFilename(filename) |
| 226 | chrome_flags = GetClangCommandFromNinjaForFilename(chrome_root, |
| 227 | filename) |
johnme | 8d6edba | 2015-01-21 14:26:28 | [diff] [blame] | 228 | final_flags = flags + chrome_flags + _system_include_flags |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 229 | |
[email protected] | a08a428 | 2013-02-05 23:45:52 | [diff] [blame] | 230 | return { |
| 231 | 'flags': final_flags, |
| 232 | 'do_cache': True |
| 233 | } |