]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | # Copyright (c) 2017-2018 Uber Technologies, Inc. |
2 | # | |
3 | # Licensed under the Apache License, Version 2.0 (the "License"); | |
4 | # you may not use this file except in compliance with the License. | |
5 | # You may obtain a copy of the License at | |
6 | # | |
7 | # http://www.apache.org/licenses/LICENSE-2.0 | |
8 | # | |
9 | # Unless required by applicable law or agreed to in writing, software | |
10 | # distributed under the License is distributed on an "AS IS" BASIS, | |
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 | # See the License for the specific language governing permissions and | |
13 | # limitations under the License. | |
14 | ||
15 | import os | |
16 | import ycm_core | |
17 | ||
18 | ||
19 | def DirectoryOfThisScript(): | |
20 | return os.path.dirname( os.path.abspath( __file__ ) ) | |
21 | ||
22 | ||
23 | project_dir = DirectoryOfThisScript() | |
24 | ||
25 | try: | |
26 | with open('build/_3rdParty/Hunter/install-root-dir') as f: | |
27 | hunter_install_dir = f.readline() | |
28 | except IOError: | |
29 | # Ignore if file does not exist | |
30 | hunter_install_dir = '' | |
31 | ||
32 | flags = [ | |
33 | '-Wall', | |
34 | '-Werror', | |
35 | '-pedantic', | |
36 | '-std=c++11', | |
37 | '-x', | |
38 | 'c++', | |
39 | '-DBOOST_COROUTINES_NO_DEPRECATION_WARNING=1', | |
40 | '-DGTEST_HAS_TR1_TUPLE=0', | |
41 | '-DGTEST_USE_OWN_TR1_TUPLE=0', | |
42 | '-isystem', | |
43 | '/usr/local/include', | |
44 | '-I', | |
45 | os.path.join(project_dir, 'src'), | |
46 | '-I', | |
47 | os.path.join(project_dir, 'build/src') | |
48 | ] | |
49 | ||
50 | if hunter_install_dir: | |
51 | flags += ['-I', hunter_install_dir] | |
52 | ||
53 | compilation_database_folder = os.path.join(project_dir, 'build') | |
54 | ||
55 | if os.path.exists( compilation_database_folder ): | |
56 | database = ycm_core.CompilationDatabase( compilation_database_folder ) | |
57 | else: | |
58 | database = None | |
59 | ||
60 | SOURCE_EXTENSIONS = [ '.cpp', '.cxx', '.cc', '.c', '.m', '.mm' ] | |
61 | ||
62 | ||
63 | def IsHeaderFile( filename ): | |
64 | extension = os.path.splitext( filename )[ 1 ] | |
65 | return extension in [ '.h', '.hxx', '.hpp', '.hh' ] | |
66 | ||
67 | ||
68 | def GetCompilationInfoForFile( filename ): | |
69 | # The compilation_commands.json file generated by CMake does not have entries | |
70 | # for header files. So we do our best by asking the db for flags for a | |
71 | # corresponding source file, if any. If one exists, the flags for that file | |
72 | # should be good enough. | |
73 | if IsHeaderFile( filename ): | |
74 | basename = os.path.splitext( filename )[ 0 ] | |
75 | for extension in SOURCE_EXTENSIONS: | |
76 | replacement_file = basename + extension | |
77 | if os.path.exists( replacement_file ): | |
78 | compilation_info = database.GetCompilationInfoForFile( | |
79 | replacement_file ) | |
80 | if compilation_info.compiler_flags_: | |
81 | return compilation_info | |
82 | return None | |
83 | return database.GetCompilationInfoForFile( filename ) | |
84 | ||
85 | ||
86 | def FlagsForFile( filename, **kwargs ): | |
87 | if not database: | |
88 | return { | |
89 | 'flags': flags, | |
90 | 'include_paths_relative_to_dir': DirectoryOfThisScript() | |
91 | } | |
92 | ||
93 | compilation_info = GetCompilationInfoForFile( filename ) | |
94 | if not compilation_info: | |
95 | return None | |
96 | ||
97 | # Bear in mind that compilation_info.compiler_flags_ does NOT return a | |
98 | # python list, but a "list-like" StringVec object. | |
99 | final_flags = list( compilation_info.compiler_flags_ ) | |
100 | ||
101 | return { | |
102 | 'flags': final_flags, | |
103 | 'include_paths_relative_to_dir': compilation_info.compiler_working_dir_ | |
104 | } |