Variable CMake with Limited Real Values

In my CMake based project, I have a variable in my CMakeLists.txt that allows you to use a backend. Valid values ​​for this variable are limited, say 6.

I want to cache a list of valid values ​​so that the user can choose which function to enable. CMake should check the variable.

Is this possible, and if so, how?

+4
source share
1 answer

If you want to check the valid values, you need to do it yourself in your file CMakeLists.txt. However, you can provide a list of values ​​for CMake to represent as a combined field for STRING cache variables in the CMake GUI application (as well as the ncurses equivalent in ccmake). You do this by setting the STRINGS property of the cache variable. For instance:

set(trafficLightColors Red Orange Green)
set(trafficLight Green CACHE STRING "Status of something")
set_property(CACHE trafficLight PROPERTY STRINGS ${trafficLightColors})

In this example, the CMake GUI will display the cache variable trafficLightjust like any other string variable, but if the user clicks on it to change it, instead of a common text field you will get a combo field containing items Red, Orangeand Green.

100% , . cmake , , . STRINGS, , . , , . :

list(FIND trafficLightColors ${trafficLight} index)
if(index EQUAL -1)
    message(FATAL_ERROR "trafficLight must be one of ${trafficLightColors}")
endif()

, CMake 3.5 :

if(NOT trafficLight IN_LIST trafficLightColors)
    message(FATAL_ERROR "trafficLight must be one of ${trafficLightColors}")
endif()
+4

Source: https://habr.com/ru/post/1655671/


All Articles