ENH: mark some vars as advanced (and resort the list)
[cmake.git] / Source / cmIfCommand.cxx
blob304145c5c312e1b36e48880455a01e00d5423314
1 /*=========================================================================
3 Program: Insight Segmentation & Registration Toolkit
4 Module: $RCSfile: cmIfCommand.cxx,v $
5 Language: C++
6 Date: $Date: 2002-10-07 13:16:31 $
7 Version: $Revision: 1.36 $
9 Copyright (c) 2002 Insight Consortium. All rights reserved.
10 See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm 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 =========================================================================*/
17 #include "cmIfCommand.h"
19 bool cmIfFunctionBlocker::
20 IsFunctionBlocked(const char *name, const std::vector<std::string> &args,
21 cmMakefile &mf)
23 // always let if statements through
24 if (!strcmp(name,"IF"))
26 return false;
29 // watch for our ELSE or ENDIF
30 if (!strcmp(name,"ELSE") || !strcmp(name,"ENDIF"))
32 if (args == m_Args)
34 // if it was an else statement then we should change state
35 // and block this Else Command
36 if (!strcmp(name,"ELSE"))
38 m_IsBlocking = !m_IsBlocking;
39 return true;
41 // otherwise it must be an ENDIF statement, in that case remove the
42 // function blocker
43 mf.RemoveFunctionBlocker("ENDIF",args);
44 return true;
46 else if(args.empty())
48 std::string err = "Empty arguments for ";
49 err += name;
50 err += ". Did you mean ";
51 err += name;
52 err += "( ";
53 for(std::vector<std::string>::const_iterator a = m_Args.begin();
54 a != m_Args.end();++a)
56 err += *a;
57 err += " ";
59 err += ")?";
60 cmSystemTools::Error(err.c_str());
63 return m_IsBlocking;
66 bool cmIfFunctionBlocker::
67 ShouldRemove(const char *name, const std::vector<std::string> &args,
68 cmMakefile &)
70 if (!strcmp(name,"ELSE") || !strcmp(name,"ENDIF"))
72 if (args == m_Args)
74 return true;
77 return false;
80 void cmIfFunctionBlocker::
81 ScopeEnded(cmMakefile &mf)
83 const char* versionValue
84 = mf.GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION");
85 if (!versionValue || (atof(versionValue) <= 1.4))
87 return;
90 std::string errmsg = "The end of a CMakeLists file was reached with an IF statement that was not closed properly.\nWithin the directory: ";
91 errmsg += mf.GetCurrentDirectory();
92 errmsg += "\nThe arguments are: ";
93 for(std::vector<std::string>::const_iterator j = m_Args.begin();
94 j != m_Args.end(); ++j)
96 errmsg += *j;
97 errmsg += " ";
99 cmSystemTools::Error(errmsg.c_str());
102 bool cmIfCommand::InitialPass(std::vector<std::string> const& args)
104 bool isValid;
105 bool isTrue = cmIfCommand::IsTrue(args,isValid,m_Makefile);
107 if (!isValid)
109 this->SetError("An IF command had incorrect arguments");
110 return false;
113 cmIfFunctionBlocker *f = new cmIfFunctionBlocker();
114 // if is isn't true block the commands
115 f->m_IsBlocking = !isTrue;
116 for(std::vector<std::string>::const_iterator j = args.begin();
117 j != args.end(); ++j)
119 f->m_Args.push_back(*j);
121 m_Makefile->AddFunctionBlocker(f);
123 return true;
126 bool cmIfCommand::IsTrue(const std::vector<std::string> &args, bool &isValid,
127 const cmMakefile *makefile)
129 // check for the different signatures
130 bool isTrue = true;
131 isValid = false;
132 const char *def;
133 const char *def2;
135 if(args.size() < 1 )
137 isValid = true;
138 return false;
141 if (args.size() == 1)
143 def = makefile->GetDefinition(args[0].c_str());
144 if(cmSystemTools::IsOff(def))
146 isTrue = false;
148 isValid = true;
151 if (args.size() == 2 && (args[0] == "NOT"))
153 def = makefile->GetDefinition(args[1].c_str());
154 if(!cmSystemTools::IsOff(def))
156 isTrue = false;
158 isValid = true;
162 if (args.size() == 2 && (args[0] == "COMMAND"))
164 if(!makefile->CommandExists(args[1].c_str()))
166 isTrue = false;
168 isValid = true;
171 if (args.size() == 2 && (args[0] == "EXISTS"))
173 if(!cmSystemTools::FileExists(args[1].c_str()))
175 isTrue = false;
177 isValid = true;
180 if (args.size() == 3 && (args[1] == "AND"))
182 def = makefile->GetDefinition(args[0].c_str());
183 def2 = makefile->GetDefinition(args[2].c_str());
184 if(cmSystemTools::IsOff(def) || cmSystemTools::IsOff(def2))
186 isTrue = false;
188 isValid = true;
191 if (args.size() == 3 && (args[1] == "OR"))
193 def = makefile->GetDefinition(args[0].c_str());
194 def2 = makefile->GetDefinition(args[2].c_str());
195 if(cmSystemTools::IsOff(def) && cmSystemTools::IsOff(def2))
197 isTrue = false;
199 isValid = true;
202 if (args.size() == 3 && (args[1] == "MATCHES"))
204 def = cmIfCommand::GetVariableOrString(args[0].c_str(), makefile);
205 cmRegularExpression regEntry(args[2].c_str());
207 // check for black line or comment
208 if (!regEntry.find(def))
210 isTrue = false;
212 isValid = true;
215 if (args.size() == 3 && (args[1] == "LESS"))
217 def = cmIfCommand::GetVariableOrString(args[0].c_str(), makefile);
218 def2 = cmIfCommand::GetVariableOrString(args[2].c_str(), makefile);
219 if (!def)
221 def = args[0].c_str();
223 if (!def2)
225 def2 = args[2].c_str();
227 if(atof(def) >= atof(def2))
229 isTrue = false;
231 isValid = true;
234 if (args.size() == 3 && (args[1] == "GREATER"))
236 def = cmIfCommand::GetVariableOrString(args[0].c_str(), makefile);
237 def2 = cmIfCommand::GetVariableOrString(args[2].c_str(), makefile);
238 if(atof(def) <= atof(def2))
240 isTrue = false;
242 isValid = true;
245 if (args.size() == 3 && (args[1] == "STRLESS"))
247 def = cmIfCommand::GetVariableOrString(args[0].c_str(), makefile);
248 def2 = cmIfCommand::GetVariableOrString(args[2].c_str(), makefile);
249 if(strcmp(def,def2) >= 0)
251 isTrue = false;
253 isValid = true;
256 if (args.size() == 3 && (args[1] == "STRGREATER"))
258 def = cmIfCommand::GetVariableOrString(args[0].c_str(), makefile);
259 def2 = cmIfCommand::GetVariableOrString(args[2].c_str(), makefile);
260 if(strcmp(def,def2) <= 0)
262 isTrue = false;
264 isValid = true;
267 return isTrue;
270 const char* cmIfCommand::GetVariableOrString(const char* str,
271 const cmMakefile* mf)
273 const char* def = mf->GetDefinition(str);
274 if(!def)
276 def = str;
278 return def;