cmForEachCommand.cxx 5.8 KB

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