CMakeLists实战解读--YouCompleteMe

    个人一直有一个想法,就是想出一系列关于CMakeLists.txt国外经典例子的实战解读。因为国内关于CMake的介绍和用法少之又少,再加上CMake本身对于实践能力的要求也比较高,过于理论化的学习只会让读者停留在Hello World和超级项目之间(其实就是理论知识要么简单很容易,单个cpp或者单个lib,或者例子复杂的要死,直接带动整个超大过程类似KDE这种),所以,我觉得我应该带领读者去学习一些首先本身不会太简单但也不至于复杂到无从下手的经典的国外的example,让读者更加深入学习和了解CMake的同时,也让我能有更好的学习机会,这也是我开Ricky.K这个bolg,记录博客写下工作心得的另一个重要原因。因此这个系列我会带上一系列经典的CMakeLists.txt的经典例子,并且带上我的个人解读,读者如果觉得有错误或者有想法可以跟我留言交流。

    废话不多说,Ricky就带领大家学习我们这个系列第一个题目--Vim补全神级插件YouCompleteMe的CMakeLists.txt.先介绍点之前我介绍这款插件的安装过程中遇到的问题,实际作者给出了一个具体的install.sh方便大家进行安装,你可以输入install.sh --help具体来查看,里面有--clang-completer(使用Clang补全)和--system-libclang(使用系统自带的libclang.so,这里需要将LD_LIBRARY_PATH设置好,因为后期的CMake脚本会去find_library这个环境变量里的libclang.so,在这里我前期只是简单的加了点PATH的维护,导致之后find_library一直去找的系统下的libclang,导致一直出错)还有--omnisharp-completer。

    其实说白了,安装这个插件最重要的两个变量就是在这个插件中CMakeLists.txt中的USE_CLANG_COMPLETER和LIBCLANG_TARGET,前者被打开之后会添加相应的ClangCompleter头文件和宏USE_CLANG_COMPLETER,后者会再最后target_link给ycm_core.so的时候去加载相应路径下的libclang.so,其实说白了,你只要将这两个变量自己维护好,脱离脚本安装,乃至脱离作者介绍的CMake安装都没有任何问题。下面正式进入正题

  

# CMake要求的最低版本号
cmake_minimum_required( VERSION 2.8 )

# 项目名称
project( ycm_support_libs )
# 设置客户端lib和服务端lib的变量名称
set( CLIENT_LIB "ycm_client_support" )
set( SERVER_LIB "ycm_core" )

# 设置Python的版本号变量
set( Python_ADDITIONAL_VERSIONS 2.7 2.6 )
# 进行Python包的查找,这里是REQUIRED表示必须
find_package( PythonLibs 2.6 REQUIRED )

# 如果Python版本号低于3.0.0就进行FATAL_ERROR的出错信息
if ( NOT PYTHONLIBS_VERSION_STRING VERSION_LESS "3.0.0" )
  message( FATAL_ERROR
    "CMake found python3 libs instead of python2 libs. YCM works only with "
    "python2.\n" )
endif()

# 各种option
option( USE_DEV_FLAGS "Use compilation flags meant for YCM developers" OFF )
# 这个变量就是我上文讲到的很关键的一个变量,来判断当前用户需要不需要libclang
option( USE_CLANG_COMPLETER "Use Clang semantic completer for C/C++/ObjC" OFF )
# install.sh中的--system-libclang就与这个变量进行交互
option( USE_SYSTEM_LIBCLANG "Set to ON to use the system libclang library" OFF )
# YCM作者推荐的用法,在这里直接写入Clang的相关路径 注意这里的CACHE PATH,表示当用户如果命令行
# 进行指定,那优先会去读用户的命令行,而不是用这里的set,并且把相关的值写入Cache中
set( PATH_TO_LLVM_ROOT "" CACHE PATH "Path to the root of a LLVM+Clang binary distribution" )
# YCM作者推荐的另外一种安装方法,直接将libclang.so全路径写死
set( EXTERNAL_LIBCLANG_PATH "" CACHE PATH "Path to the libclang library to use" )

