cmMacroCommand.cxx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmMacroCommand.h"
  11. #include "cmake.h"
  12. #include "cmAlgorithms.h"
  13. // define the class for macro commands
  14. class cmMacroHelperCommand : public cmCommand
  15. {
  16. public:
  17. cmMacroHelperCommand() {}
  18. ///! clean up any memory allocated by the macro
  19. ~cmMacroHelperCommand() {}
  20. /**
  21. * This is used to avoid including this command
  22. * in documentation. This is mainly used by
  23. * cmMacroHelperCommand and cmFunctionHelperCommand
  24. * which cannot provide appropriate documentation.
  25. */
  26. virtual bool ShouldAppearInDocumentation() const
  27. {
  28. return false;
  29. }
  30. /**
  31. * This is a virtual constructor for the command.
  32. */
  33. virtual cmCommand* Clone()
  34. {
  35. cmMacroHelperCommand *newC = new cmMacroHelperCommand;
  36. // we must copy when we clone
  37. newC->Args = this->Args;
  38. newC->Functions = this->Functions;
  39. newC->FilePath = this->FilePath;
  40. newC->Policies = this->Policies;
  41. return newC;
  42. }
  43. /**
  44. * This determines if the command is invoked when in script mode.
  45. */
  46. virtual bool IsScriptable() const { return true; }
  47. /**
  48. * This is called when the command is first encountered in
  49. * the CMakeLists.txt file.
  50. */
  51. virtual bool InvokeInitialPass(const std::vector<cmListFileArgument>& args,
  52. cmExecutionStatus &);
  53. virtual bool InitialPass(std::vector<std::string> const&,
  54. cmExecutionStatus &) { return false; }
  55. /**
  56. * The name of the command as specified in CMakeList.txt.
  57. */
  58. virtual std::string GetName() const { return this->Args[0]; }
  59. cmTypeMacro(cmMacroHelperCommand, cmCommand);
  60. std::vector<std::string> Args;
  61. std::vector<cmListFileFunction> Functions;
  62. cmPolicies::PolicyMap Policies;
  63. std::string FilePath;
  64. };
  65. bool cmMacroHelperCommand::InvokeInitialPass
  66. (const std::vector<cmListFileArgument>& args,
  67. cmExecutionStatus &inStatus)
  68. {
  69. // Expand the argument list to the macro.
  70. std::vector<std::string> expandedArgs;
  71. this->Makefile->ExpandArguments(args, expandedArgs);
  72. // make sure the number of arguments passed is at least the number
  73. // required by the signature
  74. if (expandedArgs.size() < this->Args.size() - 1)
  75. {
  76. std::string errorMsg =
  77. "Macro invoked with incorrect arguments for macro named: ";
  78. errorMsg += this->Args[0];
  79. this->SetError(errorMsg);
  80. return false;
  81. }
  82. cmMakefile::MacroPushPop macroScope(this->Makefile,
  83. this->Policies);
  84. // set the value of argc
  85. std::ostringstream argcDefStream;
  86. argcDefStream << expandedArgs.size();
  87. std::string argcDef = argcDefStream.str();
  88. std::vector<std::string>::const_iterator eit
  89. = expandedArgs.begin() + (this->Args.size() - 1);
  90. std::string expandedArgn = cmJoin(cmRange(eit, expandedArgs.end()), ";");
  91. std::string expandedArgv = cmJoin(expandedArgs, ";");
  92. std::vector<std::string> variables;
  93. variables.reserve(this->Args.size() - 1);
  94. for (unsigned int j = 1; j < this->Args.size(); ++j)
  95. {
  96. variables.push_back("${" + this->Args[j] + "}");
  97. }
  98. std::vector<std::string> argVs;
  99. argVs.reserve(expandedArgs.size());
  100. char argvName[60];
  101. for (unsigned int j = 0; j < expandedArgs.size(); ++j)
  102. {
  103. sprintf(argvName,"${ARGV%i}",j);
  104. argVs.push_back(argvName);
  105. }
  106. // Invoke all the functions that were collected in the block.
  107. cmListFileFunction newLFF;
  108. // for each function
  109. for(unsigned int c = 0; c < this->Functions.size(); ++c)
  110. {
  111. // Replace the formal arguments and then invoke the command.
  112. newLFF.Arguments.clear();
  113. newLFF.Arguments.reserve(this->Functions[c].Arguments.size());
  114. newLFF.Name = this->Functions[c].Name;
  115. newLFF.FilePath = this->Functions[c].FilePath;
  116. newLFF.Line = this->Functions[c].Line;
  117. // for each argument of the current function
  118. for (std::vector<cmListFileArgument>::iterator k =
  119. this->Functions[c].Arguments.begin();
  120. k != this->Functions[c].Arguments.end(); ++k)
  121. {
  122. // Set the FilePath on the arguments to match the function since it is
  123. // not stored and the original values may be freed
  124. k->FilePath = this->FilePath.c_str();
  125. cmListFileArgument arg;
  126. arg.Value = k->Value;
  127. if(k->Delim != cmListFileArgument::Bracket)
  128. {
  129. // replace formal arguments
  130. for (unsigned int j = 0; j < variables.size(); ++j)
  131. {
  132. cmSystemTools::ReplaceString(arg.Value, variables[j].c_str(),
  133. expandedArgs[j].c_str());
  134. }
  135. // replace argc
  136. cmSystemTools::ReplaceString(arg.Value, "${ARGC}",argcDef.c_str());
  137. cmSystemTools::ReplaceString(arg.Value, "${ARGN}",
  138. expandedArgn.c_str());
  139. cmSystemTools::ReplaceString(arg.Value, "${ARGV}",
  140. expandedArgv.c_str());
  141. // if the current argument of the current function has ${ARGV in it
  142. // then try replacing ARGV values
  143. if (arg.Value.find("${ARGV") != std::string::npos)
  144. {
  145. for (unsigned int t = 0; t < expandedArgs.size(); ++t)
  146. {
  147. cmSystemTools::ReplaceString(arg.Value, argVs[t].c_str(),
  148. expandedArgs[t].c_str());
  149. }
  150. }
  151. }
  152. arg.Delim = k->Delim;
  153. arg.FilePath = k->FilePath;
  154. arg.Line = k->Line;
  155. newLFF.Arguments.push_back(arg);
  156. }
  157. cmExecutionStatus status;
  158. if(!this->Makefile->ExecuteCommand(newLFF, status) ||
  159. status.GetNestedError())
  160. {
  161. // The error message should have already included the call stack
  162. // so we do not need to report an error here.
  163. macroScope.Quiet();
  164. inStatus.SetNestedError(true);
  165. return false;
  166. }
  167. if (status.GetReturnInvoked())
  168. {
  169. inStatus.SetReturnInvoked(true);
  170. return true;
  171. }
  172. if (status.GetBreakInvoked())
  173. {
  174. inStatus.SetBreakInvoked(true);
  175. return true;
  176. }
  177. }
  178. return true;
  179. }
  180. bool cmMacroFunctionBlocker::
  181. IsFunctionBlocked(const cmListFileFunction& lff, cmMakefile &mf,
  182. cmExecutionStatus &)
  183. {
  184. // record commands until we hit the ENDMACRO
  185. // at the ENDMACRO call we shift gears and start looking for invocations
  186. if(!cmSystemTools::Strucmp(lff.Name.c_str(),"macro"))
  187. {
  188. this->Depth++;
  189. }
  190. else if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endmacro"))
  191. {
  192. // if this is the endmacro for this macro then execute
  193. if (!this->Depth)
  194. {
  195. mf.AddMacro(this->Args[0].c_str());
  196. // create a new command and add it to cmake
  197. cmMacroHelperCommand *f = new cmMacroHelperCommand();
  198. f->Args = this->Args;
  199. f->Functions = this->Functions;
  200. f->FilePath = this->GetStartingContext().FilePath;
  201. mf.RecordPolicies(f->Policies);
  202. std::string newName = "_" + this->Args[0];
  203. mf.GetState()->RenameCommand(this->Args[0], newName);
  204. mf.GetState()->AddCommand(f);
  205. // remove the function blocker now that the macro is defined
  206. mf.RemoveFunctionBlocker(this, lff);
  207. return true;
  208. }
  209. else
  210. {
  211. // decrement for each nested macro that ends
  212. this->Depth--;
  213. }
  214. }
  215. // if it wasn't an endmacro and we are not executing then we must be
  216. // recording
  217. this->Functions.push_back(lff);
  218. return true;
  219. }
  220. bool cmMacroFunctionBlocker::
  221. ShouldRemove(const cmListFileFunction& lff, cmMakefile &mf)
  222. {
  223. if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endmacro"))
  224. {
  225. std::vector<std::string> expandedArguments;
  226. mf.ExpandArguments(lff.Arguments, expandedArguments);
  227. // if the endmacro has arguments make sure they
  228. // match the arguments of the macro
  229. if ((expandedArguments.empty() ||
  230. (expandedArguments[0] == this->Args[0])))
  231. {
  232. return true;
  233. }
  234. }
  235. return false;
  236. }
  237. bool cmMacroCommand::InitialPass(std::vector<std::string> const& args,
  238. cmExecutionStatus &)
  239. {
  240. if(args.size() < 1)
  241. {
  242. this->SetError("called with incorrect number of arguments");
  243. return false;
  244. }
  245. // create a function blocker
  246. cmMacroFunctionBlocker *f = new cmMacroFunctionBlocker();
  247. f->Args.insert(f->Args.end(), args.begin(), args.end());
  248. this->Makefile->AddFunctionBlocker(f);
  249. return true;
  250. }