cmMacroCommand.cxx 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 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 < m_Functions.size(); ++c)
  99. {
  100. // Replace the formal arguments and then invoke the command.
  101. newLFF.m_Arguments.clear();
  102. newLFF.m_Arguments.reserve(m_Functions[c].m_Arguments.size());
  103. newLFF.m_Name = m_Functions[c].m_Name;
  104. newLFF.m_FilePath = m_Functions[c].m_FilePath;
  105. newLFF.m_Line = m_Functions[c].m_Line;
  106. const char* def =
  107. m_Makefile->GetDefinition("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. m_Functions[c].m_Arguments.begin();
  116. k != m_Functions[c].m_Arguments.end(); ++k)
  117. {
  118. tmps = k->Value;
  119. // replace formal arguments
  120. for (unsigned int j = 1; j < m_Args.size(); ++j)
  121. {
  122. variable = "${";
  123. variable += m_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 >= m_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.m_Arguments.push_back(arg);
  206. }
  207. if(!m_Makefile->ExecuteCommand(newLFF))
  208. {
  209. cmOStringStream error;
  210. error << "Error in cmake code at\n"
  211. << args[0].FilePath << ":" << args[0].Line << ":\n"
  212. << "A command failed during the invocation of macro \""
  213. << this->m_Args[0].c_str() << "\".";
  214. cmSystemTools::Error(error.str().c_str());
  215. return false;
  216. }
  217. }
  218. return true;
  219. }
  220. bool cmMacroFunctionBlocker::
  221. IsFunctionBlocked(const cmListFileFunction& lff, cmMakefile &mf)
  222. {
  223. // record commands until we hit the ENDMACRO
  224. // at the ENDMACRO call we shift gears and start looking for invocations
  225. if(lff.m_Name == "ENDMACRO")
  226. {
  227. std::vector<std::string> expandedArguments;
  228. mf.ExpandArguments(lff.m_Arguments, expandedArguments);
  229. if(!expandedArguments.empty() && (expandedArguments[0] == m_Args[0]))
  230. {
  231. std::string name = m_Args[0];
  232. std::vector<std::string>::size_type cc;
  233. name += "(";
  234. for ( cc = 0; cc < m_Args.size(); cc ++ )
  235. {
  236. name += " " + m_Args[cc];
  237. }
  238. name += " )";
  239. mf.AddMacro(m_Args[0].c_str(), name.c_str());
  240. // create a new command and add it to cmake
  241. cmMacroHelperCommand *f = new cmMacroHelperCommand();
  242. f->m_Args = this->m_Args;
  243. f->m_Functions = this->m_Functions;
  244. std::string newName = "_" + this->m_Args[0];
  245. mf.GetCMakeInstance()->RenameCommand(this->m_Args[0].c_str(), newName.c_str());
  246. mf.AddCommand(f);
  247. // remove the function blocker now that the macro is defined
  248. mf.RemoveFunctionBlocker(lff);
  249. return true;
  250. }
  251. }
  252. // if it wasn't an endmacro and we are not executing then we must be
  253. // recording
  254. m_Functions.push_back(lff);
  255. return true;
  256. }
  257. bool cmMacroFunctionBlocker::
  258. ShouldRemove(const cmListFileFunction& lff, cmMakefile &mf)
  259. {
  260. if(lff.m_Name == "ENDMACRO")
  261. {
  262. std::vector<std::string> expandedArguments;
  263. mf.ExpandArguments(lff.m_Arguments, expandedArguments);
  264. if(!expandedArguments.empty() && (expandedArguments[0] == m_Args[0]))
  265. {
  266. return true;
  267. }
  268. }
  269. return false;
  270. }
  271. void cmMacroFunctionBlocker::
  272. ScopeEnded(cmMakefile &mf)
  273. {
  274. // macros should end with an EndMacro
  275. cmSystemTools::Error("The end of a CMakeLists file was reached with a MACRO statement that was not closed properly. Within the directory: ",
  276. mf.GetCurrentDirectory(), " with macro ",
  277. m_Args[0].c_str());
  278. }
  279. bool cmMacroCommand::InitialPass(std::vector<std::string> const& args)
  280. {
  281. if(args.size() < 1)
  282. {
  283. this->SetError("called with incorrect number of arguments");
  284. return false;
  285. }
  286. // create a function blocker
  287. cmMacroFunctionBlocker *f = new cmMacroFunctionBlocker();
  288. for(std::vector<std::string>::const_iterator j = args.begin();
  289. j != args.end(); ++j)
  290. {
  291. f->m_Args.push_back(*j);
  292. }
  293. m_Makefile->AddFunctionBlocker(f);
  294. return true;
  295. }