|
| 1 | +from distutils.sysconfig import get_python_inc |
| 2 | +import platform |
| 3 | +import os |
| 4 | +import subprocess |
| 5 | +import ycm_core |
| 6 | + |
| 7 | +DIR_OF_THIS_SCRIPT = os.path.abspath( os.path.dirname( __file__ ) ) |
| 8 | +SOURCE_EXTENSIONS = [ '.cpp', '.cxx', '.cc', '.c', '.m', '.mm' ] |
| 9 | + |
| 10 | +flags = [ |
| 11 | + '-Wall', |
| 12 | + '-Wextra', |
| 13 | + '-Werror', |
| 14 | + '-x', |
| 15 | + 'c++', |
| 16 | + '-std=c++23', |
| 17 | + '-I', DIR_OF_THIS_SCRIPT + '/include', |
| 18 | + '-I', '/usr/include', |
| 19 | +] |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +# Set this to the absolute path to the folder (NOT the file!) containing the |
| 24 | +# compile_commands.json file to use that instead of 'flags'. See here for |
| 25 | +# more details: http://clang.llvm.org/docs/JSONCompilationDatabase.html |
| 26 | +# |
| 27 | +# You can get CMake to generate this file for you by adding: |
| 28 | +# set( CMAKE_EXPORT_COMPILE_COMMANDS 1 ) |
| 29 | +# to your CMakeLists.txt file. |
| 30 | +# |
| 31 | +# Most projects will NOT need to set this to anything; you can just change the |
| 32 | +# 'flags' list of compilation flags. Notice that YCM itself uses that approach. |
| 33 | +compilation_database_folder = '' |
| 34 | + |
| 35 | +if os.path.exists( compilation_database_folder ): |
| 36 | + database = ycm_core.CompilationDatabase( compilation_database_folder ) |
| 37 | +else: |
| 38 | + database = None |
| 39 | + |
| 40 | + |
| 41 | +def IsHeaderFile( filename ): |
| 42 | + extension = os.path.splitext( filename )[ 1 ] |
| 43 | + return extension in [ '.h', '.hxx', '.hpp', '.hh' ] |
| 44 | + |
| 45 | + |
| 46 | +def FindCorrespondingSourceFile( filename ): |
| 47 | + if IsHeaderFile( filename ): |
| 48 | + basename = os.path.splitext( filename )[ 0 ] |
| 49 | + for extension in SOURCE_EXTENSIONS: |
| 50 | + replacement_file = basename + extension |
| 51 | + if os.path.exists( replacement_file ): |
| 52 | + return replacement_file |
| 53 | + return filename |
| 54 | + |
| 55 | + |
| 56 | +def Settings( **kwargs ): |
| 57 | + if kwargs[ 'language' ] == 'cfamily': |
| 58 | + # If the file is a header, try to find the corresponding source file and |
| 59 | + # retrieve its flags from the compilation database if using one. This is |
| 60 | + # necessary since compilation databases don't have entries for header files. |
| 61 | + # In addition, use this source file as the translation unit. This makes it |
| 62 | + # possible to jump from a declaration in the header file to its definition |
| 63 | + # in the corresponding source file. |
| 64 | + filename = FindCorrespondingSourceFile( kwargs[ 'filename' ] ) |
| 65 | + |
| 66 | + if not database: |
| 67 | + return { |
| 68 | + 'flags': flags, |
| 69 | + 'include_paths_relative_to_dir': DIR_OF_THIS_SCRIPT, |
| 70 | + 'override_filename': filename |
| 71 | + } |
| 72 | + |
| 73 | + compilation_info = database.GetCompilationInfoForFile( filename ) |
| 74 | + if not compilation_info.compiler_flags_: |
| 75 | + return {} |
| 76 | + |
| 77 | + # Bear in mind that compilation_info.compiler_flags_ does NOT return a |
| 78 | + # python list, but a "list-like" StringVec object. |
| 79 | + final_flags = list( compilation_info.compiler_flags_ ) |
| 80 | + |
| 81 | + # NOTE: This is just for YouCompleteMe; it's highly likely that your project |
| 82 | + # does NOT need to remove the stdlib flag. DO NOT USE THIS IN YOUR |
| 83 | + # ycm_extra_conf IF YOU'RE NOT 100% SURE YOU NEED IT. |
| 84 | + try: |
| 85 | + final_flags.remove( '-stdlib=libc++' ) |
| 86 | + except ValueError: |
| 87 | + pass |
| 88 | + |
| 89 | + return { |
| 90 | + 'flags': final_flags, |
| 91 | + 'include_paths_relative_to_dir': compilation_info.compiler_working_dir_, |
| 92 | + 'override_filename': filename |
| 93 | + } |
| 94 | + return {} |
| 95 | + |
0 commit comments