1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmIfCommand.h,v $
6 Date: $Date: 2009-06-12 17:25:50 $
7 Version: $Revision: 1.56 $
9 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
20 #include "cmCommand.h"
21 #include "cmFunctionBlocker.h"
23 /** \class cmIfFunctionBlocker
24 * \brief subclass of function blocker
28 class cmIfFunctionBlocker
: public cmFunctionBlocker
31 cmIfFunctionBlocker() {
32 this->HasRun
= false; this->ScopeDepth
= 0; }
33 virtual ~cmIfFunctionBlocker() {}
34 virtual bool IsFunctionBlocked(const cmListFileFunction
& lff
,
37 virtual bool ShouldRemove(const cmListFileFunction
& lff
,
40 std::vector
<cmListFileArgument
> Args
;
41 std::vector
<cmListFileFunction
> Functions
;
44 unsigned int ScopeDepth
;
47 /** \class cmIfCommand
48 * \brief starts an if block
50 * cmIfCommand starts an if block
52 class cmIfCommand
: public cmCommand
56 * This is a virtual constructor for the command.
58 virtual cmCommand
* Clone()
60 return new cmIfCommand
;
64 * This overrides the default InvokeInitialPass implementation.
65 * It records the arguments before expansion.
67 virtual bool InvokeInitialPass(const std::vector
<cmListFileArgument
>& args
,
71 * This is called when the command is first encountered in
72 * the CMakeLists.txt file.
74 virtual bool InitialPass(std::vector
<std::string
> const&,
75 cmExecutionStatus
&) { return false;};
78 * The name of the command as specified in CMakeList.txt.
80 virtual const char* GetName() { return "if";}
83 * Succinct documentation.
85 virtual const char* GetTerseDocumentation()
87 return "Conditionally execute a group of commands.";
91 * This determines if the command is invoked when in script mode.
93 virtual bool IsScriptable() { return true; }
98 virtual const char* GetFullDocumentation()
103 " COMMAND1(ARGS ...)\n"
104 " COMMAND2(ARGS ...)\n"
106 " elseif(expression2)\n"
107 " # elseif section.\n"
108 " COMMAND1(ARGS ...)\n"
109 " COMMAND2(ARGS ...)\n"
111 " else(expression)\n"
113 " COMMAND1(ARGS ...)\n"
114 " COMMAND2(ARGS ...)\n"
116 " endif(expression)\n"
117 "Evaluates the given expression. If the result is true, the commands "
118 "in the THEN section are invoked. Otherwise, the commands in the "
119 "else section are invoked. The elseif and else sections are "
120 "optional. You may have multiple elseif clauses. Note that "
121 "the expression in the else and endif clause is optional. Long "
122 "expressions can be used and there is a traditional order of "
124 "Parenthetical expressions are evaluated first followed by unary "
125 "operators such as EXISTS, COMMAND, and DEFINED. "
126 "Then any EQUAL, LESS, GREATER, STRLESS, STRGREATER, STREQUAL, MATCHES "
127 "will be evaluated. Then NOT operators and finally AND, OR operators "
128 "will be evaluated. Possible expressions are:\n"
130 "True if the variable's value is not empty, 0, N, NO, OFF, FALSE, "
131 "NOTFOUND, or <variable>-NOTFOUND.\n"
132 " if(NOT variable)\n"
133 "True if the variable's value is empty, 0, N, NO, OFF, FALSE, "
134 "NOTFOUND, or <variable>-NOTFOUND.\n"
135 " if(variable1 AND variable2)\n"
136 "True if both variables would be considered true individually.\n"
137 " if(variable1 OR variable2)\n"
138 "True if either variable would be considered true individually.\n"
139 " if(COMMAND command-name)\n"
140 "True if the given name is a command, macro or function that can be "
142 " if(POLICY policy-id)\n"
143 "True if the given name is an existing policy "
144 "(of the form CMP<NNNN>).\n"
145 " if(TARGET target-name)\n"
146 "True if the given name is an existing target, built or imported.\n"
147 " if(EXISTS file-name)\n"
148 " if(EXISTS directory-name)\n"
149 "True if the named file or directory exists. "
150 "Behavior is well-defined only for full paths.\n"
151 " if(file1 IS_NEWER_THAN file2)\n"
152 "True if file1 is newer than file2 or if one of the two files "
154 "Behavior is well-defined only for full paths.\n"
155 " if(IS_DIRECTORY directory-name)\n"
156 "True if the given name is a directory. "
157 "Behavior is well-defined only for full paths.\n"
158 " if(IS_ABSOLUTE path)\n"
159 "True if the given path is an absolute path.\n"
160 " if(variable MATCHES regex)\n"
161 " if(string MATCHES regex)\n"
162 "True if the given string or variable's value matches the given "
163 "regular expression.\n"
164 " if(variable LESS number)\n"
165 " if(string LESS number)\n"
166 " if(variable GREATER number)\n"
167 " if(string GREATER number)\n"
168 " if(variable EQUAL number)\n"
169 " if(string EQUAL number)\n"
170 "True if the given string or variable's value is a valid number and "
171 "the inequality or equality is true.\n"
172 " if(variable STRLESS string)\n"
173 " if(string STRLESS string)\n"
174 " if(variable STRGREATER string)\n"
175 " if(string STRGREATER string)\n"
176 " if(variable STREQUAL string)\n"
177 " if(string STREQUAL string)\n"
178 "True if the given string or variable's value is lexicographically "
179 "less (or greater, or equal) than the string on the right.\n"
180 " if(version1 VERSION_LESS version2)\n"
181 " if(version1 VERSION_EQUAL version2)\n"
182 " if(version1 VERSION_GREATER version2)\n"
183 "Component-wise integer version number comparison (version format is "
184 "major[.minor[.patch[.tweak]]]).\n"
185 " if(DEFINED variable)\n"
186 "True if the given variable is defined. It does not matter if the "
187 "variable is true or false just if it has been set.\n"
188 " if((expression) AND (expression OR (expression)))\n"
189 "The expressions inside the parenthesis are evaluated first and "
190 "then the remaining expression is evaluated as in the previous "
191 "examples. Where there are nested parenthesis the innermost are "
192 "evaluated as part of evaluating the expression "
193 "that contains them."
196 "The if statement was written fairly early in CMake's history "
197 "and it has some convenience features that are worth covering. "
198 "The if statement reduces operations until there is "
199 "a single remaining value, at that point if the case "
200 "insensitive value is: ON, 1, YES, TRUE, Y it returns true, if "
201 "it is OFF, 0, NO, FALSE, N, NOTFOUND, *-NOTFOUND, IGNORE it "
202 "will return false. \n"
204 "This is fairly reasonable. The convenience feature that sometimes "
205 "throws new authors is how CMake handles values that do not "
206 "match the true or false list. Those values are treated as "
207 "variables and are dereferenced even though they do not have "
208 "the required ${} syntax. This means that if you write\n"
212 "CMake will treat it as if you wrote \n"
216 "likewise if you write \n"
218 " if (fubar AND sol)\n"
220 "CMake will conveniently treat it as \n"
222 " if (\"${fubar}\" AND \"${sol}\")\n"
224 "The later is really the correct way to write it, but the "
225 "former will work as well. Only some operations in the if "
226 "statement have this special handling of arguments. The "
227 "specific details follow: \n"
229 "1) The left hand argument to MATCHES is first checked to see "
230 "if it is a defined variable, if so the variable's value is "
231 "used, otherwise the original value is used. \n"
233 "2) If the left hand argument to MATCHES is missing it returns "
234 "false without error \n"
236 "3) Both left and right hand arguments to LESS GREATER EQUAL "
237 "are independently tested to see if they are defined variables, "
238 "if so their defined values are used otherwise the original "
241 "4) Both left and right hand arguments to STRLESS STREQUAL "
242 "STRGREATER are independently tested to see if they are defined "
243 "variables, if so their defined values are used otherwise the "
244 "original value is used. \n"
246 "5) Both left and right hand argumemnts to VERSION_LESS "
247 "VERSION_EQUAL VERSION_GREATER are independently tested to see "
248 "if they are defined variables, if so their defined values are "
249 "used otherwise the original value is used. \n"
251 "6) The right hand argument to NOT is tested to see if it is a "
252 "boolean constant, if so the value is used, otherwise it is "
253 "assumed to be a variable and it is dereferenced. \n"
255 "7) The left and right hand arguments to AND OR are "
256 "independently tested to see if they are boolean constants, if "
257 "so they are used as such, otherwise they are assumed to be "
258 "variables and are dereferenced. \n"
262 // this is a shared function for both If and Else to determine if the
263 // arguments were valid, and if so, was the response true. If there is
264 // an error, the errorString will be set.
265 static bool IsTrue(const std::vector
<std::string
> &args
,
266 std::string
&errorString
, cmMakefile
*mf
,
267 cmake::MessageType
&status
);
269 // Get a definition from the makefile. If it doesn't exist,
270 // return the original string.
271 static const char* GetVariableOrString(const char* str
,
272 const cmMakefile
* mf
);
274 cmTypeMacro(cmIfCommand
, cmCommand
);