cmFunctionCommand.cxx 6.1 KB

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