CMake multiple-option setting
Asked Answered
R

1

20

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?

Rightness answered 2/12, 2017 at 18:37 Comment(2)
Can you please give more details about what you are trying to do? Are you delivering a CMake enabled project and want to limit the values of an option() possibly selectable in cmake-gui? Are you writing a CMake helper/wrapper function and you want to restrict the values of a passed parameter? Are you trying to prevent a set() call to a certain variable to get any other then a pre-defined set of values?Pulchi
@Florian: See edit. And, yes, I'd like something like a list to appear when I run ccmake (or cmake-gui).Rightness
P
27

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.

Pulchi answered 2/12, 2017 at 21:22 Comment(1)
Is there a way to provide empty string as an option?Confectionery

© 2022 - 2024 — McMap. All rights reserved.