cmFunctionCommand.cxx 6.1 KB

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