MantisBT - CMake
View Issue Details
0010773CMakeCMakepublic2010-05-28 13:012011-01-31 16:01
Modestas Vainius 
Brad King 
normalminoralways
closedfixed 
amd64Debian GNU/Linuxsid
CMake-2-8 
CMake 2.8.4CMake 2.8.4 
0010773: IF() command behaviour contradicts examples in documentation
Exerpt from `cmake --help-command if`
---------------------------------------------------

 The convenience feature that sometimes
  throws new authors is how CMake handles values that do not match the
  true or false list. Those values are treated as variables and are
 dereferenced even though they do not have the required ${} syntax.
 This means that if you write

         if (boobah)

 CMake will treat it as if you wrote

         if (${boobah})

 likewise if you write

         if (fubar AND sol)

 CMake will conveniently treat it as

         if ("${fubar}" AND "${sol}")

 The later is really the correct way to write it, but the former will
 work as well. Only some operations in the if statement have this
 special handling of arguments.
----------------------------------------

Then why almost straight copy&paste code below produces different results for each pair (see Testcase: lines) when variables are set to a string value? And why is "why_is_this_string_false" constant false? cmake appears to treat "why_is_this_string_false" as variable which is really unintuitive but maybe somewhat deductable from documentation.

So there is either a bug in cmake or in its documentation.
project(foo)
cmake_minimum_required(VERSION 2.6)

set(boobah "str")
set(fubar "str")
set(sol "str")

if (boobah)
    MESSAGE(STATUS "Testcase: boobah without brackets")
endif(boobah)

if (${boobah})
    MESSAGE(STATUS "Testcase: boobah with brackets")
endif (${boobah})

if (fubar AND sol)
    MESSAGE(STATUS "Testcase: fubar sol without brackets")
endif (fubar AND sol)

if ("${fubar}" AND "${sol}")
    MESSAGE(STATUS "Testcase: fubar sol with brackets")
endif ("${fubar}" AND "${sol}")

if ("why_is_this_string_false")
    MESSAGE(STATUS "Testcase: no, why_is_this_string_false is NOT false")
endif ("why_is_this_string_false")
$ rm CMakeCache.txt -f && cmake .
-- The C compiler identification is GNU
-- The CXX compiler identification is GNU
-- Check for working C compiler: /mnt/sda2/usr/bin/gcc
-- Check for working C compiler: /mnt/sda2/usr/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /mnt/sda2/usr/bin/c++
-- Check for working CXX compiler: /mnt/sda2/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Testcase: boobah without brackets
-- Testcase: fubar sol without brackets
-- Configuring done
-- Generating done
-- Build files have been written to: /home/modax/test
No tags attached.
related to 0009123closed Ken Martin Conditionals do not behave as expected from documentation (IF, ENDIF, ELSE) 
has duplicate 0011701closed Brad King Check condition evaluation for a passed variable 
Issue History
2010-05-28 13:01Modestas VainiusNew Issue
2010-05-31 02:15Rolf Eike BeerNote Added: 0020870
2010-05-31 03:21Modestas VainiusNote Added: 0020871
2010-05-31 07:33Rolf Eike BeerNote Added: 0020872
2011-01-14 17:20Brad KingRelationship addedhas duplicate 0011701
2011-01-14 17:22Brad KingAssigned To => Brad King
2011-01-14 17:22Brad KingStatusnew => assigned
2011-01-14 17:33Brad KingNote Added: 0024697
2011-01-14 17:35Brad KingRelationship addedrelated to 0009123
2011-01-14 18:30Brad KingNote Added: 0024698
2011-01-14 18:30Brad KingStatusassigned => closed
2011-01-14 18:30Brad KingResolutionopen => fixed
2011-01-31 16:01David ColeFixed in Version => CMake 2.8.4
2011-01-31 16:01David ColeTarget Version => CMake 2.8.4

Notes
(0020870)
Rolf Eike Beer   
2010-05-31 02:15   
Please add your CMakeLists.txt.
(0020871)
Modestas Vainius   
2010-05-31 03:21   
It's in "Steps to Reproduce" section.
(0020872)
Rolf Eike Beer   
2010-05-31 07:33   
Ah, sorry, that one is not shown by default ;)
(0024697)
Brad King   
2011-01-14 17:33   
Ugh, the phrase "CMake will treat it as if you wrote" is very misleading and needs to be corrected. The logic implemented by the command predates the ${} evaluation syntax (we're talking *ancient* history here) so it does some magic evaluation. This portion of the documentation was added by this commit:

  http://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=a73071ca#patch2 [^]

in part to address issue 0009123. That commit was a bit buggy itself so another commit was made to fix it:

  http://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=cb185d93#patch2 [^]

The latter commit clearly and correctly specifies the behavior in the uppor portion of the documentation but did not change the bad phrasing added by the former commit.
(0024698)
Brad King   
2011-01-14 18:30   
This should do it:

http://cmake.org/gitweb?p=cmake.git;a=commitdiff;h=e4e14e85 [^]