cmMacroCommand.cxx 7.9 KB

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