if¶
Conditionally execute a group of commands.
Synopsis¶
if(<condition>) <commands>elseif(<condition>) # optional block, can be repeated <commands>else() # optional block <commands>endif()
Evaluates the condition
argument of the if
clause according to theCondition syntax described below. If the result is true, then thecommands
in the if
block are executed.Otherwise, optional elseif
blocks are processed in the same way.Finally, if no condition
is true, commands
in the optional else
block are executed.
Per legacy, the else()
and endif()
commands admitan optional <condition>
argument.If used, it must be a verbatimrepeat of the argument of the openingif
command.
Condition Syntax¶
The following syntax applies to the condition
argument ofthe if
, elseif
and while()
clauses.
Compound conditions are evaluated in the following order of precedence:Innermost parentheses are evaluated first. Next come unary tests suchas EXISTS
, COMMAND
, and DEFINED
. Then binary tests such asEQUAL
, LESS
, LESS_EQUAL
, GREATER
, GREATER_EQUAL
,STREQUAL
, STRLESS
, STRLESS_EQUAL
, STRGREATER
,STRGREATER_EQUAL
, VERSION_EQUAL
, VERSION_LESS
,VERSION_LESS_EQUAL
, VERSION_GREATER
, VERSION_GREATER_EQUAL
,and MATCHES
. Then the boolean operators in the order NOT
, AND
,and finally OR
.
Possible conditions are:
if(<constant>)
True if the constant is
1
,ON
,YES
,TRUE
,Y
,or a non-zero number. False if the constant is0
,OFF
,NO
,FALSE
,N
,IGNORE
,NOTFOUND
, the empty string,or ends in the suffix-NOTFOUND
. Named boolean constants arecase-insensitive. If the argument is not one of these specificconstants, it is treated as a variable or string and the followingsignature is used.if(<variable|string>)
True if given a variable that is defined to a value that is not a falseconstant. False otherwise. (Note macro arguments are not variables.)
if(NOT <condition>)
True if the condition is not true.
if(<cond1> AND <cond2>)
True if both conditions would be considered true individually.
if(<cond1> OR <cond2>)
True if either condition would be considered true individually.
if(COMMAND command-name)
True if the given name is a command, macro or function that can beinvoked.
if(POLICY policy-id)
True if the given name is an existing policy (of the form
CMP<NNNN>
).if(TARGET target-name)
True if the given name is an existing logical target name createdby a call to the
add_executable()
,add_library()
,oradd_custom_target()
command that has already been invoked(in any directory).if(TEST test-name)
True if the given name is an existing test name created by the
add_test()
command.if(EXISTS path-to-file-or-directory)
True if the named file or directory exists. Behavior is well-definedonly for full paths. Resolves symbolic links, i.e. if the named file ordirectory is a symbolic link, returns true if the target of thesymbolic link exists.
if(file1 IS_NEWER_THAN file2)
True if
file1
is newer thanfile2
or if one of the two files doesn’texist. Behavior is well-defined only for full paths. If the filetime stamps are exactly the same, anIS_NEWER_THAN
comparison returnstrue, so that any dependent build operations will occur in the eventof a tie. This includes the case of passing the same file name forboth file1 and file2.if(IS_DIRECTORY path-to-directory)
True if the given name is a directory. Behavior is well-defined onlyfor full paths.
if(IS_SYMLINK file-name)
True if the given name is a symbolic link. Behavior is well-definedonly for full paths.
if(IS_ABSOLUTE path)
True if the given path is an absolute path.
if(<variable|string> MATCHES regex)
True if the given string or variable’s value matches the given regularcondition. See Regex Specification for regex format.
()
groups are captured inCMAKE_MATCH_<n>
variables.if(<variable|string> LESS <variable|string>)
True if the given string or variable’s value is a valid number and lessthan that on the right.
if(<variable|string> GREATER <variable|string>)
True if the given string or variable’s value is a valid number and greaterthan that on the right.
if(<variable|string> EQUAL <variable|string>)
True if the given string or variable’s value is a valid number and equalto that on the right.
if(<variable|string> LESS_EQUAL <variable|string>)
True if the given string or variable’s value is a valid number and lessthan or equal to that on the right.
if(<variable|string> GREATER_EQUAL <variable|string>)
True if the given string or variable’s value is a valid number and greaterthan or equal to that on the right.
if(<variable|string> STRLESS <variable|string>)
True if the given string or variable’s value is lexicographically lessthan the string or variable on the right.
if(<variable|string> STRGREATER <variable|string>)
True if the given string or variable’s value is lexicographically greaterthan the string or variable on the right.
if(<variable|string> STREQUAL <variable|string>)
True if the given string or variable’s value is lexicographically equalto the string or variable on the right.
if(<variable|string> STRLESS_EQUAL <variable|string>)
True if the given string or variable’s value is lexicographically lessthan or equal to the string or variable on the right.
if(<variable|string> STRGREATER_EQUAL <variable|string>)
True if the given string or variable’s value is lexicographically greaterthan or equal to the string or variable on the right.
if(<variable|string> VERSION_LESS <variable|string>)
Component-wise integer version number comparison (version format is
major[.minor[.patch[.tweak]]]
, omitted components are treated as zero).Any non-integer version component or non-integer trailing part of a versioncomponent effectively truncates the string at that point.if(<variable|string> VERSION_GREATER <variable|string>)
Component-wise integer version number comparison (version format is
major[.minor[.patch[.tweak]]]
, omitted components are treated as zero).Any non-integer version component or non-integer trailing part of a versioncomponent effectively truncates the string at that point.if(<variable|string> VERSION_EQUAL <variable|string>)
Component-wise integer version number comparison (version format is
major[.minor[.patch[.tweak]]]
, omitted components are treated as zero).Any non-integer version component or non-integer trailing part of a versioncomponent effectively truncates the string at that point.if(<variable|string> VERSION_LESS_EQUAL <variable|string>)
Component-wise integer version number comparison (version format is
major[.minor[.patch[.tweak]]]
, omitted components are treated as zero).Any non-integer version component or non-integer trailing part of a versioncomponent effectively truncates the string at that point.if(<variable|string> VERSION_GREATER_EQUAL <variable|string>)
Component-wise integer version number comparison (version format is
major[.minor[.patch[.tweak]]]
, omitted components are treated as zero).Any non-integer version component or non-integer trailing part of a versioncomponent effectively truncates the string at that point.if(<variable|string> IN_LIST <variable>)
True if the given element is contained in the named list variable.
if(DEFINED <name>|CACHE{<name>}|ENV{<name>})
True if a variable, cache variable or environment variablewith given
<name>
is defined. The value of the variabledoes not matter. Note that macro arguments are not variables.if((condition) AND (condition OR (condition)))
The conditions inside the parenthesis are evaluated first and thenthe remaining condition is evaluated as in the previous examples.Where there are nested parenthesis the innermost are evaluated as partof evaluating the condition that contains them.
Variable Expansion¶
The if command was written very early in CMake’s history, predatingthe ${}
variable evaluation syntax, and for convenience evaluatesvariables named by its arguments as shown in the above signatures.Note that normal variable evaluation with ${}
applies before the ifcommand even receives the arguments. Therefore code like
set(var1 OFF)set(var2 "var1")if(${var2})
appears to the if command as
if(var1)
and is evaluated according to the if(<variable>)
case documentedabove. The result is OFF
which is false. However, if we remove the${}
from the example then the command sees
if(var2)
which is true because var2
is defined to var1
which is not a falseconstant.
Automatic evaluation applies in the other cases whenever theabove-documented condition syntax accepts <variable|string>
:
The left hand argument to
MATCHES
is first checked to see if it isa defined variable, if so the variable’s value is used, otherwise theoriginal value is used.If the left hand argument to
MATCHES
is missing it returns falsewithout errorBoth left and right hand arguments to
LESS
,GREATER
,EQUAL
,LESS_EQUAL
, andGREATER_EQUAL
, are independently tested to see ifthey are defined variables, if so their defined values are used otherwisethe original value is used.Both left and right hand arguments to
STRLESS
,STRGREATER
,STREQUAL
,STRLESS_EQUAL
, andSTRGREATER_EQUAL
are independentlytested to see if they are defined variables, if so their defined values areused otherwise the original value is used.Both left and right hand arguments to
VERSION_LESS
,VERSION_GREATER
,VERSION_EQUAL
,VERSION_LESS_EQUAL
, andVERSION_GREATER_EQUAL
are independently tested to see if they are definedvariables, if so their defined values are used otherwise the original valueis used.The right hand argument to
NOT
is tested to see if it is a booleanconstant, if so the value is used, otherwise it is assumed to be avariable and it is dereferenced.The left and right hand arguments to
AND
andOR
are independentlytested to see if they are boolean constants, if so they are used assuch, otherwise they are assumed to be variables and are dereferenced.
To prevent ambiguity, potential variable or keyword names can bespecified in a Quoted Argument or a Bracket Argument.A quoted or bracketed variable or keyword will be interpreted as astring and not dereferenced or interpreted.See policy CMP0054
.
There is no automatic evaluation for environment or cacheVariable References. Their values must be referenced as$ENV{<name>}
or $CACHE{<name>}
wherever the above-documentedcondition syntax accepts <variable|string>
.