Blame .ycm_extra_conf.py

Packit 12c978
# This file is NOT licensed under the GPLv3, which is the license for the rest
Packit 12c978
# of YouCompleteMe.
Packit 12c978
#
Packit 12c978
# Here's the license text for this file:
Packit 12c978
#
Packit 12c978
# This is free and unencumbered software released into the public domain.
Packit 12c978
#
Packit 12c978
# Anyone is free to copy, modify, publish, use, compile, sell, or
Packit 12c978
# distribute this software, either in source code form or as a compiled
Packit 12c978
# binary, for any purpose, commercial or non-commercial, and by any
Packit 12c978
# means.
Packit 12c978
#
Packit 12c978
# In jurisdictions that recognize copyright laws, the author or authors
Packit 12c978
# of this software dedicate any and all copyright interest in the
Packit 12c978
# software to the public domain. We make this dedication for the benefit
Packit 12c978
# of the public at large and to the detriment of our heirs and
Packit 12c978
# successors. We intend this dedication to be an overt act of
Packit 12c978
# relinquishment in perpetuity of all present and future rights to this
Packit 12c978
# software under copyright law.
Packit 12c978
#
Packit 12c978
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Packit 12c978
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Packit 12c978
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
Packit 12c978
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
Packit 12c978
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
Packit 12c978
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
Packit 12c978
# OTHER DEALINGS IN THE SOFTWARE.
Packit 12c978
#
Packit 12c978
# For more information, please refer to <http://unlicense.org/>
Packit 12c978
Packit 12c978
import os
Packit 12c978
import ycm_core
Packit 12c978
Packit 12c978
# These are the compilation flags that will be used in case there's no
Packit 12c978
# compilation database set (by default, one is not set).
Packit 12c978
# CHANGE THIS LIST OF FLAGS. YES, THIS IS THE DROID YOU HAVE BEEN LOOKING FOR.
Packit 12c978
flags = [
Packit 12c978
'-Wall',
Packit 12c978
'-Wextra',
Packit 12c978
# THIS IS IMPORTANT! Without a "-std=<something>" flag, clang won't know which
Packit 12c978
# language to use when compiling headers. So it will guess. Badly. So C++
Packit 12c978
# headers will be compiled as C headers. You don't want that so ALWAYS specify
Packit 12c978
# a "-std=<something>".
Packit 12c978
# For a C project, you would set this to something like 'c99' instead of
Packit 12c978
# 'c++11'.
Packit 12c978
'-std=gnu90',
Packit 12c978
# ...and the same thing goes for the magic -x option which specifies the
Packit 12c978
# language that the files to be compiled are written in. This is mostly
Packit 12c978
# relevant for c++ headers.
Packit 12c978
# For a C project, you would set this to 'c' instead of 'c++'.
Packit 12c978
'-x',
Packit 12c978
'c',
Packit 12c978
'-I', './librabbitmq',
Packit 12c978
'-I', './librabbitmq/unix',
Packit 12c978
'-D', 'HAVE_POLL',
Packit 12c978
]
Packit 12c978
Packit 12c978
Packit 12c978
# Set this to the absolute path to the folder (NOT the file!) containing the
Packit 12c978
# compile_commands.json file to use that instead of 'flags'. See here for
Packit 12c978
# more details: http://clang.llvm.org/docs/JSONCompilationDatabase.html
Packit 12c978
#
Packit 12c978
# You can get CMake to generate this file for you by adding:
Packit 12c978
#   set( CMAKE_EXPORT_COMPILE_COMMANDS 1 )
Packit 12c978
# to your CMakeLists.txt file.
Packit 12c978
#
Packit 12c978
# Most projects will NOT need to set this to anything; you can just change the
Packit 12c978
# 'flags' list of compilation flags. Notice that YCM itself uses that approach.
Packit 12c978
compilation_database_folder = ''
Packit 12c978
Packit 12c978
if os.path.exists( compilation_database_folder ):
Packit 12c978
  database = ycm_core.CompilationDatabase( compilation_database_folder )
Packit 12c978
else:
Packit 12c978
  database = None
