cmForEachCommand.cxx 6.1 KB

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