cmMacroCommand.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 "cmMacroCommand.h"
  4. #include <stdio.h>
  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. #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. cmListFileFunction newLFF;
  73. // for each function
  74. for (cmListFileFunction const& func : this->Functions) {
  75. // Replace the formal arguments and then invoke the command.
  76. newLFF.Arguments.clear();
  77. newLFF.Arguments.reserve(func.Arguments.size());
  78. newLFF.Name = func.Name;
  79. newLFF.Line = func.Line;
  80. // for each argument of the current function
  81. for (cmListFileArgument const& k : func.Arguments) {
  82. cmListFileArgument arg;
  83. arg.Value = k.Value;
  84. if (k.Delim != cmListFileArgument::Bracket) {
  85. // replace formal arguments
  86. for (unsigned int j = 0; j < variables.size(); ++j) {
  87. cmSystemTools::ReplaceString(arg.Value, variables[j],
  88. expandedArgs[j]);
  89. }
  90. // replace argc
  91. cmSystemTools::ReplaceString(arg.Value, "${ARGC}", argcDef);
  92. cmSystemTools::ReplaceString(arg.Value, "${ARGN}", expandedArgn);
  93. cmSystemTools::ReplaceString(arg.Value, "${ARGV}", expandedArgv);
  94. // if the current argument of the current function has ${ARGV in it
  95. // then try replacing ARGV values
  96. if (arg.Value.find("${ARGV") != std::string::npos) {
  97. for (unsigned int t = 0; t < expandedArgs.size(); ++t) {
  98. cmSystemTools::ReplaceString(arg.Value, argVs[t], expandedArgs[t]);
  99. }
  100. }
  101. }
  102. arg.Delim = k.Delim;
  103. arg.Line = k.Line;
  104. newLFF.Arguments.push_back(std::move(arg));
  105. }
  106. cmExecutionStatus status(makefile);
  107. if (!makefile.ExecuteCommand(newLFF, status) || status.GetNestedError()) {
  108. // The error message should have already included the call stack
  109. // so we do not need to report an error here.
  110. macroScope.Quiet();
  111. inStatus.SetNestedError();
  112. return false;
  113. }
  114. if (status.GetReturnInvoked()) {
  115. inStatus.SetReturnInvoked();
  116. return true;
  117. }
  118. if (status.GetBreakInvoked()) {
  119. inStatus.SetBreakInvoked();
  120. return true;
  121. }
  122. }
  123. return true;
  124. }
  125. class cmMacroFunctionBlocker : public cmFunctionBlocker
  126. {
  127. public:
  128. cm::string_view StartCommandName() const override { return "macro"_s; }
  129. cm::string_view EndCommandName() const override { return "endmacro"_s; }
  130. bool ArgumentsMatch(cmListFileFunction const&,
  131. cmMakefile& mf) const override;
  132. bool Replay(std::vector<cmListFileFunction> functions,
  133. cmExecutionStatus& status) override;
  134. std::vector<std::string> Args;
  135. };
  136. bool cmMacroFunctionBlocker::ArgumentsMatch(cmListFileFunction const& lff,
  137. cmMakefile& mf) const
  138. {
  139. std::vector<std::string> expandedArguments;
  140. mf.ExpandArguments(lff.Arguments, expandedArguments,
  141. this->GetStartingContext().FilePath.c_str());
  142. return expandedArguments.empty() || expandedArguments[0] == this->Args[0];
  143. }
  144. bool cmMacroFunctionBlocker::Replay(std::vector<cmListFileFunction> functions,
  145. cmExecutionStatus& status)
  146. {
  147. cmMakefile& mf = status.GetMakefile();
  148. mf.AppendProperty("MACROS", this->Args[0].c_str());
  149. // create a new command and add it to cmake
  150. cmMacroHelperCommand f;
  151. f.Args = this->Args;
  152. f.Functions = std::move(functions);
  153. f.FilePath = this->GetStartingContext().FilePath;
  154. mf.RecordPolicies(f.Policies);
  155. mf.GetState()->AddScriptedCommand(this->Args[0], std::move(f));
  156. return true;
  157. }
  158. }
  159. bool cmMacroCommand(std::vector<std::string> const& args,
  160. cmExecutionStatus& status)
  161. {
  162. if (args.empty()) {
  163. status.SetError("called with incorrect number of arguments");
  164. return false;
  165. }
  166. // create a function blocker
  167. {
  168. auto fb = cm::make_unique<cmMacroFunctionBlocker>();
  169. cmAppend(fb->Args, args);
  170. status.GetMakefile().AddFunctionBlocker(std::move(fb));
  171. }
  172. return true;
  173. }