MantisBT - CMake
View Issue Details
0013376CMakeCMakepublic2012-07-04 22:592012-12-03 07:46
Bruce Cartland 
 
lowminoralways
closedno change required 
WindowsXP SP3
CMake 2.8.8 
 
0013376: For unset variables IF(${var}) and IF(NOT ${var}) both return FALSE which fails to work for predefined vars such as CYGWIN
(I'm a noob so please excuse if this is already report, not formatted correctly, or invalid :)

See steps to reproduce.
message(STATUS "CYGWIN = '${CYGWIN}'")
if(${CYGWIN})
    message(STATUS "CYGWIN set")
else()
    set(CYGWIN 0) #this "fixes" the problem and results in expected behaviour
    message(STATUS "CYGWIN not set")
endif()
if(NOT ${CYGWIN})
    message(STATUS "CYGWIN not set")
else()
    message(STATUS "CYGWIN set - or is it?")
endif()
The script in steps to reproduce results in the following output:

-- CYGWIN = ''
-- CYGWIN not set
-- CYGWIN set - or is it?

Seems to me that FALSE should be defined to include blank as well as NO, etc OR variables such as CYGWIN should be set to 0 and not left blank.
No tags attached.
Issue History
2012-07-04 22:59Bruce CartlandNew Issue
2012-07-04 23:02Bruce CartlandNote Added: 0029909
2012-07-09 09:37Brad KingNote Added: 0030002
2012-07-09 09:37Brad KingStatusnew => resolved
2012-07-09 09:37Brad KingResolutionopen => no change required
2012-12-03 07:46David ColeNote Added: 0031810
2012-12-03 07:46David ColeStatusresolved => closed

Notes
(0029909)
Bruce Cartland   
2012-07-04 23:02   
Oops! Sorry!

The line in the script steps to reproduce
   set(CYGWIN 0) #this "fixes" the problem and results in expected behaviour
should be commented out
   #set(CYGWIN 0) #this "fixes" the problem and results in expected behaviour

to get the output I listed.
(Can't edit the original text).
(0030002)
Brad King   
2012-07-09 09:37   
When CYGWIN is not set the code

 if(NOT ${CYGWIN})

expands to just

 if(NOT)

which is somewhat non-sensical but evaluates to false. Using double quotes ensures a value is treated as a single argument:

 if(NOT "")

is true.

Anyway, to test variables that may or may not be set just use the name of the variable:

 if(CYGWIN)
   message(STATUS "This is Cygwin")
 else()
   message(STATUS "This is not Cygwin")
 endif()

Please see full behavior of if() in the reference documentation:

 http://www.cmake.org/cmake/help/v2.8.8/cmake.html#command:if [^]

and/or join the mailing list to ask for further help:

 http://www.cmake.org/mailman/listinfo/cmake [^]
(0031810)
David Cole   
2012-12-03 07:46   
Closing resolved issues that have not been updated in more than 4 months.