cmMacroCommand.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. virtual bool InitialPass(std::vector<std::string> const&) { 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. };
  67. bool cmMacroHelperCommand::InvokeInitialPass
  68. (const std::vector<cmListFileArgument>& args)
  69. {
  70. // Expand the argument list to the macro.
  71. std::vector<std::string> expandedArgs;
  72. this->Makefile->ExpandArguments(args, expandedArgs);
  73. std::string tmps;
  74. cmListFileArgument arg;
  75. std::string variable;
  76. // make sure the number of arguments passed is at least the number
  77. // required by the signature
  78. if (expandedArgs.size() < this->Args.size() - 1)
  79. {
  80. std::string errorMsg =
  81. "Macro invoked with incorrect arguments for macro named: ";
  82. errorMsg += this->Args[0];
  83. this->SetError(errorMsg.c_str());
  84. return false;
  85. }
  86. // set the value of argc
  87. cmOStringStream argcDefStream;
  88. argcDefStream << expandedArgs.size();
  89. std::string argcDef = argcDefStream.str();
  90. // declare varuiables for ARGV ARGN but do not compute until needed
  91. std::string argvDef;
  92. std::string argnDef;
  93. bool argnDefInitialized = false;
  94. bool argvDefInitialized = false;
  95. // Invoke all the functions that were collected in the block.
  96. cmListFileFunction newLFF;
  97. // for each function
  98. for(unsigned int c = 0; c < this->Functions.size(); ++c)
  99. {
  100. // Replace the formal arguments and then invoke the command.
  101. newLFF.Arguments.clear();
  102. newLFF.Arguments.reserve(this->Functions[c].Arguments.size());
  103. newLFF.Name = this->Functions[c].Name;
  104. newLFF.FilePath = this->Functions[c].FilePath;
  105. newLFF.Line = this->Functions[c].Line;
  106. const char* def = this->Makefile->GetDefinition
  107. ("CMAKE_MACRO_REPORT_DEFINITION_LOCATION");
  108. bool macroReportLocation = false;
  109. if(def && !cmSystemTools::IsOff(def))
  110. {
  111. macroReportLocation = true;
  112. }
  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. if(macroReportLocation)
  184. {
  185. // Report the location of the argument where the macro was
  186. // defined.
  187. arg.FilePath = k->FilePath;
  188. arg.Line = k->Line;
  189. }
  190. else
  191. {
  192. // Report the location of the argument where the macro was
  193. // invoked.
  194. if (args.size())
  195. {
  196. arg.FilePath = args[0].FilePath;
  197. arg.Line = args[0].Line;
  198. }
  199. else
  200. {
  201. arg.FilePath = "Unknown";
  202. arg.Line = 0;
  203. }
  204. }
  205. newLFF.Arguments.push_back(arg);
  206. }
  207. if(!this->Makefile->ExecuteCommand(newLFF))
  208. {
  209. if(args.size())
  210. {
  211. arg.FilePath = args[0].FilePath;
  212. arg.Line = args[0].Line;
  213. }
  214. else
  215. {
  216. arg.FilePath = "Unknown";
  217. arg.Line = 0;
  218. }
  219. cmOStringStream error;
  220. error << "Error in cmake code at\n"
  221. << arg.FilePath << ":" << arg.Line << ":\n"
  222. << "A command failed during the invocation of macro \""
  223. << this->Args[0].c_str() << "\".";
  224. cmSystemTools::Error(error.str().c_str());
  225. return false;
  226. }
  227. }
  228. return true;
  229. }
  230. bool cmMacroFunctionBlocker::
  231. IsFunctionBlocked(const cmListFileFunction& lff, cmMakefile &mf)
  232. {
  233. // record commands until we hit the ENDMACRO
  234. // at the ENDMACRO call we shift gears and start looking for invocations
  235. if(!cmSystemTools::Strucmp(lff.Name.c_str(),"macro"))
  236. {
  237. this->Depth++;
  238. }
  239. else if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endmacro"))
  240. {
  241. // if this is the endmacro for this macro then execute
  242. if (!this->Depth)
  243. {
  244. std::string name = this->Args[0];
  245. std::vector<std::string>::size_type cc;
  246. name += "(";
  247. for ( cc = 0; cc < this->Args.size(); cc ++ )
  248. {
  249. name += " " + this->Args[cc];
  250. }
  251. name += " )";
  252. mf.AddMacro(this->Args[0].c_str(), name.c_str());
  253. // create a new command and add it to cmake
  254. cmMacroHelperCommand *f = new cmMacroHelperCommand();
  255. f->Args = this->Args;
  256. f->Functions = this->Functions;
  257. std::string newName = "_" + this->Args[0];
  258. mf.GetCMakeInstance()->RenameCommand(this->Args[0].c_str(),
  259. newName.c_str());
  260. mf.AddCommand(f);
  261. // remove the function blocker now that the macro is defined
  262. mf.RemoveFunctionBlocker(lff);
  263. return true;
  264. }
  265. else
  266. {
  267. // decrement for each nested macro that ends
  268. this->Depth--;
  269. }
  270. }
  271. // if it wasn't an endmacro and we are not executing then we must be
  272. // recording
  273. this->Functions.push_back(lff);
  274. return true;
  275. }
  276. bool cmMacroFunctionBlocker::
  277. ShouldRemove(const cmListFileFunction& lff, cmMakefile &mf)
  278. {
  279. if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endmacro"))
  280. {
  281. std::vector<std::string> expandedArguments;
  282. mf.ExpandArguments(lff.Arguments, expandedArguments);
  283. if ((!expandedArguments.empty() &&
  284. (expandedArguments[0] == this->Args[0]))
  285. || mf.IsOn("CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS"))
  286. {
  287. return true;
  288. }
  289. }
  290. return false;
  291. }
  292. void cmMacroFunctionBlocker::
  293. ScopeEnded(cmMakefile &mf)
  294. {
  295. // macros should end with an EndMacro
  296. cmSystemTools::Error(
  297. "The end of a CMakeLists file was reached with a MACRO statement that "
  298. "was not closed properly. Within the directory: ",
  299. mf.GetCurrentDirectory(), " with macro ",
  300. this->Args[0].c_str());
  301. }
  302. bool cmMacroCommand::InitialPass(std::vector<std::string> const& args)
  303. {
  304. if(args.size() < 1)
  305. {
  306. this->SetError("called with incorrect number of arguments");
  307. return false;
  308. }
  309. // create a function blocker
  310. cmMacroFunctionBlocker *f = new cmMacroFunctionBlocker();
  311. for(std::vector<std::string>::const_iterator j = args.begin();
  312. j != args.end(); ++j)
  313. {
  314. f->Args.push_back(*j);
  315. }
  316. this->Makefile->AddFunctionBlocker(f);
  317. return true;
  318. }