cmForEachCommand.cxx 6.6 KB

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