cmForEachCommand.cxx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 "cmForEachCommand.h"
  4. #include <sstream>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <utility>
  8. #include "cm_memory.hxx"
  9. #include "cmExecutionStatus.h"
  10. #include "cmFunctionBlocker.h"
  11. #include "cmListFileCache.h"
  12. #include "cmMakefile.h"
  13. #include "cmMessageType.h"
  14. #include "cmRange.h"
  15. #include "cmSystemTools.h"
  16. class cmForEachFunctionBlocker : public cmFunctionBlocker
  17. {
  18. public:
  19. cmForEachFunctionBlocker(cmMakefile* mf);
  20. ~cmForEachFunctionBlocker() override;
  21. bool IsFunctionBlocked(const cmListFileFunction& lff, cmMakefile& mf,
  22. cmExecutionStatus&) override;
  23. bool ShouldRemove(const cmListFileFunction& lff, cmMakefile& mf) override;
  24. bool Replay(std::vector<cmListFileFunction> const& functions,
  25. cmExecutionStatus& inStatus);
  26. std::vector<std::string> Args;
  27. std::vector<cmListFileFunction> Functions;
  28. private:
  29. cmMakefile* Makefile;
  30. int Depth;
  31. };
  32. cmForEachFunctionBlocker::cmForEachFunctionBlocker(cmMakefile* mf)
  33. : Makefile(mf)
  34. , Depth(0)
  35. {
  36. this->Makefile->PushLoopBlock();
  37. }
  38. cmForEachFunctionBlocker::~cmForEachFunctionBlocker()
  39. {
  40. this->Makefile->PopLoopBlock();
  41. }
  42. bool cmForEachFunctionBlocker::IsFunctionBlocked(const cmListFileFunction& lff,
  43. cmMakefile& mf,
  44. cmExecutionStatus& inStatus)
  45. {
  46. if (lff.Name.Lower == "foreach") {
  47. // record the number of nested foreach commands
  48. this->Depth++;
  49. } else if (lff.Name.Lower == "endforeach") {
  50. // if this is the endofreach for this statement
  51. if (!this->Depth) {
  52. // Remove the function blocker for this scope or bail.
  53. std::unique_ptr<cmFunctionBlocker> fb(
  54. mf.RemoveFunctionBlocker(this, lff));
  55. if (!fb) {
  56. return false;
  57. }
  58. return this->Replay(this->Functions, inStatus);
  59. }
  60. // close out a nested foreach
  61. this->Depth--;
  62. }
  63. // record the command
  64. this->Functions.push_back(lff);
  65. // always return true
  66. return true;
  67. }
  68. bool cmForEachFunctionBlocker::ShouldRemove(const cmListFileFunction& lff,
  69. cmMakefile& mf)
  70. {
  71. if (lff.Name.Lower == "endforeach") {
  72. std::vector<std::string> expandedArguments;
  73. mf.ExpandArguments(lff.Arguments, expandedArguments);
  74. // if the endforeach has arguments then make sure
  75. // they match the begin foreach arguments
  76. if ((expandedArguments.empty() ||
  77. (expandedArguments[0] == this->Args[0]))) {
  78. return true;
  79. }
  80. }
  81. return false;
  82. }
  83. bool cmForEachFunctionBlocker::Replay(
  84. std::vector<cmListFileFunction> const& functions,
  85. cmExecutionStatus& inStatus)
  86. {
  87. cmMakefile& mf = inStatus.GetMakefile();
  88. // at end of for each execute recorded commands
  89. // store the old value
  90. std::string oldDef;
  91. if (mf.GetDefinition(this->Args[0])) {
  92. oldDef = mf.GetDefinition(this->Args[0]);
  93. }
  94. for (std::string const& arg : cmMakeRange(this->Args).advance(1)) {
  95. // set the variable to the loop value
  96. mf.AddDefinition(this->Args[0], arg);
  97. // Invoke all the functions that were collected in the block.
  98. cmExecutionStatus status(mf);
  99. for (cmListFileFunction const& func : functions) {
  100. status.Clear();
  101. mf.ExecuteCommand(func, status);
  102. if (status.GetReturnInvoked()) {
  103. inStatus.SetReturnInvoked();
  104. // restore the variable to its prior value
  105. mf.AddDefinition(this->Args[0], oldDef);
  106. return true;
  107. }
  108. if (status.GetBreakInvoked()) {
  109. // restore the variable to its prior value
  110. mf.AddDefinition(this->Args[0], oldDef);
  111. return true;
  112. }
  113. if (status.GetContinueInvoked()) {
  114. break;
  115. }
  116. if (cmSystemTools::GetFatalErrorOccured()) {
  117. return true;
  118. }
  119. }
  120. }
  121. // restore the variable to its prior value
  122. mf.AddDefinition(this->Args[0], oldDef);
  123. return true;
  124. }
  125. bool cmForEachCommand::InitialPass(std::vector<std::string> const& args,
  126. cmExecutionStatus&)
  127. {
  128. if (args.empty()) {
  129. this->SetError("called with incorrect number of arguments");
  130. return false;
  131. }
  132. if (args.size() > 1 && args[1] == "IN") {
  133. return this->HandleInMode(args);
  134. }
  135. // create a function blocker
  136. auto fb = cm::make_unique<cmForEachFunctionBlocker>(this->Makefile);
  137. if (args.size() > 1) {
  138. if (args[1] == "RANGE") {
  139. int start = 0;
  140. int stop = 0;
  141. int step = 0;
  142. if (args.size() == 3) {
  143. stop = atoi(args[2].c_str());
  144. }
  145. if (args.size() == 4) {
  146. start = atoi(args[2].c_str());
  147. stop = atoi(args[3].c_str());
  148. }
  149. if (args.size() == 5) {
  150. start = atoi(args[2].c_str());
  151. stop = atoi(args[3].c_str());
  152. step = atoi(args[4].c_str());
  153. }
  154. if (step == 0) {
  155. if (start > stop) {
  156. step = -1;
  157. } else {
  158. step = 1;
  159. }
  160. }
  161. if ((start > stop && step > 0) || (start < stop && step < 0) ||
  162. step == 0) {
  163. std::ostringstream str;
  164. str << "called with incorrect range specification: start ";
  165. str << start << ", stop " << stop << ", step " << step;
  166. this->SetError(str.str());
  167. return false;
  168. }
  169. std::vector<std::string> range;
  170. char buffer[100];
  171. range.push_back(args[0]);
  172. int cc;
  173. for (cc = start;; cc += step) {
  174. if ((step > 0 && cc > stop) || (step < 0 && cc < stop)) {
  175. break;
  176. }
  177. sprintf(buffer, "%d", cc);
  178. range.emplace_back(buffer);
  179. if (cc == stop) {
  180. break;
  181. }
  182. }
  183. fb->Args = range;
  184. } else {
  185. fb->Args = args;
  186. }
  187. } else {
  188. fb->Args = args;
  189. }
  190. this->Makefile->AddFunctionBlocker(std::move(fb));
  191. return true;
  192. }
  193. bool cmForEachCommand::HandleInMode(std::vector<std::string> const& args)
  194. {
  195. auto fb = cm::make_unique<cmForEachFunctionBlocker>(this->Makefile);
  196. fb->Args.push_back(args[0]);
  197. enum Doing
  198. {
  199. DoingNone,
  200. DoingLists,
  201. DoingItems
  202. };
  203. Doing doing = DoingNone;
  204. for (unsigned int i = 2; i < args.size(); ++i) {
  205. if (doing == DoingItems) {
  206. fb->Args.push_back(args[i]);
  207. } else if (args[i] == "LISTS") {
  208. doing = DoingLists;
  209. } else if (args[i] == "ITEMS") {
  210. doing = DoingItems;
  211. } else if (doing == DoingLists) {
  212. const char* value = this->Makefile->GetDefinition(args[i]);
  213. if (value && *value) {
  214. cmSystemTools::ExpandListArgument(value, fb->Args, true);
  215. }
  216. } else {
  217. std::ostringstream e;
  218. e << "Unknown argument:\n"
  219. << " " << args[i] << "\n";
  220. this->Makefile->IssueMessage(MessageType::FATAL_ERROR, e.str());
  221. return true;
  222. }
  223. }
  224. this->Makefile->AddFunctionBlocker(std::move(fb));
  225. return true;
  226. }