STYLE: Fix typo in GetFilenameLastExtension docs
[cmake.git] / Source / cmMacroCommand.cxx
blobdaf9849cb953f98109e75652e89027805eead4f6
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmMacroCommand.cxx,v $
5 Language: C++
6 Date: $Date: 2008-09-24 12:51:26 $
7 Version: $Revision: 1.37 $
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;
133 // for each argument of the current function
134 for (std::vector<cmListFileArgument>::const_iterator k =
135 this->Functions[c].Arguments.begin();
136 k != this->Functions[c].Arguments.end(); ++k)
138 tmps = k->Value;
139 // replace formal arguments
140 for (unsigned int j = 1; j < this->Args.size(); ++j)
142 variable = "${";
143 variable += this->Args[j];
144 variable += "}";
145 cmSystemTools::ReplaceString(tmps, variable.c_str(),
146 expandedArgs[j-1].c_str());
148 // replace argc
149 cmSystemTools::ReplaceString(tmps, "${ARGC}",argcDef.c_str());
151 // repleace ARGN
152 if (tmps.find("${ARGN}") != std::string::npos)
154 if (!argnDefInitialized)
156 std::vector<std::string>::const_iterator eit;
157 std::vector<std::string>::size_type cnt = 0;
158 for ( eit = expandedArgs.begin(); eit != expandedArgs.end(); ++eit )
160 if ( cnt >= this->Args.size()-1 )
162 if ( argnDef.size() > 0 )
164 argnDef += ";";
166 argnDef += *eit;
168 cnt ++;
170 argnDefInitialized = true;
172 cmSystemTools::ReplaceString(tmps, "${ARGN}", argnDef.c_str());
175 // if the current argument of the current function has ${ARGV in it
176 // then try replacing ARGV values
177 if (tmps.find("${ARGV") != std::string::npos)
179 char argvName[60];
181 // repleace ARGV, compute it only once
182 if (!argvDefInitialized)
184 std::vector<std::string>::const_iterator eit;
185 for ( eit = expandedArgs.begin(); eit != expandedArgs.end(); ++eit )
187 if ( argvDef.size() > 0 )
189 argvDef += ";";
191 argvDef += *eit;
193 argvDefInitialized = true;
195 cmSystemTools::ReplaceString(tmps, "${ARGV}", argvDef.c_str());
197 // also replace the ARGV1 ARGV2 ... etc
198 for (unsigned int t = 0; t < expandedArgs.size(); ++t)
200 sprintf(argvName,"${ARGV%i}",t);
201 cmSystemTools::ReplaceString(tmps, argvName,
202 expandedArgs[t].c_str());
206 arg.Value = tmps;
207 arg.Quoted = k->Quoted;
208 arg.FilePath = k->FilePath;
209 arg.Line = k->Line;
210 newLFF.Arguments.push_back(arg);
212 cmExecutionStatus status;
213 if(!this->Makefile->ExecuteCommand(newLFF, status) ||
214 status.GetNestedError())
216 // The error message should have already included the call stack
217 // so we do not need to report an error here.
218 inStatus.SetNestedError(true);
219 return false;
221 if (status.GetReturnInvoked())
223 inStatus.SetReturnInvoked(true);
224 return true;
226 if (status.GetBreakInvoked())
228 inStatus.SetBreakInvoked(true);
229 return true;
232 return true;
235 bool cmMacroFunctionBlocker::
236 IsFunctionBlocked(const cmListFileFunction& lff, cmMakefile &mf,
237 cmExecutionStatus &)
239 // record commands until we hit the ENDMACRO
240 // at the ENDMACRO call we shift gears and start looking for invocations
241 if(!cmSystemTools::Strucmp(lff.Name.c_str(),"macro"))
243 this->Depth++;
245 else if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endmacro"))
247 // if this is the endmacro for this macro then execute
248 if (!this->Depth)
250 std::string name = this->Args[0];
251 std::vector<std::string>::size_type cc;
252 name += "(";
253 for ( cc = 0; cc < this->Args.size(); cc ++ )
255 name += " " + this->Args[cc];
257 name += " )";
258 mf.AddMacro(this->Args[0].c_str(), name.c_str());
259 // create a new command and add it to cmake
260 cmMacroHelperCommand *f = new cmMacroHelperCommand();
261 f->Args = this->Args;
262 f->Functions = this->Functions;
263 std::string newName = "_" + this->Args[0];
264 mf.GetCMakeInstance()->RenameCommand(this->Args[0].c_str(),
265 newName.c_str());
266 mf.AddCommand(f);
268 // remove the function blocker now that the macro is defined
269 mf.RemoveFunctionBlocker(lff);
270 return true;
272 else
274 // decrement for each nested macro that ends
275 this->Depth--;
279 // if it wasn't an endmacro and we are not executing then we must be
280 // recording
281 this->Functions.push_back(lff);
282 return true;
286 bool cmMacroFunctionBlocker::
287 ShouldRemove(const cmListFileFunction& lff, cmMakefile &mf)
289 if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endmacro"))
291 std::vector<std::string> expandedArguments;
292 mf.ExpandArguments(lff.Arguments, expandedArguments);
293 // if the endmacro has arguments make sure they
294 // match the arguments of the macro
295 if ((expandedArguments.empty() ||
296 (expandedArguments[0] == this->Args[0])))
298 return true;
302 return false;
305 void cmMacroFunctionBlocker::
306 ScopeEnded(cmMakefile &mf)
308 // macros should end with an EndMacro
309 cmSystemTools::Error(
310 "The end of a CMakeLists file was reached with a MACRO statement that "
311 "was not closed properly. Within the directory: ",
312 mf.GetCurrentDirectory(), " with macro ",
313 this->Args[0].c_str());
316 bool cmMacroCommand::InitialPass(std::vector<std::string> const& args,
317 cmExecutionStatus &)
319 if(args.size() < 1)
321 this->SetError("called with incorrect number of arguments");
322 return false;
325 // create a function blocker
326 cmMacroFunctionBlocker *f = new cmMacroFunctionBlocker();
327 for(std::vector<std::string>::const_iterator j = args.begin();
328 j != args.end(); ++j)
330 f->Args.push_back(*j);
332 this->Makefile->AddFunctionBlocker(f);
333 return true;