[CMake] Bug in if/else/endif in combination with option?

James Bigler jamesbigler at gmail.com
Mon Mar 30 13:55:48 EDT 2009


There may indeed be a bug (at least in the documentation).

In CMake 'if' statements are a little different than you might think.  From
the documentation of 'if' (cmake --help-command if):

        if(variable STREQUAL string)
        if(string STREQUAL string)

Now with your code you had:

foreach(opt ${options})
 message(STATUS "opt = ${opt}")
  if(${opt} STREQUAL fine)
 ...
endforeach()

Now what the expression states is 'if (${opt} STREQUAL fine)' or if (fine
STRQUAL fine).  ${opt} gets replaced by its value and you compare 'fine'
with 'fine'.  It might have appeared to work had you used opt instead of
${opt}, but you wouldn't have been able to turn the option(fine) on and
off.  The if statement would have always been false.

Things get tricky when the right side of the STREQUAL is a string that
matches the name of a variable:

set(var "stuff")
set(stuff ON)
if(var STREQUAL "stuff")
  message("var STREQUAL stuff")
else()
  message("NOT var STREQUAL stuff")
endif()

According to the docs, I would expect this to return "var STREQUAL ON", but
it instead returns "NOT var STREQUAL ON", meaning that it dereferenced the
value of stuff in the if statement rather than treating it as a string
literal.  This is inconsistent with the documentation that indicates that
right side operator is a string and not a variable.

To get what you want in your code, you need the following:

option(fine_on "Fine" OFF)
option(good_on "Good" ON)
option(bad_on "Bad" OFF) # This isn't actually used
foreach(opt ${options})
  message(STATUS "opt = ${opt}")\
 # Only accept opt == fine when fine_on is true
  if(fine_on AND opt STREQUAL "fine")
   message(STATUS "This is fine")
  # Only accept opt == good when good_on is true
 elseif(good_on AND opt STREQUAL "good")
    message(STATUS "This is good")
 # Everything else is bad
  else()
   message(ERROR " This is bad!")
  endif()
endforeach(opt ${options})

James

On Mon, Mar 30, 2009 at 4:28 AM, Marcel Loose <loose at astron.nl> wrote:
> Hi all,
>
> I am running cmake version 2.6-patch 2.
> I stumbled over the following, and I think it is a bug.
>
> If I run cmake on this CMakeLists.txt:
>
> cmake_minimum_required(VERSION 2.6)
> set(options
>  fine
>  good
>  bad)
> #option(fine "Fine" OFF)
> #option(good "Good" OFF)
> option(bad "Bad" OFF)
> foreach(opt ${options})
>  message(STATUS "opt = ${opt}")
>  if(${opt} STREQUAL fine)
>    message(STATUS "This is fine")
>  elseif(${opt} STREQUAL good)
>    message(STATUS "This is good")
>  else(${opt} STREQUAL fine)
>    message(FATAL_ERROR "This is bad!")
>  endif(${opt} STREQUAL fine)
> endforeach(opt ${options})
>
> I get the following output:
> ...
> -- val = fine
> -- fine
> -- val = good
> -- good
> -- val = bad
> CMake Error at CMakeLists.txt:14 (message):
>  bad
> ...
>
> which is to be expected.
>
> However, when I uncomment the line option(fine...), I get the following
> output:
> ...
> -- opt = fine
> -- This is fine
> -- opt = good
> -- This is good
> -- opt = bad
> -- This is fine
> -- Configuring done
> ...
>
> which is clearly wrong! Uncommenting the line option(good...) yields
> almost the same output, but now the elseif branch is followed.
>
> Is this a bug, or am I overlooking something?
>
> Best regards,
> Marcel Loose.
>
>
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the CMake FAQ at:
http://www.cmake.org/Wiki/CMake_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.cmake.org/mailman/listinfo/cmake
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20090330/805adaed/attachment.htm>


More information about the CMake mailing list