[WRTjs] Enable WRTjs
[platform/framework/web/chromium-efl.git] / v8 / .ycm_extra_conf.py
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.
4
5 # Autocompletion config for YouCompleteMe in V8.
6 #
7 # USAGE:
8 #
9 #   1. Install YCM [https://github.com/Valloric/YouCompleteMe]
10 #          (Googlers should check out [go/ycm])
11 #
12 #   2. Profit
13 #
14 #
15 # Usage notes:
16 #
17 #   * You must use ninja & clang to build V8.
18 #
19 #   * You must have run gyp_v8 and built V8 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 gTrusty.
34
35
36 import os
37 import os.path
38 import subprocess
39 import sys
40
41
42 # Flags from YCM's default config.
43 flags = [
44 '-DUSE_CLANG_COMPLETER',
45 '-x',
46 'c++',
47 ]
48
49
50 def PathExists(*args):
51   return os.path.exists(os.path.join(*args))
52
53
54 def FindV8SrcFromFilename(filename):
55   """Searches for the root of the V8 checkout.
56
57   Simply checks parent directories until it finds .gclient and v8/.
58
59   Args:
60     filename: (String) Path to source file being edited.
61
62   Returns:
63     (String) Path of 'v8/', or None if unable to find.
64   """
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, '..'))
70     if nextdir == curdir:
71       return None
72     curdir = nextdir
73   return os.path.join(curdir, 'v8')
74
75
76 def GetClangCommandFromNinjaForFilename(v8_root, filename):
77   """Returns the command line to build |filename|.
78
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.
81
82   Args:
83     v8_root: (String) Path to v8/.
84     filename: (String) Path to source file being edited.
85
86   Returns:
87     (List of Strings) Command line arguments for clang.
88   """
89   if not v8_root:
90     return []
91
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)]
95
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')
101
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):
108         filename = alternate
109         break
110     else:
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')
115
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))
119
120   # Ninja needs the path to the source file relative to the output build
121   # directory.
122   rel_filename = os.path.relpath(os.path.realpath(filename), out_dir)
123
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()
129   if p.returncode:
130     return v8_flags
131
132   # Ninja might execute several commands to build something. We want the last
133   # clang command.
134   clang_line = None
135   for line in reversed(stdout.decode('utf-8').splitlines()):
136     if 'clang' in line:
137       clang_line = line
138       break
139   else:
140     return v8_flags
141
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)
153   return v8_flags
154
155
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)] == '/':
160     return flag
161   else:
162     abs_path = os.path.normpath(os.path.join(out_dir, flag[len(prefix):]))
163     return prefix + abs_path
164
165
166 def FlagsForFile(filename):
167   """This is the main entry point for YCM. Its interface is fixed.
168
169   Args:
170     filename: (String) Path to source file being edited.
171
172   Returns:
173     (Dictionary)
174       'flags': (List of Strings) Command line flags.
175       'do_cache': (Boolean) True if the result should be cached.
176   """
177   v8_root = FindV8SrcFromFilename(filename)
178   v8_flags = GetClangCommandFromNinjaForFilename(v8_root, filename)
179   final_flags = flags + v8_flags
180   return {
181     'flags': final_flags,
182     'do_cache': True
183   }
184
185
186 def Settings(**kwargs):
187   if kwargs['language'] == 'cfamily':
188     return FlagsForFile(kwargs['filename'])
189   return {}