cmFunctionCommand.cxx 7.7 KB

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