cmMacroCommand.cxx 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. // define the class for macro commands
  15. class cmMacroHelperCommand : public cmCommand
  16. {
  17. public:
  18. cmMacroHelperCommand() {}
  19. ///! clean up any memory allocated by the macro
  20. ~cmMacroHelperCommand() {};
  21. /**
  22. * This is a virtual constructor for the command.
  23. */
  24. virtual cmCommand* Clone()
  25. {
  26. cmMacroHelperCommand *newC = new cmMacroHelperCommand;
  27. // we must copy when we clone
  28. newC->m_Args = this->m_Args;
  29. newC->m_Functions = this->m_Functions;
  30. return newC;
  31. }
  32. /**
  33. * This is called when the command is first encountered in
  34. * the CMakeLists.txt file.
  35. */
  36. virtual bool InvokeInitialPass(const std::vector<cmListFileArgument>& args);
  37. virtual bool InitialPass(std::vector<std::string> const&) { return false; };
  38. /**
  39. * The name of the command as specified in CMakeList.txt.
  40. */
  41. virtual const char* GetName() { return this->m_Args[0].c_str(); }
  42. /**
  43. * Succinct documentation.
  44. */
  45. virtual const char* GetTerseDocumentation()
  46. {
  47. std::string docs = "Macro named: ";
  48. docs += this->GetName();
  49. return docs.c_str();
  50. }
  51. /**
  52. * More documentation.
  53. */
  54. virtual const char* GetFullDocumentation()
  55. {
  56. return this->GetTerseDocumentation();
  57. }
  58. cmTypeMacro(cmMacroHelperCommand, cmCommand);
  59. std::vector<std::string> m_Args;
  60. std::vector<cmListFileFunction> m_Functions;
  61. };
  62. bool cmMacroHelperCommand::InvokeInitialPass
  63. (const std::vector<cmListFileArgument>& args)
  64. {
  65. // Expand the argument list to the macro.
  66. std::vector<std::string> expandedArgs;
  67. m_Makefile->ExpandArguments(args, expandedArgs);
  68. std::string tmps;
  69. cmListFileArgument arg;
  70. std::string variable;
  71. // make sure the number of arguments passed is at least the number
  72. // required by the signature
  73. if (expandedArgs.size() < m_Args.size() - 1)
  74. {
  75. std::string errorMsg =
  76. "Macro invoked with incorrect arguments for macro named: ";
  77. errorMsg += m_Args[0];
  78. this->SetError(errorMsg.c_str());
  79. return false;
  80. }
  81. // set the value of argc
  82. cmOStringStream argcDefStream;
  83. argcDefStream << expandedArgs.size();
  84. std::string argcDef = argcDefStream.str();
  85. // declare varuiables for ARGV ARGN but do not compute until needed
  86. std::string argvDef;
  87. std::string argnDef;
  88. bool argnDefInitialized = false;
  89. bool argvDefInitialized = false;
  90. // Invoke all the functions that were collected in the block.
  91. cmListFileFunction newLFF;
  92. // for each function
  93. for(unsigned int c = 0; c < m_Functions.size(); ++c)
  94. {
  95. // Replace the formal arguments and then invoke the command.
  96. newLFF.m_Arguments.clear();
  97. newLFF.m_Arguments.reserve(m_Functions[c].m_Arguments.size());
  98. newLFF.m_Name = m_Functions[c].m_Name;
  99. newLFF.m_FilePath = m_Functions[c].m_FilePath;
  100. newLFF.m_Line = m_Functions[c].m_Line;
  101. // for each argument of the current function
  102. for (std::vector<cmListFileArgument>::const_iterator k =
  103. m_Functions[c].m_Arguments.begin();
  104. k != m_Functions[c].m_Arguments.end(); ++k)
  105. {
  106. tmps = k->Value;
  107. // replace formal arguments
  108. for (unsigned int j = 1; j < m_Args.size(); ++j)
  109. {
  110. variable = "${";
  111. variable += m_Args[j];
  112. variable += "}";
  113. cmSystemTools::ReplaceString(tmps, variable.c_str(),
  114. expandedArgs[j-1].c_str());
  115. }
  116. // replace argc
  117. cmSystemTools::ReplaceString(tmps, "${ARGC}",argcDef.c_str());
  118. // repleace ARGN
  119. if (tmps.find("${ARGN}") != std::string::npos)
  120. {
  121. if (!argnDefInitialized)
  122. {
  123. std::vector<std::string>::const_iterator eit;
  124. std::vector<std::string>::size_type cnt = 0;
  125. for ( eit = expandedArgs.begin(); eit != expandedArgs.end(); ++eit )
  126. {
  127. if ( cnt >= m_Args.size()-1 )
  128. {
  129. if ( argnDef.size() > 0 )
  130. {
  131. argnDef += ";";
  132. }
  133. argnDef += *eit;
  134. }
  135. cnt ++;
  136. }
  137. argnDefInitialized = true;
  138. }
  139. cmSystemTools::ReplaceString(tmps, "${ARGN}", argnDef.c_str());
  140. }
  141. // if the current argument of the current function has ${ARGV in it
  142. // then try replacing ARGV values
  143. if (tmps.find("${ARGV") != std::string::npos)
  144. {
  145. char argvName[60];
  146. // repleace ARGV, compute it only once
  147. if (!argvDefInitialized)
  148. {
  149. std::vector<std::string>::const_iterator eit;
  150. for ( eit = expandedArgs.begin(); eit != expandedArgs.end(); ++eit )
  151. {
  152. if ( argvDef.size() > 0 )
  153. {
  154. argvDef += ";";
  155. }
  156. argvDef += *eit;
  157. }
  158. argvDefInitialized = true;
  159. }
  160. cmSystemTools::ReplaceString(tmps, "${ARGV}", argvDef.c_str());
  161. // also replace the ARGV1 ARGV2 ... etc
  162. for (unsigned int t = 0; t < expandedArgs.size(); ++t)
  163. {
  164. sprintf(argvName,"${ARGV%i}",t);
  165. cmSystemTools::ReplaceString(tmps, argvName,
  166. expandedArgs[t].c_str());
  167. }
  168. }
  169. arg.Value = tmps;
  170. arg.Quoted = k->Quoted;
  171. const char* def =
  172. m_Makefile->GetDefinition("CMAKE_MACRO_REPORT_DEFINITION_LOCATION");
  173. if(def && !cmSystemTools::IsOff(def))
  174. {
  175. // Report the location of the argument where the macro was
  176. // defined.
  177. arg.FilePath = k->FilePath;
  178. arg.Line = k->Line;
  179. }
  180. else
  181. {
  182. // Report the location of the argument where the macro was
  183. // invoked.
  184. if (args.size())
  185. {
  186. arg.FilePath = args[0].FilePath;
  187. arg.Line = args[0].Line;
  188. }
  189. else
  190. {
  191. arg.FilePath = "Unknown";
  192. arg.Line = 0;
  193. }
  194. }
  195. newLFF.m_Arguments.push_back(arg);
  196. }
  197. if(!m_Makefile->ExecuteCommand(newLFF))
  198. {
  199. cmOStringStream error;
  200. error << "Error in cmake code at\n"
  201. << args[0].FilePath << ":" << args[0].Line << ":\n"
  202. << "A command failed during the invocation of macro \""
  203. << this->m_Args[0].c_str() << "\".";
  204. cmSystemTools::Error(error.str().c_str());
  205. return false;
  206. }
  207. }
  208. return true;
  209. }
  210. bool cmMacroFunctionBlocker::
  211. IsFunctionBlocked(const cmListFileFunction& lff, cmMakefile &mf)
  212. {
  213. // record commands until we hit the ENDMACRO
  214. // at the ENDMACRO call we shift gears and start looking for invocations
  215. if(lff.m_Name == "ENDMACRO")
  216. {
  217. std::vector<std::string> expandedArguments;
  218. mf.ExpandArguments(lff.m_Arguments, expandedArguments);
  219. if(!expandedArguments.empty() && (expandedArguments[0] == m_Args[0]))
  220. {
  221. std::string name = m_Args[0];
  222. std::vector<std::string>::size_type cc;
  223. name += "(";
  224. for ( cc = 0; cc < m_Args.size(); cc ++ )
  225. {
  226. name += " " + m_Args[cc];
  227. }
  228. name += " )";
  229. mf.AddMacro(m_Args[0].c_str(), name.c_str());
  230. // create a new command and add it to cmake
  231. cmMacroHelperCommand *f = new cmMacroHelperCommand();
  232. f->m_Args = this->m_Args;
  233. f->m_Functions = this->m_Functions;
  234. mf.AddCommand(f);
  235. // remove the function blocker now that the macro is defined
  236. mf.RemoveFunctionBlocker(lff);
  237. return true;
  238. }
  239. }
  240. // if it wasn't an endmacro and we are not executing then we must be
  241. // recording
  242. m_Functions.push_back(lff);
  243. return true;
  244. }
  245. bool cmMacroFunctionBlocker::
  246. ShouldRemove(const cmListFileFunction& lff, cmMakefile &mf)
  247. {
  248. if(lff.m_Name == "ENDMACRO")
  249. {
  250. std::vector<std::string> expandedArguments;
  251. mf.ExpandArguments(lff.m_Arguments, expandedArguments);
  252. if(!expandedArguments.empty() && (expandedArguments[0] == m_Args[0]))
  253. {
  254. return true;
  255. }
  256. }
  257. return false;
  258. }
  259. void cmMacroFunctionBlocker::
  260. ScopeEnded(cmMakefile &mf)
  261. {
  262. // macros should end with an EndMacro
  263. cmSystemTools::Error("The end of a CMakeLists file was reached with a MACRO statement that was not closed properly. Within the directory: ",
  264. mf.GetCurrentDirectory(), " with macro ",
  265. m_Args[0].c_str());
  266. }
  267. bool cmMacroCommand::InitialPass(std::vector<std::string> const& args)
  268. {
  269. if(args.size() < 1)
  270. {
  271. this->SetError("called with incorrect number of arguments");
  272. return false;
  273. }
  274. // create a function blocker
  275. cmMacroFunctionBlocker *f = new cmMacroFunctionBlocker();
  276. for(std::vector<std::string>::const_iterator j = args.begin();
  277. j != args.end(); ++j)
  278. {
  279. f->m_Args.push_back(*j);
  280. }
  281. m_Makefile->AddFunctionBlocker(f);
  282. return true;
  283. }