cmMacroCommand.cxx 9.6 KB

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