cmForEachCommand.cxx 6.2 KB

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