cmMacroCommand.cxx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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->FilePath = this->FilePath;
  29. newC->Policies = this->Policies;
  30. return newC;
  31. }
  32. /**
  33. * This determines if the command is invoked when in script mode.
  34. */
  35. virtual bool IsScriptable() { return true; }
  36. /**
  37. * This is called when the command is first encountered in
  38. * the CMakeLists.txt file.
  39. */
  40. virtual bool InvokeInitialPass(const std::vector<cmListFileArgument>& args,
  41. cmExecutionStatus &);
  42. virtual bool InitialPass(std::vector<std::string> const&,
  43. cmExecutionStatus &) { return false; };
  44. /**
  45. * The name of the command as specified in CMakeList.txt.
  46. */
  47. virtual const char* GetName() { return this->Args[0].c_str(); }
  48. /**
  49. * Succinct documentation.
  50. */
  51. virtual const char* GetTerseDocumentation()
  52. {
  53. std::string docs = "Macro named: ";
  54. docs += this->GetName();
  55. return docs.c_str();
  56. }
  57. /**
  58. * More documentation.
  59. */
  60. virtual const char* GetFullDocumentation()
  61. {
  62. return this->GetTerseDocumentation();
  63. }
  64. cmTypeMacro(cmMacroHelperCommand, cmCommand);
  65. std::vector<std::string> Args;
  66. std::vector<cmListFileFunction> Functions;
  67. cmPolicies::PolicyMap Policies;
  68. std::string FilePath;
  69. };
  70. bool cmMacroHelperCommand::InvokeInitialPass
  71. (const std::vector<cmListFileArgument>& args,
  72. cmExecutionStatus &inStatus)
  73. {
  74. // Expand the argument list to the macro.
  75. std::vector<std::string> expandedArgs;
  76. this->Makefile->ExpandArguments(args, expandedArgs);
  77. std::string tmps;
  78. cmListFileArgument arg;
  79. std::string variable;
  80. // make sure the number of arguments passed is at least the number
  81. // required by the signature
  82. if (expandedArgs.size() < this->Args.size() - 1)
  83. {
  84. std::string errorMsg =
  85. "Macro invoked with incorrect arguments for macro named: ";
  86. errorMsg += this->Args[0];
  87. this->SetError(errorMsg.c_str());
  88. return false;
  89. }
  90. // Enforce matching logical blocks inside the macro.
  91. cmMakefile::LexicalPushPop lexScope(this->Makefile);
  92. // Push a weak policy scope which restores the policies recorded at
  93. // macro creation.
  94. cmMakefile::PolicyPushPop polScope(this->Makefile, true, this->Policies);
  95. // set the value of argc
  96. cmOStringStream argcDefStream;
  97. argcDefStream << expandedArgs.size();
  98. std::string argcDef = argcDefStream.str();
  99. // declare varuiables for ARGV ARGN but do not compute until needed
  100. std::string argvDef;
  101. std::string argnDef;
  102. bool argnDefInitialized = false;
  103. bool argvDefInitialized = false;
  104. if( this->Functions.size())
  105. {
  106. this->FilePath = this->Functions[0].FilePath;
  107. }
  108. // Invoke all the functions that were collected in the block.
  109. cmListFileFunction newLFF;
  110. // for each function
  111. for(unsigned int c = 0; c < this->Functions.size(); ++c)
  112. {
  113. // Replace the formal arguments and then invoke the command.
  114. newLFF.Arguments.clear();
  115. newLFF.Arguments.reserve(this->Functions[c].Arguments.size());
  116. newLFF.Name = this->Functions[c].Name;
  117. newLFF.FilePath = this->Functions[c].FilePath;
  118. newLFF.Line = this->Functions[c].Line;
  119. // for each argument of the current function
  120. for (std::vector<cmListFileArgument>::iterator k =
  121. this->Functions[c].Arguments.begin();
  122. k != this->Functions[c].Arguments.end(); ++k)
  123. {
  124. // Set the FilePath on the arguments to match the function since it is
  125. // not stored and the original values may be freed
  126. k->FilePath = this->FilePath.c_str();
  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.size() > 0 )
  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.size() > 0 )
  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. arg.Quoted = k->Quoted;
  192. arg.FilePath = k->FilePath;
  193. arg.Line = k->Line;
  194. newLFF.Arguments.push_back(arg);
  195. }
  196. cmExecutionStatus status;
  197. if(!this->Makefile->ExecuteCommand(newLFF, status) ||
  198. status.GetNestedError())
  199. {
  200. // The error message should have already included the call stack
  201. // so we do not need to report an error here.
  202. lexScope.Quiet();
  203. polScope.Quiet();
  204. inStatus.SetNestedError(true);
  205. return false;
  206. }
  207. if (status.GetReturnInvoked())
  208. {
  209. inStatus.SetReturnInvoked(true);
  210. return true;
  211. }
  212. if (status.GetBreakInvoked())
  213. {
  214. inStatus.SetBreakInvoked(true);
  215. return true;
  216. }
  217. }
  218. return true;
  219. }
  220. bool cmMacroFunctionBlocker::
  221. IsFunctionBlocked(const cmListFileFunction& lff, cmMakefile &mf,
  222. cmExecutionStatus &)
  223. {
  224. // record commands until we hit the ENDMACRO
  225. // at the ENDMACRO call we shift gears and start looking for invocations
  226. if(!cmSystemTools::Strucmp(lff.Name.c_str(),"macro"))
  227. {
  228. this->Depth++;
  229. }
  230. else if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endmacro"))
  231. {
  232. // if this is the endmacro for this macro then execute
  233. if (!this->Depth)
  234. {
  235. std::string name = this->Args[0];
  236. std::vector<std::string>::size_type cc;
  237. name += "(";
  238. for ( cc = 0; cc < this->Args.size(); cc ++ )
  239. {
  240. name += " " + this->Args[cc];
  241. }
  242. name += " )";
  243. mf.AddMacro(this->Args[0].c_str(), name.c_str());
  244. // create a new command and add it to cmake
  245. cmMacroHelperCommand *f = new cmMacroHelperCommand();
  246. f->Args = this->Args;
  247. f->Functions = this->Functions;
  248. mf.RecordPolicies(f->Policies);
  249. std::string newName = "_" + this->Args[0];
  250. mf.GetCMakeInstance()->RenameCommand(this->Args[0].c_str(),
  251. newName.c_str());
  252. mf.AddCommand(f);
  253. // remove the function blocker now that the macro is defined
  254. mf.RemoveFunctionBlocker(this, lff);
  255. return true;
  256. }
  257. else
  258. {
  259. // decrement for each nested macro that ends
  260. this->Depth--;
  261. }
  262. }
  263. // if it wasn't an endmacro and we are not executing then we must be
  264. // recording
  265. this->Functions.push_back(lff);
  266. return true;
  267. }
  268. bool cmMacroFunctionBlocker::
  269. ShouldRemove(const cmListFileFunction& lff, cmMakefile &mf)
  270. {
  271. if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endmacro"))
  272. {
  273. std::vector<std::string> expandedArguments;
  274. mf.ExpandArguments(lff.Arguments, expandedArguments);
  275. // if the endmacro has arguments make sure they
  276. // match the arguments of the macro
  277. if ((expandedArguments.empty() ||
  278. (expandedArguments[0] == this->Args[0])))
  279. {
  280. return true;
  281. }
  282. }
  283. return false;
  284. }
  285. bool cmMacroCommand::InitialPass(std::vector<std::string> const& args,
  286. cmExecutionStatus &)
  287. {
  288. if(args.size() < 1)
  289. {
  290. this->SetError("called with incorrect number of arguments");
  291. return false;
  292. }
  293. // create a function blocker
  294. cmMacroFunctionBlocker *f = new cmMacroFunctionBlocker();
  295. for(std::vector<std::string>::const_iterator j = args.begin();
  296. j != args.end(); ++j)
  297. {
  298. f->Args.push_back(*j);
  299. }
  300. this->Makefile->AddFunctionBlocker(f);
  301. return true;
  302. }