cmFunctionCommand.cxx 6.6 KB

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