【问题标题】:cmake find modules wrt versioncmake查找模块wrt版本
【发布时间】:2012-11-21 17:19:53
【问题描述】:

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

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

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

感谢您的帮助。

【问题讨论】:

    标签: build module cmake system


    【解决方案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)
    

    【讨论】:

    • 是的,这就是我正在寻找的行为!谢谢
    【解决方案2】:

    作为上述解决方案的可能替代方案,我发现了这个 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.
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2020-03-30
      • 2014-03-13
      • 1970-01-01
      • 1970-01-01
      • 2011-08-16
      • 1970-01-01
      • 2015-11-27
      • 2018-04-10
      • 1970-01-01
      相关资源
      最近更新 更多