cmFunctionCommand.cxx 5.9 KB

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