cmMacroCommand.cxx 8.3 KB

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