In my CMake based project I have a variable in my CMakeLists.txt that enables what backend to target. The valid values for this variable are limited, say 6.
I want to cache a list of the valid values so that the user can select which feature to enable. CMake should validate the variable.
Is this possible and if so, how?
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.
You access a variable by using ${} , such as ${MY_VARIABLE} . 1. CMake has the concept of scope; you can access the value of the variable after you set it as long as you are in the same scope. If you leave a function or a file in a sub directory, the variable will no longer be defined.
Cache variables (unless set with INTERNAL) are mostly intended for configuration settings where the first CMake run determines a suitable default value, which the user can then override, by editing the cache with tools such as ccmake or cmake-gui.
Name of the project given to the project command. This is the name given to the most recently called project() command in the current directory scope or above. To obtain the name of the top level project, see the CMAKE_PROJECT_NAME variable.
If you want to validate the allowable values, you'll need to do that yourself in your CMakeLists.txt
file. You can, however, provide a list of values for CMake to present as a combo box for STRING cache variables in the CMake GUI app (and also an ncurses-based equivalent in ccmake). You do that by setting the STRINGS property of the cache variable. For example:
set(trafficLightColors Red Orange Green)
set(trafficLight Green CACHE STRING "Status of something")
set_property(CACHE trafficLight PROPERTY STRINGS ${trafficLightColors})
In that example, the CMake GUI would show the trafficLight
cache variable just like any other string variable, but if the user clicks on it to edit it, instead of a generic text box you'd get a combo box containing the items Red
, Orange
and Green
.
While this isn't 100% robust validation, it does help users enter only valid values if using the GUI. If they are using cmake at the command line though, there's nothing stopping them from setting a cache variable to any value they like. So I'd recommend using the STRINGS cache variable property to help your users, but also do validation. If you've used the pattern of the above example, you will already have list of valid values, so validation should be easy. For example:
list(FIND trafficLightColors ${trafficLight} index)
if(index EQUAL -1)
message(FATAL_ERROR "trafficLight must be one of ${trafficLightColors}")
endif()
Or if you're using CMake 3.5 or later:
if(NOT trafficLight IN_LIST trafficLightColors)
message(FATAL_ERROR "trafficLight must be one of ${trafficLightColors}")
endif()
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