cmFunctionCommand.cxx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 <utility>
  5. #include <cm/memory>
  6. #include <cm/string_view>
  7. #include <cmext/algorithm>
  8. #include <cmext/string_view>
  9. #include "cmExecutionStatus.h"
  10. #include "cmFunctionBlocker.h"
  11. #include "cmList.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. #include "cmSystemTools.h"
  19. namespace {
  20. std::string const ARGC = "ARGC";
  21. std::string const ARGN = "ARGN";
  22. std::string const ARGV = "ARGV";
  23. std::string const CMAKE_CURRENT_FUNCTION = "CMAKE_CURRENT_FUNCTION";
  24. std::string const CMAKE_CURRENT_FUNCTION_LIST_FILE =
  25. "CMAKE_CURRENT_FUNCTION_LIST_FILE";
  26. std::string const CMAKE_CURRENT_FUNCTION_LIST_DIR =
  27. "CMAKE_CURRENT_FUNCTION_LIST_DIR";
  28. std::string const CMAKE_CURRENT_FUNCTION_LIST_LINE =
  29. "CMAKE_CURRENT_FUNCTION_LIST_LINE";
  30. // define the class for function commands
  31. class cmFunctionHelperCommand
  32. {
  33. public:
  34. /**
  35. * This is called when the command is first encountered in
  36. * the CMakeLists.txt file.
  37. */
  38. bool operator()(std::vector<cmListFileArgument> const& args,
  39. cmExecutionStatus& inStatus) const;
  40. std::vector<std::string> Args;
  41. std::vector<cmListFileFunction> Functions;
  42. cmPolicies::PolicyMap Policies;
  43. std::string FilePath;
  44. long Line;
  45. };
  46. bool cmFunctionHelperCommand::operator()(
  47. std::vector<cmListFileArgument> const& args,
  48. cmExecutionStatus& inStatus) const
  49. {
  50. cmMakefile& makefile = inStatus.GetMakefile();
  51. // Expand the argument list to the function.
  52. std::vector<std::string> expandedArgs;
  53. makefile.ExpandArguments(args, expandedArgs);
  54. // make sure the number of arguments passed is at least the number
  55. // required by the signature
  56. if (expandedArgs.size() < this->Args.size() - 1) {
  57. auto const errorMsg = cmStrCat(
  58. "Function invoked with incorrect arguments for function named: ",
  59. this->Args.front());
  60. inStatus.SetError(errorMsg);
  61. return false;
  62. }
  63. cmMakefile::FunctionPushPop functionScope(&makefile, this->FilePath,
  64. this->Policies);
  65. // set the value of argc
  66. makefile.AddDefinition(ARGC, std::to_string(expandedArgs.size()));
  67. makefile.MarkVariableAsUsed(ARGC);
  68. // set the values for ARGV0 ARGV1 ...
  69. for (auto t = 0u; t < expandedArgs.size(); ++t) {
  70. auto const value = cmStrCat(ARGV, t);
  71. makefile.AddDefinition(value, expandedArgs[t]);
  72. makefile.MarkVariableAsUsed(value);
  73. }
  74. // define the formal arguments
  75. for (auto j = 1u; j < this->Args.size(); ++j) {
  76. makefile.AddDefinition(this->Args[j], expandedArgs[j - 1]);
  77. }
  78. // define ARGV and ARGN
  79. auto const argvDef = cmList::to_string(expandedArgs);
  80. auto const expIt = expandedArgs.begin() + (this->Args.size() - 1);
  81. auto const argnDef =
  82. cmList::to_string(cmMakeRange(expIt, expandedArgs.end()));
  83. makefile.AddDefinition(ARGV, argvDef);
  84. makefile.MarkVariableAsUsed(ARGV);
  85. makefile.AddDefinition(ARGN, argnDef);
  86. makefile.MarkVariableAsUsed(ARGN);
  87. makefile.AddDefinition(CMAKE_CURRENT_FUNCTION, this->Args.front());
  88. makefile.MarkVariableAsUsed(CMAKE_CURRENT_FUNCTION);
  89. makefile.AddDefinition(CMAKE_CURRENT_FUNCTION_LIST_FILE, this->FilePath);
  90. makefile.MarkVariableAsUsed(CMAKE_CURRENT_FUNCTION_LIST_FILE);
  91. makefile.AddDefinition(CMAKE_CURRENT_FUNCTION_LIST_DIR,
  92. cmSystemTools::GetFilenamePath(this->FilePath));
  93. makefile.MarkVariableAsUsed(CMAKE_CURRENT_FUNCTION_LIST_DIR);
  94. makefile.AddDefinition(CMAKE_CURRENT_FUNCTION_LIST_LINE,
  95. std::to_string(this->Line));
  96. makefile.MarkVariableAsUsed(CMAKE_CURRENT_FUNCTION_LIST_LINE);
  97. // Invoke all the functions that were collected in the block.
  98. // for each function
  99. for (cmListFileFunction const& func : this->Functions) {
  100. cmExecutionStatus status(makefile);
  101. if (!makefile.ExecuteCommand(func, status) || status.GetNestedError()) {
  102. // The error message should have already included the call stack
  103. // so we do not need to report an error here.
  104. functionScope.Quiet();
  105. inStatus.SetNestedError();
  106. return false;
  107. }
  108. if (status.GetReturnInvoked()) {
  109. makefile.RaiseScope(status.GetReturnVariables());
  110. break;
  111. }
  112. if (status.HasExitCode()) {
  113. inStatus.SetExitCode(status.GetExitCode());
  114. break;
  115. }
  116. }
  117. // pop scope on the makefile
  118. return true;
  119. }
  120. class cmFunctionFunctionBlocker : public cmFunctionBlocker
  121. {
  122. public:
  123. cm::string_view StartCommandName() const override { return "function"_s; }
  124. cm::string_view EndCommandName() const override { return "endfunction"_s; }
  125. bool ArgumentsMatch(cmListFileFunction const&,
  126. cmMakefile& mf) const override;
  127. bool Replay(std::vector<cmListFileFunction> functions,
  128. cmExecutionStatus& status) override;
  129. std::vector<std::string> Args;
  130. };
  131. bool cmFunctionFunctionBlocker::ArgumentsMatch(cmListFileFunction const& lff,
  132. cmMakefile& mf) const
  133. {
  134. std::vector<std::string> expandedArguments;
  135. mf.ExpandArguments(lff.Arguments(), expandedArguments);
  136. return expandedArguments.empty() ||
  137. expandedArguments.front() == this->Args.front();
  138. }
  139. bool cmFunctionFunctionBlocker::Replay(
  140. std::vector<cmListFileFunction> functions, cmExecutionStatus& status)
  141. {
  142. cmMakefile& mf = status.GetMakefile();
  143. // create a new command and add it to cmake
  144. cmFunctionHelperCommand f;
  145. f.Args = this->Args;
  146. f.Functions = std::move(functions);
  147. f.FilePath = this->GetStartingContext().FilePath;
  148. f.Line = this->GetStartingContext().Line;
  149. mf.RecordPolicies(f.Policies);
  150. return mf.GetState()->AddScriptedCommand(
  151. this->Args.front(),
  152. BT<cmState::Command>(std::move(f),
  153. mf.GetBacktrace().Push(this->GetStartingContext())),
  154. mf);
  155. }
  156. } // anonymous namespace
  157. bool cmFunctionCommand(std::vector<std::string> const& args,
  158. cmExecutionStatus& status)
  159. {
  160. if (args.empty()) {
  161. status.SetError("called with incorrect number of arguments");
  162. return false;
  163. }
  164. // create a function blocker
  165. auto fb = cm::make_unique<cmFunctionFunctionBlocker>();
  166. cm::append(fb->Args, args);
  167. status.GetMakefile().AddFunctionBlocker(std::move(fb));
  168. return true;
  169. }