cmFunctionCommand.cxx 6.0 KB

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