3 # SPDX-License-Identifier: Unlicense
5 # Based on the template file provided by the 'YCM-Generator' project authored by
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,
11 # The original license is preserved as it is.
14 # This is free and unencumbered software released into the public domain.
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
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.
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.
37 # For more information, please refer to <http://unlicense.org/>
40 YouCompleteMe configuration file tailored to support the 'meson' build system
41 used by the 'systemd' project.
49 SOURCE_EXTENSIONS
= (".C", ".cpp", ".cxx", ".cc", ".c", ".m", ".mm")
50 HEADER_EXTENSIONS
= (".H", ".h", ".hxx", ".hpp", ".hh")
53 def DirectoryOfThisScript():
54 return os
.path
.dirname(os
.path
.abspath(__file__
))
57 def GuessBuildDirectory():
58 result
= os
.path
.join(DirectoryOfThisScript(), "build")
60 if os
.path
.exists(result
):
63 result
= glob
.glob(os
.path
.join(DirectoryOfThisScript(),
64 "..", "..", "*", ".ninja_log"))
72 return os
.path
.split(result
[0])[0]
75 def TraverseByDepth(root
, include_extensions
):
78 # Perform a depth first top down traverse of the given directory tree.
79 for root_dir
, subdirs
, file_list
in os
.walk(root
):
81 # print("Relative Root: ", root_dir)
83 if include_extensions
:
84 get_ext
= os
.path
.splitext
86 get_ext(f
)[-1] for f
in file_list
if get_ext(f
)[-1]
88 if subdir_extensions
& include_extensions
:
98 _project_src_dir
= os
.path
.join(DirectoryOfThisScript(), "src")
99 _include_dirs_set
= TraverseByDepth(_project_src_dir
, frozenset({".h"}))
103 # The following flags are partially redundant due to the existence of
104 # 'compile_commands.json'.
113 for include_dir
in _include_dirs_set
:
114 flags
.append("-I" + include_dir
)
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
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.
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()
128 if os
.path
.exists(compilation_database_folder
):
129 database
= ycm_core
.CompilationDatabase(compilation_database_folder
)
134 def MakeRelativePathsInFlagsAbsolute(flags
, working_directory
):
135 if not working_directory
:
138 make_next_absolute
= False
139 path_flags
= ["-isystem", "-I", "-iquote", "--sysroot="]
143 if make_next_absolute
:
144 make_next_absolute
= False
145 if not flag
.startswith("/"):
146 new_flag
= os
.path
.join(working_directory
, flag
)
148 for path_flag
in path_flags
:
149 if flag
== path_flag
:
150 make_next_absolute
= True
153 if flag
.startswith(path_flag
):
154 path
= flag
[len(path_flag
):]
155 new_flag
= path_flag
+ os
.path
.join(working_directory
, path
)
159 new_flags
.append(new_flag
)
163 def IsHeaderFile(filename
):
164 extension
= os
.path
.splitext(filename
)[1]
165 return extension
in HEADER_EXTENSIONS
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.
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
):
182 database
.GetCompilationInfoForFile(replacement_file
)
183 if compilation_info
.compiler_flags_
:
184 return compilation_info
186 return database
.GetCompilationInfoForFile(filename
)
189 def FlagsForFile(filename
, **kwargs
):
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
:
197 final_flags
= MakeRelativePathsInFlagsAbsolute(
198 compilation_info
.compiler_flags_
,
199 compilation_info
.compiler_working_dir_
)
202 relative_to
= DirectoryOfThisScript()
203 final_flags
= MakeRelativePathsInFlagsAbsolute(flags
, relative_to
)
206 "flags": final_flags
,