set a cmake variable if it is not changed by the user

前端 未结 2 909
眼角桃花
眼角桃花 2020-12-19 14:29

How can i (re)set cmake variable only when it is not changed by the user?

I have this variables:

set(DIR \"testdir\" CACHE PATH \"main directory\")
s         


        
2条回答
  •  悲&欢浪女
    2020-12-19 15:11

    Turning my comment into an answer

    You could use MODIFIED cache variable property, but the documentation says

    Do not set or get.

    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()
    

    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)")
    

提交回复
热议问题