cmForEachCommand.cxx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 <cstdio>
  5. #include <cstdlib>
  6. #include <utility>
  7. #include "cm_memory.hxx"
  8. #include "cm_static_string_view.hxx"
  9. #include "cm_string_view.hxx"
  10. #include "cmExecutionStatus.h"
  11. #include "cmFunctionBlocker.h"
  12. #include "cmListFileCache.h"
  13. #include "cmMakefile.h"
  14. #include "cmMessageType.h"
  15. #include "cmRange.h"
  16. #include "cmStringAlgorithms.h"
  17. #include "cmSystemTools.h"
  18. namespace {
  19. bool HandleInMode(std::vector<std::string> const& args, cmMakefile& makefile);
  20. class cmForEachFunctionBlocker : public cmFunctionBlocker
  21. {
  22. public:
  23. cmForEachFunctionBlocker(cmMakefile* mf);
  24. ~cmForEachFunctionBlocker() override;
  25. cm::string_view StartCommandName() const override { return "foreach"_s; }
  26. cm::string_view EndCommandName() const override { return "endforeach"_s; }
  27. bool ArgumentsMatch(cmListFileFunction const& lff,
  28. cmMakefile& mf) const override;
  29. bool Replay(std::vector<cmListFileFunction> functions,
  30. cmExecutionStatus& inStatus) override;
  31. std::vector<std::string> Args;
  32. private:
  33. cmMakefile* Makefile;
  34. };
  35. cmForEachFunctionBlocker::cmForEachFunctionBlocker(cmMakefile* mf)
  36. : Makefile(mf)
  37. {
  38. this->Makefile->PushLoopBlock();
  39. }
  40. cmForEachFunctionBlocker::~cmForEachFunctionBlocker()
  41. {
  42. this->Makefile->PopLoopBlock();
  43. }
  44. bool cmForEachFunctionBlocker::ArgumentsMatch(cmListFileFunction const& lff,
  45. cmMakefile& mf) const
  46. {
  47. std::vector<std::string> expandedArguments;
  48. mf.ExpandArguments(lff.Arguments, expandedArguments);
  49. return expandedArguments.empty() || expandedArguments[0] == this->Args[0];
  50. }
  51. bool cmForEachFunctionBlocker::Replay(
  52. std::vector<cmListFileFunction> functions, cmExecutionStatus& inStatus)
  53. {
  54. cmMakefile& mf = inStatus.GetMakefile();
  55. // at end of for each execute recorded commands
  56. // store the old value
  57. std::string oldDef;
  58. if (mf.GetDefinition(this->Args[0])) {
  59. oldDef = mf.GetDefinition(this->Args[0]);
  60. }
  61. for (std::string const& arg : cmMakeRange(this->Args).advance(1)) {
  62. // set the variable to the loop value
  63. mf.AddDefinition(this->Args[0], arg);
  64. // Invoke all the functions that were collected in the block.
  65. for (cmListFileFunction const& func : functions) {
  66. cmExecutionStatus status(mf);
  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. }
  92. bool cmForEachCommand(std::vector<std::string> const& args,
  93. cmExecutionStatus& status)
  94. {
  95. if (args.empty()) {
  96. status.SetError("called with incorrect number of arguments");
  97. return false;
  98. }
  99. if (args.size() > 1 && args[1] == "IN") {
  100. return HandleInMode(args, status.GetMakefile());
  101. }
  102. // create a function blocker
  103. auto fb = cm::make_unique<cmForEachFunctionBlocker>(&status.GetMakefile());
  104. if (args.size() > 1) {
  105. if (args[1] == "RANGE") {
  106. int start = 0;
  107. int stop = 0;
  108. int step = 0;
  109. if (args.size() == 3) {
  110. stop = atoi(args[2].c_str());
  111. }
  112. if (args.size() == 4) {
  113. start = atoi(args[2].c_str());
  114. stop = atoi(args[3].c_str());
  115. }
  116. if (args.size() == 5) {
  117. start = atoi(args[2].c_str());
  118. stop = atoi(args[3].c_str());
  119. step = atoi(args[4].c_str());
  120. }
  121. if (step == 0) {
  122. if (start > stop) {
  123. step = -1;
  124. } else {
  125. step = 1;
  126. }
  127. }
  128. if ((start > stop && step > 0) || (start < stop && step < 0) ||
  129. step == 0) {
  130. status.SetError(
  131. cmStrCat("called with incorrect range specification: start ", start,
  132. ", stop ", stop, ", step ", step));
  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. status.GetMakefile().AddFunctionBlocker(std::move(fb));
  157. return true;
  158. }
  159. namespace {
  160. bool HandleInMode(std::vector<std::string> const& args, cmMakefile& makefile)
  161. {
  162. auto fb = cm::make_unique<cmForEachFunctionBlocker>(&makefile);
  163. fb->Args.push_back(args[0]);
  164. enum Doing
  165. {
  166. DoingNone,
  167. DoingLists,
  168. DoingItems
  169. };
  170. Doing doing = DoingNone;
  171. for (unsigned int i = 2; i < args.size(); ++i) {
  172. if (doing == DoingItems) {
  173. fb->Args.push_back(args[i]);
  174. } else if (args[i] == "LISTS") {
  175. doing = DoingLists;
  176. } else if (args[i] == "ITEMS") {
  177. doing = DoingItems;
  178. } else if (doing == DoingLists) {
  179. const char* value = makefile.GetDefinition(args[i]);
  180. if (value && *value) {
  181. cmExpandList(value, fb->Args, true);
  182. }
  183. } else {
  184. makefile.IssueMessage(
  185. MessageType::FATAL_ERROR,
  186. cmStrCat("Unknown argument:\n", " ", args[i], "\n"));
  187. return true;
  188. }
  189. }
  190. makefile.AddFunctionBlocker(std::move(fb));
  191. return true;
  192. }
  193. }