cmFunctionCommand.cxx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 &)
  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. this->Makefile->PushScope();
  88. // set the value of argc
  89. cmOStringStream strStream;
  90. strStream << expandedArgs.size();
  91. this->Makefile->AddDefinition("ARGC",strStream.str().c_str());
  92. // set the values for ARGV0 ARGV1 ...
  93. for (unsigned int t = 0; t < expandedArgs.size(); ++t)
  94. {
  95. cmOStringStream tmpStream;
  96. tmpStream << "ARGV" << t;
  97. this->Makefile->AddDefinition(tmpStream.str().c_str(),
  98. expandedArgs[t].c_str());
  99. }
  100. // define the formal arguments
  101. for (unsigned int j = 1; j < this->Args.size(); ++j)
  102. {
  103. this->Makefile->AddDefinition(this->Args[j].c_str(),
  104. expandedArgs[j-1].c_str());
  105. }
  106. // define ARGV and ARGN
  107. std::vector<std::string>::const_iterator eit;
  108. std::string argvDef;
  109. std::string argnDef;
  110. unsigned int cnt = 0;
  111. for ( eit = expandedArgs.begin(); eit != expandedArgs.end(); ++eit )
  112. {
  113. if ( argvDef.size() > 0 )
  114. {
  115. argvDef += ";";
  116. }
  117. argvDef += *eit;
  118. if ( cnt >= this->Args.size()-1 )
  119. {
  120. if ( argnDef.size() > 0 )
  121. {
  122. argnDef += ";";
  123. }
  124. argnDef += *eit;
  125. }
  126. cnt ++;
  127. }
  128. this->Makefile->AddDefinition("ARGV", argvDef.c_str());
  129. this->Makefile->AddDefinition("ARGN", argnDef.c_str());
  130. // Invoke all the functions that were collected in the block.
  131. // for each function
  132. for(unsigned int c = 0; c < this->Functions.size(); ++c)
  133. {
  134. cmExecutionStatus status;
  135. if (!this->Makefile->ExecuteCommand(this->Functions[c],status))
  136. {
  137. cmOStringStream error;
  138. error << "Error in cmake code at\n"
  139. << this->Functions[c].FilePath << ":"
  140. << this->Functions[c].Line << ":\n"
  141. << "A command failed during the invocation of function \""
  142. << this->Args[0].c_str() << "\".";
  143. cmSystemTools::Error(error.str().c_str());
  144. // pop scope on the makefile and return
  145. this->Makefile->PopScope();
  146. return false;
  147. }
  148. if (status.GetReturnInvoked())
  149. {
  150. this->Makefile->PopScope();
  151. return true;
  152. }
  153. }
  154. // pop scope on the makefile
  155. this->Makefile->PopScope();
  156. return true;
  157. }
  158. bool cmFunctionFunctionBlocker::
  159. IsFunctionBlocked(const cmListFileFunction& lff, cmMakefile &mf,
  160. cmExecutionStatus &)
  161. {
  162. // record commands until we hit the ENDFUNCTION
  163. // at the ENDFUNCTION call we shift gears and start looking for invocations
  164. if(!cmSystemTools::Strucmp(lff.Name.c_str(),"function"))
  165. {
  166. this->Depth++;
  167. }
  168. else if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endfunction"))
  169. {
  170. // if this is the endfunction for this function then execute
  171. if (!this->Depth)
  172. {
  173. std::string name = this->Args[0];
  174. std::vector<std::string>::size_type cc;
  175. name += "(";
  176. for ( cc = 0; cc < this->Args.size(); cc ++ )
  177. {
  178. name += " " + this->Args[cc];
  179. }
  180. name += " )";
  181. // create a new command and add it to cmake
  182. cmFunctionHelperCommand *f = new cmFunctionHelperCommand();
  183. f->Args = this->Args;
  184. f->Functions = this->Functions;
  185. // Set the FilePath on the arguments to match the function since it is
  186. // not stored and the original values may be freed
  187. for (unsigned int i = 0; i < f->Functions.size(); ++i)
  188. {
  189. for (unsigned int j = 0; j < f->Functions[i].Arguments.size(); ++j)
  190. {
  191. f->Functions[i].Arguments[j].FilePath =
  192. f->Functions[i].FilePath.c_str();
  193. }
  194. }
  195. std::string newName = "_" + this->Args[0];
  196. mf.GetCMakeInstance()->RenameCommand(this->Args[0].c_str(),
  197. newName.c_str());
  198. mf.AddCommand(f);
  199. // remove the function blocker now that the function is defined
  200. mf.RemoveFunctionBlocker(lff);
  201. return true;
  202. }
  203. else
  204. {
  205. // decrement for each nested function that ends
  206. this->Depth--;
  207. }
  208. }
  209. // if it wasn't an endfunction and we are not executing then we must be
  210. // recording
  211. this->Functions.push_back(lff);
  212. return true;
  213. }
  214. bool cmFunctionFunctionBlocker::
  215. ShouldRemove(const cmListFileFunction& lff, cmMakefile &mf)
  216. {
  217. if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endfunction"))
  218. {
  219. std::vector<std::string> expandedArguments;
  220. mf.ExpandArguments(lff.Arguments, expandedArguments);
  221. if ((!expandedArguments.empty() &&
  222. (expandedArguments[0] == this->Args[0]))
  223. || cmSystemTools::IsOn
  224. (mf.GetPropertyOrDefinition("CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS")))
  225. {
  226. return true;
  227. }
  228. }
  229. return false;
  230. }
  231. void cmFunctionFunctionBlocker::
  232. ScopeEnded(cmMakefile &mf)
  233. {
  234. // functions should end with an EndFunction
  235. cmSystemTools::Error(
  236. "The end of a CMakeLists file was reached with a FUNCTION statement that "
  237. "was not closed properly. Within the directory: ",
  238. mf.GetCurrentDirectory(), " with function ",
  239. this->Args[0].c_str());
  240. }
  241. bool cmFunctionCommand
  242. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  243. {
  244. if(args.size() < 1)
  245. {
  246. this->SetError("called with incorrect number of arguments");
  247. return false;
  248. }
  249. // create a function blocker
  250. cmFunctionFunctionBlocker *f = new cmFunctionFunctionBlocker();
  251. for(std::vector<std::string>::const_iterator j = args.begin();
  252. j != args.end(); ++j)
  253. {
  254. f->Args.push_back(*j);
  255. }
  256. this->Makefile->AddFunctionBlocker(f);
  257. return true;
  258. }