cmMacroCommand.cxx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. bool cmMacroFunctionBlocker::
  15. IsFunctionBlocked(const cmListFileFunction& lff, cmMakefile &mf)
  16. {
  17. // record commands until we hit the ENDMACRO
  18. if (!m_Executing)
  19. {
  20. // at the ENDMACRO call we shift gears and start looking for invocations
  21. if(lff.m_Name == "ENDMACRO")
  22. {
  23. std::vector<std::string> expandedArguments;
  24. mf.ExpandArguments(lff.m_Arguments, expandedArguments);
  25. if(!expandedArguments.empty() && (expandedArguments[0] == m_Args[0]))
  26. {
  27. m_Executing = true;
  28. std::string name = m_Args[0];
  29. std::vector<std::string>::size_type cc;
  30. name += "(";
  31. for ( cc = 0; cc < m_Args.size(); cc ++ )
  32. {
  33. name += " " + m_Args[cc];
  34. }
  35. name += " )";
  36. mf.AddMacro(m_Args[0].c_str(), name.c_str());
  37. return true;
  38. }
  39. }
  40. // if it wasn't an endmacro and we are not executing then we must be
  41. // recording
  42. m_Functions.push_back(lff);
  43. return true;
  44. }
  45. // otherwise the macro has been recorded and we are executing
  46. // so we look for macro invocations
  47. if(lff.m_Name == m_Args[0])
  48. {
  49. std::string tmps;
  50. cmListFileArgument arg;
  51. std::string variable;
  52. // Expand the argument list to the macro.
  53. std::vector<std::string> expandedArguments;
  54. mf.ExpandArguments(lff.m_Arguments, expandedArguments);
  55. // make sure the number of arguments passed is at least the number
  56. // required by the signature
  57. if (expandedArguments.size() < m_Args.size() - 1)
  58. {
  59. cmOStringStream error;
  60. error << "Error in cmake code at\n"
  61. << lff.m_FilePath << ":" << lff.m_Line << ":\n"
  62. << "Invocation of macro \""
  63. << lff.m_Name.c_str() << "\" with incorrect number of arguments.";
  64. cmSystemTools::Error(error.str().c_str());
  65. return true;
  66. }
  67. // set the value of argc
  68. cmOStringStream argcDefStream;
  69. argcDefStream << expandedArguments.size();
  70. std::string argcDef = argcDefStream.str();
  71. // declare varuiables for ARGV ARGN but do not compute until needed
  72. std::string argvDef;
  73. std::string argnDef;
  74. bool argnDefInitialized = false;
  75. bool argvDefInitialized = false;
  76. // Invoke all the functions that were collected in the block.
  77. cmListFileFunction newLFF;
  78. // for each function
  79. for(unsigned int c = 0; c < m_Functions.size(); ++c)
  80. {
  81. // Replace the formal arguments and then invoke the command.
  82. newLFF.m_Arguments.clear();
  83. newLFF.m_Arguments.reserve(m_Functions[c].m_Arguments.size());
  84. newLFF.m_Name = m_Functions[c].m_Name;
  85. newLFF.m_FilePath = m_Functions[c].m_FilePath;
  86. newLFF.m_Line = m_Functions[c].m_Line;
  87. // for each argument of the current function
  88. for (std::vector<cmListFileArgument>::const_iterator k =
  89. m_Functions[c].m_Arguments.begin();
  90. k != m_Functions[c].m_Arguments.end(); ++k)
  91. {
  92. tmps = k->Value;
  93. // replace formal arguments
  94. for (unsigned int j = 1; j < m_Args.size(); ++j)
  95. {
  96. variable = "${";
  97. variable += m_Args[j];
  98. variable += "}";
  99. cmSystemTools::ReplaceString(tmps, variable.c_str(),
  100. expandedArguments[j-1].c_str());
  101. }
  102. // replace argc
  103. cmSystemTools::ReplaceString(tmps, "${ARGC}",argcDef.c_str());
  104. // repleace ARGN
  105. if (tmps.find("${ARGN}") != std::string::npos)
  106. {
  107. if (!argnDefInitialized)
  108. {
  109. std::vector<std::string>::iterator eit;
  110. std::vector<std::string>::size_type cnt = 0;
  111. for ( eit = expandedArguments.begin();
  112. eit != expandedArguments.end();
  113. ++ eit )
  114. {
  115. if ( cnt >= m_Args.size()-1 )
  116. {
  117. if ( argnDef.size() > 0 )
  118. {
  119. argnDef += ";";
  120. }
  121. argnDef += *eit;
  122. }
  123. cnt ++;
  124. }
  125. argnDefInitialized = true;
  126. }
  127. cmSystemTools::ReplaceString(tmps, "${ARGN}", argnDef.c_str());
  128. }
  129. // if the current argument of the current function has ${ARGV in it
  130. // then try replacing ARGV values
  131. if (tmps.find("${ARGV") != std::string::npos)
  132. {
  133. char argvName[60];
  134. // repleace ARGV, compute it only once
  135. if (!argvDefInitialized)
  136. {
  137. std::vector<std::string>::iterator eit;
  138. for ( eit = expandedArguments.begin();
  139. eit != expandedArguments.end();
  140. ++ eit )
  141. {
  142. if ( argvDef.size() > 0 )
  143. {
  144. argvDef += ";";
  145. }
  146. argvDef += *eit;
  147. }
  148. argvDefInitialized = true;
  149. }
  150. cmSystemTools::ReplaceString(tmps, "${ARGV}", argvDef.c_str());
  151. // also replace the ARGV1 ARGV2 ... etc
  152. for (unsigned int t = 0; t < expandedArguments.size(); ++t)
  153. {
  154. sprintf(argvName,"${ARGV%i}",t);
  155. cmSystemTools::ReplaceString(tmps, argvName,
  156. expandedArguments[t].c_str());
  157. }
  158. }
  159. arg.Value = tmps;
  160. arg.Quoted = k->Quoted;
  161. newLFF.m_Arguments.push_back(arg);
  162. }
  163. if(!mf.ExecuteCommand(newLFF))
  164. {
  165. cmOStringStream error;
  166. error << "Error in cmake code at\n"
  167. << lff.m_FilePath << ":" << lff.m_Line << ":\n"
  168. << "A command failed during the invocation of macro \""
  169. << lff.m_Name.c_str() << "\".";
  170. cmSystemTools::Error(error.str().c_str());
  171. }
  172. }
  173. return true;
  174. }
  175. // if not an invocation then it is just an ordinary line
  176. return false;
  177. }
  178. bool cmMacroFunctionBlocker::
  179. ShouldRemove(const cmListFileFunction&, cmMakefile &)
  180. {
  181. return false;
  182. }
  183. void cmMacroFunctionBlocker::
  184. ScopeEnded(cmMakefile &mf)
  185. {
  186. // macros never leave scope but we should have seen the ENDMACRO call by now
  187. if (m_Executing != true)
  188. {
  189. cmSystemTools::Error("The end of a CMakeLists file was reached with a MACRO statement that was not closed properly. Within the directory: ",
  190. mf.GetCurrentDirectory(), " with macro ",
  191. m_Args[0].c_str());
  192. }
  193. }
  194. bool cmMacroCommand::InitialPass(std::vector<std::string> const& args)
  195. {
  196. if(args.size() < 1)
  197. {
  198. this->SetError("called with incorrect number of arguments");
  199. return false;
  200. }
  201. // create a function blocker
  202. cmMacroFunctionBlocker *f = new cmMacroFunctionBlocker();
  203. for(std::vector<std::string>::const_iterator j = args.begin();
  204. j != args.end(); ++j)
  205. {
  206. f->m_Args.push_back(*j);
  207. }
  208. m_Makefile->AddFunctionBlocker(f);
  209. return true;
  210. }