OLD | NEW |
(Empty) | |
| 1 # Copyright 2015 the V8 project 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 V8. |
| 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 # '<v8_root>/tools/vim/v8.ycm_extra_conf.py' |
| 15 # |
| 16 # 3. Profit |
| 17 # |
| 18 # |
| 19 # Usage notes: |
| 20 # |
| 21 # * You must use ninja & clang to build V8. |
| 22 # |
| 23 # * You must have run gyp_v8 and built V8 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 gTrusty. |
| 38 |
| 39 |
| 40 import os |
| 41 import os.path |
| 42 import subprocess |
| 43 import sys |
| 44 |
| 45 |
| 46 # Flags from YCM's default config. |
| 47 flags = [ |
| 48 '-DUSE_CLANG_COMPLETER', |
| 49 '-std=c++11', |
| 50 '-x', |
| 51 'c++', |
| 52 ] |
| 53 |
| 54 |
| 55 def PathExists(*args): |
| 56 return os.path.exists(os.path.join(*args)) |
| 57 |
| 58 |
| 59 def FindV8SrcFromFilename(filename): |
| 60 """Searches for the root of the V8 checkout. |
| 61 |
| 62 Simply checks parent directories until it finds .gclient and v8/. |
| 63 |
| 64 Args: |
| 65 filename: (String) Path to source file being edited. |
| 66 |
| 67 Returns: |
| 68 (String) Path of 'v8/', or None if unable to find. |
| 69 """ |
| 70 curdir = os.path.normpath(os.path.dirname(filename)) |
| 71 while not (PathExists(curdir, 'v8') and PathExists(curdir, 'v8', 'DEPS') |
| 72 and (PathExists(curdir, '.gclient') |
| 73 or PathExists(curdir, 'v8', '.git'))): |
| 74 nextdir = os.path.normpath(os.path.join(curdir, '..')) |
| 75 if nextdir == curdir: |
| 76 return None |
| 77 curdir = nextdir |
| 78 return os.path.join(curdir, 'v8') |
| 79 |
| 80 |
| 81 def GetClangCommandFromNinjaForFilename(v8_root, filename): |
| 82 """Returns the command line to build |filename|. |
| 83 |
| 84 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. |
| 86 |
| 87 Args: |
| 88 v8_root: (String) Path to v8/. |
| 89 filename: (String) Path to source file being edited. |
| 90 |
| 91 Returns: |
| 92 (List of Strings) Command line arguments for clang. |
| 93 """ |
| 94 if not v8_root: |
| 95 return [] |
| 96 |
| 97 # Generally, everyone benefits from including V8's root, because all of |
| 98 # V8's includes are relative to that. |
| 99 v8_flags = ['-I' + os.path.join(v8_root)] |
| 100 |
| 101 # Version of Clang used to compile V8 can be newer then version of |
| 102 # libclang that YCM uses for completion. So it's possible that YCM's libclang |
| 103 # doesn't know about some used warning options, which causes compilation |
| 104 # warnings (and errors, because of '-Werror'); |
| 105 v8_flags.append('-Wno-unknown-warning-option') |
| 106 |
| 107 # Header files can't be built. Instead, try to match a header file to its |
| 108 # corresponding source file. |
| 109 if filename.endswith('.h'): |
| 110 alternates = ['.cc', '.cpp'] |
| 111 for alt_extension in alternates: |
| 112 alt_name = filename[:-2] + alt_extension |
| 113 if os.path.exists(alt_name): |
| 114 filename = alt_name |
| 115 break |
| 116 else: |
| 117 if filename.endswith('-inl.h'): |
| 118 for alt_extension in alternates: |
| 119 alt_name = filename[:-6] + alt_extension |
| 120 if os.path.exists(alt_name): |
| 121 filename = alt_name |
| 122 break; |
| 123 else: |
| 124 # If this is a standalone -inl.h file with no source, the best we can |
| 125 # do is try to use the default flags. |
| 126 return v8_flags |
| 127 else: |
| 128 # If this is a standalone .h file with no source, the best we can do is |
| 129 # try to use the default flags. |
| 130 return v8_flags |
| 131 |
| 132 sys.path.append(os.path.join(v8_root, 'tools', 'vim')) |
| 133 from ninja_output import GetNinjaOutputDirectory |
| 134 out_dir = os.path.realpath(GetNinjaOutputDirectory(v8_root)) |
| 135 |
| 136 # Ninja needs the path to the source file relative to the output build |
| 137 # directory. |
| 138 rel_filename = os.path.relpath(os.path.realpath(filename), out_dir) |
| 139 |
| 140 # Ask ninja how it would build our source file. |
| 141 p = subprocess.Popen(['ninja', '-v', '-C', out_dir, '-t', |
| 142 'commands', rel_filename + '^'], |
| 143 stdout=subprocess.PIPE) |
| 144 stdout, stderr = p.communicate() |
| 145 if p.returncode: |
| 146 return v8_flags |
| 147 |
| 148 # Ninja might execute several commands to build something. We want the last |
| 149 # clang command. |
| 150 clang_line = None |
| 151 for line in reversed(stdout.split('\n')): |
| 152 if 'clang' in line: |
| 153 clang_line = line |
| 154 break |
| 155 else: |
| 156 return v8_flags |
| 157 |
| 158 # Parse flags that are important for YCM's purposes. |
| 159 for flag in clang_line.split(' '): |
| 160 if flag.startswith('-I'): |
| 161 # Relative paths need to be resolved, because they're relative to the |
| 162 # output dir, not the source. |
| 163 if flag[2] == '/': |
| 164 v8_flags.append(flag) |
| 165 else: |
| 166 abs_path = os.path.normpath(os.path.join(out_dir, flag[2:])) |
| 167 v8_flags.append('-I' + abs_path) |
| 168 elif flag.startswith('-std'): |
| 169 v8_flags.append(flag) |
| 170 elif flag.startswith('-') and flag[1] in 'DWFfmO': |
| 171 if flag == '-Wno-deprecated-register' or flag == '-Wno-header-guard': |
| 172 # These flags causes libclang (3.3) to crash. Remove it until things |
| 173 # are fixed. |
| 174 continue |
| 175 v8_flags.append(flag) |
| 176 |
| 177 return v8_flags |
| 178 |
| 179 |
| 180 def FlagsForFile(filename): |
| 181 """This is the main entry point for YCM. Its interface is fixed. |
| 182 |
| 183 Args: |
| 184 filename: (String) Path to source file being edited. |
| 185 |
| 186 Returns: |
| 187 (Dictionary) |
| 188 'flags': (List of Strings) Command line flags. |
| 189 'do_cache': (Boolean) True if the result should be cached. |
| 190 """ |
| 191 v8_root = FindV8SrcFromFilename(filename) |
| 192 v8_flags = GetClangCommandFromNinjaForFilename(v8_root, filename) |
| 193 final_flags = flags + v8_flags |
| 194 return { |
| 195 'flags': final_flags, |
| 196 'do_cache': True |
| 197 } |
OLD | NEW |