| OLD | NEW |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 # Autocompletion config for YouCompleteMe in Chromium. | 5 # Autocompletion config for YouCompleteMe in Chromium. |
| 6 # | 6 # |
| 7 # USAGE: | 7 # USAGE: |
| 8 # | 8 # |
| 9 # 1. Install YCM [https://github.com/Valloric/YouCompleteMe] | 9 # 1. Install YCM [https://github.com/Valloric/YouCompleteMe] |
| 10 # (Googlers should check out [go/ycm]) | 10 # (Googlers should check out [go/ycm]) |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 # | 33 # |
| 34 # * That whole ninja & clang thing? We could support other configs if someone | 34 # * That whole ninja & clang thing? We could support other configs if someone |
| 35 # were willing to write the correct commands and a parser. | 35 # were willing to write the correct commands and a parser. |
| 36 # | 36 # |
| 37 # * This has only been tested on gPrecise. | 37 # * This has only been tested on gPrecise. |
| 38 | 38 |
| 39 | 39 |
| 40 import os | 40 import os |
| 41 import os.path | 41 import os.path |
| 42 import subprocess | 42 import subprocess |
| 43 import sys |
| 43 | 44 |
| 44 | 45 |
| 45 # Flags from YCM's default config. | 46 # Flags from YCM's default config. |
| 46 flags = [ | 47 flags = [ |
| 47 '-DUSE_CLANG_COMPLETER', | 48 '-DUSE_CLANG_COMPLETER', |
| 48 '-std=c++11', | 49 '-std=c++11', |
| 49 '-x', | 50 '-x', |
| 50 'c++', | 51 'c++', |
| 51 ] | 52 ] |
| 52 | 53 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 70 while not (PathExists(curdir, 'src') and PathExists(curdir, 'src', 'DEPS') | 71 while not (PathExists(curdir, 'src') and PathExists(curdir, 'src', 'DEPS') |
| 71 and (PathExists(curdir, '.gclient') | 72 and (PathExists(curdir, '.gclient') |
| 72 or PathExists(curdir, 'src', '.git'))): | 73 or PathExists(curdir, 'src', '.git'))): |
| 73 nextdir = os.path.normpath(os.path.join(curdir, '..')) | 74 nextdir = os.path.normpath(os.path.join(curdir, '..')) |
| 74 if nextdir == curdir: | 75 if nextdir == curdir: |
| 75 return None | 76 return None |
| 76 curdir = nextdir | 77 curdir = nextdir |
| 77 return os.path.join(curdir, 'src') | 78 return os.path.join(curdir, 'src') |
| 78 | 79 |
| 79 | 80 |
| 80 # Largely copied from ninja-build.vim (guess_configuration) | |
| 81 def GetNinjaOutputDirectory(chrome_root): | |
| 82 """Returns <chrome_root>/<output_dir>/(Release|Debug). | |
| 83 | |
| 84 The configuration chosen is the one most recently generated/built. Detects | |
| 85 a custom output_dir specified by GYP_GENERATOR_FLAGS.""" | |
| 86 | |
| 87 output_dir = 'out' | |
| 88 generator_flags = os.getenv('GYP_GENERATOR_FLAGS', '').split(' ') | |
| 89 for flag in generator_flags: | |
| 90 name_value = flag.split('=', 1) | |
| 91 if len(name_value) == 2 and name_value[0] == 'output_dir': | |
| 92 output_dir = name_value[1] | |
| 93 | |
| 94 root = os.path.join(chrome_root, output_dir) | |
| 95 debug_path = os.path.join(root, 'Debug') | |
| 96 release_path = os.path.join(root, 'Release') | |
| 97 | |
| 98 def is_release_15s_newer(test_path): | |
| 99 try: | |
| 100 debug_mtime = os.path.getmtime(os.path.join(debug_path, test_path)) | |
| 101 except os.error: | |
| 102 debug_mtime = 0 | |
| 103 try: | |
| 104 rel_mtime = os.path.getmtime(os.path.join(release_path, test_path)) | |
| 105 except os.error: | |
| 106 rel_mtime = 0 | |
| 107 return rel_mtime - debug_mtime >= 15 | |
| 108 | |
| 109 if is_release_15s_newer('build.ninja') or is_release_15s_newer('protoc'): | |
| 110 return release_path | |
| 111 return debug_path | |
| 112 | |
| 113 | |
| 114 def GetClangCommandFromNinjaForFilename(chrome_root, filename): | 81 def GetClangCommandFromNinjaForFilename(chrome_root, filename): |
| 115 """Returns the command line to build |filename|. | 82 """Returns the command line to build |filename|. |
| 116 | 83 |
| 117 Asks ninja how it would build the source file. If the specified file is a | 84 Asks ninja how it would build the source file. If the specified file is a |
| 118 header, tries to find its companion source file first. | 85 header, tries to find its companion source file first. |
| 119 | 86 |
| 120 Args: | 87 Args: |
| 121 chrome_root: (String) Path to src/. | 88 chrome_root: (String) Path to src/. |
| 122 filename: (String) Path to source file being edited. | 89 filename: (String) Path to source file being edited. |
| 123 | 90 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 else: | 126 else: |
| 160 if filename.startswith(blink_root): | 127 if filename.startswith(blink_root): |
| 161 # If this is a Blink file, we can at least try to get a reasonable | 128 # If this is a Blink file, we can at least try to get a reasonable |
| 162 # approximation. | 129 # approximation. |
| 163 filename = default_blink_file | 130 filename = default_blink_file |
| 164 else: | 131 else: |
| 165 # If this is a standalone .h file with no source, the best we can do is | 132 # If this is a standalone .h file with no source, the best we can do is |
| 166 # try to use the default flags. | 133 # try to use the default flags. |
| 167 return chrome_flags | 134 return chrome_flags |
| 168 | 135 |
| 136 sys.path.append(os.path.join(chrome_root, 'tools', 'vim')) |
| 137 from ninja_output import GetNinjaOutputDirectory |
| 169 out_dir = os.path.realpath(GetNinjaOutputDirectory(chrome_root)) | 138 out_dir = os.path.realpath(GetNinjaOutputDirectory(chrome_root)) |
| 170 | 139 |
| 171 # Ninja needs the path to the source file relative to the output build | 140 # Ninja needs the path to the source file relative to the output build |
| 172 # directory. | 141 # directory. |
| 173 rel_filename = os.path.relpath(os.path.realpath(filename), out_dir) | 142 rel_filename = os.path.relpath(os.path.realpath(filename), out_dir) |
| 174 | 143 |
| 175 # Ask ninja how it would build our source file. | 144 # Ask ninja how it would build our source file. |
| 176 p = subprocess.Popen(['ninja', '-v', '-C', out_dir, '-t', | 145 p = subprocess.Popen(['ninja', '-v', '-C', out_dir, '-t', |
| 177 'commands', rel_filename + '^'], | 146 'commands', rel_filename + '^'], |
| 178 stdout=subprocess.PIPE) | 147 stdout=subprocess.PIPE) |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 """ | 194 """ |
| 226 chrome_root = FindChromeSrcFromFilename(filename) | 195 chrome_root = FindChromeSrcFromFilename(filename) |
| 227 chrome_flags = GetClangCommandFromNinjaForFilename(chrome_root, | 196 chrome_flags = GetClangCommandFromNinjaForFilename(chrome_root, |
| 228 filename) | 197 filename) |
| 229 final_flags = flags + chrome_flags | 198 final_flags = flags + chrome_flags |
| 230 | 199 |
| 231 return { | 200 return { |
| 232 'flags': final_flags, | 201 'flags': final_flags, |
| 233 'do_cache': True | 202 'do_cache': True |
| 234 } | 203 } |
| OLD | NEW |