CMake - 强制find_package更新缓存变量 [英] CMake - Force find_package to update cache variables

查看:1443
本文介绍了CMake - 强制find_package更新缓存变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的CMake项目中,我提供一个默认路径Boost可以由用户编辑。

 设置(PATH_BOOST_DEFAULT / boost / $ {BOOST_VER} / $ {ARCH} / gcc / $ {GCCVER})
set(PATH_BOOST$ {PATH_BOOST_DEFAULT}CACHE PATHBoost的默认路径)

之后,我尝试找到libs:

 
set(BOOST_ROOT$ {PATH_BOOST})
set(Boost_USE_MULTITHREAD ON)
set(Boost_USE_STATIC_LIBS ON)
find_package(Boost 1.53.0 REQUIRED COMPONENTS thread system)

这很好,很多缓存变量,例如 Boost_INCLUDE_DIR Boost_LIBRARY_DIRS Boost_THREAD_LIBRARY_DEBUG



我的问题出现时,我尝试修改缓存变量 PATH_BOOST :由 FindBoost.cmake生成的缓存变量 aren' t更新。脚本 FindBoost.cmake 似乎被再次调用(它打印有关找到的组件的消息)。我认为 Boost_INCLUDE_DIR 的变量不会更新,因为它们在缓存中。



有没有办法说cmake如果路径被用户修改,通过强制缓存变量来重写包。?



此外,有一个更好的方法来检测缓存变量修改比下面的丑陋的想法? : - /

  set(MY_VAR $ {MY_VAR_DEFAULT} CACHE TYPE)
if(NOT DEFINED MY_VAR_copy)
set(MY_VAR_copy $ {MY_VAR} CACHE INTERNAL)
mark_as_advanced(FORCE MY_VAR_copy)
endif()
if(NOT$ {MY_VAR}STREQUAL$ {MY_VAR_copy} )
#my_var is modified:do something
set(MY_VAR_copy $ {MY_VAR} CACHE INTERNAL)
endif()


解决方案

我想我有同样的问题。我的设置尝试找到一个包的特定版本:

 设置(MYPACK_REQUIRED_VERSION 1.2.3)
find_package $ {MYPACK_REQUIRED_VERSION} EXACT)

包配置脚本设置缓存的变量 MYPACK_LIBRARIES ,然后在稍后阶段使用。但是当我改变 MYPACK_REQUIRED_VERSION 变量cmake仍然使用旧的 MYPACK_LIBRARIES ,而不是试图寻找新版本。 / p>

我想我现在通过取消设置此缓存变量解决了这个问题:

  set(MYPACK_REQUIRED_VERSION 1.2.3)
unset(MYPACK_LIBRARIES CACHE)
find_package(mypack $ {MYPACK_REQUIRED_VERSION} EXACT)

这似乎在我的情况下再次触发find_package过程。有一些更细致的细节的 find_package 程序,我不完全理解,所以这可能不工作在你的情况,但它可能值得一试。


In my CMake project, I provide a default path to Boost editable by the user.

set(PATH_BOOST_DEFAULT "/softs/boost/${BOOST_VER}/${ARCH}/gcc/${GCCVER}")
set(PATH_BOOST "${PATH_BOOST_DEFAULT}" CACHE PATH "Default path to Boost")

After that, I try to find the libs with :

set(BOOST_ROOT "${PATH_BOOST}")
set(Boost_USE_MULTITHREAD ON)
set(Boost_USE_STATIC_LIBS ON)
find_package(Boost 1.53.0 REQUIRED COMPONENTS thread system)

This works fine and a lot of cache variables like Boost_INCLUDE_DIR, Boost_LIBRARY_DIRS or Boost_THREAD_LIBRARY_DEBUG are generated.

My problem comes when I try to modify the cache variable PATH_BOOST : the cache variables generated by FindBoost.cmake aren't updated. The script FindBoost.cmake seems to be called again (it print messages about found components). I think the variables like Boost_INCLUDE_DIR are not updated because they are in cache.

Is there a way to say to cmake "if the path is modified by the user, refind the package by forcing the cache variables" ?

Also, is there a nicer way to detect a cache variable was just modified than the following ugly idea ? :-/

set(MY_VAR ${MY_VAR_DEFAULT} CACHE TYPE "")
if(NOT DEFINED MY_VAR_copy)
  set(MY_VAR_copy ${MY_VAR} CACHE INTERNAL "")
  mark_as_advanced(FORCE MY_VAR_copy)
endif()
if(NOT "${MY_VAR}" STREQUAL "${MY_VAR_copy}")
  # my_var is modified : do something
  set(MY_VAR_copy ${MY_VAR} CACHE INTERNAL "")
endif()

解决方案

I think I've got the same problem as you. My setup tries to find a specific version of a package:

set (MYPACK_REQUIRED_VERSION 1.2.3)
find_package (mypack ${MYPACK_REQUIRED_VERSION} EXACT)

The package config script sets the cached variable MYPACK_LIBRARIES which then is used at a later stage. However when I change the MYPACK_REQUIRED_VERSION variable cmake still use the old MYPACK_LIBRARIES instead of trying to look for the new version.

I think I've solved the problem now by unsetting this cache variable:

set (MYPACK_REQUIRED_VERSION 1.2.3)
unset (MYPACK_LIBRARIES CACHE)
find_package (mypack ${MYPACK_REQUIRED_VERSION} EXACT)

This seems to trigger the find_package procedure again in my case. There are some finer details of the find_package procedure that I don't completely understand so this might not work in your case, but it might be worth a try.

这篇关于CMake - 强制find_package更新缓存变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