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")
set(SUBDIR ${DIR}/subdir CACHE PATH "subdirectory")
On the first run the variables are initialized to testdir and testdir/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.
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.
Options and variables are defined on the CMake command line like this: $ cmake -DVARIABLE=value path/to/source You can set a variable after the initial `CMake` invocation to change its value. You can also undefine a variable: $ cmake -UVARIABLE path/to/source Variables are stored in the `CMake` cache.
Every process (e.g. your bash) that creates a child process (your cmake) provides the newly process a copy of its environment. You cannot change the environment of the parent process later. But you can start a intermediate process that sets the environment variables and then starts cmake.
Set a CMake, cache or environment variable to a given value. set(<variable> <value> [[CACHE <type> <docstring> [FORCE]] | PARENT_SCOPE]) Within CMake sets <variable> to the value <value>. < value> is expanded before <variable> is set to it. Normally, set will set a regular CMake variable.
Set a normal, cache, or environment variable to a given value. See the cmake-language(7) variables documentation for the scopes and interaction of normal variables and cache entries. Signatures of this command that specify a <value>... placeholder expect zero or more arguments.
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)")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With