[CMake] Feature wish - constrained string values for configuration options

Robert Haines rhaines at manchester.ac.uk
Thu Jul 24 13:30:18 EDT 2008


On 18 Jul 2008, at 18:40, Bill Hoffman wrote:
> Alexander Neundorf wrote:
>> On Friday 18 July 2008, Michael Wild wrote:
>>> On 18Jul, 2008, at 12:27, Robert Haines wrote:
>>>>> I would like to point at my feature wish:
>>>>> http://www.vtk.org/Bug/view.php?id=7313
>>>>>
>>>>> Opinions?
>>>> I was looking for this functionality the other day, I think it's a
>>>> good idea. I had thought that it was already in there for the
>>>> CMAKE_BUILD_TYPE option that you always get but that seems to be
>>>> implemented in a different way.
>>>>
>>>> So +1 from me. Is there anyway I can reflect that in the bug  
>>>> tracker?
>>> That would be a very desirable feature. I suggest this be split even
>>> further: options with fixed values, like what you get from drop-down
>>> menus and options with a set of "proposals", as you get in a combo- 
>>> box.
>>>
>>> definitely +1
>> This seems to be a duplicate of http://public.kitware.com/Bug/view.php?id=1527 
>>  , which is a duplicate of bug 39 according to the note, but I  
>> can't find bug 39 anymore :-/
>
> Yup, this is a very old feature request...
>
> However, it is not that easy to implement, since the basic type in  
> CMake is a string.  There is no way to constrain the value of a  
> variable, or even store more information about a variable.

I decided to knock something up using macros that does what I need. It  
obviously doesn't constrain choices in a UI or provide a drop down  
menu but it does raise an error if the user tries to configure a  
project with a value set outside of a list of allowed values. It does  
enough for me for now and hopefully someone else will find it useful.

There are two versions. My first attempt needs you to define your  
variable first:
set(TEST_OPT "None" CACHE STRING "Some Documentation")
then call the assert_option macro like this:
assert_option(${TEST_OPT} "TEST_OPT" "None, One, Two, Three")

But I felt that was a little ungainly so my second attempt is called  
like this (without a prior "set"):
select_option(VARIABLE INITIAL-VALUE ALLOWED-VALUE-LIST DOCUMENTATION)
example:
select_option(TEST_OPT "None" "None, One, Two, Three" "Some  
Documentation")

You can grab it straight from my git repository here: http://git.kato.mvc.mcc.ac.uk/zzcgurh/option-macros.git/

If you just want a tarball use the "snapshot" link on that page - you  
want the line with the yellow "v-1.0" tag on it.

Once you've got it (and un-tarred it) you can just point ccmake at the  
directory as it has a full top-level CMakeLists.txt which uses both  
forms of the macro.

I'd like to hear if people find it useful!

Cheers,
Rob



More information about the CMake mailing list