BUG: fix some bad changes in progress calc
[cmake.git] / Source / cmMacroCommand.cxx
blob7e4627e5de2fb48344cb2067635b189fb7c55d22
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmMacroCommand.cxx,v $
5 Language: C++
6 Date: $Date: 2008-03-07 13:40:36 $
7 Version: $Revision: 1.36 $
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 =========================================================================*/
17 #include "cmMacroCommand.h"
19 #include "cmake.h"
21 // define the class for macro commands
22 class cmMacroHelperCommand : public cmCommand
24 public:
25 cmMacroHelperCommand() {}
27 ///! clean up any memory allocated by the macro
28 ~cmMacroHelperCommand() {};
30 /**
31 * This is a virtual constructor for the command.
33 virtual cmCommand* Clone()
35 cmMacroHelperCommand *newC = new cmMacroHelperCommand;
36 // we must copy when we clone
37 newC->Args = this->Args;
38 newC->Functions = this->Functions;
39 return newC;
42 /**
43 * This determines if the command is invoked when in script mode.
45 virtual bool IsScriptable() { return true; }
47 /**
48 * This is called when the command is first encountered in
49 * the CMakeLists.txt file.
51 virtual bool InvokeInitialPass(const std::vector<cmListFileArgument>& args,
52 cmExecutionStatus &);
54 virtual bool InitialPass(std::vector<std::string> const&,
55 cmExecutionStatus &) { return false; };
57 /**
58 * The name of the command as specified in CMakeList.txt.
60 virtual const char* GetName() { return this->Args[0].c_str(); }
62 /**
63 * Succinct documentation.
65 virtual const char* GetTerseDocumentation()
67 std::string docs = "Macro named: ";
68 docs += this->GetName();
69 return docs.c_str();
72 /**
73 * More documentation.
75 virtual const char* GetFullDocumentation()
77 return this->GetTerseDocumentation();
80 cmTypeMacro(cmMacroHelperCommand, cmCommand);
82 std::vector<std::string> Args;
83 std::vector<cmListFileFunction> Functions;
87 bool cmMacroHelperCommand::InvokeInitialPass
88 (const std::vector<cmListFileArgument>& args,
89 cmExecutionStatus &inStatus)
91 // Expand the argument list to the macro.
92 std::vector<std::string> expandedArgs;
93 this->Makefile->ExpandArguments(args, expandedArgs);
95 std::string tmps;
96 cmListFileArgument arg;
97 std::string variable;
99 // make sure the number of arguments passed is at least the number
100 // required by the signature
101 if (expandedArgs.size() < this->Args.size() - 1)
103 std::string errorMsg =
104 "Macro invoked with incorrect arguments for macro named: ";
105 errorMsg += this->Args[0];
106 this->SetError(errorMsg.c_str());
107 return false;
110 // set the value of argc
111 cmOStringStream argcDefStream;
112 argcDefStream << expandedArgs.size();
113 std::string argcDef = argcDefStream.str();
115 // declare varuiables for ARGV ARGN but do not compute until needed
116 std::string argvDef;
117 std::string argnDef;
118 bool argnDefInitialized = false;
119 bool argvDefInitialized = false;
121 // Invoke all the functions that were collected in the block.
122 cmListFileFunction newLFF;
123 // for each function
124 for(unsigned int c = 0; c < this->Functions.size(); ++c)
126 // Replace the formal arguments and then invoke the command.
127 newLFF.Arguments.clear();
128 newLFF.Arguments.reserve(this->Functions[c].Arguments.size());
129 newLFF.Name = this->Functions[c].Name;
130 newLFF.FilePath = this->Functions[c].FilePath;
131 newLFF.Line = this->Functions[c].Line;
132 const char* def = this->Makefile->GetDefinition
133 ("CMAKE_MACRO_REPORT_DEFINITION_LOCATION");
134 bool macroReportLocation = false;
135 if(def && !cmSystemTools::IsOff(def))
137 macroReportLocation = true;
140 // for each argument of the current function
141 for (std::vector<cmListFileArgument>::const_iterator k =
142 this->Functions[c].Arguments.begin();
143 k != this->Functions[c].Arguments.end(); ++k)
145 tmps = k->Value;
146 // replace formal arguments
147 for (unsigned int j = 1; j < this->Args.size(); ++j)
149 variable = "${";
150 variable += this->Args[j];
151 variable += "}";
152 cmSystemTools::ReplaceString(tmps, variable.c_str(),
153 expandedArgs[j-1].c_str());
155 // replace argc
156 cmSystemTools::ReplaceString(tmps, "${ARGC}",argcDef.c_str());
158 // repleace ARGN
159 if (tmps.find("${ARGN}") != std::string::npos)
161 if (!argnDefInitialized)
163 std::vector<std::string>::const_iterator eit;
164 std::vector<std::string>::size_type cnt = 0;
165 for ( eit = expandedArgs.begin(); eit != expandedArgs.end(); ++eit )
167 if ( cnt >= this->Args.size()-1 )
169 if ( argnDef.size() > 0 )
171 argnDef += ";";
173 argnDef += *eit;
175 cnt ++;
177 argnDefInitialized = true;
179 cmSystemTools::ReplaceString(tmps, "${ARGN}", argnDef.c_str());
182 // if the current argument of the current function has ${ARGV in it
183 // then try replacing ARGV values
184 if (tmps.find("${ARGV") != std::string::npos)
186 char argvName[60];
188 // repleace ARGV, compute it only once
189 if (!argvDefInitialized)
191 std::vector<std::string>::const_iterator eit;
192 for ( eit = expandedArgs.begin(); eit != expandedArgs.end(); ++eit )
194 if ( argvDef.size() > 0 )
196 argvDef += ";";
198 argvDef += *eit;
200 argvDefInitialized = true;
202 cmSystemTools::ReplaceString(tmps, "${ARGV}", argvDef.c_str());
204 // also replace the ARGV1 ARGV2 ... etc
205 for (unsigned int t = 0; t < expandedArgs.size(); ++t)
207 sprintf(argvName,"${ARGV%i}",t);
208 cmSystemTools::ReplaceString(tmps, argvName,
209 expandedArgs[t].c_str());
213 arg.Value = tmps;
214 arg.Quoted = k->Quoted;
215 if(macroReportLocation)
217 // Report the location of the argument where the macro was
218 // defined.
219 arg.FilePath = k->FilePath;
220 arg.Line = k->Line;
222 else
224 // Report the location of the argument where the macro was
225 // invoked.
226 if (args.size())
228 arg.FilePath = args[0].FilePath;
229 arg.Line = args[0].Line;
231 else
233 arg.FilePath = "Unknown";
234 arg.Line = 0;
237 newLFF.Arguments.push_back(arg);
239 cmExecutionStatus status;
240 if(!this->Makefile->ExecuteCommand(newLFF, status) ||
241 status.GetNestedError())
243 // The error message should have already included the call stack
244 // so we do not need to report an error here.
245 inStatus.SetNestedError(true);
246 return false;
248 if (status.GetReturnInvoked())
250 inStatus.SetReturnInvoked(true);
251 return true;
253 if (status.GetBreakInvoked())
255 inStatus.SetBreakInvoked(true);
256 return true;
259 return true;
262 bool cmMacroFunctionBlocker::
263 IsFunctionBlocked(const cmListFileFunction& lff, cmMakefile &mf,
264 cmExecutionStatus &)
266 // record commands until we hit the ENDMACRO
267 // at the ENDMACRO call we shift gears and start looking for invocations
268 if(!cmSystemTools::Strucmp(lff.Name.c_str(),"macro"))
270 this->Depth++;
272 else if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endmacro"))
274 // if this is the endmacro for this macro then execute
275 if (!this->Depth)
277 std::string name = this->Args[0];
278 std::vector<std::string>::size_type cc;
279 name += "(";
280 for ( cc = 0; cc < this->Args.size(); cc ++ )
282 name += " " + this->Args[cc];
284 name += " )";
285 mf.AddMacro(this->Args[0].c_str(), name.c_str());
286 // create a new command and add it to cmake
287 cmMacroHelperCommand *f = new cmMacroHelperCommand();
288 f->Args = this->Args;
289 f->Functions = this->Functions;
290 std::string newName = "_" + this->Args[0];
291 mf.GetCMakeInstance()->RenameCommand(this->Args[0].c_str(),
292 newName.c_str());
293 mf.AddCommand(f);
295 // remove the function blocker now that the macro is defined
296 mf.RemoveFunctionBlocker(lff);
297 return true;
299 else
301 // decrement for each nested macro that ends
302 this->Depth--;
306 // if it wasn't an endmacro and we are not executing then we must be
307 // recording
308 this->Functions.push_back(lff);
309 return true;
313 bool cmMacroFunctionBlocker::
314 ShouldRemove(const cmListFileFunction& lff, cmMakefile &mf)
316 if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endmacro"))
318 std::vector<std::string> expandedArguments;
319 mf.ExpandArguments(lff.Arguments, expandedArguments);
320 // if the endmacro has arguments make sure they
321 // match the arguments of the macro
322 if ((expandedArguments.empty() ||
323 (expandedArguments[0] == this->Args[0])))
325 return true;
329 return false;
332 void cmMacroFunctionBlocker::
333 ScopeEnded(cmMakefile &mf)
335 // macros should end with an EndMacro
336 cmSystemTools::Error(
337 "The end of a CMakeLists file was reached with a MACRO statement that "
338 "was not closed properly. Within the directory: ",
339 mf.GetCurrentDirectory(), " with macro ",
340 this->Args[0].c_str());
343 bool cmMacroCommand::InitialPass(std::vector<std::string> const& args,
344 cmExecutionStatus &)
346 if(args.size() < 1)
348 this->SetError("called with incorrect number of arguments");
349 return false;
352 // create a function blocker
353 cmMacroFunctionBlocker *f = new cmMacroFunctionBlocker();
354 for(std::vector<std::string>::const_iterator j = args.begin();
355 j != args.end(); ++j)
357 f->Args.push_back(*j);
359 this->Makefile->AddFunctionBlocker(f);
360 return true;