# 如果你使用libclang但是没有指定用不用系统的libclang,没有指定llvm_root,没有指定额外的libclang.so,那么就会带你去下载
if ( USE_CLANG_COMPLETER AND
     NOT USE_SYSTEM_LIBCLANG AND
     NOT PATH_TO_LLVM_ROOT AND
     NOT EXTERNAL_LIBCLANG_PATH )
  message( "Downloading Clang 3.4" )

  # 这就是llvm官网的3.4下载路径
  set( CLANG_URL "http://llvm.org/releases/3.4" )

  # 如果当前客户端是苹果 Mac OS X
  if ( APPLE )
      # 设置Clang的文件夹名称
    set( CLANG_DIRNAME "clang+llvm-3.4-x86_64-apple-darwin10.9" )
      # 设置Clang的MD5校验码
    set( CLANG_MD5 "4f43ea0e87090ae5e7bec12373ca4927" )
      # 设置Clang文件名称为之后加上tar.gz
    set( CLANG_FILENAME "${CLANG_DIRNAME}.tar.gz" )
  else()
      # 如果是64位平台
    if ( 64_BIT_PLATFORM )
      # 设置Clang的文件夹名称
      set( CLANG_DIRNAME "clang+llvm-3.4-x86_64-unknown-ubuntu12.04" )
      # 设置Clang的MD5校验码
      set( CLANG_MD5 "6077459d20a7ff412eefc6ce3b9f5c85" )
      # 设置Clang文件名称为之后加上tar.gz
      set( CLANG_FILENAME "${CLANG_DIRNAME}.tar.xz" )
    else()
      # 表示此时为32位的Linux,下载3.3版本
      message( "No pre-built Clang 3.4 binaries for 32 bit linux, "
               "downloading Clang 3.3" )
      set( CLANG_URL "http://llvm.org/releases/3.3" )
      # 设置Clang的文件夹名称
      set( CLANG_DIRNAME "clang+llvm-3.3-i386-debian6" )
      # 设置Clang的MD5校验码
      set( CLANG_MD5 "415d033b60659433d4631df894673802" )
      # 设置Clang文件名称为之后加上tar.gz
      set( CLANG_FILENAME "${CLANG_DIRNAME}.tar.bz2" )
    endif()
  endif()

      # 下载命令
  file(
    DOWNLOAD "${CLANG_URL}/${CLANG_FILENAME}" "./${CLANG_FILENAME}"
    SHOW_PROGRESS EXPECTED_MD5 "${CLANG_MD5}"
  )

  # 文件名正则表达式匹配,进行相应的解压
  if ( CLANG_FILENAME MATCHES ".+bz2" )
      # 执行相关的外部命令 tar
    execute_process( COMMAND tar -xjf ${CLANG_FILENAME} )
  elseif( CLANG_FILENAME MATCHES ".+xz" )
    execute_process( COMMAND tar -xJf ${CLANG_FILENAME} )
  else()
    execute_process( COMMAND tar -xzf ${CLANG_FILENAME} )
  endif()

  # 设置PATH_TO_LLVM_ROOT的路径为当前CMake二进制路径下的Clang目录
  set( PATH_TO_LLVM_ROOT "${CMAKE_CURRENT_BINARY_DIR}/../${CLANG_DIRNAME}" )
endif()

# 如果设置了PATH_TO_LLVM_ROOT或者用户使用系统libclang或者有额外的libclang,就开启USE_CLANG_COMPLETER
# 这个变量我上文提过,很关键
if ( PATH_TO_LLVM_ROOT OR USE_SYSTEM_LIBCLANG OR EXTERNAL_LIBCLANG_PATH )
  set( USE_CLANG_COMPLETER TRUE )
endif()

