Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between "option" and "set CACHE BOOL" for a CMake variable?

Is there any difference between the following two?

set(FOO true CACHE BOOL "description")

option(FOO "description" ON)

Documentation: set - option

Background: Even if I have been using CMake for a while, I only noticed the option command today and I have therefore always been using set: I was wondering if it is safe/worth to replace the first with the second.

like image 336
Antonio Avatar asked Apr 01 '16 13:04

Antonio


People also ask

What is cache variable in CMake?

The CMake cache may be thought of as a configuration file. The first time CMake is run on a project, it produces a CMakeCache. txt file in the top directory of the build tree. CMake uses this file to store a set of global cache variables, whose values persist across multiple runs within a project build tree.

What is option in CMake?

Provide a boolean option that the user can optionally select. option(<variable> "<help_text>" [value]) If no initial <value> is provided, boolean OFF is the default value. If <variable> is already set as a normal or cache variable, then the command does nothing (see policy CMP0077 ).

How do I set CMake cache variables?

You can use the command line to set entries in the Cache with the syntax cmake -D var:type=value , just cmake -D var=value or with cmake -C CMakeInitialCache. cmake .

What is Parent_scope in CMake?

If the PARENT_SCOPE option is given the variable will be set in the scope above the current scope. Each new directory or function creates a new scope. This command will set the value of a variable into the parent directory or calling function (whichever is applicable to the case at hand).


2 Answers

In your example, there is no difference. But there can be differences:

  • Option has a default value of OFF.
  • You can make an option dependent on other options with CMakeDependentOption
  • Set accepts types other than BOOL, especially ;-lists.

Additionally, when you use an option you are indicating to your user that it can be set safely. Setting internal variables from the outside might subtly break the script with invalid values.

like image 105
usr1234567 Avatar answered Oct 22 '22 18:10

usr1234567


Stumbled on this question, and thought I added an update.

As explained here, the option command does not create a CACHE variable if a normal variable with the same name exists. This behavior was introduced in version 3.13 (where it is the default). On the other hand, setting a CACHE variable named FOO when FOO exists as a normal variable, will yield two copies of FOO, a normal variable and a CACHE one.

Therefore, whether to use option(FOO "" ON) or set(FOO ON CACHE BOOL "") may also depend on what you want your configuration system to do when a variable with the same name has already been defined upstream.

Note: the two still behave the same way if FOO has been defined upstream in the cache. The difference is only if the upstream is a normal variable.

like image 25
bartgol Avatar answered Oct 22 '22 18:10

bartgol