cmFunctionCommand.cxx 6.4 KB

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