cmMacroCommand.cxx 10 KB

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