Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Autocompletion config for YouCompleteMe in Chromium. | |
| 2 # | |
| 3 # USAGE: | |
| 4 # | |
| 5 # 1. Install YCM [https://github.com/Valloric/YouCompleteMe] | |
| 6 # (Googlers should check out [go/ycm]) | |
| 7 # | |
| 8 # 2. Point to this config file in your .vimrc: | |
| 9 # let g:ycm_global_ycm_extra_conf = | |
| 10 # '<chrome_depot>/src/tools/vim/chromium.ycm_extra_conf.py' | |
| 11 # | |
| 12 # 3. Profit | |
| 13 # | |
| 14 # | |
| 15 # Usage notes: | |
| 16 # | |
| 17 # * You must use ninja & clang to build Chromium. | |
| 18 # | |
| 19 # * You must have run gyp_chromium and built Chromium recently. | |
| 20 # | |
| 21 # | |
| 22 # Hacking notes: | |
| 23 # | |
| 24 # * The purpose of this script is to construct an accurate enough command line | |
| 25 # for YCM to pass to clang so it can build and extract the symbols. | |
| 26 # | |
| 27 # * Right now, we only pull the -I and -D flags. That seems to be sufficient | |
| 28 # for everything I've used it for. | |
| 29 # | |
| 30 # * That whole ninja & clang thing? We could support other configs if someone | |
| 31 # were willing to write the correct commands and a parser. | |
| 32 # | |
| 33 # * This has only been tested on gPrecise. | |
| 34 | |
| 35 | |
| 36 import os | |
| 37 import subprocess | |
| 38 | |
| 39 # Flags from YCM's default config. | |
| 40 flags = [ | |
| 41 '-Wall', | |
| 42 '-Wextra', | |
| 43 '-Werror', | |
| 44 '-Wno-long-long', | |
| 45 '-Wno-variadic-macros', | |
| 46 '-Wno-unused-parameter', | |
| 47 '-fexceptions', | |
| 48 '-DNDEBUG', | |
| 49 '-DUSE_CLANG_COMPLETER', | |
| 50 '-std=c++11', | |
| 51 '-x', | |
| 52 'c++', | |
| 53 ] | |
| 54 | |
| 55 | |
| 56 def FindChromeSrcFromFilename(filename): | |
| 57 """Searches for the root of the Chromium checkout. | |
| 58 | |
| 59 Simply checks parent directories until it finds .gclient and src/. | |
| 60 | |
| 61 Args: | |
| 62 filename: (String) Path to source file being edited. | |
| 63 | |
| 64 Returns: | |
| 65 (String) Path of 'src/', or None if unable to find. | |
| 66 """ | |
| 67 curdir = os.path.normpath(os.path.dirname(filename)) | |
| 68 while not (os.path.exists(os.path.join(curdir, '.gclient')) | |
| 69 and os.path.exists(os.path.join(curdir, 'src'))): | |
| 70 nextdir = os.path.normpath(os.path.join(curdir, '..')) | |
| 71 if nextdir == curdir: | |
| 72 return None | |
| 73 curdir = nextdir | |
| 74 return os.path.join(curdir, 'src') | |
| 75 | |
| 76 | |
| 77 def GetClangCommandFromNinjaForFilename(chrome_root, filename): | |
| 78 """Returns the command line to build |filename|. | |
| 79 | |
| 80 Figures out where the .o file for this source file will end up. Then asks | |
| 81 ninja how it would build that object and parses the result. | |
| 82 | |
| 83 Args: | |
| 84 chrome_root: (String) Path to src/. | |
| 85 filename: (String) Path to source file being edited. | |
| 86 | |
| 87 Returns: | |
| 88 (List of Strings) Command line arguments for clang. | |
| 89 """ | |
| 90 if not chrome_root: | |
| 91 return [] | |
| 92 | |
| 93 # First we need to determine which directory this source file's .o will end up | |
| 94 # in. This should be the same relative path as the source file, but in | |
| 95 # out/Release/obj/. | |
| 96 dirname = os.path.dirname(filename) | |
| 97 # Cut off the common part and /. | |
| 98 rel_dirname = dirname[len(chrome_root)+1:] | |
| 99 out_dir = os.path.abspath(os.path.join( | |
| 100 chrome_root, 'out', 'Release', 'obj', rel_dirname)) | |
| 101 | |
| 102 # The object file will be prefixed with its GYP target. We have no idea what | |
| 103 # the target is, so we search for files with the same name. | |
| 104 # First, remove the extension so .cc and .h map to the same .o. | |
| 105 filebase = os.path.basename(filename[:filename.find('.')]) | |
| 106 obj_path = None | |
| 107 for name in os.listdir(out_dir): | |
|
jamesr
2013/02/05 21:15:53
This won't work for files that haven't been compil
jamesr
2013/02/05 21:26:07
FWIW wrapping this in a try:/except: is needed, bu
| |
| 108 # The .d file is slightly more likely to exist, so check for that. | |
| 109 if name.endswith('.' + filebase + '.o.d'): | |
| 110 # But really we want to build the .o, so trim off the .d. | |
|
Nico
2013/02/05 21:26:16
Urgh.
Could you do something like `ninja -C out/R
| |
| 111 obj_path = os.path.join('obj', rel_dirname, name[:-2]) | |
| 112 if not obj_path: | |
| 113 return [] | |
| 114 | |
| 115 # Ask ninja how it would build our .o file. | |
| 116 p = subprocess.Popen(['ninja', '-v', '-C', chrome_root + '/out/Release', '-t', | |
| 117 'commands', obj_path], | |
| 118 stdout=subprocess.PIPE) | |
| 119 stdout, stderr = p.communicate() | |
| 120 if p.returncode: | |
| 121 return [] | |
| 122 | |
| 123 # Ninja might execute several commands to build something. We want the last | |
| 124 # clang command. | |
| 125 clang_line = None | |
| 126 for line in reversed(stdout.split('\n')): | |
| 127 if line.startswith('clang'): | |
| 128 clang_line = line | |
| 129 break | |
| 130 if not clang_line: | |
| 131 return [] | |
| 132 | |
| 133 # Parse out the -I and -D flags. These seem to be the only ones that are | |
| 134 # important for YCM's purposes. | |
| 135 chrome_flags = [] | |
| 136 for flag in clang_line.split(' '): | |
| 137 if flag.startswith('-I'): | |
| 138 # Relative paths need to be resolved, because they're relative to the | |
| 139 # output dir, not the source. | |
| 140 if flag[2] == '/': | |
| 141 chrome_flags.append(flag) | |
| 142 else: | |
| 143 abs_path = os.path.normpath(os.path.join( | |
| 144 chrome_root, 'out', 'Release', flag[2:])) | |
| 145 chrome_flags.append('-I' + abs_path) | |
| 146 elif flag.startswith('-D'): | |
| 147 chrome_flags.append(flag) | |
| 148 | |
| 149 # Also include Chromium's src/, because all of Chromium's includes are | |
| 150 # relative to that. | |
| 151 chrome_flags.append('-I' + os.path.join(chrome_root)) | |
| 152 return chrome_flags | |
| 153 | |
| 154 | |
| 155 def FlagsForFile(filename): | |
| 156 """This is the main entry point for YCM. Its interface is fixed. | |
| 157 | |
| 158 Args: | |
| 159 filename: (String) Path to source file being edited. | |
| 160 | |
| 161 Returns: | |
| 162 (Dictionary) | |
| 163 'flags': (List of Strings) Command line flags. | |
| 164 'do_cache': (Boolean) True if the result should be cached. | |
| 165 """ | |
| 166 chrome_root = FindChromeSrcFromFilename(filename) | |
| 167 chrome_flags = GetClangCommandFromNinjaForFilename(chrome_root, | |
| 168 filename) | |
| 169 final_flags = flags + chrome_flags | |
| 170 | |
| 171 return { | |
| 172 'flags': final_flags, | |
| 173 'do_cache': True | |
| 174 } | |
| OLD | NEW |