cmMacroCommand.cxx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 "cmAlgorithms.h"
  9. #include "cmExecutionStatus.h"
  10. #include "cmFunctionBlocker.h"
  11. #include "cmListFileCache.h"
  12. #include "cmMakefile.h"
  13. #include "cmPolicies.h"
  14. #include "cmRange.h"
  15. #include "cmState.h"
  16. #include "cmStringAlgorithms.h"
  17. #include "cmSystemTools.h"
  18. // define the class for macro commands
  19. class cmMacroHelperCommand
  20. {
  21. public:
  22. /**
  23. * This is called when the command is first encountered in
  24. * the CMakeLists.txt file.
  25. */
  26. bool operator()(std::vector<cmListFileArgument> const& args,
  27. cmExecutionStatus& inStatus) const;
  28. std::vector<std::string> Args;
  29. std::vector<cmListFileFunction> Functions;
  30. cmPolicies::PolicyMap Policies;
  31. std::string FilePath;
  32. };
  33. bool cmMacroHelperCommand::operator()(
  34. std::vector<cmListFileArgument> const& args,
  35. cmExecutionStatus& inStatus) const
  36. {
  37. cmMakefile& makefile = inStatus.GetMakefile();
  38. // Expand the argument list to the macro.
  39. std::vector<std::string> expandedArgs;
  40. makefile.ExpandArguments(args, expandedArgs);
  41. // make sure the number of arguments passed is at least the number
  42. // required by the signature
  43. if (expandedArgs.size() < this->Args.size() - 1) {
  44. std::string errorMsg =
  45. "Macro invoked with incorrect arguments for macro named: ";
  46. errorMsg += this->Args[0];
  47. inStatus.SetError(errorMsg);
  48. return false;
  49. }
  50. cmMakefile::MacroPushPop macroScope(&makefile, this->FilePath,
  51. this->Policies);
  52. // set the value of argc
  53. std::ostringstream argcDefStream;
  54. argcDefStream << expandedArgs.size();
  55. std::string argcDef = argcDefStream.str();
  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. bool IsFunctionBlocked(const cmListFileFunction&, cmMakefile& mf,
  130. cmExecutionStatus&) override;
  131. bool ShouldRemove(const cmListFileFunction&, cmMakefile& mf) override;
  132. std::vector<std::string> Args;
  133. std::vector<cmListFileFunction> Functions;
  134. int Depth = 0;
  135. };
  136. bool cmMacroFunctionBlocker::IsFunctionBlocked(const cmListFileFunction& lff,
  137. cmMakefile& mf,
  138. cmExecutionStatus&)
  139. {
  140. // record commands until we hit the ENDMACRO
  141. // at the ENDMACRO call we shift gears and start looking for invocations
  142. if (lff.Name.Lower == "macro") {
  143. this->Depth++;
  144. } else if (lff.Name.Lower == "endmacro") {
  145. // if this is the endmacro for this macro then execute
  146. if (!this->Depth) {
  147. mf.AppendProperty("MACROS", this->Args[0].c_str());
  148. // create a new command and add it to cmake
  149. cmMacroHelperCommand f;
  150. f.Args = this->Args;
  151. f.Functions = this->Functions;
  152. f.FilePath = this->GetStartingContext().FilePath;
  153. mf.RecordPolicies(f.Policies);
  154. mf.GetState()->AddScriptedCommand(this->Args[0], std::move(f));
  155. // remove the function blocker now that the macro is defined
  156. mf.RemoveFunctionBlocker(this, lff);
  157. return true;
  158. }
  159. // decrement for each nested macro that ends
  160. this->Depth--;
  161. }
  162. // if it wasn't an endmacro and we are not executing then we must be
  163. // recording
  164. this->Functions.push_back(lff);
  165. return true;
  166. }
  167. bool cmMacroFunctionBlocker::ShouldRemove(const cmListFileFunction& lff,
  168. cmMakefile& mf)
  169. {
  170. if (lff.Name.Lower == "endmacro") {
  171. std::vector<std::string> expandedArguments;
  172. mf.ExpandArguments(lff.Arguments, expandedArguments,
  173. this->GetStartingContext().FilePath.c_str());
  174. // if the endmacro has arguments make sure they
  175. // match the arguments of the macro
  176. if ((expandedArguments.empty() ||
  177. (expandedArguments[0] == this->Args[0]))) {
  178. return true;
  179. }
  180. }
  181. return false;
  182. }
  183. bool cmMacroCommand::InitialPass(std::vector<std::string> const& args,
  184. cmExecutionStatus&)
  185. {
  186. if (args.empty()) {
  187. this->SetError("called with incorrect number of arguments");
  188. return false;
  189. }
  190. // create a function blocker
  191. {
  192. auto fb = cm::make_unique<cmMacroFunctionBlocker>();
  193. cmAppend(fb->Args, args);
  194. this->Makefile->AddFunctionBlocker(std::move(fb));
  195. }
  196. return true;
  197. }