cmForEachCommand.cxx 6.1 KB

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