cmake學習筆記(五)

weixin_33782386發表於2018-12-07

原文地址: https://blog.csdn.net/dbzhang800/article/details/6341029

Cmake學習筆記三 中簡單學習了 find_package 的 model 模式,在Cmake學習筆記四 中瞭解一個CMakeCache相關的東西。但靠這些知識還是不能看懂PySide使用CMakeLists檔案,接下來繼續學習find_package的 config 模式及package configure檔案相關知識


當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


該檔案至少需提供標頭檔案路徑和庫檔案資訊。比如 ApiExtractorConfig.cmake 在Windows下一個例子:

# - try to find APIEXTRACTOR
#  APIEXTRACTOR_INCLUDE_DIR   - Directories to include to use APIEXTRACTOR
#  APIEXTRACTOR_LIBRARIES     - Files to link against to use APIEXTRACTOR

SET(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)


該檔案用來比對版本是否匹配,看看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


相關文章