cmFunctionCommand.cxx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. for ( eit = expandedArgs.begin(); eit != expandedArgs.end(); ++eit )
  110. {
  111. if (!argvDef.empty())
  112. {
  113. argvDef += ";";
  114. }
  115. argvDef += *eit;
  116. }
  117. std::string argnDef;
  118. eit = expandedArgs.begin() + (this->Args.size()-1);
  119. for ( ; eit != expandedArgs.end(); ++eit)
  120. if (!argnDef.empty())
  121. {
  122. argnDef += ";";
  123. }
  124. argnDef += *eit;
  125. }
  126. this->Makefile->AddDefinition("ARGV", argvDef.c_str());
  127. this->Makefile->MarkVariableAsUsed("ARGV");
  128. this->Makefile->AddDefinition("ARGN", argnDef.c_str());
  129. this->Makefile->MarkVariableAsUsed("ARGN");
  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. status.GetNestedError())
  137. {
  138. // The error message should have already included the call stack
  139. // so we do not need to report an error here.
  140. lexScope.Quiet();
  141. polScope.Quiet();
  142. inStatus.SetNestedError(true);
  143. return false;
  144. }
  145. if (status.GetReturnInvoked())
  146. {
  147. return true;
  148. }
  149. }
  150. // pop scope on the makefile
  151. return true;
  152. }
  153. bool cmFunctionFunctionBlocker::
  154. IsFunctionBlocked(const cmListFileFunction& lff, cmMakefile &mf,
  155. cmExecutionStatus &)
  156. {
  157. // record commands until we hit the ENDFUNCTION
  158. // at the ENDFUNCTION call we shift gears and start looking for invocations
  159. if(!cmSystemTools::Strucmp(lff.Name.c_str(),"function"))
  160. {
  161. this->Depth++;
  162. }
  163. else if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endfunction"))
  164. {
  165. // if this is the endfunction for this function then execute
  166. if (!this->Depth)
  167. {
  168. std::string name = this->Args[0];
  169. name += "( ";
  170. name += cmJoin(this->Args, " ");
  171. name += " )";
  172. // create a new command and add it to cmake
  173. cmFunctionHelperCommand *f = new cmFunctionHelperCommand();
  174. f->Args = this->Args;
  175. f->Functions = this->Functions;
  176. mf.RecordPolicies(f->Policies);
  177. // Set the FilePath on the arguments to match the function since it is
  178. // not stored and the original values may be freed
  179. for (unsigned int i = 0; i < f->Functions.size(); ++i)
  180. {
  181. for (unsigned int j = 0; j < f->Functions[i].Arguments.size(); ++j)
  182. {
  183. f->Functions[i].Arguments[j].FilePath =
  184. f->Functions[i].FilePath.c_str();
  185. }
  186. }
  187. std::string newName = "_" + this->Args[0];
  188. mf.GetCMakeInstance()->RenameCommand(this->Args[0],
  189. newName);
  190. mf.AddCommand(f);
  191. // remove the function blocker now that the function is defined
  192. mf.RemoveFunctionBlocker(this, lff);
  193. return true;
  194. }
  195. else
  196. {
  197. // decrement for each nested function that ends
  198. this->Depth--;
  199. }
  200. }
  201. // if it wasn't an endfunction and we are not executing then we must be
  202. // recording
  203. this->Functions.push_back(lff);
  204. return true;
  205. }
  206. bool cmFunctionFunctionBlocker::
  207. ShouldRemove(const cmListFileFunction& lff, cmMakefile &mf)
  208. {
  209. if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endfunction"))
  210. {
  211. std::vector<std::string> expandedArguments;
  212. mf.ExpandArguments(lff.Arguments, expandedArguments);
  213. // if the endfunction has arguments then make sure
  214. // they match the ones in the opening function command
  215. if ((expandedArguments.empty() ||
  216. (expandedArguments[0] == this->Args[0])))
  217. {
  218. return true;
  219. }
  220. }
  221. return false;
  222. }
  223. bool cmFunctionCommand
  224. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  225. {
  226. if(args.size() < 1)
  227. {
  228. this->SetError("called with incorrect number of arguments");
  229. return false;
  230. }
  231. // create a function blocker
  232. cmFunctionFunctionBlocker *f = new cmFunctionFunctionBlocker();
  233. f->Args.insert(f->Args.end(), args.begin(), args.end());
  234. this->Makefile->AddFunctionBlocker(f);
  235. return true;
  236. }