cmMacroCommand.cxx 7.2 KB

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