cmMacroCommand.cxx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. if(!this->Functions.empty())
  107. {
  108. this->FilePath = this->Functions[0].FilePath;
  109. }
  110. // Invoke all the functions that were collected in the block.
  111. cmListFileFunction newLFF;
  112. // for each function
  113. for(unsigned int c = 0; c < this->Functions.size(); ++c)
  114. {
  115. // Replace the formal arguments and then invoke the command.
  116. newLFF.Arguments.clear();
  117. newLFF.Arguments.reserve(this->Functions[c].Arguments.size());
  118. newLFF.Name = this->Functions[c].Name;
  119. newLFF.FilePath = this->Functions[c].FilePath;
  120. newLFF.Line = this->Functions[c].Line;
  121. // for each argument of the current function
  122. for (std::vector<cmListFileArgument>::iterator k =
  123. this->Functions[c].Arguments.begin();
  124. k != this->Functions[c].Arguments.end(); ++k)
  125. {
  126. // Set the FilePath on the arguments to match the function since it is
  127. // not stored and the original values may be freed
  128. k->FilePath = this->FilePath.c_str();
  129. cmListFileArgument arg;
  130. arg.Value = k->Value;
  131. if(k->Delim != cmListFileArgument::Bracket)
  132. {
  133. // replace formal arguments
  134. for (unsigned int j = 0; j < variables.size(); ++j)
  135. {
  136. cmSystemTools::ReplaceString(arg.Value, variables[j].c_str(),
  137. expandedArgs[j].c_str());
  138. }
  139. // replace argc
  140. cmSystemTools::ReplaceString(arg.Value, "${ARGC}",argcDef.c_str());
  141. cmSystemTools::ReplaceString(arg.Value, "${ARGN}",
  142. expandedArgn.c_str());
  143. cmSystemTools::ReplaceString(arg.Value, "${ARGV}",
  144. expandedArgv.c_str());
  145. // if the current argument of the current function has ${ARGV in it
  146. // then try replacing ARGV values
  147. if (arg.Value.find("${ARGV") != std::string::npos)
  148. {
  149. for (unsigned int t = 0; t < expandedArgs.size(); ++t)
  150. {
  151. cmSystemTools::ReplaceString(arg.Value, argVs[t].c_str(),
  152. expandedArgs[t].c_str());
  153. }
  154. }
  155. }
  156. arg.Delim = k->Delim;
  157. arg.FilePath = k->FilePath;
  158. arg.Line = k->Line;
  159. newLFF.Arguments.push_back(arg);
  160. }
  161. cmExecutionStatus status;
  162. if(!this->Makefile->ExecuteCommand(newLFF, status) ||
  163. status.GetNestedError())
  164. {
  165. // The error message should have already included the call stack
  166. // so we do not need to report an error here.
  167. macroScope.Quiet();
  168. inStatus.SetNestedError(true);
  169. return false;
  170. }
  171. if (status.GetReturnInvoked())
  172. {
  173. inStatus.SetReturnInvoked(true);
  174. return true;
  175. }
  176. if (status.GetBreakInvoked())
  177. {
  178. inStatus.SetBreakInvoked(true);
  179. return true;
  180. }
  181. }
  182. return true;
  183. }
  184. bool cmMacroFunctionBlocker::
  185. IsFunctionBlocked(const cmListFileFunction& lff, cmMakefile &mf,
  186. cmExecutionStatus &)
  187. {
  188. // record commands until we hit the ENDMACRO
  189. // at the ENDMACRO call we shift gears and start looking for invocations
  190. if(!cmSystemTools::Strucmp(lff.Name.c_str(),"macro"))
  191. {
  192. this->Depth++;
  193. }
  194. else if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endmacro"))
  195. {
  196. // if this is the endmacro for this macro then execute
  197. if (!this->Depth)
  198. {
  199. mf.AddMacro(this->Args[0].c_str());
  200. // create a new command and add it to cmake
  201. cmMacroHelperCommand *f = new cmMacroHelperCommand();
  202. f->Args = this->Args;
  203. f->Functions = this->Functions;
  204. mf.RecordPolicies(f->Policies);
  205. std::string newName = "_" + this->Args[0];
  206. mf.GetState()->RenameCommand(this->Args[0], newName);
  207. mf.GetState()->AddCommand(f);
  208. // remove the function blocker now that the macro is defined
  209. mf.RemoveFunctionBlocker(this, lff);
  210. return true;
  211. }
  212. else
  213. {
  214. // decrement for each nested macro that ends
  215. this->Depth--;
  216. }
  217. }
  218. // if it wasn't an endmacro and we are not executing then we must be
  219. // recording
  220. this->Functions.push_back(lff);
  221. return true;
  222. }
  223. bool cmMacroFunctionBlocker::
  224. ShouldRemove(const cmListFileFunction& lff, cmMakefile &mf)
  225. {
  226. if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endmacro"))
  227. {
  228. std::vector<std::string> expandedArguments;
  229. mf.ExpandArguments(lff.Arguments, expandedArguments);
  230. // if the endmacro has arguments make sure they
  231. // match the arguments of the macro
  232. if ((expandedArguments.empty() ||
  233. (expandedArguments[0] == this->Args[0])))
  234. {
  235. return true;
  236. }
  237. }
  238. return false;
  239. }
  240. bool cmMacroCommand::InitialPass(std::vector<std::string> const& args,
  241. cmExecutionStatus &)
  242. {
  243. if(args.size() < 1)
  244. {
  245. this->SetError("called with incorrect number of arguments");
  246. return false;
  247. }
  248. // create a function blocker
  249. cmMacroFunctionBlocker *f = new cmMacroFunctionBlocker();
  250. f->Args.insert(f->Args.end(), args.begin(), args.end());
  251. this->Makefile->AddFunctionBlocker(f);
  252. return true;
  253. }