|
| 1 | +# Copyright (c) 2018, Uber Technologies, Inc |
| 2 | +# |
| 3 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +# of this software and associated documentation files (the "Software"), to deal |
| 5 | +# in the Software without restriction, including without limitation the rights |
| 6 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +# copies of the Software, and to permit persons to whom the Software is |
| 8 | +# furnished to do so, subject to the following conditions: |
| 9 | +# |
| 10 | +# The above copyright notice and this permission notice shall be included in |
| 11 | +# all copies or substantial portions of the Software. |
| 12 | +# |
| 13 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | +# THE SOFTWARE. |
| 20 | + |
| 21 | +import os |
| 22 | +import ycm_core |
| 23 | + |
| 24 | + |
| 25 | +def DirectoryOfThisScript(): |
| 26 | + return os.path.dirname( os.path.abspath( __file__ ) ) |
| 27 | + |
| 28 | + |
| 29 | +project_dir = DirectoryOfThisScript() |
| 30 | + |
| 31 | +flags = [ |
| 32 | +'-Wall', |
| 33 | +'-Werror', |
| 34 | +'-pedantic', |
| 35 | +'-std=c11', |
| 36 | +'-pthread', |
| 37 | +'-x', |
| 38 | +'c', |
| 39 | +'-isystem', |
| 40 | +'/usr/local/include', |
| 41 | +'-I', |
| 42 | +os.path.join(project_dir, 'src/h3lib/include'), |
| 43 | +'-I', |
| 44 | +os.path.join(project_dir, 'src/apps/applib/include') |
| 45 | +] |
| 46 | + |
| 47 | +compilation_database_folder = os.path.join(project_dir, 'build') |
| 48 | + |
| 49 | +if os.path.exists( compilation_database_folder ): |
| 50 | + database = ycm_core.CompilationDatabase( compilation_database_folder ) |
| 51 | +else: |
| 52 | + database = None |
| 53 | + |
| 54 | +SOURCE_EXTENSIONS = [ '.cpp', '.cxx', '.cc', '.c', '.m', '.mm' ] |
| 55 | + |
| 56 | + |
| 57 | +def IsHeaderFile( filename ): |
| 58 | + extension = os.path.splitext( filename )[ 1 ] |
| 59 | + return extension in [ '.h', '.hxx', '.hpp', '.hh' ] |
| 60 | + |
| 61 | + |
| 62 | +def GetCompilationInfoForFile( filename ): |
| 63 | + # The compilation_commands.json file generated by CMake does not have entries |
| 64 | + # for header files. So we do our best by asking the db for flags for a |
| 65 | + # corresponding source file, if any. If one exists, the flags for that file |
| 66 | + # should be good enough. |
| 67 | + if IsHeaderFile( filename ): |
| 68 | + basename = os.path.splitext( filename )[ 0 ] |
| 69 | + for extension in SOURCE_EXTENSIONS: |
| 70 | + replacement_file = basename + extension |
| 71 | + if os.path.exists( replacement_file ): |
| 72 | + compilation_info = database.GetCompilationInfoForFile( |
| 73 | + replacement_file ) |
| 74 | + if compilation_info.compiler_flags_: |
| 75 | + return compilation_info |
| 76 | + return None |
| 77 | + return database.GetCompilationInfoForFile( filename ) |
| 78 | + |
| 79 | + |
| 80 | +def FlagsForFile( filename, **kwargs ): |
| 81 | + if not database: |
| 82 | + return { |
| 83 | + 'flags': flags, |
| 84 | + 'include_paths_relative_to_dir': DirectoryOfThisScript() |
| 85 | + } |
| 86 | + |
| 87 | + compilation_info = GetCompilationInfoForFile( filename ) |
| 88 | + if not compilation_info: |
| 89 | + return None |
| 90 | + |
| 91 | + # Bear in mind that compilation_info.compiler_flags_ does NOT return a |
| 92 | + # python list, but a "list-like" StringVec object. |
| 93 | + final_flags = list( compilation_info.compiler_flags_ ) |
| 94 | + |
| 95 | + return { |
| 96 | + 'flags': final_flags, |
| 97 | + 'include_paths_relative_to_dir': compilation_info.compiler_working_dir_ |
| 98 | + } |
0 commit comments