cmMacroCommand.cxx 9.3 KB

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