FIX: stupid pb fixed (close to being medieval'ed by The Ken)
[cmake.git] / Source / cmIfCommand.cxx
blobe2f159deb9c51a559b7ff1fa3ab73d8fcd523a5f
1 /*=========================================================================
3 Program: Insight Segmentation & Registration Toolkit
4 Module: $RCSfile: cmIfCommand.cxx,v $
5 Language: C++
6 Date: $Date: 2002-07-11 18:58:26 $
7 Version: $Revision: 1.29 $
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"
18 #include "cmCacheManager.h"
20 bool cmIfFunctionBlocker::
21 IsFunctionBlocked(const char *name, const std::vector<std::string> &args,
22 cmMakefile &)
24 if (!strcmp(name,"ELSE") || !strcmp(name,"ENDIF"))
26 if (args == m_Args)
28 return false;
30 else if(args.empty())
32 std::string err = "Empty arguments for ";
33 err += name;
34 err += ". Did you mean ";
35 err += name;
36 err += "( ";
37 for(std::vector<std::string>::const_iterator a = m_Args.begin();
38 a != m_Args.end();++a)
40 err += *a;
41 err += " ";
43 err += ")?";
44 cmSystemTools::Error(err.c_str());
47 return m_IsBlocking;
50 bool cmIfFunctionBlocker::
51 ShouldRemove(const char *name, const std::vector<std::string> &args,
52 cmMakefile &)
54 if (!strcmp(name,"ELSE") || !strcmp(name,"ENDIF"))
56 if (args == m_Args)
58 return true;
61 return false;
64 void cmIfFunctionBlocker::
65 ScopeEnded(cmMakefile &mf)
67 std::string errmsg = "The end of a CMakeLists file was reached with an IF statement that was not closed properly.\nWithin the directory: ";
68 errmsg += mf.GetCurrentDirectory();
69 errmsg += "\nThe arguments are: ";
70 for(std::vector<std::string>::const_iterator j = m_Args.begin();
71 j != m_Args.end(); ++j)
73 errmsg += *j;
74 errmsg += " ";
76 cmSystemTools::Error(errmsg.c_str());
79 bool cmIfCommand::InitialPass(std::vector<std::string> const& args)
81 bool isValid;
82 bool isTrue = cmIfCommand::IsTrue(args,isValid,m_Makefile);
84 if (!isValid)
86 this->SetError("An IF command had incorrect arguments");
87 return false;
90 cmIfFunctionBlocker *f = new cmIfFunctionBlocker();
91 // if is isn't true block the commands
92 f->m_IsBlocking = !isTrue;
93 for(std::vector<std::string>::const_iterator j = args.begin();
94 j != args.end(); ++j)
96 f->m_Args.push_back(*j);
98 m_Makefile->AddFunctionBlocker(f);
100 return true;
103 bool cmIfCommand::IsTrue(const std::vector<std::string> &args, bool &isValid,
104 const cmMakefile *makefile)
106 // check for the different signatures
107 bool isTrue = true;
108 isValid = false;
109 const char *def;
110 const char *def2;
112 if(args.size() < 1 )
114 return false;
117 if (args.size() == 1)
119 def = makefile->GetDefinition(args[0].c_str());
120 if(cmSystemTools::IsOff(def))
122 isTrue = false;
124 isValid = true;
127 if (args.size() == 2 && (args[0] == "NOT"))
129 def = makefile->GetDefinition(args[1].c_str());
130 if(!cmSystemTools::IsOff(def))
132 isTrue = false;
134 isValid = true;
138 if (args.size() == 2 && (args[0] == "COMMAND"))
140 if(!makefile->CommandExists(args[1].c_str()))
142 isTrue = false;
144 isValid = true;
147 if (args.size() == 2 && (args[0] == "EXISTS"))
149 if(!cmSystemTools::FileExists(args[1].c_str()))
151 isTrue = false;
153 isValid = true;
156 if (args.size() == 3 && (args[1] == "AND"))
158 def = makefile->GetDefinition(args[0].c_str());
159 def2 = makefile->GetDefinition(args[2].c_str());
160 if(cmSystemTools::IsOff(def) || cmSystemTools::IsOff(def2))
162 isTrue = false;
164 isValid = true;
167 if (args.size() == 3 && (args[1] == "OR"))
169 def = makefile->GetDefinition(args[0].c_str());
170 def2 = makefile->GetDefinition(args[2].c_str());
171 if(cmSystemTools::IsOff(def) && cmSystemTools::IsOff(def2))
173 isTrue = false;
175 isValid = true;
178 if (args.size() == 3 && (args[1] == "MATCHES"))
180 def = makefile->GetDefinition(args[0].c_str());
181 if (!def)
183 def = args[0].c_str();
185 cmRegularExpression regEntry(args[2].c_str());
187 // check for black line or comment
188 if (!regEntry.find(def))
190 isTrue = false;
192 isValid = true;
195 if (args.size() == 3 && (args[1] == "LESS"))
197 def = makefile->GetDefinition(args[0].c_str());
198 def2 = makefile->GetDefinition(args[2].c_str());
199 if (!def)
201 def = args[0].c_str();
203 if (!def2)
205 def2 = args[2].c_str();
207 if(atof(def) >= atof(def2))
209 isTrue = false;
211 isValid = true;
214 if (args.size() == 3 && (args[1] == "GREATER"))
216 def = makefile->GetDefinition(args[0].c_str());
217 def2 = makefile->GetDefinition(args[2].c_str());
218 if (!def)
220 def = args[0].c_str();
222 if (!def2)
224 def2 = args[2].c_str();
226 if(atof(def) <= atof(def2))
228 isTrue = false;
230 isValid = true;
233 if (args.size() == 3 && (args[1] == "STRLESS"))
235 def = makefile->GetDefinition(args[0].c_str());
236 def2 = makefile->GetDefinition(args[2].c_str());
237 if(strcmp(def,def2) >= 0)
239 isTrue = false;
241 isValid = true;
244 if (args.size() == 3 && (args[1] == "STRGREATER"))
246 def = makefile->GetDefinition(args[0].c_str());
247 def2 = makefile->GetDefinition(args[2].c_str());
248 if(strcmp(def,def2) <= 0)
250 isTrue = false;
252 isValid = true;
255 return isTrue;