cmFunctionCommand.cxx 6.3 KB

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