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