cmMacroCommand.cxx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. const char* def =
  102. m_Makefile->GetDefinition("CMAKE_MACRO_REPORT_DEFINITION_LOCATION");
  103. bool macroReportLocation = false;
  104. if(def && !cmSystemTools::IsOff(def))
  105. {
  106. macroReportLocation = true;
  107. }
  108. // for each argument of the current function
  109. for (std::vector<cmListFileArgument>::const_iterator k =
  110. m_Functions[c].m_Arguments.begin();
  111. k != m_Functions[c].m_Arguments.end(); ++k)
  112. {
  113. tmps = k->Value;
  114. // replace formal arguments
  115. for (unsigned int j = 1; j < m_Args.size(); ++j)
  116. {
  117. variable = "${";
  118. variable += m_Args[j];
  119. variable += "}";
  120. cmSystemTools::ReplaceString(tmps, variable.c_str(),
  121. expandedArgs[j-1].c_str());
  122. }
  123. // replace argc
  124. cmSystemTools::ReplaceString(tmps, "${ARGC}",argcDef.c_str());
  125. // repleace ARGN
  126. if (tmps.find("${ARGN}") != std::string::npos)
  127. {
  128. if (!argnDefInitialized)
  129. {
  130. std::vector<std::string>::const_iterator eit;
  131. std::vector<std::string>::size_type cnt = 0;
  132. for ( eit = expandedArgs.begin(); eit != expandedArgs.end(); ++eit )
  133. {
  134. if ( cnt >= m_Args.size()-1 )
  135. {
  136. if ( argnDef.size() > 0 )
  137. {
  138. argnDef += ";";
  139. }
  140. argnDef += *eit;
  141. }
  142. cnt ++;
  143. }
  144. argnDefInitialized = true;
  145. }
  146. cmSystemTools::ReplaceString(tmps, "${ARGN}", argnDef.c_str());
  147. }
  148. // if the current argument of the current function has ${ARGV in it
  149. // then try replacing ARGV values
  150. if (tmps.find("${ARGV") != std::string::npos)
  151. {
  152. char argvName[60];
  153. // repleace ARGV, compute it only once
  154. if (!argvDefInitialized)
  155. {
  156. std::vector<std::string>::const_iterator eit;
  157. for ( eit = expandedArgs.begin(); eit != expandedArgs.end(); ++eit )
  158. {
  159. if ( argvDef.size() > 0 )
  160. {
  161. argvDef += ";";
  162. }
  163. argvDef += *eit;
  164. }
  165. argvDefInitialized = true;
  166. }
  167. cmSystemTools::ReplaceString(tmps, "${ARGV}", argvDef.c_str());
  168. // also replace the ARGV1 ARGV2 ... etc
  169. for (unsigned int t = 0; t < expandedArgs.size(); ++t)
  170. {
  171. sprintf(argvName,"${ARGV%i}",t);
  172. cmSystemTools::ReplaceString(tmps, argvName,
  173. expandedArgs[t].c_str());
  174. }
  175. }
  176. arg.Value = tmps;
  177. arg.Quoted = k->Quoted;
  178. if(macroReportLocation)
  179. {
  180. // Report the location of the argument where the macro was
  181. // defined.
  182. arg.FilePath = k->FilePath;
  183. arg.Line = k->Line;
  184. }
  185. else
  186. {
  187. // Report the location of the argument where the macro was
  188. // invoked.
  189. if (args.size())
  190. {
  191. arg.FilePath = args[0].FilePath;
  192. arg.Line = args[0].Line;
  193. }
  194. else
  195. {
  196. arg.FilePath = "Unknown";
  197. arg.Line = 0;
  198. }
  199. }
  200. newLFF.m_Arguments.push_back(arg);
  201. }
  202. if(!m_Makefile->ExecuteCommand(newLFF))
  203. {
  204. cmOStringStream error;
  205. error << "Error in cmake code at\n"
  206. << args[0].FilePath << ":" << args[0].Line << ":\n"
  207. << "A command failed during the invocation of macro \""
  208. << this->m_Args[0].c_str() << "\".";
  209. cmSystemTools::Error(error.str().c_str());
  210. return false;
  211. }
  212. }
  213. return true;
  214. }
  215. bool cmMacroFunctionBlocker::
  216. IsFunctionBlocked(const cmListFileFunction& lff, cmMakefile &mf)
  217. {
  218. // record commands until we hit the ENDMACRO
  219. // at the ENDMACRO call we shift gears and start looking for invocations
  220. if(lff.m_Name == "ENDMACRO")
  221. {
  222. std::vector<std::string> expandedArguments;
  223. mf.ExpandArguments(lff.m_Arguments, expandedArguments);
  224. if(!expandedArguments.empty() && (expandedArguments[0] == m_Args[0]))
  225. {
  226. std::string name = m_Args[0];
  227. std::vector<std::string>::size_type cc;
  228. name += "(";
  229. for ( cc = 0; cc < m_Args.size(); cc ++ )
  230. {
  231. name += " " + m_Args[cc];
  232. }
  233. name += " )";
  234. mf.AddMacro(m_Args[0].c_str(), name.c_str());
  235. // create a new command and add it to cmake
  236. cmMacroHelperCommand *f = new cmMacroHelperCommand();
  237. f->m_Args = this->m_Args;
  238. f->m_Functions = this->m_Functions;
  239. mf.AddCommand(f);
  240. // remove the function blocker now that the macro is defined
  241. mf.RemoveFunctionBlocker(lff);
  242. return true;
  243. }
  244. }
  245. // if it wasn't an endmacro and we are not executing then we must be
  246. // recording
  247. m_Functions.push_back(lff);
  248. return true;
  249. }
  250. bool cmMacroFunctionBlocker::
  251. ShouldRemove(const cmListFileFunction& lff, cmMakefile &mf)
  252. {
  253. if(lff.m_Name == "ENDMACRO")
  254. {
  255. std::vector<std::string> expandedArguments;
  256. mf.ExpandArguments(lff.m_Arguments, expandedArguments);
  257. if(!expandedArguments.empty() && (expandedArguments[0] == m_Args[0]))
  258. {
  259. return true;
  260. }
  261. }
  262. return false;
  263. }
  264. void cmMacroFunctionBlocker::
  265. ScopeEnded(cmMakefile &mf)
  266. {
  267. // macros should end with an EndMacro
  268. cmSystemTools::Error("The end of a CMakeLists file was reached with a MACRO statement that was not closed properly. Within the directory: ",
  269. mf.GetCurrentDirectory(), " with macro ",
  270. m_Args[0].c_str());
  271. }
  272. bool cmMacroCommand::InitialPass(std::vector<std::string> const& args)
  273. {
  274. if(args.size() < 1)
  275. {
  276. this->SetError("called with incorrect number of arguments");
  277. return false;
  278. }
  279. // create a function blocker
  280. cmMacroFunctionBlocker *f = new cmMacroFunctionBlocker();
  281. for(std::vector<std::string>::const_iterator j = args.begin();
  282. j != args.end(); ++j)
  283. {
  284. f->m_Args.push_back(*j);
  285. }
  286. m_Makefile->AddFunctionBlocker(f);
  287. return true;
  288. }