cmFunctionCommand.cxx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmFunctionCommand.h"
  11. #include "cmake.h"
  12. // define the class for function commands
  13. class cmFunctionHelperCommand : public cmCommand
  14. {
  15. public:
  16. cmFunctionHelperCommand() {}
  17. ///! clean up any memory allocated by the function
  18. ~cmFunctionHelperCommand() {};
  19. /**
  20. * This is used to avoid including this command
  21. * in documentation. This is mainly used by
  22. * cmMacroHelperCommand and cmFunctionHelperCommand
  23. * which cannot provide appropriate documentation.
  24. */
  25. virtual bool ShouldAppearInDocumentation() const
  26. {
  27. return false;
  28. }
  29. /**
  30. * This is a virtual constructor for the command.
  31. */
  32. virtual cmCommand* Clone()
  33. {
  34. cmFunctionHelperCommand *newC = new cmFunctionHelperCommand;
  35. // we must copy when we clone
  36. newC->Args = this->Args;
  37. newC->Functions = this->Functions;
  38. newC->Policies = this->Policies;
  39. return newC;
  40. }
  41. /**
  42. * This determines if the command is invoked when in script mode.
  43. */
  44. virtual bool IsScriptable() const { return true; }
  45. /**
  46. * This is called when the command is first encountered in
  47. * the CMakeLists.txt file.
  48. */
  49. virtual bool InvokeInitialPass(const std::vector<cmListFileArgument>& args,
  50. cmExecutionStatus &);
  51. virtual bool InitialPass(std::vector<std::string> const&,
  52. cmExecutionStatus &) { return false; };
  53. /**
  54. * The name of the command as specified in CMakeList.txt.
  55. */
  56. virtual const char* GetName() const { return this->Args[0].c_str(); }
  57. /**
  58. * Succinct documentation.
  59. */
  60. virtual const char* GetTerseDocumentation() const
  61. {
  62. std::string docs = "Function named: ";
  63. docs += this->GetName();
  64. return docs.c_str();
  65. }
  66. /**
  67. * More documentation.
  68. */
  69. virtual const char* GetFullDocumentation() const
  70. {
  71. return this->GetTerseDocumentation();
  72. }
  73. cmTypeMacro(cmFunctionHelperCommand, cmCommand);
  74. std::vector<std::string> Args;
  75. std::vector<cmListFileFunction> Functions;
  76. cmPolicies::PolicyMap Policies;
  77. };
  78. bool cmFunctionHelperCommand::InvokeInitialPass
  79. (const std::vector<cmListFileArgument>& args,
  80. cmExecutionStatus & inStatus)
  81. {
  82. // Expand the argument list to the function.
  83. std::vector<std::string> expandedArgs;
  84. this->Makefile->ExpandArguments(args, expandedArgs);
  85. // make sure the number of arguments passed is at least the number
  86. // required by the signature
  87. if (expandedArgs.size() < this->Args.size() - 1)
  88. {
  89. std::string errorMsg =
  90. "Function invoked with incorrect arguments for function named: ";
  91. errorMsg += this->Args[0];
  92. this->SetError(errorMsg.c_str());
  93. return false;
  94. }
  95. // we push a scope on the makefile
  96. cmMakefile::LexicalPushPop lexScope(this->Makefile);
  97. cmMakefile::ScopePushPop varScope(this->Makefile);
  98. static_cast<void>(varScope);
  99. // Push a weak policy scope which restores the policies recorded at
  100. // function creation.
  101. cmMakefile::PolicyPushPop polScope(this->Makefile, true, this->Policies);
  102. // set the value of argc
  103. cmOStringStream strStream;
  104. strStream << expandedArgs.size();
  105. this->Makefile->AddDefinition("ARGC",strStream.str().c_str());
  106. this->Makefile->MarkVariableAsUsed("ARGC");
  107. // set the values for ARGV0 ARGV1 ...
  108. for (unsigned int t = 0; t < expandedArgs.size(); ++t)
  109. {
  110. cmOStringStream tmpStream;
  111. tmpStream << "ARGV" << t;
  112. this->Makefile->AddDefinition(tmpStream.str().c_str(),
  113. expandedArgs[t].c_str());
  114. this->Makefile->MarkVariableAsUsed(tmpStream.str().c_str());
  115. }
  116. // define the formal arguments
  117. for (unsigned int j = 1; j < this->Args.size(); ++j)
  118. {
  119. this->Makefile->AddDefinition(this->Args[j].c_str(),
  120. expandedArgs[j-1].c_str());
  121. }
  122. // define ARGV and ARGN
  123. std::vector<std::string>::const_iterator eit;
  124. std::string argvDef;
  125. std::string argnDef;
  126. unsigned int cnt = 0;
  127. for ( eit = expandedArgs.begin(); eit != expandedArgs.end(); ++eit )
  128. {
  129. if ( argvDef.size() > 0 )
  130. {
  131. argvDef += ";";
  132. }
  133. argvDef += *eit;
  134. if ( cnt >= this->Args.size()-1 )
  135. {
  136. if ( argnDef.size() > 0 )
  137. {
  138. argnDef += ";";
  139. }
  140. argnDef += *eit;
  141. }
  142. cnt ++;
  143. }
  144. this->Makefile->AddDefinition("ARGV", argvDef.c_str());
  145. this->Makefile->MarkVariableAsUsed("ARGV");
  146. this->Makefile->AddDefinition("ARGN", argnDef.c_str());
  147. this->Makefile->MarkVariableAsUsed("ARGN");
  148. // Invoke all the functions that were collected in the block.
  149. // for each function
  150. for(unsigned int c = 0; c < this->Functions.size(); ++c)
  151. {
  152. cmExecutionStatus status;
  153. if (!this->Makefile->ExecuteCommand(this->Functions[c],status) ||
  154. status.GetNestedError())
  155. {
  156. // The error message should have already included the call stack
  157. // so we do not need to report an error here.
  158. lexScope.Quiet();
  159. polScope.Quiet();
  160. inStatus.SetNestedError(true);
  161. return false;
  162. }
  163. if (status.GetReturnInvoked())
  164. {
  165. return true;
  166. }
  167. }
  168. // pop scope on the makefile
  169. return true;
  170. }
  171. bool cmFunctionFunctionBlocker::
  172. IsFunctionBlocked(const cmListFileFunction& lff, cmMakefile &mf,
  173. cmExecutionStatus &)
  174. {
  175. // record commands until we hit the ENDFUNCTION
  176. // at the ENDFUNCTION call we shift gears and start looking for invocations
  177. if(!cmSystemTools::Strucmp(lff.Name.c_str(),"function"))
  178. {
  179. this->Depth++;
  180. }
  181. else if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endfunction"))
  182. {
  183. // if this is the endfunction for this function then execute
  184. if (!this->Depth)
  185. {
  186. std::string name = this->Args[0];
  187. std::vector<std::string>::size_type cc;
  188. name += "(";
  189. for ( cc = 0; cc < this->Args.size(); cc ++ )
  190. {
  191. name += " " + this->Args[cc];
  192. }
  193. name += " )";
  194. // create a new command and add it to cmake
  195. cmFunctionHelperCommand *f = new cmFunctionHelperCommand();
  196. f->Args = this->Args;
  197. f->Functions = this->Functions;
  198. mf.RecordPolicies(f->Policies);
  199. // Set the FilePath on the arguments to match the function since it is
  200. // not stored and the original values may be freed
  201. for (unsigned int i = 0; i < f->Functions.size(); ++i)
  202. {
  203. for (unsigned int j = 0; j < f->Functions[i].Arguments.size(); ++j)
  204. {
  205. f->Functions[i].Arguments[j].FilePath =
  206. f->Functions[i].FilePath.c_str();
  207. }
  208. }
  209. std::string newName = "_" + this->Args[0];
  210. mf.GetCMakeInstance()->RenameCommand(this->Args[0].c_str(),
  211. newName.c_str());
  212. mf.AddCommand(f);
  213. // remove the function blocker now that the function is defined
  214. mf.RemoveFunctionBlocker(this, lff);
  215. return true;
  216. }
  217. else
  218. {
  219. // decrement for each nested function that ends
  220. this->Depth--;
  221. }
  222. }
  223. // if it wasn't an endfunction and we are not executing then we must be
  224. // recording
  225. this->Functions.push_back(lff);
  226. return true;
  227. }
  228. bool cmFunctionFunctionBlocker::
  229. ShouldRemove(const cmListFileFunction& lff, cmMakefile &mf)
  230. {
  231. if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endfunction"))
  232. {
  233. std::vector<std::string> expandedArguments;
  234. mf.ExpandArguments(lff.Arguments, expandedArguments);
  235. // if the endfunction has arguments then make sure
  236. // they match the ones in the opening function command
  237. if ((expandedArguments.empty() ||
  238. (expandedArguments[0] == this->Args[0])))
  239. {
  240. return true;
  241. }
  242. }
  243. return false;
  244. }
  245. bool cmFunctionCommand
  246. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  247. {
  248. if(args.size() < 1)
  249. {
  250. this->SetError("called with incorrect number of arguments");
  251. return false;
  252. }
  253. // create a function blocker
  254. cmFunctionFunctionBlocker *f = new cmFunctionFunctionBlocker();
  255. for(std::vector<std::string>::const_iterator j = args.begin();
  256. j != args.end(); ++j)
  257. {
  258. f->Args.push_back(*j);
  259. }
  260. this->Makefile->AddFunctionBlocker(f);
  261. return true;
  262. }