cmMacroCommand.cxx 6.4 KB

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