cmMacroCommand.cxx 8.6 KB

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