CMake multiple-option setting

9k views Asked by At

I'm working on a project which is built with CMake, so I'm writing up a CMakeLists.txt for it. In this file I want to have a (cached) CMake variable that can only take one of several options (which I would specify somehow), rather than any arbitrary string. For simplicity, let's take it to be a string which can take "red", "green" or "blue" - but nothing else.

Can I achieve this with a recent CMake version, other than setting an arbitrary string then checking it for validity?

1

There are 1 answers

1
Florian On BEST ANSWER

The answer is to be found in one of Kitware's Blog Posts named "Constraining Values with ComboBoxes in CMake (cmake-gui)":

So here’s how it works: for each cache entry that you want to constrain to some set of values, define it as usual with a default value as a STRING cache variable, for example:

set(BaseName "binary" CACHE STRING "BaseName chosen by the user at CMake configure time")

Now, after defining the cache entry with its initial default value, define the set of strings to which its value should be constrained:

set_property(CACHE BaseName PROPERTY STRINGS binary octal decimal hexadecimal)

After the set_property call, cmake-gui knows to present a drop-down combo box for editing the “BaseName” cache entry, and it knows that the four valid choices for this entry that it should present are binary, octal, decimal and hexadecimal.