cmFunctionCommand.cxx 5.8 KB

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