1 /*============================================================================
2 CMake - Cross Platform Makefile Generator
3 Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
5 Distributed under the OSI-approved BSD License (the "License");
6 see accompanying file Copyright.txt for details.
8 This software is distributed WITHOUT ANY WARRANTY; without even the
9 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License for more information.
11 ============================================================================*/
15 #include "cmCommand.h"
16 #include "cmFunctionBlocker.h"
18 class cmIfFunctionBlocker : public cmFunctionBlocker
21 cmIfFunctionBlocker() {
22 this->HasRun = false; this->ScopeDepth = 0; }
23 virtual ~cmIfFunctionBlocker() {}
24 virtual bool IsFunctionBlocked(const cmListFileFunction& lff,
27 virtual bool ShouldRemove(const cmListFileFunction& lff,
30 std::vector<cmListFileArgument> Args;
31 std::vector<cmListFileFunction> Functions;
34 unsigned int ScopeDepth;
37 /// Starts an if block
38 class cmIfCommand : public cmCommand
42 * This is a virtual constructor for the command.
44 virtual cmCommand* Clone()
46 return new cmIfCommand;
50 * This overrides the default InvokeInitialPass implementation.
51 * It records the arguments before expansion.
53 virtual bool InvokeInitialPass(const std::vector<cmListFileArgument>& args,
57 * This is called when the command is first encountered in
58 * the CMakeLists.txt file.
60 virtual bool InitialPass(std::vector<std::string> const&,
61 cmExecutionStatus &) { return false;};
64 * The name of the command as specified in CMakeList.txt.
66 virtual const char* GetName() const { return "if";}
69 * Succinct documentation.
71 virtual const char* GetTerseDocumentation() const
73 return "Conditionally execute a group of commands.";
77 * This determines if the command is invoked when in script mode.
79 virtual bool IsScriptable() const { return true; }
84 virtual const char* GetFullDocumentation() const
89 " COMMAND1(ARGS ...)\n"
90 " COMMAND2(ARGS ...)\n"
92 " elseif(expression2)\n"
93 " # elseif section.\n"
94 " COMMAND1(ARGS ...)\n"
95 " COMMAND2(ARGS ...)\n"
99 " COMMAND1(ARGS ...)\n"
100 " COMMAND2(ARGS ...)\n"
102 " endif(expression)\n"
103 "Evaluates the given expression. If the result is true, the commands "
104 "in the THEN section are invoked. Otherwise, the commands in the "
105 "else section are invoked. The elseif and else sections are "
106 "optional. You may have multiple elseif clauses. Note that "
107 "the expression in the else and endif clause is optional. Long "
108 "expressions can be used and there is a traditional order of "
110 "Parenthetical expressions are evaluated first followed by unary "
111 "operators such as EXISTS, COMMAND, and DEFINED. "
112 "Then any EQUAL, LESS, GREATER, STRLESS, STRGREATER, STREQUAL, MATCHES "
113 "will be evaluated. Then NOT operators and finally AND, OR operators "
114 "will be evaluated. Possible expressions are:\n"
116 "True if the constant is 1, ON, YES, TRUE, Y, or a non-zero number. "
117 "False if the constant is 0, OFF, NO, FALSE, N, IGNORE, \"\", "
118 "or ends in the suffix '-NOTFOUND'. "
119 "Named boolean constants are case-insensitive. "
120 "If the argument is not one of these constants, "
121 "it is treated as a variable:"
124 "True if the variable is defined to a value that is not a false "
125 "constant. False otherwise. "
126 "(Note macro arguments are not variables.)"
128 " if(NOT <expression>)\n"
129 "True if the expression is not true."
131 " if(<expr1> AND <expr2>)\n"
132 "True if both expressions would be considered true individually."
134 " if(<expr1> OR <expr2>)\n"
135 "True if either expression would be considered true individually."
137 " if(COMMAND command-name)\n"
138 "True if the given name is a command, macro or function that can be "
140 " if(POLICY policy-id)\n"
141 "True if the given name is an existing policy "
142 "(of the form CMP<NNNN>).\n"
143 " if(TARGET target-name)\n"
144 "True if the given name is an existing target, built or imported.\n"
145 " if(EXISTS file-name)\n"
146 " if(EXISTS directory-name)\n"
147 "True if the named file or directory exists. "
148 "Behavior is well-defined only for full paths.\n"
149 " if(file1 IS_NEWER_THAN file2)\n"
150 "True if file1 is newer than file2 or if one of the two files "
152 "Behavior is well-defined only for full paths.\n"
153 " if(IS_DIRECTORY directory-name)\n"
154 "True if the given name is a directory. "
155 "Behavior is well-defined only for full paths.\n"
156 " if(IS_SYMLINK file-name)\n"
157 "True if the given name is a symbolic link. "
158 "Behavior is well-defined only for full paths.\n"
159 " if(IS_ABSOLUTE path)\n"
160 "True if the given path is an absolute path.\n"
161 " if(<variable|string> MATCHES regex)\n"
162 "True if the given string or variable's value matches the given "
163 "regular expression.\n"
164 " if(<variable|string> LESS <variable|string>)\n"
165 " if(<variable|string> GREATER <variable|string>)\n"
166 " if(<variable|string> EQUAL <variable|string>)\n"
167 "True if the given string or variable's value is a valid number and "
168 "the inequality or equality is true.\n"
169 " if(<variable|string> STRLESS <variable|string>)\n"
170 " if(<variable|string> STRGREATER <variable|string>)\n"
171 " if(<variable|string> STREQUAL <variable|string>)\n"
172 "True if the given string or variable's value is lexicographically "
173 "less (or greater, or equal) than the string or variable on the right.\n"
174 " if(<variable|string> VERSION_LESS <variable|string>)\n"
175 " if(<variable|string> VERSION_EQUAL <variable|string>)\n"
176 " if(<variable|string> VERSION_GREATER <variable|string>)\n"
177 "Component-wise integer version number comparison (version format is "
178 "major[.minor[.patch[.tweak]]]).\n"
179 " if(DEFINED <variable>)\n"
180 "True if the given variable is defined. It does not matter if the "
181 "variable is true or false just if it has been set.\n"
182 " if((expression) AND (expression OR (expression)))\n"
183 "The expressions inside the parenthesis are evaluated first and "
184 "then the remaining expression is evaluated as in the previous "
185 "examples. Where there are nested parenthesis the innermost are "
186 "evaluated as part of evaluating the expression "
187 "that contains them."
190 "The if command was written very early in CMake's history, predating "
191 "the ${} variable evaluation syntax, and for convenience evaluates "
192 "variables named by its arguments as shown in the above signatures. "
193 "Note that normal variable evaluation with ${} applies before the "
194 "if command even receives the arguments. "
195 "Therefore code like\n"
197 " set(var2 \"var1\")\n"
199 "appears to the if command as\n"
201 "and is evaluated according to the if(<variable>) case "
203 "The result is OFF which is false. "
204 "However, if we remove the ${} from the example then the command sees\n"
206 "which is true because var2 is defined to \"var1\" which is not "
209 "Automatic evaluation applies in the other cases whenever the "
210 "above-documented signature accepts <variable|string>:\n"
212 "1) The left hand argument to MATCHES is first checked to see "
213 "if it is a defined variable, if so the variable's value is "
214 "used, otherwise the original value is used. \n"
216 "2) If the left hand argument to MATCHES is missing it returns "
217 "false without error \n"
219 "3) Both left and right hand arguments to LESS GREATER EQUAL "
220 "are independently tested to see if they are defined variables, "
221 "if so their defined values are used otherwise the original "
224 "4) Both left and right hand arguments to STRLESS STREQUAL "
225 "STRGREATER are independently tested to see if they are defined "
226 "variables, if so their defined values are used otherwise the "
227 "original value is used. \n"
229 "5) Both left and right hand argumemnts to VERSION_LESS "
230 "VERSION_EQUAL VERSION_GREATER are independently tested to see "
231 "if they are defined variables, if so their defined values are "
232 "used otherwise the original value is used. \n"
234 "6) The right hand argument to NOT is tested to see if it is a "
235 "boolean constant, if so the value is used, otherwise it is "
236 "assumed to be a variable and it is dereferenced. \n"
238 "7) The left and right hand arguments to AND OR are "
239 "independently tested to see if they are boolean constants, if "
240 "so they are used as such, otherwise they are assumed to be "
241 "variables and are dereferenced. \n"
245 // this is a shared function for both If and Else to determine if the
246 // arguments were valid, and if so, was the response true. If there is
247 // an error, the errorString will be set.
248 static bool IsTrue(const std::vector<std::string> &args,
249 std::string &errorString, cmMakefile *mf,
250 cmake::MessageType &status);
252 // Get a definition from the makefile. If it doesn't exist,
253 // return the original string.
254 static const char* GetVariableOrString(const char* str,
255 const cmMakefile* mf);
257 cmTypeMacro(cmIfCommand, cmCommand);