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') | |
|
Nico
2013/02/05 23:48:50
Something like this is in ninja_build.vim too. May
| |
| 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 # Ninja needs the path to the source file from the output build directory. | |
| 94 # Cut off the common part and /. | |
| 95 subdir_filename = filename[len(chrome_root)+1:] | |
| 96 rel_filename = os.path.join('..', '..', subdir_filename) | |
| 97 | |
| 98 # Ask ninja how it would build our source file. | |
| 99 p = subprocess.Popen(['ninja', '-v', '-C', chrome_root + '/out/Release', '-t', | |
| 100 'commands', rel_filename + '^'], | |
| 101 stdout=subprocess.PIPE) | |
| 102 stdout, stderr = p.communicate() | |
| 103 if p.returncode: | |
| 104 return [] | |
| 105 | |
| 106 # Ninja might execute several commands to build something. We want the last | |
| 107 # clang command. | |
| 108 clang_line = None | |
| 109 for line in reversed(stdout.split('\n')): | |
| 110 if line.startswith('clang'): | |
| 111 clang_line = line | |
| 112 break | |
| 113 if not clang_line: | |
| 114 return [] | |
| 115 | |
| 116 # Parse out the -I and -D flags. These seem to be the only ones that are | |
| 117 # important for YCM's purposes. | |
| 118 chrome_flags = [] | |
| 119 for flag in clang_line.split(' '): | |
| 120 if flag.startswith('-I'): | |
| 121 # Relative paths need to be resolved, because they're relative to the | |
| 122 # output dir, not the source. | |
| 123 if flag[2] == '/': | |
| 124 chrome_flags.append(flag) | |
| 125 else: | |
| 126 abs_path = os.path.normpath(os.path.join( | |
| 127 chrome_root, 'out', 'Release', flag[2:])) | |
| 128 chrome_flags.append('-I' + abs_path) | |
| 129 elif flag.startswith('-D'): | |
| 130 chrome_flags.append(flag) | |
| 131 | |
| 132 # Also include Chromium's src/, because all of Chromium's includes are | |
| 133 # relative to that. | |
| 134 chrome_flags.append('-I' + os.path.join(chrome_root)) | |
| 135 return chrome_flags | |
| 136 | |
| 137 | |
| 138 def FlagsForFile(filename): | |
| 139 """This is the main entry point for YCM. Its interface is fixed. | |
| 140 | |
| 141 Args: | |
| 142 filename: (String) Path to source file being edited. | |
| 143 | |
| 144 Returns: | |
| 145 (Dictionary) | |
| 146 'flags': (List of Strings) Command line flags. | |
| 147 'do_cache': (Boolean) True if the result should be cached. | |
| 148 """ | |
| 149 chrome_root = FindChromeSrcFromFilename(filename) | |
| 150 chrome_flags = GetClangCommandFromNinjaForFilename(chrome_root, | |
| 151 filename) | |
| 152 final_flags = flags + chrome_flags | |
| 153 | |
| 154 return { | |
| 155 'flags': final_flags, | |
| 156 'do_cache': True | |
| 157 } | |
| OLD | NEW |