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