1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmMacroCommand.cxx,v $
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"
21 // define the class for macro commands
22 class cmMacroHelperCommand
: public cmCommand
25 cmMacroHelperCommand() {}
27 ///! clean up any memory allocated by the macro
28 ~cmMacroHelperCommand() {};
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
;
43 * This determines if the command is invoked when in script mode.
45 virtual bool IsScriptable() { return true; }
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
,
54 virtual bool InitialPass(std::vector
<std::string
> const&,
55 cmExecutionStatus
&) { return false; };
58 * The name of the command as specified in CMakeList.txt.
60 virtual const char* GetName() { return this->Args
[0].c_str(); }
63 * Succinct documentation.
65 virtual const char* GetTerseDocumentation()
67 std::string docs
= "Macro named: ";
68 docs
+= this->GetName();
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
);
96 cmListFileArgument arg
;
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());
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
118 bool argnDefInitialized
= false;
119 bool argvDefInitialized
= false;
121 // Invoke all the functions that were collected in the block.
122 cmListFileFunction newLFF
;
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
)
139 // replace formal arguments
140 for (unsigned int j
= 1; j
< this->Args
.size(); ++j
)
143 variable
+= this->Args
[j
];
145 cmSystemTools::ReplaceString(tmps
, variable
.c_str(),
146 expandedArgs
[j
-1].c_str());
149 cmSystemTools::ReplaceString(tmps
, "${ARGC}",argcDef
.c_str());
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 )
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
)
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 )
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());
207 arg
.Quoted
= k
->Quoted
;
208 arg
.FilePath
= k
->FilePath
;
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);
221 if (status
.GetReturnInvoked())
223 inStatus
.SetReturnInvoked(true);
226 if (status
.GetBreakInvoked())
228 inStatus
.SetBreakInvoked(true);
235 bool cmMacroFunctionBlocker::
236 IsFunctionBlocked(const cmListFileFunction
& lff
, cmMakefile
&mf
,
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"))
245 else if(!cmSystemTools::Strucmp(lff
.Name
.c_str(),"endmacro"))
247 // if this is the endmacro for this macro then execute
250 std::string name
= this->Args
[0];
251 std::vector
<std::string
>::size_type cc
;
253 for ( cc
= 0; cc
< this->Args
.size(); cc
++ )
255 name
+= " " + this->Args
[cc
];
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(),
268 // remove the function blocker now that the macro is defined
269 mf
.RemoveFunctionBlocker(lff
);
274 // decrement for each nested macro that ends
279 // if it wasn't an endmacro and we are not executing then we must be
281 this->Functions
.push_back(lff
);
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])))
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
,
321 this->SetError("called with incorrect number of arguments");
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
);