BUG: fix some bad changes in progress calc
[cmake.git] / Source / cmFunctionCommand.cxx
blobd18fd1a6c33371fe2d9ba8f7cfc058174520357a
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmFunctionCommand.cxx,v $
5 Language: C++
6 Date: $Date: 2008-03-07 13:40:36 $
7 Version: $Revision: 1.6 $
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 "cmFunctionCommand.h"
19 #include "cmake.h"
21 // define the class for function commands
22 class cmFunctionHelperCommand : public cmCommand
24 public:
25 cmFunctionHelperCommand() {}
27 ///! clean up any memory allocated by the function
28 ~cmFunctionHelperCommand() {};
30 /**
31 * This is a virtual constructor for the command.
33 virtual cmCommand* Clone()
35 cmFunctionHelperCommand *newC = new cmFunctionHelperCommand;
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 = "Function 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(cmFunctionHelperCommand, cmCommand);
82 std::vector<std::string> Args;
83 std::vector<cmListFileFunction> Functions;
87 bool cmFunctionHelperCommand::InvokeInitialPass
88 (const std::vector<cmListFileArgument>& args,
89 cmExecutionStatus & inStatus)
91 // Expand the argument list to the function.
92 std::vector<std::string> expandedArgs;
93 this->Makefile->ExpandArguments(args, expandedArgs);
95 // make sure the number of arguments passed is at least the number
96 // required by the signature
97 if (expandedArgs.size() < this->Args.size() - 1)
99 std::string errorMsg =
100 "Function invoked with incorrect arguments for function named: ";
101 errorMsg += this->Args[0];
102 this->SetError(errorMsg.c_str());
103 return false;
106 // we push a scope on the makefile
107 this->Makefile->PushScope();
109 // set the value of argc
110 cmOStringStream strStream;
111 strStream << expandedArgs.size();
112 this->Makefile->AddDefinition("ARGC",strStream.str().c_str());
114 // set the values for ARGV0 ARGV1 ...
115 for (unsigned int t = 0; t < expandedArgs.size(); ++t)
117 cmOStringStream tmpStream;
118 tmpStream << "ARGV" << t;
119 this->Makefile->AddDefinition(tmpStream.str().c_str(),
120 expandedArgs[t].c_str());
123 // define the formal arguments
124 for (unsigned int j = 1; j < this->Args.size(); ++j)
126 this->Makefile->AddDefinition(this->Args[j].c_str(),
127 expandedArgs[j-1].c_str());
130 // define ARGV and ARGN
131 std::vector<std::string>::const_iterator eit;
132 std::string argvDef;
133 std::string argnDef;
134 unsigned int cnt = 0;
135 for ( eit = expandedArgs.begin(); eit != expandedArgs.end(); ++eit )
137 if ( argvDef.size() > 0 )
139 argvDef += ";";
141 argvDef += *eit;
142 if ( cnt >= this->Args.size()-1 )
144 if ( argnDef.size() > 0 )
146 argnDef += ";";
148 argnDef += *eit;
150 cnt ++;
152 this->Makefile->AddDefinition("ARGV", argvDef.c_str());
153 this->Makefile->AddDefinition("ARGN", argnDef.c_str());
155 // Invoke all the functions that were collected in the block.
156 // for each function
157 for(unsigned int c = 0; c < this->Functions.size(); ++c)
159 cmExecutionStatus status;
160 if (!this->Makefile->ExecuteCommand(this->Functions[c],status) ||
161 status.GetNestedError())
163 // The error message should have already included the call stack
164 // so we do not need to report an error here.
165 inStatus.SetNestedError(true);
166 return false;
168 if (status.GetReturnInvoked())
170 this->Makefile->PopScope();
171 return true;
175 // pop scope on the makefile
176 this->Makefile->PopScope();
177 return true;
180 bool cmFunctionFunctionBlocker::
181 IsFunctionBlocked(const cmListFileFunction& lff, cmMakefile &mf,
182 cmExecutionStatus &)
184 // record commands until we hit the ENDFUNCTION
185 // at the ENDFUNCTION call we shift gears and start looking for invocations
186 if(!cmSystemTools::Strucmp(lff.Name.c_str(),"function"))
188 this->Depth++;
190 else if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endfunction"))
192 // if this is the endfunction for this function then execute
193 if (!this->Depth)
195 std::string name = this->Args[0];
196 std::vector<std::string>::size_type cc;
197 name += "(";
198 for ( cc = 0; cc < this->Args.size(); cc ++ )
200 name += " " + this->Args[cc];
202 name += " )";
204 // create a new command and add it to cmake
205 cmFunctionHelperCommand *f = new cmFunctionHelperCommand();
206 f->Args = this->Args;
207 f->Functions = this->Functions;
209 // Set the FilePath on the arguments to match the function since it is
210 // not stored and the original values may be freed
211 for (unsigned int i = 0; i < f->Functions.size(); ++i)
213 for (unsigned int j = 0; j < f->Functions[i].Arguments.size(); ++j)
215 f->Functions[i].Arguments[j].FilePath =
216 f->Functions[i].FilePath.c_str();
220 std::string newName = "_" + this->Args[0];
221 mf.GetCMakeInstance()->RenameCommand(this->Args[0].c_str(),
222 newName.c_str());
223 mf.AddCommand(f);
225 // remove the function blocker now that the function is defined
226 mf.RemoveFunctionBlocker(lff);
227 return true;
229 else
231 // decrement for each nested function that ends
232 this->Depth--;
236 // if it wasn't an endfunction and we are not executing then we must be
237 // recording
238 this->Functions.push_back(lff);
239 return true;
243 bool cmFunctionFunctionBlocker::
244 ShouldRemove(const cmListFileFunction& lff, cmMakefile &mf)
246 if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endfunction"))
248 std::vector<std::string> expandedArguments;
249 mf.ExpandArguments(lff.Arguments, expandedArguments);
250 // if the endfunction has arguments then make sure
251 // they match the ones in the openeing function command
252 if ((expandedArguments.empty() ||
253 (expandedArguments[0] == this->Args[0])))
255 return true;
259 return false;
262 void cmFunctionFunctionBlocker::
263 ScopeEnded(cmMakefile &mf)
265 // functions should end with an EndFunction
266 cmSystemTools::Error(
267 "The end of a CMakeLists file was reached with a FUNCTION statement that "
268 "was not closed properly. Within the directory: ",
269 mf.GetCurrentDirectory(), " with function ",
270 this->Args[0].c_str());
273 bool cmFunctionCommand
274 ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
276 if(args.size() < 1)
278 this->SetError("called with incorrect number of arguments");
279 return false;
282 // create a function blocker
283 cmFunctionFunctionBlocker *f = new cmFunctionFunctionBlocker();
284 for(std::vector<std::string>::const_iterator j = args.begin();
285 j != args.end(); ++j)
287 f->Args.push_back(*j);
289 this->Makefile->AddFunctionBlocker(f);
290 return true;