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',
50 def PathExists(*args):
51 return os.path.exists(os.path.join(*args))
54 def FindV8SrcFromFilename(filename):
55 """Searches for the root of the V8 checkout.
57 Simply checks parent directories until it finds .gclient and v8/.
60 filename: (String) Path to source file being edited.
63 (String) Path of 'v8/', or None if unable to find.
65 curdir = os.path.normpath(os.path.dirname(filename))
66 while not (PathExists(curdir, 'v8') and PathExists(curdir, 'v8', 'DEPS')
67 and (PathExists(curdir, '.gclient')
68 or PathExists(curdir, 'v8', '.git'))):
69 nextdir = os.path.normpath(os.path.join(curdir, '..'))
73 return os.path.join(curdir, 'v8')
76 def GetClangCommandFromNinjaForFilename(v8_root, filename):
77 """Returns the command line to build |filename|.
79 Asks ninja how it would build the source file. If the specified file is a
80 header, tries to find its companion source file first.
83 v8_root: (String) Path to v8/.
84 filename: (String) Path to source file being edited.
87 (List of Strings) Command line arguments for clang.
92 # Generally, everyone benefits from including V8's root, because all of
93 # V8's includes are relative to that.
94 v8_flags = ['-I' + os.path.join(v8_root)]
96 # Version of Clang used to compile V8 can be newer then version of
97 # libclang that YCM uses for completion. So it's possible that YCM's libclang
98 # doesn't know about some used warning options, which causes compilation
99 # warnings (and errors, because of '-Werror');
100 v8_flags.append('-Wno-unknown-warning-option')
102 # Header files can't be built. Instead, try to match a header file to its
103 # corresponding source file.
104 if filename.endswith('.h'):
105 base = filename[:-6] if filename.endswith('-inl.h') else filename[:-2]
106 for alternate in [base + e for e in ['.cc', '.cpp']]:
107 if os.path.exists(alternate):
111 # If this is a standalone .h file with no source, we ask ninja for the
112 # compile flags of some generic cc file ('src/utils/utils.cc'). This
113 # should contain most/all of the interesting flags for other targets too.
114 filename = os.path.join(v8_root, 'src', 'utils', 'utils.cc')
116 sys.path.append(os.path.join(v8_root, 'tools', 'vim'))
117 from ninja_output import GetNinjaOutputDirectory
118 out_dir = os.path.realpath(GetNinjaOutputDirectory(v8_root))
120 # Ninja needs the path to the source file relative to the output build
122 rel_filename = os.path.relpath(os.path.realpath(filename), out_dir)
124 # Ask ninja how it would build our source file.
125 p = subprocess.Popen(['ninja', '-v', '-C', out_dir, '-t',
126 'commands', rel_filename + '^'],
127 stdout=subprocess.PIPE)
128 stdout, stderr = p.communicate()
132 # Ninja might execute several commands to build something. We want the last
135 for line in reversed(stdout.decode('utf-8').splitlines()):
142 # Parse flags that are important for YCM's purposes.
143 for flag in clang_line.split(' '):
144 if flag.startswith('-I'):
145 v8_flags.append(MakeIncludePathAbsolute(flag, "-I", out_dir))
146 elif flag.startswith('-isystem'):
147 v8_flags.append(MakeIncludePathAbsolute(flag, "-isystem", out_dir))
148 elif flag.startswith('-std') or flag.startswith(
149 '-pthread') or flag.startswith('-no'):
150 v8_flags.append(flag)
151 elif flag.startswith('-') and flag[1] in 'DWFfmgOX':
152 v8_flags.append(flag)
156 def MakeIncludePathAbsolute(flag, prefix, out_dir):
157 # Relative paths need to be resolved, because they're relative to the
158 # output dir, not the source.
159 if flag[len(prefix)] == '/':
162 abs_path = os.path.normpath(os.path.join(out_dir, flag[len(prefix):]))
163 return prefix + abs_path
166 def FlagsForFile(filename):
167 """This is the main entry point for YCM. Its interface is fixed.
170 filename: (String) Path to source file being edited.
174 'flags': (List of Strings) Command line flags.
175 'do_cache': (Boolean) True if the result should be cached.
177 v8_root = FindV8SrcFromFilename(filename)
178 v8_flags = GetClangCommandFromNinjaForFilename(v8_root, filename)
179 final_flags = flags + v8_flags
181 'flags': final_flags,
186 def Settings(**kwargs):
187 if kwargs['language'] == 'cfamily':
188 return FlagsForFile(kwargs['filename'])