Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

cmake

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.

like image 244
allo Avatar asked Apr 21 '17 12:04

allo


People also ask

How do I assign a variable in CMake?

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.

Can CMake set environment variables?

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.

How do you define a variable in Cmakelist?

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.

WHAT IS SET command in CMake?

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.


1 Answers

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)")
like image 120
Florian Avatar answered Sep 21 '22 07:09

Florian