1 /*=========================================================================
3 Program: Insight Segmentation & Registration Toolkit
4 Module: $RCSfile: cmIfCommand.cxx,v $
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
,
23 // always let if statements through
24 if (!strcmp(name
,"IF"))
29 // watch for our ELSE or ENDIF
30 if (!strcmp(name
,"ELSE") || !strcmp(name
,"ENDIF"))
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
;
41 // otherwise it must be an ENDIF statement, in that case remove the
43 mf
.RemoveFunctionBlocker("ENDIF",args
);
48 std::string err
= "Empty arguments for ";
50 err
+= ". Did you mean ";
53 for(std::vector
<std::string
>::const_iterator a
= m_Args
.begin();
54 a
!= m_Args
.end();++a
)
60 cmSystemTools::Error(err
.c_str());
66 bool cmIfFunctionBlocker::
67 ShouldRemove(const char *name
, const std::vector
<std::string
> &args
,
70 if (!strcmp(name
,"ELSE") || !strcmp(name
,"ENDIF"))
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))
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
)
99 cmSystemTools::Error(errmsg
.c_str());
102 bool cmIfCommand::InitialPass(std::vector
<std::string
> const& args
)
105 bool isTrue
= cmIfCommand::IsTrue(args
,isValid
,m_Makefile
);
109 this->SetError("An IF command had incorrect arguments");
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
);
126 bool cmIfCommand::IsTrue(const std::vector
<std::string
> &args
, bool &isValid
,
127 const cmMakefile
*makefile
)
129 // check for the different signatures
141 if (args
.size() == 1)
143 def
= makefile
->GetDefinition(args
[0].c_str());
144 if(cmSystemTools::IsOff(def
))
151 if (args
.size() == 2 && (args
[0] == "NOT"))
153 def
= makefile
->GetDefinition(args
[1].c_str());
154 if(!cmSystemTools::IsOff(def
))
162 if (args
.size() == 2 && (args
[0] == "COMMAND"))
164 if(!makefile
->CommandExists(args
[1].c_str()))
171 if (args
.size() == 2 && (args
[0] == "EXISTS"))
173 if(!cmSystemTools::FileExists(args
[1].c_str()))
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
))
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
))
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
))
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
);
221 def
= args
[0].c_str();
225 def2
= args
[2].c_str();
227 if(atof(def
) >= atof(def2
))
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
))
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)
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)
270 const char* cmIfCommand::GetVariableOrString(const char* str
,
271 const cmMakefile
* mf
)
273 const char* def
= mf
->GetDefinition(str
);