cmMacroCommand.cxx 7.7 KB

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