]>
Commit | Line | Data |
---|---|---|
1c5ea591 JX |
1 | #!/usr/bin/env python |
2 | ||
3 | # SPDX-License-Identifier: Unlicense | |
4 | # | |
5 | # Based on the template file provided by the 'YCM-Generator' project authored by | |
6 | # Reuben D'Netto. | |
7 | # Jiahui Xie has re-reformatted and expanded the original script in accordance | |
8 | # to the requirements of the PEP 8 style guide and 'systemd' project, | |
9 | # respectively. | |
10 | # | |
11 | # The original license is preserved as it is. | |
12 | # | |
13 | # | |
14 | # This is free and unencumbered software released into the public domain. | |
15 | # | |
16 | # Anyone is free to copy, modify, publish, use, compile, sell, or | |
17 | # distribute this software, either in source code form or as a compiled | |
18 | # binary, for any purpose, commercial or non-commercial, and by any | |
19 | # means. | |
20 | # | |
21 | # In jurisdictions that recognize copyright laws, the author or authors | |
22 | # of this software dedicate any and all copyright interest in the | |
23 | # software to the public domain. We make this dedication for the benefit | |
24 | # of the public at large and to the detriment of our heirs and | |
25 | # successors. We intend this dedication to be an overt act of | |
26 | # relinquishment in perpetuity of all present and future rights to this | |
27 | # software under copyright law. | |
28 | # | |
29 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
30 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
31 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
32 | # IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | |
33 | # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
34 | # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
35 | # OTHER DEALINGS IN THE SOFTWARE. | |
36 | # | |
37 | # For more information, please refer to <http://unlicense.org/> | |
38 | ||
39 | """ | |
40 | YouCompleteMe configuration file tailored to support the 'meson' build system | |
41 | used by the 'systemd' project. | |
42 | """ | |
43 | ||
44 | import glob | |
328b5bc9 | 45 | import os |
1c5ea591 | 46 | import ycm_core |
328b5bc9 DR |
47 | |
48 | ||
49 | def DirectoryOfThisScript(): | |
1c5ea591 JX |
50 | return os.path.dirname(os.path.abspath(__file__)) |
51 | ||
52 | ||
53 | def GuessBuildDirectory(): | |
54 | result = os.path.join(DirectoryOfThisScript(), "build") | |
55 | ||
56 | if os.path.exists(result): | |
57 | return result | |
58 | ||
59 | result = glob.glob(os.path.join(DirectoryOfThisScript(), | |
60 | "..", "..", "*", ".ninja_log")) | |
61 | ||
62 | if not result: | |
63 | return "" | |
64 | ||
65 | if 1 != len(result): | |
66 | return "" | |
67 | ||
68 | return os.path.split(result[0])[0] | |
69 | ||
70 | ||
71 | def TraverseByDepth(root, include_extensions): | |
72 | is_root = True | |
73 | result = set() | |
74 | # Perform a depth first top down traverse of the given directory tree. | |
75 | for root_dir, subdirs, file_list in os.walk(root): | |
76 | if not is_root: | |
77 | # print("Relative Root: ", root_dir) | |
78 | # print(subdirs) | |
79 | if include_extensions: | |
80 | get_ext = os.path.splitext | |
81 | subdir_extensions = { | |
82 | get_ext(f)[-1] for f in file_list if get_ext(f)[-1] | |
83 | } | |
84 | if subdir_extensions & include_extensions: | |
85 | result.add(root_dir) | |
86 | else: | |
87 | result.add(root_dir) | |
88 | else: | |
89 | is_root = False | |
90 | ||
91 | return result | |
92 | ||
93 | ||
94 | _project_src_dir = os.path.join(DirectoryOfThisScript(), "src") | |
95 | _include_dirs_set = TraverseByDepth(_project_src_dir, frozenset({".h"})) | |
96 | flags = [ | |
97 | "-x", | |
98 | "c" | |
99 | # The following flags are partially redundant due to the existence of | |
100 | # 'compile_commands.json'. | |
101 | # '-Wall', | |
102 | # '-Wextra', | |
103 | # '-Wfloat-equal', | |
104 | # '-Wpointer-arith', | |
105 | # '-Wshadow', | |
106 | # '-std=gnu99', | |
107 | ] | |
108 | ||
109 | for include_dir in _include_dirs_set: | |
110 | flags.append("-I" + include_dir) | |
111 | ||
112 | # Set this to the absolute path to the folder (NOT the file!) containing the | |
113 | # compile_commands.json file to use that instead of 'flags'. See here for | |
114 | # more details: http://clang.llvm.org/docs/JSONCompilationDatabase.html | |
115 | # | |
116 | # You can get CMake to generate this file for you by adding: | |
117 | # set( CMAKE_EXPORT_COMPILE_COMMANDS 1 ) | |
118 | # to your CMakeLists.txt file. | |
119 | # | |
120 | # Most projects will NOT need to set this to anything; you can just change the | |
121 | # 'flags' list of compilation flags. Notice that YCM itself uses that approach. | |
122 | compilation_database_folder = GuessBuildDirectory() | |
123 | ||
124 | if os.path.exists(compilation_database_folder): | |
125 | database = ycm_core.CompilationDatabase(compilation_database_folder) | |
126 | else: | |
127 | database = None | |
128 | ||
129 | SOURCE_EXTENSIONS = [".C", ".cpp", ".cxx", ".cc", ".c", ".m", ".mm"] | |
328b5bc9 DR |
130 | |
131 | ||
132 | def MakeRelativePathsInFlagsAbsolute(flags, working_directory): | |
1c5ea591 JX |
133 | if not working_directory: |
134 | return list(flags) | |
135 | new_flags = [] | |
136 | make_next_absolute = False | |
137 | path_flags = ["-isystem", "-I", "-iquote", "--sysroot="] | |
138 | for flag in flags: | |
139 | new_flag = flag | |
140 | ||
141 | if make_next_absolute: | |
142 | make_next_absolute = False | |
143 | if not flag.startswith("/"): | |
144 | new_flag = os.path.join(working_directory, flag) | |
145 | ||
146 | for path_flag in path_flags: | |
147 | if flag == path_flag: | |
148 | make_next_absolute = True | |
149 | break | |
150 | ||
151 | if flag.startswith(path_flag): | |
152 | path = flag[len(path_flag):] | |
153 | new_flag = path_flag + os.path.join(working_directory, path) | |
154 | break | |
155 | ||
156 | if new_flag: | |
157 | new_flags.append(new_flag) | |
158 | return new_flags | |
159 | ||
160 | ||
161 | def IsHeaderFile(filename): | |
162 | extension = os.path.splitext(filename)[1] | |
163 | return extension in [".H", ".h", ".hxx", ".hpp", ".hh"] | |
164 | ||
165 | ||
166 | def GetCompilationInfoForFile(filename): | |
167 | # The compilation_commands.json file generated by CMake does not have | |
168 | # entries for header files. So we do our best by asking the db for flags for | |
169 | # a corresponding source file, if any. If one exists, the flags for that | |
170 | # file should be good enough. | |
171 | if not database: | |
172 | return None | |
173 | ||
174 | if IsHeaderFile(filename): | |
175 | basename = os.path.splitext(filename)[0] | |
176 | for extension in SOURCE_EXTENSIONS: | |
177 | replacement_file = basename + extension | |
178 | if os.path.exists(replacement_file): | |
179 | compilation_info = \ | |
180 | database.GetCompilationInfoForFile(replacement_file) | |
181 | if compilation_info.compiler_flags_: | |
182 | return compilation_info | |
183 | return None | |
184 | return database.GetCompilationInfoForFile(filename) | |
185 | ||
186 | ||
187 | def FlagsForFile(filename, **kwargs): | |
188 | if database: | |
189 | # Bear in mind that compilation_info.compiler_flags_ does NOT return a | |
190 | # python list, but a "list-like" StringVec object | |
191 | compilation_info = GetCompilationInfoForFile(filename) | |
192 | if not compilation_info: | |
193 | return None | |
194 | ||
195 | final_flags = MakeRelativePathsInFlagsAbsolute( | |
196 | compilation_info.compiler_flags_, | |
197 | compilation_info.compiler_working_dir_) | |
198 | ||
199 | else: | |
200 | relative_to = DirectoryOfThisScript() | |
201 | final_flags = MakeRelativePathsInFlagsAbsolute(flags, relative_to) | |
202 | ||
203 | return { | |
204 | "flags": final_flags, | |
205 | "do_cache": True | |
206 | } |