cmFunctionCommand.cxx 5.0 KB

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