[CMake] BOOL type

Brandon Van Every bvanevery at gmail.com
Mon Dec 17 09:17:05 EST 2007


I propose the addition of a BOOL type to the CMake language.

  bool(variable [value])

would declare a variable of type BOOL, with an optional value
supplied.  Any SET commands performed on the variable in its scope
would be subject to boolean type constraint.  A BOOL can take on the
following values.  Any other assignment is an error.

  "TRUE" class - TRUE, 1, Y, Yes, YES, y, yes, ON
  "FALSE" class - FALSE, 0, N, n, No, NO, OFF, <empty>

if(bool_var) would succeed if bool_var is set to a member of the TRUE
class.  It would fail if set to a member of the FALSE class.  Equality
comparison would be by boolean class, not the specific boolean value.
The following code succeeds:

  set(bool_var ON)
  [...]
  if(bool_var EQUAL Yes)
    # this code is executed

The above functionality could be added without affecting legacy CMake
script in any way.

In addition, a variable CMAKE_REQUIRE_BOOL, or some other appropriate
interface, would be provided.  If set(CMAKE_REQUIRE_BOOL TRUE) is
performed, then instead of if(some_string) failing when some_string
has a value of:

  empty, 0, N, NO, OFF, FALSE, NOTFOUND, or <variable>-NOTFOUND

it fails when some_string has a value of:

  empty, 0, NOTFOUND, or <variable>-NOTFOUND

The intent is to reduce the level of co-option / pollution in string
processing.  Under the current rules, it is fairly easy for a regex to
inadvertently fail:

set(sillystring "The rain in spain")
string(REGEX MATCH
  ".$"
  lastchar "${sillystring}")
if(NOT lastchar)
  message("Zero length string")
endif(NOT lastchar)

C:\devel\src\cbugs\trueorfalse>cmake -P trueorfalse.cmake
Zero length string

NOTFOUND and <variable>-NOTFOUND are considered acceptable string
pollutions, because they are significantly longer and unlikely to be
matched in typical string processing.  "0" is problematic, as it is a
single character like "n".  A similar proposal for a NUMBER data type,
and CMAKE_REQUIRE_NUMBER, could be used to eliminate the problem of
the "0".


Cheers,
Brandon Van Every


More information about the CMake mailing list