cmForEachCommand.cxx 5.8 KB

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