cmake查找模块wrt版本

时间:2012-11-13 17:29:25

标签: build module cmake system

我对CMake有疑问:最近在标准安装中添加了很多模块,例如GLEW或Mercurial。

然而,许多安装基础可能没有所有新模块的旧版本,因此您不得不发布自己的版本(例如)FindGLEW.cmake

是否可以检查给定的FindXXX模块是否可用并在这种情况下使用它,否则提供适当的替代方案?或者甚至在运行时检查cmake版本(但这并不总是可靠且难以维护)......?

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

CMake的include命令w.r.t模块的默认搜索行为是:

  

首先在<modulename>.cmake中搜索名为CMAKE_MODULE_PATH的文件,然后在CMake模块目录中搜索。

(还有更多内容 - 请参阅文档或运行cmake --help-command include

你所要求的似乎与此相反;首先检查CMake模块目录中的官方模块,如果不存在,则返回使用您自己的模块。

假设您的模块位于${CMAKE_SOURCE_DIR}/my_cmake_modules,您可以通过执行以下操作来反转CMake的默认行为:

set(CMAKE_MODULE_PATH ${CMAKE_ROOT}/Modules ${CMAKE_SOURCE_DIR}/my_cmake_modules)

如果您按照官方模块命名自己的模块,那么这应该达到您的目标。如果您希望稍后在脚本中恢复正常的CMake行为,可以执行以下操作:

set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/my_cmake_modules)

答案 1 :(得分:0)

作为上述解决方案的可能替代方案,我找到了这个cmake政策

CMP0017
   Prefer files from the CMake module directory when including from
   there.

   Starting with CMake 2.8.4, if a cmake-module shipped with CMake (i.e.
   located in the CMake module directory) calls include() or
   find_package(), the files located in the the CMake module directory
   are preferred over the files in CMAKE_MODULE_PATH.  This makes sure
   that the modules belonging to CMake always get those files included
   which they expect, and against which they were developed and tested.
   In call other cases, the files found in CMAKE_MODULE_PATH still take
   precedence over the ones in the CMake module directory.  The OLD
   behaviour is to always prefer files from CMAKE_MODULE_PATH over files
   from the CMake modules directory.

   This policy was introduced in CMake version 2.8.4.  CMake version
   2.8.9 warns when the policy is not set and uses OLD behavior.  Use the
   cmake_policy command to set it to OLD or NEW explicitly.

希望这有帮助。