cmMacroCommand.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmMacroCommand.h"
  14. #include "cmake.h"
  15. // define the class for macro commands
  16. class cmMacroHelperCommand : public cmCommand
  17. {
  18. public:
  19. cmMacroHelperCommand() {}
  20. ///! clean up any memory allocated by the macro
  21. ~cmMacroHelperCommand() {};
  22. /**
  23. * This is a virtual constructor for the command.
  24. */
  25. virtual cmCommand* Clone()
  26. {
  27. cmMacroHelperCommand *newC = new cmMacroHelperCommand;
  28. // we must copy when we clone
  29. newC->Args = this->Args;
  30. newC->Functions = this->Functions;
  31. return newC;
  32. }
  33. /**
  34. * This determines if the command is invoked when in script mode.
  35. */
  36. virtual bool IsScriptable() { return true; }
  37. /**
  38. * This is called when the command is first encountered in
  39. * the CMakeLists.txt file.
  40. */
  41. virtual bool InvokeInitialPass(const std::vector<cmListFileArgument>& args,
  42. cmExecutionStatus &);
  43. virtual bool InitialPass(std::vector<std::string> const&,
  44. cmExecutionStatus &) { return false; };
  45. /**
  46. * The name of the command as specified in CMakeList.txt.
  47. */
  48. virtual const char* GetName() { return this->Args[0].c_str(); }
  49. /**
  50. * Succinct documentation.
  51. */
  52. virtual const char* GetTerseDocumentation()
  53. {
  54. std::string docs = "Macro named: ";
  55. docs += this->GetName();
  56. return docs.c_str();
  57. }
  58. /**
  59. * More documentation.
  60. */
  61. virtual const char* GetFullDocumentation()
  62. {
  63. return this->GetTerseDocumentation();
  64. }
  65. cmTypeMacro(cmMacroHelperCommand, cmCommand);
  66. std::vector<std::string> Args;
  67. std::vector<cmListFileFunction> Functions;
  68. };
  69. bool cmMacroHelperCommand::InvokeInitialPass
  70. (const std::vector<cmListFileArgument>& args,
  71. cmExecutionStatus &inStatus)
  72. {
  73. // Expand the argument list to the macro.
  74. std::vector<std::string> expandedArgs;
  75. this->Makefile->ExpandArguments(args, expandedArgs);
  76. std::string tmps;
  77. cmListFileArgument arg;
  78. std::string variable;
  79. // make sure the number of arguments passed is at least the number
  80. // required by the signature
  81. if (expandedArgs.size() < this->Args.size() - 1)
  82. {
  83. std::string errorMsg =
  84. "Macro invoked with incorrect arguments for macro named: ";
  85. errorMsg += this->Args[0];
  86. this->SetError(errorMsg.c_str());
  87. return false;
  88. }
  89. // set the value of argc
  90. cmOStringStream 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. // Invoke all the functions that were collected in the block.
  99. cmListFileFunction newLFF;
  100. // for each function
  101. for(unsigned int c = 0; c < this->Functions.size(); ++c)
  102. {
  103. // Replace the formal arguments and then invoke the command.
  104. newLFF.Arguments.clear();
  105. newLFF.Arguments.reserve(this->Functions[c].Arguments.size());
  106. newLFF.Name = this->Functions[c].Name;
  107. newLFF.FilePath = this->Functions[c].FilePath;
  108. newLFF.Line = this->Functions[c].Line;
  109. const char* def = this->Makefile->GetDefinition
  110. ("CMAKE_MACRO_REPORT_DEFINITION_LOCATION");
  111. bool macroReportLocation = false;
  112. if(def && !cmSystemTools::IsOff(def))
  113. {
  114. macroReportLocation = true;
  115. }
  116. // for each argument of the current function
  117. for (std::vector<cmListFileArgument>::const_iterator k =
  118. this->Functions[c].Arguments.begin();
  119. k != this->Functions[c].Arguments.end(); ++k)
  120. {
  121. tmps = k->Value;
  122. // replace formal arguments
  123. for (unsigned int j = 1; j < this->Args.size(); ++j)
  124. {
  125. variable = "${";
  126. variable += this->Args[j];
  127. variable += "}";
  128. cmSystemTools::ReplaceString(tmps, variable.c_str(),
  129. expandedArgs[j-1].c_str());
  130. }
  131. // replace argc
  132. cmSystemTools::ReplaceString(tmps, "${ARGC}",argcDef.c_str());
  133. // repleace ARGN
  134. if (tmps.find("${ARGN}") != std::string::npos)
  135. {
  136. if (!argnDefInitialized)
  137. {
  138. std::vector<std::string>::const_iterator eit;
  139. std::vector<std::string>::size_type cnt = 0;
  140. for ( eit = expandedArgs.begin(); eit != expandedArgs.end(); ++eit )
  141. {
  142. if ( cnt >= this->Args.size()-1 )
  143. {
  144. if ( argnDef.size() > 0 )
  145. {
  146. argnDef += ";";
  147. }
  148. argnDef += *eit;
  149. }
  150. cnt ++;
  151. }
  152. argnDefInitialized = true;
  153. }
  154. cmSystemTools::ReplaceString(tmps, "${ARGN}", argnDef.c_str());
  155. }
  156. // if the current argument of the current function has ${ARGV in it
  157. // then try replacing ARGV values
  158. if (tmps.find("${ARGV") != std::string::npos)
  159. {
  160. char argvName[60];
  161. // repleace ARGV, compute it only once
  162. if (!argvDefInitialized)
  163. {
  164. std::vector<std::string>::const_iterator eit;
  165. for ( eit = expandedArgs.begin(); eit != expandedArgs.end(); ++eit )
  166. {
  167. if ( argvDef.size() > 0 )
  168. {
  169. argvDef += ";";
  170. }
  171. argvDef += *eit;
  172. }
  173. argvDefInitialized = true;
  174. }
  175. cmSystemTools::ReplaceString(tmps, "${ARGV}", argvDef.c_str());
  176. // also replace the ARGV1 ARGV2 ... etc
  177. for (unsigned int t = 0; t < expandedArgs.size(); ++t)
  178. {
  179. sprintf(argvName,"${ARGV%i}",t);
  180. cmSystemTools::ReplaceString(tmps, argvName,
  181. expandedArgs[t].c_str());
  182. }
  183. }
  184. arg.Value = tmps;
  185. arg.Quoted = k->Quoted;
  186. if(macroReportLocation)
  187. {
  188. // Report the location of the argument where the macro was
  189. // defined.
  190. arg.FilePath = k->FilePath;
  191. arg.Line = k->Line;
  192. }
  193. else
  194. {
  195. // Report the location of the argument where the macro was
  196. // invoked.
  197. if (args.size())
  198. {
  199. arg.FilePath = args[0].FilePath;
  200. arg.Line = args[0].Line;
  201. }
  202. else
  203. {
  204. arg.FilePath = "Unknown";
  205. arg.Line = 0;
  206. }
  207. }
  208. newLFF.Arguments.push_back(arg);
  209. }
  210. cmExecutionStatus status;
  211. if(!this->Makefile->ExecuteCommand(newLFF, status) ||
  212. status.GetNestedError())
  213. {
  214. // The error message should have already included the call stack
  215. // so we do not need to report an error here.
  216. inStatus.SetNestedError(true);
  217. return false;
  218. }
  219. if (status.GetReturnInvoked())
  220. {
  221. inStatus.SetReturnInvoked(true);
  222. return true;
  223. }
  224. if (status.GetBreakInvoked())
  225. {
  226. inStatus.SetBreakInvoked(true);
  227. return true;
  228. }
  229. }
  230. return true;
  231. }
  232. bool cmMacroFunctionBlocker::
  233. IsFunctionBlocked(const cmListFileFunction& lff, cmMakefile &mf,
  234. cmExecutionStatus &)
  235. {
  236. // record commands until we hit the ENDMACRO
  237. // at the ENDMACRO call we shift gears and start looking for invocations
  238. if(!cmSystemTools::Strucmp(lff.Name.c_str(),"macro"))
  239. {
  240. this->Depth++;
  241. }
  242. else if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endmacro"))
  243. {
  244. // if this is the endmacro for this macro then execute
  245. if (!this->Depth)
  246. {
  247. std::string name = this->Args[0];
  248. std::vector<std::string>::size_type cc;
  249. name += "(";
  250. for ( cc = 0; cc < this->Args.size(); cc ++ )
  251. {
  252. name += " " + this->Args[cc];
  253. }
  254. name += " )";
  255. mf.AddMacro(this->Args[0].c_str(), name.c_str());
  256. // create a new command and add it to cmake
  257. cmMacroHelperCommand *f = new cmMacroHelperCommand();
  258. f->Args = this->Args;
  259. f->Functions = this->Functions;
  260. std::string newName = "_" + this->Args[0];
  261. mf.GetCMakeInstance()->RenameCommand(this->Args[0].c_str(),
  262. newName.c_str());
  263. mf.AddCommand(f);
  264. // remove the function blocker now that the macro is defined
  265. mf.RemoveFunctionBlocker(lff);
  266. return true;
  267. }
  268. else
  269. {
  270. // decrement for each nested macro that ends
  271. this->Depth--;
  272. }
  273. }
  274. // if it wasn't an endmacro and we are not executing then we must be
  275. // recording
  276. this->Functions.push_back(lff);
  277. return true;
  278. }
  279. bool cmMacroFunctionBlocker::
  280. ShouldRemove(const cmListFileFunction& lff, cmMakefile &mf)
  281. {
  282. if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endmacro"))
  283. {
  284. std::vector<std::string> expandedArguments;
  285. mf.ExpandArguments(lff.Arguments, expandedArguments);
  286. // if the endmacro has arguments make sure they
  287. // match the arguments of the macro
  288. if ((expandedArguments.empty() ||
  289. (expandedArguments[0] == this->Args[0])))
  290. {
  291. return true;
  292. }
  293. }
  294. return false;
  295. }
  296. void cmMacroFunctionBlocker::
  297. ScopeEnded(cmMakefile &mf)
  298. {
  299. // macros should end with an EndMacro
  300. cmSystemTools::Error(
  301. "The end of a CMakeLists file was reached with a MACRO statement that "
  302. "was not closed properly. Within the directory: ",
  303. mf.GetCurrentDirectory(), " with macro ",
  304. this->Args[0].c_str());
  305. }
  306. bool cmMacroCommand::InitialPass(std::vector<std::string> const& args,
  307. cmExecutionStatus &)
  308. {
  309. if(args.size() < 1)
  310. {
  311. this->SetError("called with incorrect number of arguments");
  312. return false;
  313. }
  314. // create a function blocker
  315. cmMacroFunctionBlocker *f = new cmMacroFunctionBlocker();
  316. for(std::vector<std::string>::const_iterator j = args.begin();
  317. j != args.end(); ++j)
  318. {
  319. f->Args.push_back(*j);
  320. }
  321. this->Makefile->AddFunctionBlocker(f);
  322. return true;
  323. }