c++ - 如何配置 Vim 插件 YouCompleteMe 来读取 Makefile?

标签 c++ qt vim plugins makefile

我正在尝试获取 YouCompleteMe运行我的 Qt 项目。问题是,我有很多子项目(数百个)都有不同的编译器标志。因此,一个全局 .ycm_extra_conf.py 是不够的。每个子项目都有一个 Makefile(由 qmake 生成)包含要传递给 GCC 的所有编译器标志。

是否可以配置 YouCompleteMe读取项目的 Makefile 而不是需要为每个创建不同的 .ycm_extra_conf.py

最佳答案

在出现更好的解决方案之前,我编写了这个丑陋的 Bash 脚本来完成工作。如果要使用它,则需要将目录设置为在顶部递归:

#!/bin/bash
    declare -a dirs_to_recurse=('project1' 'project2' 'project3')

template_file="ycm-template.txt"

generate_ycm_config ()
{
    file=.ycmtmp.txt
    dir=$(echo "$1" | sed -e 's/ /\\ /g')
    if [ -d "$dir" ]; then
        cd "$dir"
        if [ -f "Makefile" ] && $(ls -U *.pro > /dev/null 2>&1); then
            list=""
            for i in DEFINES CXXFLAG INCPATH; do
                list="${list} '$(cat Makefile | grep "$i.*=" | sed -e "s/$i.*= //g" | sed -e "s/ \$(DEFINES)//g" | sed -e "s/ -Wno-unused-local-typedefs//g" | sed -e "s/ /\', \'/g")', "
            done
            echo "$list"
            rm -f $file
            preifs="$IFS"
            IFS=''
            while read line; do
                echo $line >> $file
                if [[ $line =~ ^compilation_database_folder ]]; then
                    read -r -d '' VAR << __EOF__

flags = [
$list
]

__EOF__
                    echo "$VAR" >> $file
                fi
            done < $template_file
            mv $file .ycm_extra_conf.py
            IFS=$preifs
        fi
    fi
}


for dir in ${dirs_to_recurse[@]}; do
    for i in $(find $HOME/$dir -type d 2> /dev/null); do
        generate_ycm_config "$i"
    done
done

这是我的模板文件:

import os
import ycm_core
from clang_helpers import PrepareClangFlags

# Set this to the absolute path to the folder (NOT the file!) containing the
# compile_commands.json file to use that instead of 'flags'. See here for
# more details: http://clang.llvm.org/docs/JSONCompilationDatabase.html
# Most projects will NOT need to set this to anything; you can just change the
# 'flags' list of compilation flags. Notice that YCM itself uses that approach.
compilation_database_folder = ''


if compilation_database_folder:
  database = ycm_core.CompilationDatabase( compilation_database_folder )
else:
  database = None


def DirectoryOfThisScript():
  return os.path.dirname( os.path.abspath( __file__ ) )


def MakeRelativePathsInFlagsAbsolute( flags, working_directory ):
  if not working_directory:
    return flags
  new_flags = []
  make_next_absolute = False
  path_flags = [ '-isystem', '-I', '-iquote', '--sysroot=' ]
  for flag in flags:
    new_flag = flag

    if make_next_absolute:
      make_next_absolute = False
      if not flag.startswith( '/' ):
        new_flag = os.path.join( working_directory, flag )

    for path_flag in path_flags:
      if flag == path_flag:
        make_next_absolute = True
        break

      if flag.startswith( path_flag ):
        path = flag[ len( path_flag ): ]
        new_flag = path_flag + os.path.join( working_directory, path )
        break

    if new_flag:
      new_flags.append( new_flag )
  return new_flags


def FlagsForFile( filename ):
  if database:
    # Bear in mind that compilation_info.compiler_flags_ does NOT return a
    # python list, but a "list-like" StringVec object
    compilation_info = database.GetCompilationInfoForFile( filename )
    final_flags = PrepareClangFlags(
        MakeRelativePathsInFlagsAbsolute(
            compilation_info.compiler_flags_,
            compilation_info.compiler_working_dir_ ),
        filename )
  else:
    relative_to = DirectoryOfThisScript()
    final_flags = MakeRelativePathsInFlagsAbsolute( flags, relative_to )

  return {
    'flags': final_flags,
    'do_cache': True
  }

关于c++ - 如何配置 Vim 插件 YouCompleteMe 来读取 Makefile?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23505444/

相关文章:

c++ - std::make_unique SFINAE 友好吗?

qt - 诺基亚E63手机CPU瓶颈

c++ - QCalendarWidget 带日期

compilation - 在后台运行:make from gVim

c++ - HLSL 几何着色器四边形正在移动

c++ - 编译未解析的外部符号 gdcm::system::getHostName 等

c++ - 友元函数及其与 I/O 操作符的关系

c++ - QString复制

vim - 如何在VIM标签搜索和导航中前进?

vim - 仅在 vim 中搜索并替换出现固定次数的模式