cmFunctionCommand.cxx 7.1 KB

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