首页 > 代码库 > cmake学习笔记(五)

cmake学习笔记(五)

在cmake 学习笔记(三) 中简单学习了 find_package 的 model 模式,在cmake 学习笔记(四)中了解一个CMakeCache相关的东西。但靠这些知识还是不能看懂PySide使用CMakeLists文件,接下来继续学习find_package的 config 模式及package configure文件相关知识

find_package 的 config 模式

当CMakeLists.txt中使用find_package命令时,首先启用的是 module 模式:

  • 按照 CMAKE_MODULE_PATH 路径和cmake的安装路径去搜索finder文件 Find<package>.cmake

如果finder未找到,则开始 config 模式:

  • 将在下列路径下查找 配置 文件 <name>Config.cmake 或 <lower-case-name>-config.cmake

<prefix>/

(W)

<prefix>/(cmake|CMake)/

(W)

<prefix>/<name>*/

(W)

<prefix>/<name>*/(cmake|CMake)/

(W)

<prefix>/(share|lib)/cmake/<name>*/

(U)

<prefix>/(share|lib)/<name>*/

(U)

<prefix>/(share|lib)/<name>*/(cmake|CMake)/

(U)

  • find_package 参数及规则见manual

<name>Config.cmake

该文件至少需提供头文件路径和库文件信息。比如 ApiExtractorConfig.cmake 在Windows下一个例子:

# - try to find APIEXTRACTOR#  APIEXTRACTOR_INCLUDE_DIR   - Directories to include to use APIEXTRACTOR#  APIEXTRACTOR_LIBRARIES     - Files to link against to use APIEXTRACTORSET(APIEXTRACTOR_INCLUDE_DIR "D:/shiboken/dist/include/apiextractor")if(MSVC)    SET(APIEXTRACTOR_LIBRARY "D:/shiboken/dist/lib/apiextractor.lib")elseif(WIN32)    SET(APIEXTRACTOR_LIBRARY "D:/shiboken/dist/bin/apiextractor.dll")else()    SET(APIEXTRACTOR_LIBRARY "D:/shiboken/dist/lib/apiextractor.dll")endif()

该文件是通过 configure_file 机制生成的,我们看看 ApiExtractorConfig.cmake.in 文件:

SET(APIEXTRACTOR_INCLUDE_DIR "@CMAKE_INSTALL_PREFIX@/include/apiextractor@apiextractor_SUFFIX@")if(MSVC)    SET(APIEXTRACTOR_LIBRARY "@LIB_INSTALL_DIR@/@CMAKE_SHARED_LIBRARY_PREFIX@apiextractor@apiextractor_SUFFIX@@LIBRARY_OUTPUT_SUFFIX@.lib")elseif(WIN32)    SET(APIEXTRACTOR_LIBRARY "@CMAKE_INSTALL_PREFIX@/bin/@CMAKE_SHARED_LIBRARY_PREFIX@apiextractor@apiextractor_SUFFIX@@LIBRARY_OUTPUT_SUFFIX@@CMAKE_SHARED_LIBRARY_SUFFIX@")else()    SET(APIEXTRACTOR_LIBRARY "@LIB_INSTALL_DIR@/@CMAKE_SHARED_LIBRARY_PREFIX@apiextractor@apiextractor_SUFFIX@@LIBRARY_OUTPUT_SUFFIX@@CMAKE_SHARED_LIBRARY_SUFFIX@")endif()

对应的命令(变量的定义略过)

configure_file("${CMAKE_CURRENT_SOURCE_DIR}/ApiExtractorConfig.cmake.in" "${CMAKE_CURRENT_BINARY_DIR}/ApiExtractorConfig.cmake" @ONLY)

<name>ConfigVersion.cmake

该文件用来比对版本是否匹配,看看ApiExtractorConfigVersion.cmake.in 的内容:

set(PACKAGE_VERSION @apiextractor_VERSION@)if("${PACKAGE_VERSION}" VERSION_LESS "${PACKAGE_FIND_VERSION}" )   set(PACKAGE_VERSION_COMPATIBLE FALSE)else("${PACKAGE_VERSION}" VERSION_LESS "${PACKAGE_FIND_VERSION}" )   set(PACKAGE_VERSION_COMPATIBLE TRUE)   if( "${PACKAGE_FIND_VERSION}" STREQUAL "${PACKAGE_VERSION}")      set(PACKAGE_VERSION_EXACT TRUE)   endif( "${PACKAGE_FIND_VERSION}" STREQUAL "${PACKAGE_VERSION}")endif("${PACKAGE_VERSION}" VERSION_LESS "${PACKAGE_FIND_VERSION}" )

一般提供设置下面的变量

PACKAGE_VERSION

完整的版本字符串

PACKAGE_VERSION_EXACT

如果完全匹配为真

PACKAGE_VERSION_COMPATIBLE

如果兼容为真

PACKAGE_VERSION_UNSUITABLE

如果不可用为真

find_package进而根据这些设置

<package>_VERSION

full provided version string

<package>_VERSION_MAJOR

major version if provided, else 0

<package>_VERSION_MINOR

minor version if provided, else 0

<package>_VERSION_PATCH

patch version if provided, else 0

<package>_VERSION_TWEAK

tweak version if provided, else 0

参考

  • http://www.cmake.org/cmake/help/cmake-2-8-docs.html#command:find_package

  • http://www.cmake.org/Wiki/CMake/Tutorials/Packaging

  • http://www.cmake.org/Wiki/CMake:How_to_create_a_ProjectConfig.cmake_file

from:http://blog.csdn.net/dbzhang800/article/details/6341029

cmake学习笔记(五)