# 开始使用这个变量,如果用户确定使用libclang,但是没有root没有系统clang没有额外clang,那么
# 进行错误性提示
if ( USE_CLANG_COMPLETER AND
     NOT PATH_TO_LLVM_ROOT AND
     NOT USE_SYSTEM_LIBCLANG AND
     NOT EXTERNAL_LIBCLANG_PATH )
  message( FATAL_ERROR
    "You have not specified which libclang to use. You have several options:\n"
    " 1. Set PATH_TO_LLVM_ROOT to a path to the root of a LLVM+Clang binary "
    "distribution. You can download such a binary distro from llvm.org. This "
    "is the recommended approach.\n"
    " 2. Set USE_SYSTEM_LIBCLANG to ON; this makes YCM search for the system "
    "version of libclang.\n"
    " 3. Set EXTERNAL_LIBCLANG_PATH to a path to whatever "
    "libclang.[so|dylib|dll] you wish to use.\n"
    "You HAVE to pick one option. See the docs for more information.")
endif()

# 进行用户提醒,提醒用户当前是否使用libclang
if ( USE_CLANG_COMPLETER )
  message( "Using libclang to provide semantic completion for C/C++/ObjC" )
else()
  message( "NOT using libclang, no semantic completion for C/C++/ObjC will be "
           "available" )
endif()

# 如果设置了root就设置CLANG_INCLUDES_DIR为root下的include
# 否则CLANG_INCLUDES_DIR为CMake下llvm/include
if ( PATH_TO_LLVM_ROOT )
  set( CLANG_INCLUDES_DIR "${PATH_TO_LLVM_ROOT}/include" )
else()
  set( CLANG_INCLUDES_DIR "${CMAKE_SOURCE_DIR}/llvm/include" )
endif()

# 如果当前的include路径不是绝对路径
if ( NOT IS_ABSOLUTE "${CLANG_INCLUDES_DIR}" )
    # 设置它为绝对路径
  get_filename_component(CLANG_INCLUDES_DIR
    "${CMAKE_BINARY_DIR}/${CLANG_INCLUDES_DIR}" ABSOLUTE)
endif()

# 如果没有额外的libclang,但是有root
if ( NOT EXTERNAL_LIBCLANG_PATH AND PATH_TO_LLVM_ROOT )
    if ( MINGW ) # 如果是MINGW
        # 设置libclang的寻找路径(后面的find_library会去寻找)
    set( LIBCLANG_SEARCH_PATH "${PATH_TO_LLVM_ROOT}/bin" )
  else()
    set( LIBCLANG_SEARCH_PATH "${PATH_TO_LLVM_ROOT}/lib" )
  endif()

  # 这里TEMP会被find_library去寻找clang,和libclang两个so,并且复制路径给TEMP
  find_library( TEMP NAMES clang libclang
                PATHS ${LIBCLANG_SEARCH_PATH}
                NO_DEFAULT_PATH )

            message("temp is ${TEMP}")
            # 设置额外的libclang为这个路径
  set( EXTERNAL_LIBCLANG_PATH ${TEMP} )
endif()

 # 如果当前为苹果,设置额外的flag
if ( APPLE )
  set( CMAKE_INCLUDE_SYSTEM_FLAG_CXX "-isystem " )
endif()

# 如果用户使用系统自带的boost
if ( USE_SYSTEM_BOOST )
    # 进行find boost命令,会用到python,filesystem,system,regex,thread等components
  find_package( Boost REQUIRED COMPONENTS python filesystem system regex thread )
else()
    # 使用自己的Boost
  set( Boost_INCLUDE_DIR ${BoostParts_SOURCE_DIR} )
  set( Boost_LIBRARIES BoostParts )
endif()

 # 相关头文件的加入,Boost,Python和Clang
include_directories(
  SYSTEM
  ${Boost_INCLUDE_DIR}
  ${PYTHON_INCLUDE_DIRS}
  ${CLANG_INCLUDES_DIR}
  )

# 全局递归查找h,cpp文件给SERVER_SOURCES
file( GLOB_RECURSE SERVER_SOURCES *.h *.cpp )

