1
0

cmMacroCommand.cxx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 "cmMacroCommand.h"
  4. #include <cstdio>
  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 "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. // define the class for macro commands
  21. class cmMacroHelperCommand
  22. {
  23. public:
  24. /**
  25. * This is called when the command is first encountered in
  26. * the CMakeLists.txt file.
  27. */
  28. bool operator()(std::vector<cmListFileArgument> const& args,
  29. cmExecutionStatus& inStatus) const;
  30. std::vector<std::string> Args;
  31. std::vector<cmListFileFunction> Functions;
  32. cmPolicies::PolicyMap Policies;
  33. std::string FilePath;
  34. };
  35. bool cmMacroHelperCommand::operator()(
  36. std::vector<cmListFileArgument> const& args,
  37. cmExecutionStatus& inStatus) const
  38. {
  39. cmMakefile& makefile = inStatus.GetMakefile();
  40. // Expand the argument list to the macro.
  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 =
  47. cmStrCat("Macro invoked with incorrect arguments for macro named: ",
  48. this->Args[0]);
  49. inStatus.SetError(errorMsg);
  50. return false;
  51. }
  52. cmMakefile::MacroPushPop macroScope(&makefile, this->FilePath,
  53. this->Policies);
  54. // set the value of argc
  55. std::string argcDef = std::to_string(expandedArgs.size());
  56. auto eit = expandedArgs.begin() + (this->Args.size() - 1);
  57. std::string expandedArgn = cmJoin(cmMakeRange(eit, expandedArgs.end()), ";");
  58. std::string expandedArgv = cmJoin(expandedArgs, ";");
  59. std::vector<std::string> variables;
  60. variables.reserve(this->Args.size() - 1);
  61. for (unsigned int j = 1; j < this->Args.size(); ++j) {
  62. variables.push_back("${" + this->Args[j] + "}");
  63. }
  64. std::vector<std::string> argVs;
  65. argVs.reserve(expandedArgs.size());
  66. char argvName[60];
  67. for (unsigned int j = 0; j < expandedArgs.size(); ++j) {
  68. sprintf(argvName, "${ARGV%u}", j);
  69. argVs.emplace_back(argvName);
  70. }
  71. // Invoke all the functions that were collected in the block.
  72. // for each function
  73. for (cmListFileFunction const& func : this->Functions) {
  74. // Replace the formal arguments and then invoke the command.
  75. std::vector<cmListFileArgument> newLFFArgs;
  76. newLFFArgs.reserve(func.Arguments().size());
  77. // for each argument of the current function
  78. for (cmListFileArgument const& k : func.Arguments()) {
  79. cmListFileArgument arg;
  80. arg.Value = k.Value;
  81. if (k.Delim != cmListFileArgument::Bracket) {
  82. // replace formal arguments
  83. for (unsigned int j = 0; j < variables.size(); ++j) {
  84. cmSystemTools::ReplaceString(arg.Value, variables[j],
  85. expandedArgs[j]);
  86. }
  87. // replace argc
  88. cmSystemTools::ReplaceString(arg.Value, "${ARGC}", argcDef);
  89. cmSystemTools::ReplaceString(arg.Value, "${ARGN}", expandedArgn);
  90. cmSystemTools::ReplaceString(arg.Value, "${ARGV}", expandedArgv);
  91. // if the current argument of the current function has ${ARGV in it
  92. // then try replacing ARGV values
  93. if (arg.Value.find("${ARGV") != std::string::npos) {
  94. for (unsigned int t = 0; t < expandedArgs.size(); ++t) {
  95. cmSystemTools::ReplaceString(arg.Value, argVs[t], expandedArgs[t]);
  96. }
  97. }
  98. }
  99. arg.Delim = k.Delim;
  100. arg.Line = k.Line;
  101. newLFFArgs.push_back(std::move(arg));
  102. }
  103. cmListFileFunction newLFF{ func.OriginalName(), func.Line(),
  104. std::move(newLFFArgs) };
  105. cmExecutionStatus status(makefile);
  106. if (!makefile.ExecuteCommand(newLFF, status) || status.GetNestedError()) {
  107. // The error message should have already included the call stack
  108. // so we do not need to report an error here.
  109. macroScope.Quiet();
  110. inStatus.SetNestedError();
  111. return false;
  112. }
  113. if (status.GetReturnInvoked()) {
  114. inStatus.SetReturnInvoked();
  115. return true;
  116. }
  117. if (status.GetBreakInvoked()) {
  118. inStatus.SetBreakInvoked();
  119. return true;
  120. }
  121. }
  122. return true;
  123. }
  124. class cmMacroFunctionBlocker : public cmFunctionBlocker
  125. {
  126. public:
  127. cm::string_view StartCommandName() const override { return "macro"_s; }
  128. cm::string_view EndCommandName() const override { return "endmacro"_s; }
  129. bool ArgumentsMatch(cmListFileFunction const&,
  130. cmMakefile& mf) const override;
  131. bool Replay(std::vector<cmListFileFunction> functions,
  132. cmExecutionStatus& status) override;
  133. std::vector<std::string> Args;
  134. };
  135. bool cmMacroFunctionBlocker::ArgumentsMatch(cmListFileFunction const& lff,
  136. cmMakefile& mf) const
  137. {
  138. std::vector<std::string> expandedArguments;
  139. mf.ExpandArguments(lff.Arguments(), expandedArguments);
  140. return expandedArguments.empty() || expandedArguments[0] == this->Args[0];
  141. }
  142. bool cmMacroFunctionBlocker::Replay(std::vector<cmListFileFunction> functions,
  143. cmExecutionStatus& status)
  144. {
  145. cmMakefile& mf = status.GetMakefile();
  146. mf.AppendProperty("MACROS", this->Args[0]);
  147. // create a new command and add it to cmake
  148. cmMacroHelperCommand f;
  149. f.Args = this->Args;
  150. f.Functions = std::move(functions);
  151. f.FilePath = this->GetStartingContext().FilePath;
  152. mf.RecordPolicies(f.Policies);
  153. return mf.GetState()->AddScriptedCommand(
  154. this->Args[0],
  155. BT<cmState::Command>(std::move(f),
  156. mf.GetBacktrace().Push(this->GetStartingContext())),
  157. mf);
  158. }
  159. }
  160. bool cmMacroCommand(std::vector<std::string> const& args,
  161. cmExecutionStatus& status)
  162. {
  163. if (args.empty()) {
  164. status.SetError("called with incorrect number of arguments");
  165. return false;
  166. }
  167. // create a function blocker
  168. {
  169. auto fb = cm::make_unique<cmMacroFunctionBlocker>();
  170. cm::append(fb->Args, args);
  171. status.GetMakefile().AddFunctionBlocker(std::move(fb));
  172. }
  173. return true;
  174. }