cmFunctionCommand.cxx 5.3 KB

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