cmMacroCommand.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. // define the class for macro commands
  13. class cmMacroHelperCommand : public cmCommand
  14. {
  15. public:
  16. cmMacroHelperCommand() {}
  17. ///! clean up any memory allocated by the macro
  18. ~cmMacroHelperCommand() {}
  19. /**
  20. * This is used to avoid including this command
  21. * in documentation. This is mainly used by
  22. * cmMacroHelperCommand and cmFunctionHelperCommand
  23. * which cannot provide appropriate documentation.
  24. */
  25. virtual bool ShouldAppearInDocumentation() const
  26. {
  27. return false;
  28. }
  29. /**
  30. * This is a virtual constructor for the command.
  31. */
  32. virtual cmCommand* Clone()
  33. {
  34. cmMacroHelperCommand *newC = new cmMacroHelperCommand;
  35. // we must copy when we clone
  36. newC->Args = this->Args;
  37. newC->Functions = this->Functions;
  38. newC->FilePath = this->FilePath;
  39. newC->Policies = this->Policies;
  40. return newC;
  41. }
  42. /**
  43. * This determines if the command is invoked when in script mode.
  44. */
  45. virtual bool IsScriptable() const { return true; }
  46. /**
  47. * This is called when the command is first encountered in
  48. * the CMakeLists.txt file.
  49. */
  50. virtual bool InvokeInitialPass(const std::vector<cmListFileArgument>& args,
  51. cmExecutionStatus &);
  52. virtual bool InitialPass(std::vector<std::string> const&,
  53. cmExecutionStatus &) { return false; }
  54. /**
  55. * The name of the command as specified in CMakeList.txt.
  56. */
  57. virtual std::string GetName() const { return this->Args[0]; }
  58. cmTypeMacro(cmMacroHelperCommand, cmCommand);
  59. std::vector<std::string> Args;
  60. std::vector<cmListFileFunction> Functions;
  61. cmPolicies::PolicyMap Policies;
  62. std::string FilePath;
  63. };
  64. bool cmMacroHelperCommand::InvokeInitialPass
  65. (const std::vector<cmListFileArgument>& args,
  66. cmExecutionStatus &inStatus)
  67. {
  68. // Expand the argument list to the macro.
  69. std::vector<std::string> expandedArgs;
  70. this->Makefile->ExpandArguments(args, expandedArgs);
  71. std::string tmps;
  72. cmListFileArgument arg;
  73. std::string variable;
  74. // make sure the number of arguments passed is at least the number
  75. // required by the signature
  76. if (expandedArgs.size() < this->Args.size() - 1)
  77. {
  78. std::string errorMsg =
  79. "Macro invoked with incorrect arguments for macro named: ";
  80. errorMsg += this->Args[0];
  81. this->SetError(errorMsg);
  82. return false;
  83. }
  84. // Enforce matching logical blocks inside the macro.
  85. cmMakefile::LexicalPushPop lexScope(this->Makefile);
  86. // Push a weak policy scope which restores the policies recorded at
  87. // macro creation.
  88. cmMakefile::PolicyPushPop polScope(this->Makefile, true, this->Policies);
  89. // set the value of argc
  90. std::ostringstream argcDefStream;
  91. argcDefStream << expandedArgs.size();
  92. std::string argcDef = argcDefStream.str();
  93. // declare varuiables for ARGV ARGN but do not compute until needed
  94. std::string argvDef;
  95. std::string argnDef;
  96. bool argnDefInitialized = false;
  97. bool argvDefInitialized = false;
  98. if(!this->Functions.empty())
  99. {
  100. this->FilePath = this->Functions[0].FilePath;
  101. }
  102. // Invoke all the functions that were collected in the block.
  103. cmListFileFunction newLFF;
  104. // for each function
  105. for(unsigned int c = 0; c < this->Functions.size(); ++c)
  106. {
  107. // Replace the formal arguments and then invoke the command.
  108. newLFF.Arguments.clear();
  109. newLFF.Arguments.reserve(this->Functions[c].Arguments.size());
  110. newLFF.Name = this->Functions[c].Name;
  111. newLFF.FilePath = this->Functions[c].FilePath;
  112. newLFF.Line = this->Functions[c].Line;
  113. // for each argument of the current function
  114. for (std::vector<cmListFileArgument>::iterator k =
  115. this->Functions[c].Arguments.begin();
  116. k != this->Functions[c].Arguments.end(); ++k)
  117. {
  118. // Set the FilePath on the arguments to match the function since it is
  119. // not stored and the original values may be freed
  120. k->FilePath = this->FilePath.c_str();
  121. if(k->Delim == cmListFileArgument::Bracket)
  122. {
  123. arg.Value = k->Value;
  124. }
  125. else
  126. {
  127. tmps = k->Value;
  128. // replace formal arguments
  129. for (unsigned int j = 1; j < this->Args.size(); ++j)
  130. {
  131. variable = "${";
  132. variable += this->Args[j];
  133. variable += "}";
  134. cmSystemTools::ReplaceString(tmps, variable.c_str(),
  135. expandedArgs[j-1].c_str());
  136. }
  137. // replace argc
  138. cmSystemTools::ReplaceString(tmps, "${ARGC}",argcDef.c_str());
  139. // repleace ARGN
  140. if (tmps.find("${ARGN}") != std::string::npos)
  141. {
  142. if (!argnDefInitialized)
  143. {
  144. std::vector<std::string>::const_iterator eit;
  145. std::vector<std::string>::size_type cnt = 0;
  146. for(eit = expandedArgs.begin(); eit != expandedArgs.end(); ++eit)
  147. {
  148. if ( cnt >= this->Args.size()-1 )
  149. {
  150. if (!argnDef.empty())
  151. {
  152. argnDef += ";";
  153. }
  154. argnDef += *eit;
  155. }
  156. cnt ++;
  157. }
  158. argnDefInitialized = true;
  159. }
  160. cmSystemTools::ReplaceString(tmps, "${ARGN}", argnDef.c_str());
  161. }
  162. // if the current argument of the current function has ${ARGV in it
  163. // then try replacing ARGV values
  164. if (tmps.find("${ARGV") != std::string::npos)
  165. {
  166. char argvName[60];
  167. // repleace ARGV, compute it only once
  168. if (!argvDefInitialized)
  169. {
  170. std::vector<std::string>::const_iterator eit;
  171. for(eit = expandedArgs.begin(); eit != expandedArgs.end(); ++eit)
  172. {
  173. if (!argvDef.empty())
  174. {
  175. argvDef += ";";
  176. }
  177. argvDef += *eit;
  178. }
  179. argvDefInitialized = true;
  180. }
  181. cmSystemTools::ReplaceString(tmps, "${ARGV}", argvDef.c_str());
  182. // also replace the ARGV1 ARGV2 ... etc
  183. for (unsigned int t = 0; t < expandedArgs.size(); ++t)
  184. {
  185. sprintf(argvName,"${ARGV%i}",t);
  186. cmSystemTools::ReplaceString(tmps, argvName,
  187. expandedArgs[t].c_str());
  188. }
  189. }
  190. arg.Value = tmps;
  191. }
  192. arg.Delim = k->Delim;
  193. arg.FilePath = k->FilePath;
  194. arg.Line = k->Line;
  195. newLFF.Arguments.push_back(arg);
  196. }
  197. cmExecutionStatus status;
  198. if(!this->Makefile->ExecuteCommand(newLFF, status) ||
  199. status.GetNestedError())
  200. {
  201. // The error message should have already included the call stack
  202. // so we do not need to report an error here.
  203. lexScope.Quiet();
  204. polScope.Quiet();
  205. inStatus.SetNestedError(true);
  206. return false;
  207. }
  208. if (status.GetReturnInvoked())
  209. {
  210. inStatus.SetReturnInvoked(true);
  211. return true;
  212. }
  213. if (status.GetBreakInvoked())
  214. {
  215. inStatus.SetBreakInvoked(true);
  216. return true;
  217. }
  218. }
  219. return true;
  220. }
  221. bool cmMacroFunctionBlocker::
  222. IsFunctionBlocked(const cmListFileFunction& lff, cmMakefile &mf,
  223. cmExecutionStatus &)
  224. {
  225. // record commands until we hit the ENDMACRO
  226. // at the ENDMACRO call we shift gears and start looking for invocations
  227. if(!cmSystemTools::Strucmp(lff.Name.c_str(),"macro"))
  228. {
  229. this->Depth++;
  230. }
  231. else if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endmacro"))
  232. {
  233. // if this is the endmacro for this macro then execute
  234. if (!this->Depth)
  235. {
  236. std::string name = this->Args[0];
  237. std::vector<std::string>::size_type cc;
  238. name += "(";
  239. for ( cc = 0; cc < this->Args.size(); cc ++ )
  240. {
  241. name += " " + this->Args[cc];
  242. }
  243. name += " )";
  244. mf.AddMacro(this->Args[0].c_str(), name.c_str());
  245. // create a new command and add it to cmake
  246. cmMacroHelperCommand *f = new cmMacroHelperCommand();
  247. f->Args = this->Args;
  248. f->Functions = this->Functions;
  249. mf.RecordPolicies(f->Policies);
  250. std::string newName = "_" + this->Args[0];
  251. mf.GetCMakeInstance()->RenameCommand(this->Args[0],
  252. newName);
  253. mf.AddCommand(f);
  254. // remove the function blocker now that the macro is defined
  255. mf.RemoveFunctionBlocker(this, lff);
  256. return true;
  257. }
  258. else
  259. {
  260. // decrement for each nested macro that ends
  261. this->Depth--;
  262. }
  263. }
  264. // if it wasn't an endmacro and we are not executing then we must be
  265. // recording
  266. this->Functions.push_back(lff);
  267. return true;
  268. }
  269. bool cmMacroFunctionBlocker::
  270. ShouldRemove(const cmListFileFunction& lff, cmMakefile &mf)
  271. {
  272. if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endmacro"))
  273. {
  274. std::vector<std::string> expandedArguments;
  275. mf.ExpandArguments(lff.Arguments, expandedArguments);
  276. // if the endmacro has arguments make sure they
  277. // match the arguments of the macro
  278. if ((expandedArguments.empty() ||
  279. (expandedArguments[0] == this->Args[0])))
  280. {
  281. return true;
  282. }
  283. }
  284. return false;
  285. }
  286. bool cmMacroCommand::InitialPass(std::vector<std::string> const& args,
  287. cmExecutionStatus &)
  288. {
  289. if(args.size() < 1)
  290. {
  291. this->SetError("called with incorrect number of arguments");
  292. return false;
  293. }
  294. // create a function blocker
  295. cmMacroFunctionBlocker *f = new cmMacroFunctionBlocker();
  296. f->Args.insert(f->Args.end(), args.begin(), args.end());
  297. this->Makefile->AddFunctionBlocker(f);
  298. return true;
  299. }