Packit 12c978
Packit 12c978
SOURCE_EXTENSIONS = [ '.cpp', '.cxx', '.cc', '.c', '.m', '.mm' ]
Packit 12c978
Packit 12c978
def DirectoryOfThisScript():
Packit 12c978
  return os.path.dirname( os.path.abspath( __file__ ) )
Packit 12c978
Packit 12c978
Packit 12c978
def MakeRelativePathsInFlagsAbsolute( flags, working_directory ):
Packit 12c978
  if not working_directory:
Packit 12c978
    return list( flags )
Packit 12c978
  new_flags = []
Packit 12c978
  make_next_absolute = False
Packit 12c978
  path_flags = [ '-isystem', '-I', '-iquote', '--sysroot=' ]
Packit 12c978
  for flag in flags:
Packit 12c978
    new_flag = flag
Packit 12c978
Packit 12c978
    if make_next_absolute:
Packit 12c978
      make_next_absolute = False
Packit 12c978
      if not flag.startswith( '/' ):
Packit 12c978
        new_flag = os.path.join( working_directory, flag )
Packit 12c978
Packit 12c978
    for path_flag in path_flags:
Packit 12c978
      if flag == path_flag:
Packit 12c978
        make_next_absolute = True
Packit 12c978
        break
Packit 12c978
Packit 12c978
      if flag.startswith( path_flag ):
Packit 12c978
        path = flag[ len( path_flag ): ]
Packit 12c978
        new_flag = path_flag + os.path.join( working_directory, path )
Packit 12c978
        break
Packit 12c978
Packit 12c978
    if new_flag:
Packit 12c978
      new_flags.append( new_flag )
Packit 12c978
  return new_flags
Packit 12c978
Packit 12c978
Packit 12c978
def IsHeaderFile( filename ):
Packit 12c978
  extension = os.path.splitext( filename )[ 1 ]
Packit 12c978
  return extension in [ '.h', '.hxx', '.hpp', '.hh' ]
Packit 12c978
Packit 12c978
Packit 12c978
def GetCompilationInfoForFile( filename ):
Packit 12c978
  # The compilation_commands.json file generated by CMake does not have entries
Packit 12c978
  # for header files. So we do our best by asking the db for flags for a
Packit 12c978
  # corresponding source file, if any. If one exists, the flags for that file
Packit 12c978
  # should be good enough.
Packit 12c978
  if IsHeaderFile( filename ):
Packit 12c978
    basename = os.path.splitext( filename )[ 0 ]
Packit 12c978
    for extension in SOURCE_EXTENSIONS:
Packit 12c978
      replacement_file = basename + extension
Packit 12c978
      if os.path.exists( replacement_file ):
Packit 12c978
        compilation_info = database.GetCompilationInfoForFile(
Packit 12c978
          replacement_file )
Packit 12c978
        if compilation_info.compiler_flags_:
Packit 12c978
          return compilation_info
Packit 12c978
    return None
Packit 12c978
  return database.GetCompilationInfoForFile( filename )
Packit 12c978
Packit 12c978
Packit 12c978
def FlagsForFile( filename, **kwargs ):
Packit 12c978
  if database:
Packit 12c978
    # Bear in mind that compilation_info.compiler_flags_ does NOT return a
Packit 12c978
    # python list, but a "list-like" StringVec object
Packit 12c978
    compilation_info = GetCompilationInfoForFile( filename )
Packit 12c978
    if not compilation_info:
Packit 12c978
      relative_to = DirectoryOfThisScript()
Packit 12c978
      return {
Packit 12c978
        'flags': MakeRelativePathsInFlagsAbsolute( flags, relative_to ),
Packit 12c978
        'do_cache': True
Packit 12c978
      }
Packit 12c978
Packit 12c978
    final_flags = MakeRelativePathsInFlagsAbsolute(
Packit 12c978
      compilation_info.compiler_flags_,
Packit 12c978
      compilation_info.compiler_working_dir_ )
Packit 12c978
Packit 12c978
  else:
Packit 12c978
    relative_to = DirectoryOfThisScript()
Packit 12c978
    final_flags = MakeRelativePathsInFlagsAbsolute( flags, relative_to )
Packit 12c978
Packit 12c978
  return {
Packit 12c978
    'flags': final_flags,
Packit 12c978
    'do_cache': True
Packit 12c978
  }