[CMake] How to handle options with more than two possible values?

Petr Kmoch petr.kmoch at gmail.com
Thu Oct 13 07:41:00 EDT 2016


Hi.

option() is a handy shortcut for boolean options, but it's little more than
syntactic sugar for a cache variable of type BOOL. To create a tristate
variable, you can do this:

  set(ENABLE_SOMETHING AUTO CACHE STRING "Enable SOMETHING support")  #
create the variable
  set_property(CACHE ENABLE_SOMETHING PROPERTY STRINGS AUTO ON OFF)  #
define list of values GUI will offer for the variable

Then, you can test the variable like this:

  if(ENABLE_SOMETHING STREQUAL "AUTO")
    # AUTO was used
  elseif(ENABLE_SOMETHING)
    # a true value (such as ON) was used
  else()
    # a false value (such as OFF) was used
  endif()

Hope this helps.

Petr

On 13 October 2016 at 13:32, YuGiOhJCJ Mailing-List via CMake <
cmake at cmake.org> wrote:

> Hello,
>
> Regarding the cmake-commands manual [1], the "option" command seems to
> take as argument a boolean constant that can have the two possible values:
> ON or OFF.
>
> I would like to use three possible values for an option: AUTO (as default
> value), ON or OFF.
>
> Example:
> option(ENABLE_SOMETHING "Enable SOMETHING support" AUTO)
>
> But of course this example is wrong because "AUTO" is not ON or OFF.
>
> Regarding the cmake-commands manual [2], the "if" command seems to return
> TRUE when the boolean constant is 1, ON, YES, TRUE, Y, or a non-zero number.
> It returns FALSE when the boolean constant is 0, OFF, NO, FALSE, N,
> IGNORE, NOTFOUND, the empty string, or ends in the suffix -NOTFOUND.
>
> I would like to check the value myself.
>
> Example:
> if(ENABLE_SOMETHING=AUTO)
>         message(STATUS "ENABLE_SOMETHING=AUTO")
> endif()
> if(ENABLE_SOMETHING=ON)
>         message(STATUS "ENABLE_SOMETHING=ON")
> endif()
> if(ENABLE_SOMETHING=OFF)
>         message(STATUS "ENABLE_SOMETHING=OFF")
> endif()
>
> But of course this example is wrong because "=" is not accepted for an
> expression in a "if" command.
>
> So my question is: How to handle options with more than two possible
> values?
>
> Thank you.
> Best regards.
>
> [1] https://cmake.org/cmake/help/v3.7/command/option.html
> [2] https://cmake.org/cmake/help/v3.7/command/if.html
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more
> information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at http://www.kitware.com/
> opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> http://public.kitware.com/mailman/listinfo/cmake
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20161013/55637305/attachment-0001.html>


More information about the CMake mailing list