2

我有一个关于 CMake 的问题:最近在标准安装中添加了很多模块,例如 GLEW 或 Mercurial。

然而,很多安装基础可能有一个旧版本,没有所有可用的新模块,所以你不得不发布你自己的版本(例如)FindGLEW.cmake

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

谢谢你的帮助。

4

2 回答 2

1

CMake 的include命令 wrt 模块的默认搜索行为是:

<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)
于 2012-11-13T19:38:13.773 回答
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.

希望这可以帮助。

于 2012-11-21T09:19:53.747 回答