cmFunctionCommand.cxx 5.8 KB

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