cmMacroCommand.cxx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 "cmAlgorithms.h"
  8. #include "cmExecutionStatus.h"
  9. #include "cmMakefile.h"
  10. #include "cmPolicies.h"
  11. #include "cmRange.h"
  12. #include "cmState.h"
  13. #include "cmSystemTools.h"
  14. // define the class for macro commands
  15. class cmMacroHelperCommand : public cmCommand
  16. {
  17. public:
  18. /**
  19. * This is a virtual constructor for the command.
  20. */
  21. cmCommand* Clone() override
  22. {
  23. cmMacroHelperCommand* newC = new cmMacroHelperCommand;
  24. // we must copy when we clone
  25. newC->Args = this->Args;
  26. newC->Functions = this->Functions;
  27. newC->FilePath = this->FilePath;
  28. newC->Policies = this->Policies;
  29. return newC;
  30. }
  31. /**
  32. * This is called when the command is first encountered in
  33. * the CMakeLists.txt file.
  34. */
  35. bool InvokeInitialPass(const std::vector<cmListFileArgument>& args,
  36. cmExecutionStatus&) override;
  37. bool InitialPass(std::vector<std::string> const&,
  38. cmExecutionStatus&) override
  39. {
  40. return false;
  41. }
  42. std::vector<std::string> Args;
  43. std::vector<cmListFileFunction> Functions;
  44. cmPolicies::PolicyMap Policies;
  45. std::string FilePath;
  46. };
  47. bool cmMacroHelperCommand::InvokeInitialPass(
  48. const std::vector<cmListFileArgument>& args, cmExecutionStatus& inStatus)
  49. {
  50. // Expand the argument list to the macro.
  51. std::vector<std::string> expandedArgs;
  52. this->Makefile->ExpandArguments(args, expandedArgs);
  53. // make sure the number of arguments passed is at least the number
  54. // required by the signature
  55. if (expandedArgs.size() < this->Args.size() - 1) {
  56. std::string errorMsg =
  57. "Macro invoked with incorrect arguments for macro named: ";
  58. errorMsg += this->Args[0];
  59. this->SetError(errorMsg);
  60. return false;
  61. }
  62. cmMakefile::MacroPushPop macroScope(this->Makefile, this->FilePath,
  63. this->Policies);
  64. // set the value of argc
  65. std::ostringstream argcDefStream;
  66. argcDefStream << expandedArgs.size();
  67. std::string argcDef = argcDefStream.str();
  68. std::vector<std::string>::const_iterator eit =
  69. expandedArgs.begin() + (this->Args.size() - 1);
  70. std::string expandedArgn = cmJoin(cmMakeRange(eit, expandedArgs.end()), ";");
  71. std::string expandedArgv = cmJoin(expandedArgs, ";");
  72. std::vector<std::string> variables;
  73. variables.reserve(this->Args.size() - 1);
  74. for (unsigned int j = 1; j < this->Args.size(); ++j) {
  75. variables.push_back("${" + this->Args[j] + "}");
  76. }
  77. std::vector<std::string> argVs;
  78. argVs.reserve(expandedArgs.size());
  79. char argvName[60];
  80. for (unsigned int j = 0; j < expandedArgs.size(); ++j) {
  81. sprintf(argvName, "${ARGV%u}", j);
  82. argVs.emplace_back(argvName);
  83. }
  84. // Invoke all the functions that were collected in the block.
  85. cmListFileFunction newLFF;
  86. // for each function
  87. for (cmListFileFunction const& func : this->Functions) {
  88. // Replace the formal arguments and then invoke the command.
  89. newLFF.Arguments.clear();
  90. newLFF.Arguments.reserve(func.Arguments.size());
  91. newLFF.Name = func.Name;
  92. newLFF.Line = func.Line;
  93. // for each argument of the current function
  94. for (cmListFileArgument const& k : func.Arguments) {
  95. cmListFileArgument arg;
  96. arg.Value = k.Value;
  97. if (k.Delim != cmListFileArgument::Bracket) {
  98. // replace formal arguments
  99. for (unsigned int j = 0; j < variables.size(); ++j) {
  100. cmSystemTools::ReplaceString(arg.Value, variables[j],
  101. expandedArgs[j]);
  102. }
  103. // replace argc
  104. cmSystemTools::ReplaceString(arg.Value, "${ARGC}", argcDef);
  105. cmSystemTools::ReplaceString(arg.Value, "${ARGN}", expandedArgn);
  106. cmSystemTools::ReplaceString(arg.Value, "${ARGV}", expandedArgv);
  107. // if the current argument of the current function has ${ARGV in it
  108. // then try replacing ARGV values
  109. if (arg.Value.find("${ARGV") != std::string::npos) {
  110. for (unsigned int t = 0; t < expandedArgs.size(); ++t) {
  111. cmSystemTools::ReplaceString(arg.Value, argVs[t], expandedArgs[t]);
  112. }
  113. }
  114. }
  115. arg.Delim = k.Delim;
  116. arg.Line = k.Line;
  117. newLFF.Arguments.push_back(std::move(arg));
  118. }
  119. cmExecutionStatus status;
  120. if (!this->Makefile->ExecuteCommand(newLFF, status) ||
  121. status.GetNestedError()) {
  122. // The error message should have already included the call stack
  123. // so we do not need to report an error here.
  124. macroScope.Quiet();
  125. inStatus.SetNestedError();
  126. return false;
  127. }
  128. if (status.GetReturnInvoked()) {
  129. inStatus.SetReturnInvoked();
  130. return true;
  131. }
  132. if (status.GetBreakInvoked()) {
  133. inStatus.SetBreakInvoked();
  134. return true;
  135. }
  136. }
  137. return true;
  138. }
  139. bool cmMacroFunctionBlocker::IsFunctionBlocked(const cmListFileFunction& lff,
  140. cmMakefile& mf,
  141. cmExecutionStatus&)
  142. {
  143. // record commands until we hit the ENDMACRO
  144. // at the ENDMACRO call we shift gears and start looking for invocations
  145. if (lff.Name.Lower == "macro") {
  146. this->Depth++;
  147. } else if (lff.Name.Lower == "endmacro") {
  148. // if this is the endmacro for this macro then execute
  149. if (!this->Depth) {
  150. mf.AppendProperty("MACROS", this->Args[0].c_str());
  151. // create a new command and add it to cmake
  152. cmMacroHelperCommand* f = new cmMacroHelperCommand();
  153. f->Args = this->Args;
  154. f->Functions = this->Functions;
  155. f->FilePath = this->GetStartingContext().FilePath;
  156. mf.RecordPolicies(f->Policies);
  157. mf.GetState()->AddScriptedCommand(this->Args[0], f);
  158. // remove the function blocker now that the macro is defined
  159. mf.RemoveFunctionBlocker(this, lff);
  160. return true;
  161. }
  162. // decrement for each nested macro that ends
  163. this->Depth--;
  164. }
  165. // if it wasn't an endmacro and we are not executing then we must be
  166. // recording
  167. this->Functions.push_back(lff);
  168. return true;
  169. }
  170. bool cmMacroFunctionBlocker::ShouldRemove(const cmListFileFunction& lff,
  171. cmMakefile& mf)
  172. {
  173. if (lff.Name.Lower == "endmacro") {
  174. std::vector<std::string> expandedArguments;
  175. mf.ExpandArguments(lff.Arguments, expandedArguments,
  176. this->GetStartingContext().FilePath.c_str());
  177. // if the endmacro has arguments make sure they
  178. // match the arguments of the macro
  179. if ((expandedArguments.empty() ||
  180. (expandedArguments[0] == this->Args[0]))) {
  181. return true;
  182. }
  183. }
  184. return false;
  185. }
  186. bool cmMacroCommand::InitialPass(std::vector<std::string> const& args,
  187. cmExecutionStatus&)
  188. {
  189. if (args.empty()) {
  190. this->SetError("called with incorrect number of arguments");
  191. return false;
  192. }
  193. // create a function blocker
  194. cmMacroFunctionBlocker* f = new cmMacroFunctionBlocker();
  195. cmAppend(f->Args, args);
  196. this->Makefile->AddFunctionBlocker(f);
  197. return true;
  198. }