cmFunctionCommand.cxx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. newC->FilePath = this->FilePath;
  40. return newC;
  41. }
  42. /**
  43. * This determines if the command is invoked when in script mode.
  44. */
  45. virtual bool IsScriptable() const { return true; }
  46. /**
  47. * This is called when the command is first encountered in
  48. * the CMakeLists.txt file.
  49. */
  50. virtual bool InvokeInitialPass(const std::vector<cmListFileArgument>& args,
  51. cmExecutionStatus &);
  52. virtual bool InitialPass(std::vector<std::string> const&,
  53. cmExecutionStatus &) { return false; }
  54. /**
  55. * The name of the command as specified in CMakeList.txt.
  56. */
  57. virtual std::string GetName() const { return this->Args[0]; }
  58. cmTypeMacro(cmFunctionHelperCommand, cmCommand);
  59. std::vector<std::string> Args;
  60. std::vector<cmListFileFunction> Functions;
  61. cmPolicies::PolicyMap Policies;
  62. std::string FilePath;
  63. };
  64. bool cmFunctionHelperCommand::InvokeInitialPass
  65. (const std::vector<cmListFileArgument>& args,
  66. cmExecutionStatus & inStatus)
  67. {
  68. // Expand the argument list to the function.
  69. std::vector<std::string> expandedArgs;
  70. this->Makefile->ExpandArguments(args, expandedArgs);
  71. // make sure the number of arguments passed is at least the number
  72. // required by the signature
  73. if (expandedArgs.size() < this->Args.size() - 1)
  74. {
  75. std::string errorMsg =
  76. "Function invoked with incorrect arguments for function named: ";
  77. errorMsg += this->Args[0];
  78. this->SetError(errorMsg);
  79. return false;
  80. }
  81. cmMakefile::FunctionPushPop functionScope(this->Makefile,
  82. this->Policies);
  83. // set the value of argc
  84. std::ostringstream strStream;
  85. strStream << expandedArgs.size();
  86. this->Makefile->AddDefinition("ARGC",strStream.str().c_str());
  87. this->Makefile->MarkVariableAsUsed("ARGC");
  88. // set the values for ARGV0 ARGV1 ...
  89. for (unsigned int t = 0; t < expandedArgs.size(); ++t)
  90. {
  91. std::ostringstream tmpStream;
  92. tmpStream << "ARGV" << t;
  93. this->Makefile->AddDefinition(tmpStream.str(),
  94. expandedArgs[t].c_str());
  95. this->Makefile->MarkVariableAsUsed(tmpStream.str());
  96. }
  97. // define the formal arguments
  98. for (unsigned int j = 1; j < this->Args.size(); ++j)
  99. {
  100. this->Makefile->AddDefinition(this->Args[j],
  101. expandedArgs[j-1].c_str());
  102. }
  103. // define ARGV and ARGN
  104. std::string argvDef = cmJoin(expandedArgs, ";");
  105. std::vector<std::string>::const_iterator eit
  106. = expandedArgs.begin() + (this->Args.size()-1);
  107. std::string argnDef = cmJoin(cmRange(eit, expandedArgs.end()), ";");
  108. this->Makefile->AddDefinition("ARGV", argvDef.c_str());
  109. this->Makefile->MarkVariableAsUsed("ARGV");
  110. this->Makefile->AddDefinition("ARGN", argnDef.c_str());
  111. this->Makefile->MarkVariableAsUsed("ARGN");
  112. // Invoke all the functions that were collected in the block.
  113. // for each function
  114. for(unsigned int c = 0; c < this->Functions.size(); ++c)
  115. {
  116. cmExecutionStatus status;
  117. if (!this->Makefile->ExecuteCommand(this->Functions[c],status) ||
  118. status.GetNestedError())
  119. {
  120. // The error message should have already included the call stack
  121. // so we do not need to report an error here.
  122. functionScope.Quiet();
  123. inStatus.SetNestedError(true);
  124. return false;
  125. }
  126. if (status.GetReturnInvoked())
  127. {
  128. return true;
  129. }
  130. }
  131. // pop scope on the makefile
  132. return true;
  133. }
  134. bool cmFunctionFunctionBlocker::
  135. IsFunctionBlocked(const cmListFileFunction& lff, cmMakefile &mf,
  136. cmExecutionStatus &)
  137. {
  138. // record commands until we hit the ENDFUNCTION
  139. // at the ENDFUNCTION call we shift gears and start looking for invocations
  140. if(!cmSystemTools::Strucmp(lff.Name.c_str(),"function"))
  141. {
  142. this->Depth++;
  143. }
  144. else if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endfunction"))
  145. {
  146. // if this is the endfunction for this function then execute
  147. if (!this->Depth)
  148. {
  149. // create a new command and add it to cmake
  150. cmFunctionHelperCommand *f = new cmFunctionHelperCommand();
  151. f->Args = this->Args;
  152. f->Functions = this->Functions;
  153. f->FilePath = this->GetStartingContext().FilePath;
  154. mf.RecordPolicies(f->Policies);
  155. std::string newName = "_" + this->Args[0];
  156. mf.GetState()->RenameCommand(this->Args[0], newName);
  157. mf.GetState()->AddCommand(f);
  158. // remove the function blocker now that the function is defined
  159. mf.RemoveFunctionBlocker(this, lff);
  160. return true;
  161. }
  162. else
  163. {
  164. // decrement for each nested function that ends
  165. this->Depth--;
  166. }
  167. }
  168. // if it wasn't an endfunction and we are not executing then we must be
  169. // recording
  170. this->Functions.push_back(lff);
  171. return true;
  172. }
  173. bool cmFunctionFunctionBlocker::
  174. ShouldRemove(const cmListFileFunction& lff, cmMakefile &mf)
  175. {
  176. if(!cmSystemTools::Strucmp(lff.Name.c_str(),"endfunction"))
  177. {
  178. std::vector<std::string> expandedArguments;
  179. mf.ExpandArguments(lff.Arguments, expandedArguments,
  180. this->GetStartingContext().FilePath.c_str());
  181. // if the endfunction has arguments then make sure
  182. // they match the ones in the opening function command
  183. if ((expandedArguments.empty() ||
  184. (expandedArguments[0] == this->Args[0])))
  185. {
  186. return true;
  187. }
  188. }
  189. return false;
  190. }
  191. bool cmFunctionCommand
  192. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  193. {
  194. if(args.size() < 1)
  195. {
  196. this->SetError("called with incorrect number of arguments");
  197. return false;
  198. }
  199. // create a function blocker
  200. cmFunctionFunctionBlocker *f = new cmFunctionFunctionBlocker();
  201. f->Args.insert(f->Args.end(), args.begin(), args.end());
  202. this->Makefile->AddFunctionBlocker(f);
  203. return true;
  204. }