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.
5 # Autocompletion config for YouCompleteMe in V8.
9 # 1. Install YCM [https://github.com/Valloric/YouCompleteMe]
10 # (Googlers should check out [go/ycm])
17 # * You must use ninja & clang to build V8.
19 # * You must have run gyp_v8 and built V8 recently.
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.
27 # * Right now, we only pull the -I and -D flags. That seems to be sufficient
28 # for everything I've used it for.
30 # * That whole ninja & clang thing? We could support other configs if someone
31 # were willing to write the correct commands and a parser.
33 # * This has only been tested on gTrusty.
42 # Flags from YCM's default config.
44 '-DUSE_CLANG_COMPLETER',
51 def PathExists(*args):
52 return os.path.exists(os.path.join(*args))
55 def FindV8SrcFromFilename(filename):
56 """Searches for the root of the V8 checkout.
58 Simply checks parent directories until it finds .gclient and v8/.
61 filename: (String) Path to source file being edited.
64 (String) Path of 'v8/', or None if unable to find.
66 curdir = os.path.normpath(os.path.dirname(filename))
67 while not (PathExists(curdir, 'v8') and PathExists(curdir, 'v8', 'DEPS')
68 and (PathExists(curdir, '.gclient')
69 or PathExists(curdir, 'v8', '.git'))):
70 nextdir = os.path.normpath(os.path.join(curdir, '..'))
74 return os.path.join(curdir, 'v8')
77 def GetClangCommandFromNinjaForFilename(v8_root, filename):
78 """Returns the command line to build |filename|.
80 Asks ninja how it would build the source file. If the specified file is a
81 header, tries to find its companion source file first.
84 v8_root: (String) Path to v8/.
85 filename: (String) Path to source file being edited.
88 (List of Strings) Command line arguments for clang.
93 # Generally, everyone benefits from including V8's root, because all of
94 # V8's includes are relative to that.
95 v8_flags = ['-I' + os.path.join(v8_root)]
97 # Version of Clang used to compile V8 can be newer then version of
98 # libclang that YCM uses for completion. So it's possible that YCM's libclang
99 # doesn't know about some used warning options, which causes compilation
100 # warnings (and errors, because of '-Werror');
101 v8_flags.append('-Wno-unknown-warning-option')
103 # Header files can't be built. Instead, try to match a header file to its
104 # corresponding source file.
105 if filename.endswith('.h'):
106 alternates = ['.cc', '.cpp']
107 for alt_extension in alternates:
108 alt_name = filename[:-2] + alt_extension
109 if os.path.exists(alt_name):
113 if filename.endswith('-inl.h'):
114 for alt_extension in alternates:
115 alt_name = filename[:-6] + alt_extension
116 if os.path.exists(alt_name):
120 # If this is a standalone -inl.h file with no source, the best we can
121 # do is try to use the default flags.
124 # If this is a standalone .h file with no source, the best we can do is
125 # try to use the default flags.
128 sys.path.append(os.path.join(v8_root, 'tools', 'ninja'))
129 from ninja_output import GetNinjaOutputDirectory
130 out_dir = os.path.realpath(GetNinjaOutputDirectory(v8_root))
132 # Ninja needs the path to the source file relative to the output build
134 rel_filename = os.path.relpath(os.path.realpath(filename), out_dir)
136 # Ask ninja how it would build our source file.
137 p = subprocess.Popen(['ninja', '-v', '-C', out_dir, '-t',
138 'commands', rel_filename + '^'],
139 stdout=subprocess.PIPE)
140 stdout, stderr = p.communicate()
144 # Ninja might execute several commands to build something. We want the last
147 for line in reversed(stdout.split('\n')):
154 # Parse flags that are important for YCM's purposes.
155 for flag in clang_line.split(' '):
156 if flag.startswith('-I'):
157 # Relative paths need to be resolved, because they're relative to the
158 # output dir, not the source.
160 v8_flags.append(flag)
162 abs_path = os.path.normpath(os.path.join(out_dir, flag[2:]))
163 v8_flags.append('-I' + abs_path)
164 elif flag.startswith('-std'):
165 v8_flags.append(flag)
166 elif flag.startswith('-') and flag[1] in 'DWFfmO':
167 if flag == '-Wno-deprecated-register' or flag == '-Wno-header-guard':
168 # These flags causes libclang (3.3) to crash. Remove it until things
171 v8_flags.append(flag)
176 def FlagsForFile(filename):
177 """This is the main entry point for YCM. Its interface is fixed.
180 filename: (String) Path to source file being edited.
184 'flags': (List of Strings) Command line flags.
185 'do_cache': (Boolean) True if the result should be cached.
187 v8_root = FindV8SrcFromFilename(filename)
188 v8_flags = GetClangCommandFromNinjaForFilename(v8_root, filename)
189 final_flags = flags + v8_flags
191 'flags': final_flags,