cmFunctionCommand.cxx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 "cmFunctionCommand.h"
  14. #include "cmake.h"
  15. // define the class for function commands
  16. class cmFunctionHelperCommand : public cmCommand
  17. {
  18. public:
  19. cmFunctionHelperCommand() {}
  20. ///! clean up any memory allocated by the function
  21. ~cmFunctionHelperCommand() {};
  22. /**
  23. * This is a virtual constructor for the command.
  24. */
  25. virtual cmCommand* Clone()
  26. {
  27. cmFunctionHelperCommand *newC = new cmFunctionHelperCommand;
  28. // we must copy when we clone
  29. newC->Args = this->Args;
  30. newC->Functions = this->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. cmExecutionStatus &);
  43. virtual bool InitialPass(std::vector<std::string> const&,
  44. cmExecutionStatus &) { return false; };
  45. /**
  46. * The name of the command as specified in CMakeList.txt.
  47. */
  48. virtual const char* GetName() { return this->Args[0].c_str(); }
  49. /**
  50. * Succinct documentation.
  51. */
  52. virtual const char* GetTerseDocumentation()
  53. {
  54. std::string docs = "Function named: ";
  55. docs += this->GetName();
  56. return docs.c_str();
  57. }
  58. /**
  59. * More documentation.
  60. */
  61. virtual const char* GetFullDocumentation()
  62. {
  63. return this->GetTerseDocumentation();
  64. }
  65. cmTypeMacro(cmFunctionHelperCommand, cmCommand);
  66. std::vector<std::string> Args;
  67. std::vector<cmListFileFunction> Functions;
  68. };
  69. bool cmFunctionHelperCommand::InvokeInitialPass
  70. (const std::vector<cmListFileArgument>& args,
  71. cmExecutionStatus & inStatus)
  72. {
  73. // Expand the argument list to the function.
  74. std::vector<std::string> expandedArgs;
  75. this->Makefile->ExpandArguments(args, expandedArgs);
  76. // make sure the number of arguments passed is at least the number
  77. // required by the signature
  78. if (expandedArgs.size() < this->Args.size() - 1)
  79. {
  80. std::string errorMsg =
  81. "Function invoked with incorrect arguments for function named: ";
  82. errorMsg += this->Args[0];
  83. this->SetError(errorMsg.c_str());
  84. return false;
  85. }
  86. // we push a scope on the makefile
  87. cmMakefile::ScopePushPop varScope(this->Makefile);
  88. static_cast<void>(varScope);
  89. // set the value of argc
  90. cmOStringStream strStream;
  91. strStream << expandedArgs.size();
  92. this->Makefile->AddDefinition("ARGC",strStream.str().c_str());
  93. // set the values for ARGV0 ARGV1 ...
  94. for (unsigned int t = 0; t < expandedArgs.size(); ++t)
  95. {
  96. cmOStringStream tmpStream;
  97. tmpStream << "ARGV" << t;
  98. this->Makefile->AddDefinition(tmpStream.str().c_str(),
  99. expandedArgs[t].c_str());
  100. }
  101. // define the formal arguments
  102. for (unsigned int j = 1; j < this->Args.size(); ++j)
  103. {
  104. this->Makefile->AddDefinition(this->Args[j].c_str(),
  105. expandedArgs[j-1].c_str());
  106. }
  107. // define ARGV and ARGN
  108. std::vector<std::string>::const_iterator eit;
  109. std::string argvDef;
  110. std::string argnDef;
  111. unsigned int cnt = 0;
  112. for ( eit = expandedArgs.begin(); eit != expandedArgs.end(); ++eit )
  113. {
  114. if ( argvDef.size() > 0 )
  115. {
  116. argvDef += ";";
  117. }
  118. argvDef += *eit;
  119. if ( cnt >= this->Args.size()-1 )
  120. {
  121. if ( argnDef.size() > 0 )
  122. {
  123. argnDef += ";";
  124. }
  125. argnDef += *eit;
  126. }
  127. cnt ++;
  128. }
  129. this->Makefile->AddDefinition("ARGV", argvDef.c_str());
  130. this->Makefile->AddDefinition("ARGN", argnDef.c_str());
  131. // Invoke all the functions that were collected in the block.
  132. // for each function
  133. for(unsigned int c = 0; c < this->Functions.size(); ++c)
  134. {
  135. cmExecutionStatus status;
  136. if (!this->Makefile->ExecuteCommand(this->Functions[c],status) ||
  137. status.GetNestedError())
  138. {
  139. // The error message should have already included the call stack
  140. // so we do not need to report an error here.
  141. inStatus.SetNestedError(true);
  142. return false;
  143. }
  144. if (status.GetReturnInvoked())
  145. {
  146. return true;
  147. }
  148. }
  149. // pop scope on the makefile
  150. return true;
  151. }
  152. bool cmFunctionFunctionBlocker::
  153. IsFunctionBlocked(const cmListFileFunction& lff, cmMakefile &mf,
  154. cmExecutionStatus &)
  155. {
  156. // record commands until we hit the ENDFUNCTION
  157. // at the ENDFUNCTION call we shift gears and start looking for invocations
  158. if(!cmSystemTools::Strucmp(lff.Name.c_str(),"function"))
  159. {
  160. this->Depth++;
  161. }
  162. else if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endfunction"))
  163. {
  164. // if this is the endfunction for this function then execute
  165. if (!this->Depth)
  166. {
  167. std::string name = this->Args[0];
  168. std::vector<std::string>::size_type cc;
  169. name += "(";
  170. for ( cc = 0; cc < this->Args.size(); cc ++ )
  171. {
  172. name += " " + this->Args[cc];
  173. }
  174. name += " )";
  175. // create a new command and add it to cmake
  176. cmFunctionHelperCommand *f = new cmFunctionHelperCommand();
  177. f->Args = this->Args;
  178. f->Functions = this->Functions;
  179. // Set the FilePath on the arguments to match the function since it is
  180. // not stored and the original values may be freed
  181. for (unsigned int i = 0; i < f->Functions.size(); ++i)
  182. {
  183. for (unsigned int j = 0; j < f->Functions[i].Arguments.size(); ++j)
  184. {
  185. f->Functions[i].Arguments[j].FilePath =
  186. f->Functions[i].FilePath.c_str();
  187. }
  188. }
  189. std::string newName = "_" + this->Args[0];
  190. mf.GetCMakeInstance()->RenameCommand(this->Args[0].c_str(),
  191. newName.c_str());
  192. mf.AddCommand(f);
  193. // remove the function blocker now that the function is defined
  194. mf.RemoveFunctionBlocker(lff);
  195. return true;
  196. }
  197. else
  198. {
  199. // decrement for each nested function that ends
  200. this->Depth--;
  201. }
  202. }
  203. // if it wasn't an endfunction and we are not executing then we must be
  204. // recording
  205. this->Functions.push_back(lff);
  206. return true;
  207. }
  208. bool cmFunctionFunctionBlocker::
  209. ShouldRemove(const cmListFileFunction& lff, cmMakefile &mf)
  210. {
  211. if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endfunction"))
  212. {
  213. std::vector<std::string> expandedArguments;
  214. mf.ExpandArguments(lff.Arguments, expandedArguments);
  215. // if the endfunction has arguments then make sure
  216. // they match the ones in the openeing function command
  217. if ((expandedArguments.empty() ||
  218. (expandedArguments[0] == this->Args[0])))
  219. {
  220. return true;
  221. }
  222. }
  223. return false;
  224. }
  225. void cmFunctionFunctionBlocker::
  226. ScopeEnded(cmMakefile &mf)
  227. {
  228. // functions should end with an EndFunction
  229. cmSystemTools::Error(
  230. "The end of a CMakeLists file was reached with a FUNCTION statement that "
  231. "was not closed properly. Within the directory: ",
  232. mf.GetCurrentDirectory(), " with function ",
  233. this->Args[0].c_str());
  234. }
  235. bool cmFunctionCommand
  236. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  237. {
  238. if(args.size() < 1)
  239. {
  240. this->SetError("called with incorrect number of arguments");
  241. return false;
  242. }
  243. // create a function blocker
  244. cmFunctionFunctionBlocker *f = new cmFunctionFunctionBlocker();
  245. for(std::vector<std::string>::const_iterator j = args.begin();
  246. j != args.end(); ++j)
  247. {
  248. f->Args.push_back(*j);
  249. }
  250. this->Makefile->AddFunctionBlocker(f);
  251. return true;
  252. }