cmMacroCommand.cxx 6.6 KB

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