Typo in ColorConversion code: converColorComponents
[WebKit-https.git] / Tools / gtk / ycm_extra_conf.py
1 #!/usr/bin/env python
2 # Copyright (C) 2013 Danilo Cesar Lemes de Paula <danilo.eu@gmail.com>
3 # Copyright (C) 2014 ChangSeok Oh <shivamidow@gmail.com>
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2 of the License, or (at your option) any later version.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18
19 import os
20 import sys
21 import ycm_core
22
23 # It's very likely that this script is a symlink somewhere in the WebKit directory,
24 # so we try to find the actual script location so that we can locate the tools
25 # directory.
26 original_file = __file__[:-1] if __file__.endswith(".pyc") else __file__
27 if os.path.islink(original_file):
28     parent_folder = os.path.abspath(os.path.dirname(original_file))
29     link_file = os.path.join(parent_folder, os.readlink(original_file))
30     __tools_directory = os.path.dirname(link_file)
31 else:
32     __tools_directory = os.path.dirname(original_file)
33
34 top_level_directory = os.path.normpath(os.path.join(os.path.abspath(__tools_directory), '..', '..'))
35 sys.path.insert(0, os.path.join(top_level_directory, 'Tools', 'glib'))
36 import common
37
38
39 FLAGS_PRECEDING_PATHS = ['-isystem', '-I', '-iquote', '--sysroot=']
40 def transform_relative_paths_to_absolute_paths(arguments, build_path):
41     result = []
42     make_next_absolute = False
43     for argument in arguments:
44         if make_next_absolute:
45             make_next_absolute = False
46             if not argument.startswith('/'):
47                 argument = os.path.join(build_path, argument)
48         elif argument in FLAGS_PRECEDING_PATHS:
49             # Some flags precede the path in the list. For those we make the
50             # next argument absolute.
51             make_next_absolute = True
52         else:
53             # Some argument contain the flag and the path together. For these
54             # we parse the argument out of the path.
55             for flag in FLAGS_PRECEDING_PATHS:
56                 if argument.startswith(flag):
57                     argument = flag + os.path.join(build_path, argument[len(flag):])
58                     break
59
60         result.append(argument)
61     return result
62
63
64 def get_build_path():
65     webkitbuild_path = os.path.join(common.get_build_path(fatal=False), '..')
66     if not os.path.exists(webkitbuild_path):
67         return None
68
69     release_build_path = os.path.join(webkitbuild_path, 'Release')
70     debug_build_path = os.path.join(webkitbuild_path, 'Debug')
71
72     try:
73         release_mtime = os.path.getmtime(os.path.join(release_build_path, 'compile_commands.json'))
74     except os.error:
75         release_mtime = 0
76     try:
77         debug_mtime = os.path.getmtime(os.path.join(debug_build_path, 'compile_commands.json'))
78     except os.error:
79         debug_mtime = 0
80
81     return release_build_path if release_mtime >= debug_mtime else debug_build_path
82
83
84 def getImplementationFilename(filename):
85     alternative_extensions = ['.cpp', '.c']
86     for alternative_extension in alternative_extensions:
87         alternative_filename = filename[:-2] + alternative_extension
88         if os.path.exists(alternative_filename):
89             return alternative_filename
90     return None
91
92
93 def FlagsForFile(filename, **kwargs):
94     """This is the main entry point for YCM. Its interface is fixed.
95
96     Args:
97       filename: (String) Path to source file being edited.
98
99     Returns:
100       (Dictionary)
101         'flags': (List of Strings) Command line flags.
102         'do_cache': (Boolean) True if the result should be cached.
103     """
104
105     result = {'flags': ['-std=c++17', '-x', 'c++'], 'do_cache': True}
106
107     # Headers can't be built, so we get the source file flags instead.
108     if filename.endswith('.h'):
109         implementationFilename = getImplementationFilename(filename)
110         if implementationFilename:
111             filename = implementationFilename
112         else:
113             if not filename.endswith('Inlines.h'):
114                 return result
115             implementationFilename = getImplementationFilename(filename[:-len('Inlines.h')] + '.h')
116             if not implementationFilename:
117                 return result
118             filename = implementationFilename
119         # Force config.h file inclusion, for GLib macros.
120         result['flags'].append("-includeconfig.h")
121
122     build_path = os.path.normpath(get_build_path())
123     if not build_path:
124         print("Could not find WebKit build path.")
125         return result
126
127     database = ycm_core.CompilationDatabase(build_path)
128     if not database:
129         print("Could not find compile_commands.json in %s, You might forget to add CMAKE_EXPORT_COMPILE_COMMANDS=ON to cmakeargs" % build_path)
130         return result
131
132     compilation_info = database.GetCompilationInfoForFile(filename)
133     if not compilation_info:
134         print("No compilation info.")
135         return result
136
137     result['flags'] = transform_relative_paths_to_absolute_paths(list(compilation_info.compiler_flags_), compilation_info.compiler_working_dir_)
138     return result
139
140
141 if __name__ == "__main__":
142     import sys
143     if len(sys.argv) >= 2:
144         print(FlagsForFile(sys.argv[1]))