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

Marcel Loose loose at astron.nl
Tue Mar 31 03:31:11 EDT 2009


Hi James,

Thanks for your reply. I'm going to comment on your remarks inline. But
first, let me explain what I wanted to accomplish. 

My intention is to keep a list of valid CMake options so that I can
iterate over it. It would have been nice if CMake offered another way of
doing this, but I haven't found one. So, that's the reason for having
the foreach() loop; the variable names being identical to options is in
this case intentional!

On Mon, 2009-03-30 at 11:55 -0600, James Bigler wrote:
> 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.
I agree with the first part of your paragraph, but you've lost me in the
second part. The string should match, that's what I expect, because I
compare the contents of opt to the string "fine". I don't see why I
should have written 'if(opt STREQUAL fine)', because that would IMO have
compared the string "opt" to the string "fine". I also don't see the
connection of the string "fine" with the option fine (since an option is
nothing more than a cached variable of type bool).

> 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.
Well, I would expect it to return "var" STREQUAL "ON" which evaluates to
false, and hence the else branch is followed. Are you assuming that var
would be converted to bool, and the string "stuff" to the contents of
the variable "stuff"? I don't think that's how it's supposed to work.
To access the contents of a variable, you need to enclose it in ${}. So,
had you written ${var} STREQUAL ${stuff}, it would translate IMO to
"stuff" STREQUAL "ON" (which also evaluates to false BTW).

> 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})
This will probably work, but that's not what I wanted (see above).
However, here you're doing something completely different: you're
creating a boolean expression consisting of a bool on the LHS of AND and
a string comparison (which evaluates to a bool) on the RHS of AND.

I still think it's a bug in the code, not in the documentation.

Best regards,
Marcel Loose.


> 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
> >
> 
> _______________________________________________
> 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



More information about the CMake mailing list