# 全局递归查找测试相关文件
file( GLOB_RECURSE to_remove tests/*.h tests/*.cpp CMakeFiles/*.cpp *client* )

if( to_remove )
# 学习list相关的REMOVE_ITEM命令
list( REMOVE_ITEM SERVER_SOURCES ${to_remove} )
endif()

# 这里就是这个变量最关键的地方,它会去include并且开启宏
if ( USE_CLANG_COMPLETER )
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}
"${CMAKE_CURRENT_SOURCE_DIR}/ClangCompleter" )
add_definitions( -DUSE_CLANG_COMPLETER )
else()
# 否则的话寻找所有ClangCompleter下的头和源文件进行删除
file( GLOB_RECURSE to_remove_clang ClangCompleter/*.h ClangCompleter/*.cpp )

if( to_remove_clang )
list( REMOVE_ITEM SERVER_SOURCES ${to_remove_clang} )
endif()
endif()

# 如果用户使用额外的libclang或者使用系统自带的libclang
if ( EXTERNAL_LIBCLANG_PATH OR USE_SYSTEM_LIBCLANG )
if ( USE_SYSTEM_LIBCLANG ) # 如果是系统自带
if ( APPLE )
set( ENV_LIB_PATHS ENV DYLD_LIBRARY_PATH ) # 将环境变量下的DYLD_LIBRARY_PATH给ENV_LIB_PATHS
elseif ( UNIX )
set( ENV_LIB_PATHS ENV LD_LIBRARY_PATH ) # 这也是我之前讲的一定要把你编译的libclang加入到这个环境变量中,因为它会根据这个去寻找
elseif ( WIN32 )
set( ENV_LIB_PATHS ENV PATH )
else ()
set( ENV_LIB_PATHS "" )
endif()
file( GLOB SYS_LLVM_PATHS "/usr/lib/llvm*/lib" )
# 进行相关的libclang查找,在你之前给它指定的环境变量中 find_library( TEMP clang PATHS ${ENV_LIB_PATHS} /usr/lib /usr/lib/llvm ${SYS_LLVM_PATHS} /Library/Developer/CommandLineTools/usr/lib, /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib ) # 将寻找到的变量给EXTERNAL_LIBCLANG_PATH set( EXTERNAL_LIBCLANG_PATH ${TEMP} ) else() if ( NOT APPLE ) # 设置相关rpath set( CMAKE_BUILD_WITH_INSTALL_RPATH TRUE ) # 设置make install之后的rpath set( CMAKE_INSTALL_RPATH "\$ORIGIN" ) endif() endif() set( LIBCLANG_TARGET "" ) message( "Using external libclang: ${EXTERNAL_LIBCLANG_PATH}" ) message("libclang_target is ${LIBCLANG_TARGET}") else() set( LIBCLANG_TARGET ) endif() # 如果有额外的rpath,在这里进行设置 if ( EXTRA_RPATH ) set( CMAKE_INSTALL_RPATH "${EXTRA_RPATH}:${CMAKE_INSTALL_RPATH}" ) endif() # 如果在Linux下需要额外的rt库 if ( UNIX AND NOT APPLE ) set( EXTRA_LIBS rt ) endif() # 将目录下所有的h和cpp给CLIENT_SOURCES file( GLOB CLIENT_SOURCES *.h *.cpp ) # 相应SERVER_SPECIFIC赋值 file( GLOB SERVER_SPECIFIC *ycm_core* ) if( SERVER_SPECIFIC ) # 移除相关CLIEN_SOURCES下的SERVER_SPECIFIC list( REMOVE_ITEM CLIENT_SOURCES ${SERVER_SPECIFIC} ) endif() # 创建client的library,并且是动态库 add_library( ${CLIENT_LIB} SHARED ${CLIENT_SOURCES} ) # 将这个库与之前的rt,Boost,Python进行链接 target_link_libraries( ${CLIENT_LIB} ${Boost_LIBRARIES} ${PYTHON_LIBRARIES} ${EXTRA_LIBS} ) # 创建server的library,并且是动态库 add_library( ${SERVER_LIB} SHARED ${SERVER_SOURCES} ) # 将这个库与之前的rt,Boost,Python,libclang,而外的server lib进行链接 target_link_libraries( ${SERVER_LIB} ${Boost_LIBRARIES} ${PYTHON_LIBRARIES} ${LIBCLANG_TARGET} ${EXTRA_LIBS} ) # 如果定义了LIBCLANG_TARGET if( LIBCLANG_TARGET ) if( NOT WIN32 ) # 在非WIN32情况下增加自定义命令,将libclang.so/dll拷贝到自己目录下 add_custom_command( TARGET ${SERVER_LIB} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy "${LIBCLANG_TARGET}" "$<TARGET_FILE_DIR:${SERVER_LIB}>" ) else() add_custom_command( TARGET ${SERVER_LIB} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy "${PATH_TO_LLVM_ROOT}/bin/libclang.dll" "$<TARGET_FILE_DIR:${SERVER_LIB}>") endif() endif() # 建立依赖关系,表示这个项目需要这两个库共同完成 add_custom_target( ${PROJECT_NAME} DEPENDS ${CLIENT_LIB} ${SERVER_LIB} ) # Mac下的相关设置,如果是利用rpath的话Mac下还是会去寻找系统库,即使用户显示指定 # 这里需要改用@loader_path if ( EXTERNAL_LIBCLANG_PATH AND APPLE ) add_custom_command( TARGET ${SERVER_LIB} POST_BUILD COMMAND install_name_tool "-change" "@rpath/libclang.dylib" "@loader_path/libclang.dylib" "$<TARGET_FILE:${SERVER_LIB}>" ) endif() # 将这些库的前缀lib去掉,因为会扰乱Python模块的查找 set_target_properties( ${CLIENT_LIB} PROPERTIES PREFIX "") set_target_properties( ${SERVER_LIB} PROPERTIES PREFIX "") if ( WIN32 OR CYGWIN ) # 进行Windows下相关库的转移存放 set_target_properties( ${CLIENT_LIB} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/../.. ) set_target_properties( ${SERVER_LIB} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/../.. ) foreach( OUTPUTCONFIG ${CMAKE_CONFIGURATION_TYPES} ) string( TOUPPER ${OUTPUTCONFIG} OUTPUTCONFIG ) set_target_properties( ${CLIENT_LIB} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${PROJECT_SOURCE_DIR}/../.. ) set_target_properties( ${SERVER_LIB} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${PROJECT_SOURCE_DIR}/../.. ) endforeach() if ( WIN32 ) # 建立后缀名.pyd set_target_properties( ${CLIENT_LIB} PROPERTIES SUFFIX ".pyd") set_target_properties( ${SERVER_LIB} PROPERTIES SUFFIX ".pyd") elseif ( CYGWIN ) # CYGIN下后缀为dll set_target_properties( ${CLIENT_LIB} PROPERTIES SUFFIX ".dll") set_target_properties( ${SERVER_LIB} PROPERTIES SUFFIX ".dll") endif() else() # Mac和Linux下都为.so,虽然Mac下应该默认为.dylib,但Python识别不了dylib,因此这里还是设置成.so set_target_properties( ${CLIENT_LIB} PROPERTIES SUFFIX ".so") set_target_properties( ${SERVER_LIB} PROPERTIES SUFFIX ".so") endif() # 设置相关lib的输出目录 set_target_properties( ${CLIENT_LIB} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/../.. ) set_target_properties( ${SERVER_LIB} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/../.. ) if ( USE_DEV_FLAGS AND ( CMAKE_COMPILER_IS_GNUCXX OR COMPILER_IS_CLANG ) AND NOT CMAKE_GENERATOR_IS_XCODE ) # 增加相应flag set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Werror" ) endif() # 提出警告在使用C++11特性下 if ( USE_DEV_FLAGS AND COMPILER_IS_CLANG AND NOT CMAKE_GENERATOR_IS_XCODE AND NOT SYSTEM_IS_FREEBSD ) set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wc++98-compat" ) endif() if( SYSTEM_IS_SUNOS ) # SunOS需要-pthreads这个flag才能正常使用 set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthreads" ) endif()
# 增加测试子目录tests add_subdirectory( tests )

 

posted @ 2014-07-30 09:46 Ricky.K 阅读(...) 评论(...) 编辑 收藏