cmFunctionCommand.cxx 5.3 KB

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