cmMacroCommand.cxx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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->m_Args = this->m_Args;
  30. newC->m_Functions = this->m_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->m_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> m_Args;
  65. std::vector<cmListFileFunction> m_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. m_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() < m_Args.size() - 1)
  79. {
  80. std::string errorMsg =
  81. "Macro invoked with incorrect arguments for macro named: ";
  82. errorMsg += m_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 argvDefInitialized = false;
  94. // save the current definitions of all vars that we will be setting
  95. std::string oldARGC;
  96. if (m_Makefile->GetDefinition("ARGC"))
  97. {
  98. oldARGC = m_Makefile->GetDefinition("ARGC");
  99. }
  100. m_Makefile->AddDefinition("ARGC",argcDef.c_str());
  101. // store ARGN, ARGV
  102. std::vector<std::string> oldARGVArgs;
  103. std::vector<std::string>::const_iterator eit;
  104. std::vector<std::string>::size_type cnt = 0;
  105. char argvName[60];
  106. for ( eit = expandedArgs.begin(); eit != expandedArgs.end(); ++eit )
  107. {
  108. if ( cnt >= m_Args.size()-1 )
  109. {
  110. if ( argnDef.size() > 0 )
  111. {
  112. argnDef += ";";
  113. }
  114. argnDef += *eit;
  115. }
  116. if ( argvDef.size() > 0 )
  117. {
  118. argvDef += ";";
  119. }
  120. argvDef += *eit;
  121. oldARGVArgs.push_back(std::string());
  122. sprintf(argvName,"ARGV%i",cnt);
  123. if (m_Makefile->GetDefinition(argvName))
  124. {
  125. oldARGVArgs[cnt] = m_Makefile->GetDefinition(argvName);
  126. }
  127. m_Makefile->AddDefinition(argvName,eit->c_str());
  128. cnt++;
  129. }
  130. std::string oldARGN;
  131. if (m_Makefile->GetDefinition("ARGN"))
  132. {
  133. oldARGN = m_Makefile->GetDefinition("ARGN");
  134. }
  135. m_Makefile->AddDefinition("ARGN",argnDef.c_str());
  136. std::string oldARGV;
  137. if (m_Makefile->GetDefinition("ARGV"))
  138. {
  139. oldARGV = m_Makefile->GetDefinition("ARGV");
  140. }
  141. m_Makefile->AddDefinition("ARGV",argvDef.c_str());
  142. // store old defs for formal args
  143. std::vector<std::string> oldFormalArgs;
  144. for (unsigned int j = 1; j < m_Args.size(); ++j)
  145. {
  146. oldFormalArgs.push_back(std::string());
  147. if (m_Makefile->GetDefinition(m_Args[j].c_str()))
  148. {
  149. oldFormalArgs[j-1] = m_Makefile->GetDefinition(m_Args[j].c_str());
  150. }
  151. m_Makefile->AddDefinition(m_Args[j].c_str(),expandedArgs[j-1].c_str());
  152. }
  153. // Invoke all the functions that were collected in the block.
  154. for(unsigned int c = 0; c < m_Functions.size(); ++c)
  155. {
  156. if(!m_Makefile->ExecuteCommand(m_Functions[c]))
  157. {
  158. cmOStringStream error;
  159. error << "Error in cmake code at\n"
  160. << args[0].FilePath << ":" << args[0].Line << "\n"
  161. << "A command failed during the invocation of macro \""
  162. << this->m_Args[0].c_str() << "\".\nThe failing line "
  163. << "in the macro definition was at\n"
  164. << m_Functions[c].m_FilePath << ":"
  165. << m_Functions[c].m_Line << "\n";
  166. cmSystemTools::Error(error.str().c_str());
  167. return false;
  168. }
  169. }
  170. // restore all args
  171. m_Makefile->AddDefinition("ARGC",oldARGC.c_str());
  172. m_Makefile->AddDefinition("ARGN",oldARGN.c_str());
  173. m_Makefile->AddDefinition("ARGV",oldARGV.c_str());
  174. // restore old defs for formal args
  175. for (unsigned int j = 1; j < m_Args.size(); ++j)
  176. {
  177. m_Makefile->AddDefinition(m_Args[j].c_str(),oldFormalArgs[j-1].c_str());
  178. }
  179. // restore old defs for formal args
  180. for (unsigned int j = 0; j < oldARGVArgs.size(); ++j)
  181. {
  182. sprintf(argvName,"ARGV%i",j);
  183. m_Makefile->AddDefinition(argvName,oldARGVArgs[j].c_str());
  184. }
  185. return true;
  186. }
  187. bool cmMacroFunctionBlocker::
  188. IsFunctionBlocked(const cmListFileFunction& lff, cmMakefile &mf)
  189. {
  190. // record commands until we hit the ENDMACRO
  191. // at the ENDMACRO call we shift gears and start looking for invocations
  192. if(cmSystemTools::LowerCase(lff.m_Name) == "endmacro")
  193. {
  194. std::vector<std::string> expandedArguments;
  195. mf.ExpandArguments(lff.m_Arguments, expandedArguments);
  196. if(!expandedArguments.empty() && (expandedArguments[0] == m_Args[0]))
  197. {
  198. std::string name = m_Args[0];
  199. std::vector<std::string>::size_type cc;
  200. name += "(";
  201. for ( cc = 0; cc < m_Args.size(); cc ++ )
  202. {
  203. name += " " + m_Args[cc];
  204. }
  205. name += " )";
  206. mf.AddMacro(m_Args[0].c_str(), name.c_str());
  207. // create a new command and add it to cmake
  208. cmMacroHelperCommand *f = new cmMacroHelperCommand();
  209. f->m_Args = this->m_Args;
  210. f->m_Functions = this->m_Functions;
  211. std::string newName = "_" + this->m_Args[0];
  212. mf.GetCMakeInstance()->RenameCommand(this->m_Args[0].c_str(), newName.c_str());
  213. mf.AddCommand(f);
  214. // remove the function blocker now that the macro is defined
  215. mf.RemoveFunctionBlocker(lff);
  216. return true;
  217. }
  218. }
  219. // if it wasn't an endmacro and we are not executing then we must be
  220. // recording
  221. m_Functions.push_back(lff);
  222. return true;
  223. }
  224. bool cmMacroFunctionBlocker::
  225. ShouldRemove(const cmListFileFunction& lff, cmMakefile &mf)
  226. {
  227. if(cmSystemTools::LowerCase(lff.m_Name) == "endmacro")
  228. {
  229. std::vector<std::string> expandedArguments;
  230. mf.ExpandArguments(lff.m_Arguments, expandedArguments);
  231. if(!expandedArguments.empty() && (expandedArguments[0] == m_Args[0]))
  232. {
  233. return true;
  234. }
  235. }
  236. return false;
  237. }
  238. void cmMacroFunctionBlocker::
  239. ScopeEnded(cmMakefile &mf)
  240. {
  241. // macros should end with an EndMacro
  242. cmSystemTools::Error("The end of a CMakeLists file was reached with a MACRO statement that was not closed properly. Within the directory: ",
  243. mf.GetCurrentDirectory(), " with macro ",
  244. m_Args[0].c_str());
  245. }
  246. bool cmMacroCommand::InitialPass(std::vector<std::string> const& args)
  247. {
  248. if(args.size() < 1)
  249. {
  250. this->SetError("called with incorrect number of arguments");
  251. return false;
  252. }
  253. // create a function blocker
  254. cmMacroFunctionBlocker *f = new cmMacroFunctionBlocker();
  255. for(std::vector<std::string>::const_iterator j = args.begin();
  256. j != args.end(); ++j)
  257. {
  258. f->m_Args.push_back(*j);
  259. }
  260. m_Makefile->AddFunctionBlocker(f);
  261. return true;
  262. }