设置一个cmake变量(如果用户未更改) [英] set a cmake variable if it is not changed by the user
问题描述
仅当用户未更改cmake变量时,我才能(重新)设置该变量?
How can i (re)set cmake variable only when it is not changed by the user?
我有以下变量:
set(DIR "testdir" CACHE PATH "main directory")
set(SUBDIR ${DIR}/subdir CACHE PATH "subdirectory")
在第一次运行时,变量被初始化为 testdir
和 testdir / subdir
。
On the first run the variables are initialized to testdir
and testdir/subdir
.
当用户更改 DIR
并重新运行cmake而不更改 SUBDIR
,我想生成一个新的 SUBDIR
路径,而我想保留 SUBDIR
路径,当用户更改它时。
When the user changed DIR
and reruns cmake without changing SUBDIR
, i would like to generate a new SUBDIR
path, while i want to keep the SUBDIR
path, when the user changed it.
所以 SUBDIR $ c $如果更改了
从未更改过。 DIR
并<<,则应基于 DIR
的新值将c>设置为新的默认值。 code> SUBDIR
So SUBDIR
should be set to the new default value based on the new value of DIR
, if DIR
was changed and SUBDIR
was never changed before.
推荐答案
将我的评论变成答案
您可以使用 MODIFIED
缓存变量属性,但文档显示
You could use MODIFIED
cache variable property, but the documentation says
不要设置或获取。
Do not set or get.
也许更好的方法是使用if语句检查修改:
Maybe a better approach would be to check the modification with if statements:
set(DIR "testdir" CACHE PATH "main directory")
if (NOT DEFINED SUBDIR OR SUBDIR MATCHES "/subdir$")
set(SUBDIR "${DIR}/subdir" CACHE PATH "subdirectory" FORCE)
endif()
或者您只是不将 DIR
放在 SUBDIR
内,然后将详细信息放入描述:
Or you just don't put DIR
inside SUBDIR
and put the details into the description:
set(SUBDIR "subdir" CACHE PATH "subdirectory of main directory (see DIR)")
这篇关于设置一个cmake变量(如果用户未更改)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!