]>
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 | ||
50918c2c JX |
49 | SOURCE_EXTENSIONS = (".C", ".cpp", ".cxx", ".cc", ".c", ".m", ".mm") |
50 | HEADER_EXTENSIONS = (".H", ".h", ".hxx", ".hpp", ".hh") | |
51 | ||
52 | ||
328b5bc9 | 53 | def DirectoryOfThisScript(): |
1c5ea591 JX |
54 | return os.path.dirname(os.path.abspath(__file__)) |
55 | ||
56 | ||
57 | def GuessBuildDirectory(): | |
58 | result = os.path.join(DirectoryOfThisScript(), "build") | |
59 | ||
60 | if os.path.exists(result): | |
61 | return result | |
62 | ||
63 | result = glob.glob(os.path.join(DirectoryOfThisScript(), | |
64 | "..", "..", "*", ".ninja_log")) | |
65 | ||
66 | if not result: | |
67 | return "" | |
68 | ||
69 | if 1 != len(result): | |
70 | return "" | |
71 | ||
72 | return os.path.split(result[0])[0] | |
73 | ||
74 | ||
75 | def TraverseByDepth(root, include_extensions): | |
76 | is_root = True | |
77 | result = set() | |
78 | # Perform a depth first top down traverse of the given directory tree. | |
79 | for root_dir, subdirs, file_list in os.walk(root): | |
80 | if not is_root: | |
81 | # print("Relative Root: ", root_dir) | |
82 | # print(subdirs) | |
83 | if include_extensions: | |
84 | get_ext = os.path.splitext | |
85 | subdir_extensions = { | |
86 | get_ext(f)[-1] for f in file_list if get_ext(f)[-1] | |
87 | } | |
88 | if subdir_extensions & include_extensions: | |
89 | result.add(root_dir) | |
90 | else: | |
91 | result.add(root_dir) | |
92 | else: | |
93 | is_root = False | |
94 | ||
95 | return result | |
96 | ||
97 | ||
98 | _project_src_dir = os.path.join(DirectoryOfThisScript(), "src") | |
99 | _include_dirs_set = TraverseByDepth(_project_src_dir, frozenset({".h"})) | |
100 | flags = [ | |
101 | "-x", | |
102 | "c" | |
103 | # The following flags are partially redundant due to the existence of | |
104 | # 'compile_commands.json'. | |
105 | # '-Wall', | |
106 | # '-Wextra', | |
107 | # '-Wfloat-equal', | |
108 | # '-Wpointer-arith', | |
109 | # '-Wshadow', | |
110 | # '-std=gnu99', | |
111 | ] | |
112 | ||
113 | for include_dir in _include_dirs_set: | |
114 | flags.append("-I" + include_dir) | |
115 | ||
116 | # Set this to the absolute path to the folder (NOT the file!) containing the | |
117 | # compile_commands.json file to use that instead of 'flags'. See here for | |
118 | # more details: http://clang.llvm.org/docs/JSONCompilationDatabase.html | |
119 | # | |
120 | # You can get CMake to generate this file for you by adding: | |
121 | # set( CMAKE_EXPORT_COMPILE_COMMANDS 1 ) | |
122 | # to your CMakeLists.txt file. | |
123 | # | |
124 | # Most projects will NOT need to set this to anything; you can just change the | |
125 | # 'flags' list of compilation flags. Notice that YCM itself uses that approach. | |
126 | compilation_database_folder = GuessBuildDirectory() | |
127 | ||
128 | if os.path.exists(compilation_database_folder): | |
129 | database = ycm_core.CompilationDatabase(compilation_database_folder) | |
130 | else: | |
131 | database = None | |
132 | ||
328b5bc9 DR |
133 | |
134 | def MakeRelativePathsInFlagsAbsolute(flags, working_directory): | |
1c5ea591 JX |
135 | if not working_directory: |
136 | return list(flags) | |
137 | new_flags = [] | |
138 | make_next_absolute = False | |
139 | path_flags = ["-isystem", "-I", "-iquote", "--sysroot="] | |
140 | for flag in flags: | |
141 | new_flag = flag | |
142 | ||
143 | if make_next_absolute: | |
144 | make_next_absolute = False | |
145 | if not flag.startswith("/"): | |
146 | new_flag = os.path.join(working_directory, flag) | |
147 | ||
148 | for path_flag in path_flags: | |
149 | if flag == path_flag: | |
150 | make_next_absolute = True | |
151 | break | |
152 | ||
153 | if flag.startswith(path_flag): | |
154 | path = flag[len(path_flag):] | |
155 | new_flag = path_flag + os.path.join(working_directory, path) | |
156 | break | |
157 | ||
158 | if new_flag: | |
159 | new_flags.append(new_flag) | |
160 | return new_flags | |
161 | ||
162 | ||
163 | def IsHeaderFile(filename): | |
164 | extension = os.path.splitext(filename)[1] | |
50918c2c | 165 | return extension in HEADER_EXTENSIONS |
1c5ea591 JX |
166 | |
167 | ||
168 | def GetCompilationInfoForFile(filename): | |
169 | # The compilation_commands.json file generated by CMake does not have | |
170 | # entries for header files. So we do our best by asking the db for flags for | |
171 | # a corresponding source file, if any. If one exists, the flags for that | |
172 | # file should be good enough. | |
173 | if not database: | |
174 | return None | |
175 | ||
176 | if IsHeaderFile(filename): | |
177 | basename = os.path.splitext(filename)[0] | |
178 | for extension in SOURCE_EXTENSIONS: | |
179 | replacement_file = basename + extension | |
180 | if os.path.exists(replacement_file): | |
181 | compilation_info = \ | |
182 | database.GetCompilationInfoForFile(replacement_file) | |
183 | if compilation_info.compiler_flags_: | |
184 | return compilation_info | |
185 | return None | |
186 | return database.GetCompilationInfoForFile(filename) | |
187 | ||
188 | ||
189 | def FlagsForFile(filename, **kwargs): | |
190 | if database: | |
191 | # Bear in mind that compilation_info.compiler_flags_ does NOT return a | |
192 | # python list, but a "list-like" StringVec object | |
193 | compilation_info = GetCompilationInfoForFile(filename) | |
194 | if not compilation_info: | |
195 | return None | |
196 | ||
197 | final_flags = MakeRelativePathsInFlagsAbsolute( | |
198 | compilation_info.compiler_flags_, | |
199 | compilation_info.compiler_working_dir_) | |
200 | ||
201 | else: | |
202 | relative_to = DirectoryOfThisScript() | |
203 | final_flags = MakeRelativePathsInFlagsAbsolute(flags, relative_to) | |
204 | ||
205 | return { | |
206 | "flags": final_flags, | |
207 | "do_cache": True | |
208 | } |