cmFunctionCommand.cxx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 std::string GetName() const { return this->Args[0]; }
  57. cmTypeMacro(cmFunctionHelperCommand, cmCommand);
  58. std::vector<std::string> Args;
  59. std::vector<cmListFileFunction> Functions;
  60. cmPolicies::PolicyMap Policies;
  61. };
  62. bool cmFunctionHelperCommand::InvokeInitialPass
  63. (const std::vector<cmListFileArgument>& args,
  64. cmExecutionStatus & inStatus)
  65. {
  66. // Expand the argument list to the function.
  67. std::vector<std::string> expandedArgs;
  68. this->Makefile->ExpandArguments(args, expandedArgs);
  69. // make sure the number of arguments passed is at least the number
  70. // required by the signature
  71. if (expandedArgs.size() < this->Args.size() - 1)
  72. {
  73. std::string errorMsg =
  74. "Function invoked with incorrect arguments for function named: ";
  75. errorMsg += this->Args[0];
  76. this->SetError(errorMsg);
  77. return false;
  78. }
  79. // we push a scope on the makefile
  80. cmMakefile::LexicalPushPop lexScope(this->Makefile);
  81. cmMakefile::ScopePushPop varScope(this->Makefile);
  82. static_cast<void>(varScope);
  83. // Push a weak policy scope which restores the policies recorded at
  84. // function creation.
  85. cmMakefile::PolicyPushPop polScope(this->Makefile, true, this->Policies);
  86. // set the value of argc
  87. std::ostringstream strStream;
  88. strStream << expandedArgs.size();
  89. this->Makefile->AddDefinition("ARGC",strStream.str().c_str());
  90. this->Makefile->MarkVariableAsUsed("ARGC");
  91. // set the values for ARGV0 ARGV1 ...
  92. for (unsigned int t = 0; t < expandedArgs.size(); ++t)
  93. {
  94. std::ostringstream tmpStream;
  95. tmpStream << "ARGV" << t;
  96. this->Makefile->AddDefinition(tmpStream.str(),
  97. expandedArgs[t].c_str());
  98. this->Makefile->MarkVariableAsUsed(tmpStream.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],
  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.empty())
  114. {
  115. argvDef += ";";
  116. }
  117. argvDef += *eit;
  118. if ( cnt >= this->Args.size()-1 )
  119. {
  120. if (!argnDef.empty())
  121. {
  122. argnDef += ";";
  123. }
  124. argnDef += *eit;
  125. }
  126. cnt ++;
  127. }
  128. this->Makefile->AddDefinition("ARGV", argvDef.c_str());
  129. this->Makefile->MarkVariableAsUsed("ARGV");
  130. this->Makefile->AddDefinition("ARGN", argnDef.c_str());
  131. this->Makefile->MarkVariableAsUsed("ARGN");
  132. // Invoke all the functions that were collected in the block.
  133. // for each function
  134. for(unsigned int c = 0; c < this->Functions.size(); ++c)
  135. {
  136. cmExecutionStatus status;
  137. if (!this->Makefile->ExecuteCommand(this->Functions[c],status) ||
  138. status.GetNestedError())
  139. {
  140. // The error message should have already included the call stack
  141. // so we do not need to report an error here.
  142. lexScope.Quiet();
  143. polScope.Quiet();
  144. inStatus.SetNestedError(true);
  145. return false;
  146. }
  147. if (status.GetReturnInvoked())
  148. {
  149. return true;
  150. }
  151. }
  152. // pop scope on the makefile
  153. return true;
  154. }
  155. bool cmFunctionFunctionBlocker::
  156. IsFunctionBlocked(const cmListFileFunction& lff, cmMakefile &mf,
  157. cmExecutionStatus &)
  158. {
  159. // record commands until we hit the ENDFUNCTION
  160. // at the ENDFUNCTION call we shift gears and start looking for invocations
  161. if(!cmSystemTools::Strucmp(lff.Name.c_str(),"function"))
  162. {
  163. this->Depth++;
  164. }
  165. else if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endfunction"))
  166. {
  167. // if this is the endfunction for this function then execute
  168. if (!this->Depth)
  169. {
  170. std::string name = this->Args[0];
  171. std::vector<std::string>::size_type cc;
  172. name += "(";
  173. for ( cc = 0; cc < this->Args.size(); cc ++ )
  174. {
  175. name += " " + this->Args[cc];
  176. }
  177. name += " )";
  178. // create a new command and add it to cmake
  179. cmFunctionHelperCommand *f = new cmFunctionHelperCommand();
  180. f->Args = this->Args;
  181. f->Functions = this->Functions;
  182. mf.RecordPolicies(f->Policies);
  183. // Set the FilePath on the arguments to match the function since it is
  184. // not stored and the original values may be freed
  185. for (unsigned int i = 0; i < f->Functions.size(); ++i)
  186. {
  187. for (unsigned int j = 0; j < f->Functions[i].Arguments.size(); ++j)
  188. {
  189. f->Functions[i].Arguments[j].FilePath =
  190. f->Functions[i].FilePath.c_str();
  191. }
  192. }
  193. std::string newName = "_" + this->Args[0];
  194. mf.GetCMakeInstance()->RenameCommand(this->Args[0],
  195. newName);
  196. mf.AddCommand(f);
  197. // remove the function blocker now that the function is defined
  198. mf.RemoveFunctionBlocker(this, lff);
  199. return true;
  200. }
  201. else
  202. {
  203. // decrement for each nested function that ends
  204. this->Depth--;
  205. }
  206. }
  207. // if it wasn't an endfunction and we are not executing then we must be
  208. // recording
  209. this->Functions.push_back(lff);
  210. return true;
  211. }
  212. bool cmFunctionFunctionBlocker::
  213. ShouldRemove(const cmListFileFunction& lff, cmMakefile &mf)
  214. {
  215. if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endfunction"))
  216. {
  217. std::vector<std::string> expandedArguments;
  218. mf.ExpandArguments(lff.Arguments, expandedArguments);
  219. // if the endfunction has arguments then make sure
  220. // they match the ones in the opening function command
  221. if ((expandedArguments.empty() ||
  222. (expandedArguments[0] == this->Args[0])))
  223. {
  224. return true;
  225. }
  226. }
  227. return false;
  228. }
  229. bool cmFunctionCommand
  230. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  231. {
  232. if(args.size() < 1)
  233. {
  234. this->SetError("called with incorrect number of arguments");
  235. return false;
  236. }
  237. // create a function blocker
  238. cmFunctionFunctionBlocker *f = new cmFunctionFunctionBlocker();
  239. f->Args.insert(f->Args.end(), args.begin(), args.end());
  240. this->Makefile->AddFunctionBlocker(f);
  241. return true;
  242. }