cmListCommand.cxx 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496
  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 "cmListCommand.h"
  4. #include "cmsys/RegularExpression.hxx"
  5. #include <algorithm>
  6. #include <assert.h>
  7. #include <cstddef>
  8. #include <functional>
  9. #include <iterator>
  10. #include <set>
  11. #include <sstream>
  12. #include <stdexcept>
  13. #include <stdio.h>
  14. #include <stdlib.h> // required for atoi
  15. #include <utility>
  16. #include <vector>
  17. #include "cm_memory.hxx"
  18. #include "cm_static_string_view.hxx"
  19. #include "cmAlgorithms.h"
  20. #include "cmExecutionStatus.h"
  21. #include "cmGeneratorExpression.h"
  22. #include "cmMakefile.h"
  23. #include "cmMessageType.h"
  24. #include "cmPolicies.h"
  25. #include "cmRange.h"
  26. #include "cmStringAlgorithms.h"
  27. #include "cmStringReplaceHelper.h"
  28. #include "cmSubcommandTable.h"
  29. #include "cmSystemTools.h"
  30. namespace {
  31. bool FilterRegex(std::vector<std::string> const& args, bool includeMatches,
  32. std::string const& listName,
  33. std::vector<std::string>& varArgsExpanded,
  34. cmExecutionStatus& status);
  35. bool GetListString(std::string& listString, const std::string& var,
  36. const cmMakefile& makefile)
  37. {
  38. // get the old value
  39. const char* cacheValue = makefile.GetDefinition(var);
  40. if (!cacheValue) {
  41. return false;
  42. }
  43. listString = cacheValue;
  44. return true;
  45. }
  46. bool GetList(std::vector<std::string>& list, const std::string& var,
  47. const cmMakefile& makefile)
  48. {
  49. std::string listString;
  50. if (!GetListString(listString, var, makefile)) {
  51. return false;
  52. }
  53. // if the size of the list
  54. if (listString.empty()) {
  55. return true;
  56. }
  57. // expand the variable into a list
  58. cmExpandList(listString, list, true);
  59. // if no empty elements then just return
  60. if (!cmContains(list, std::string())) {
  61. return true;
  62. }
  63. // if we have empty elements we need to check policy CMP0007
  64. switch (makefile.GetPolicyStatus(cmPolicies::CMP0007)) {
  65. case cmPolicies::WARN: {
  66. // Default is to warn and use old behavior
  67. // OLD behavior is to allow compatibility, so recall
  68. // ExpandListArgument without the true which will remove
  69. // empty values
  70. list.clear();
  71. cmExpandList(listString, list);
  72. std::string warn =
  73. cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0007),
  74. " List has value = [", listString, "].");
  75. makefile.IssueMessage(MessageType::AUTHOR_WARNING, warn);
  76. return true;
  77. }
  78. case cmPolicies::OLD:
  79. // OLD behavior is to allow compatibility, so recall
  80. // ExpandListArgument without the true which will remove
  81. // empty values
  82. list.clear();
  83. cmExpandList(listString, list);
  84. return true;
  85. case cmPolicies::NEW:
  86. return true;
  87. case cmPolicies::REQUIRED_IF_USED:
  88. case cmPolicies::REQUIRED_ALWAYS:
  89. makefile.IssueMessage(
  90. MessageType::FATAL_ERROR,
  91. cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0007));
  92. return false;
  93. }
  94. return true;
  95. }
  96. bool HandleLengthCommand(std::vector<std::string> const& args,
  97. cmExecutionStatus& status)
  98. {
  99. if (args.size() != 3) {
  100. status.SetError("sub-command LENGTH requires two arguments.");
  101. return false;
  102. }
  103. const std::string& listName = args[1];
  104. const std::string& variableName = args.back();
  105. std::vector<std::string> varArgsExpanded;
  106. // do not check the return value here
  107. // if the list var is not found varArgsExpanded will have size 0
  108. // and we will return 0
  109. GetList(varArgsExpanded, listName, status.GetMakefile());
  110. size_t length = varArgsExpanded.size();
  111. char buffer[1024];
  112. sprintf(buffer, "%d", static_cast<int>(length));
  113. status.GetMakefile().AddDefinition(variableName, buffer);
  114. return true;
  115. }
  116. bool HandleGetCommand(std::vector<std::string> const& args,
  117. cmExecutionStatus& status)
  118. {
  119. if (args.size() < 4) {
  120. status.SetError("sub-command GET requires at least three arguments.");
  121. return false;
  122. }
  123. const std::string& listName = args[1];
  124. const std::string& variableName = args.back();
  125. // expand the variable
  126. std::vector<std::string> varArgsExpanded;
  127. if (!GetList(varArgsExpanded, listName, status.GetMakefile())) {
  128. status.GetMakefile().AddDefinition(variableName, "NOTFOUND");
  129. return true;
  130. }
  131. // FIXME: Add policy to make non-existing lists an error like empty lists.
  132. if (varArgsExpanded.empty()) {
  133. status.SetError("GET given empty list");
  134. return false;
  135. }
  136. std::string value;
  137. size_t cc;
  138. const char* sep = "";
  139. size_t nitem = varArgsExpanded.size();
  140. for (cc = 2; cc < args.size() - 1; cc++) {
  141. int item = atoi(args[cc].c_str());
  142. value += sep;
  143. sep = ";";
  144. if (item < 0) {
  145. item = static_cast<int>(nitem) + item;
  146. }
  147. if (item < 0 || nitem <= static_cast<size_t>(item)) {
  148. std::ostringstream str;
  149. str << "index: " << item << " out of range (-" << nitem << ", "
  150. << nitem - 1 << ")";
  151. status.SetError(str.str());
  152. return false;
  153. }
  154. value += varArgsExpanded[item];
  155. }
  156. status.GetMakefile().AddDefinition(variableName, value);
  157. return true;
  158. }
  159. bool HandleAppendCommand(std::vector<std::string> const& args,
  160. cmExecutionStatus& status)
  161. {
  162. assert(args.size() >= 2);
  163. // Skip if nothing to append.
  164. if (args.size() < 3) {
  165. return true;
  166. }
  167. cmMakefile& makefile = status.GetMakefile();
  168. std::string const& listName = args[1];
  169. // expand the variable
  170. std::string listString;
  171. GetListString(listString, listName, makefile);
  172. // If `listString` or `args` is empty, no need to append `;`,
  173. // then index is going to be `1` and points to the end-of-string ";"
  174. auto const offset =
  175. std::string::size_type(listString.empty() || args.empty());
  176. listString += &";"[offset] + cmJoin(cmMakeRange(args).advance(2), ";");
  177. makefile.AddDefinition(listName, listString);
  178. return true;
  179. }
  180. bool HandlePrependCommand(std::vector<std::string> const& args,
  181. cmExecutionStatus& status)
  182. {
  183. assert(args.size() >= 2);
  184. // Skip if nothing to prepend.
  185. if (args.size() < 3) {
  186. return true;
  187. }
  188. cmMakefile& makefile = status.GetMakefile();
  189. std::string const& listName = args[1];
  190. // expand the variable
  191. std::string listString;
  192. GetListString(listString, listName, makefile);
  193. // If `listString` or `args` is empty, no need to append `;`,
  194. // then `offset` is going to be `1` and points to the end-of-string ";"
  195. auto const offset =
  196. std::string::size_type(listString.empty() || args.empty());
  197. listString.insert(0,
  198. cmJoin(cmMakeRange(args).advance(2), ";") + &";"[offset]);
  199. makefile.AddDefinition(listName, listString);
  200. return true;
  201. }
  202. bool HandlePopBackCommand(std::vector<std::string> const& args,
  203. cmExecutionStatus& status)
  204. {
  205. assert(args.size() >= 2);
  206. cmMakefile& makefile = status.GetMakefile();
  207. auto ai = args.cbegin();
  208. ++ai; // Skip subcommand name
  209. std::string const& listName = *ai++;
  210. std::vector<std::string> varArgsExpanded;
  211. if (!GetList(varArgsExpanded, listName, makefile)) {
  212. // Can't get the list definition... undefine any vars given after.
  213. for (; ai != args.cend(); ++ai) {
  214. makefile.RemoveDefinition(*ai);
  215. }
  216. return true;
  217. }
  218. if (!varArgsExpanded.empty()) {
  219. if (ai == args.cend()) {
  220. // No variables are given... Just remove one element.
  221. varArgsExpanded.pop_back();
  222. } else {
  223. // Ok, assign elements to be removed to the given variables
  224. for (; !varArgsExpanded.empty() && ai != args.cend(); ++ai) {
  225. assert(!ai->empty());
  226. makefile.AddDefinition(*ai, varArgsExpanded.back());
  227. varArgsExpanded.pop_back();
  228. }
  229. // Undefine the rest variables if the list gets empty earlier...
  230. for (; ai != args.cend(); ++ai) {
  231. makefile.RemoveDefinition(*ai);
  232. }
  233. }
  234. makefile.AddDefinition(listName, cmJoin(varArgsExpanded, ";"));
  235. } else if (ai !=
  236. args.cend()) { // The list is empty, but some args were given
  237. // Need to *undefine* 'em all, cuz there are no items to assign...
  238. for (; ai != args.cend(); ++ai) {
  239. makefile.RemoveDefinition(*ai);
  240. }
  241. }
  242. return true;
  243. }
  244. bool HandlePopFrontCommand(std::vector<std::string> const& args,
  245. cmExecutionStatus& status)
  246. {
  247. assert(args.size() >= 2);
  248. cmMakefile& makefile = status.GetMakefile();
  249. auto ai = args.cbegin();
  250. ++ai; // Skip subcommand name
  251. std::string const& listName = *ai++;
  252. std::vector<std::string> varArgsExpanded;
  253. if (!GetList(varArgsExpanded, listName, makefile)) {
  254. // Can't get the list definition... undefine any vars given after.
  255. for (; ai != args.cend(); ++ai) {
  256. makefile.RemoveDefinition(*ai);
  257. }
  258. return true;
  259. }
  260. if (!varArgsExpanded.empty()) {
  261. if (ai == args.cend()) {
  262. // No variables are given... Just remove one element.
  263. varArgsExpanded.erase(varArgsExpanded.begin());
  264. } else {
  265. // Ok, assign elements to be removed to the given variables
  266. auto vi = varArgsExpanded.begin();
  267. for (; vi != varArgsExpanded.end() && ai != args.cend(); ++ai, ++vi) {
  268. assert(!ai->empty());
  269. makefile.AddDefinition(*ai, *vi);
  270. }
  271. varArgsExpanded.erase(varArgsExpanded.begin(), vi);
  272. // Undefine the rest variables if the list gets empty earlier...
  273. for (; ai != args.cend(); ++ai) {
  274. makefile.RemoveDefinition(*ai);
  275. }
  276. }
  277. makefile.AddDefinition(listName, cmJoin(varArgsExpanded, ";"));
  278. } else if (ai !=
  279. args.cend()) { // The list is empty, but some args were given
  280. // Need to *undefine* 'em all, cuz there are no items to assign...
  281. for (; ai != args.cend(); ++ai) {
  282. makefile.RemoveDefinition(*ai);
  283. }
  284. }
  285. return true;
  286. }
  287. bool HandleFindCommand(std::vector<std::string> const& args,
  288. cmExecutionStatus& status)
  289. {
  290. if (args.size() != 4) {
  291. status.SetError("sub-command FIND requires three arguments.");
  292. return false;
  293. }
  294. const std::string& listName = args[1];
  295. const std::string& variableName = args.back();
  296. // expand the variable
  297. std::vector<std::string> varArgsExpanded;
  298. if (!GetList(varArgsExpanded, listName, status.GetMakefile())) {
  299. status.GetMakefile().AddDefinition(variableName, "-1");
  300. return true;
  301. }
  302. auto it = std::find(varArgsExpanded.begin(), varArgsExpanded.end(), args[2]);
  303. if (it != varArgsExpanded.end()) {
  304. status.GetMakefile().AddDefinition(
  305. variableName,
  306. std::to_string(std::distance(varArgsExpanded.begin(), it)));
  307. return true;
  308. }
  309. status.GetMakefile().AddDefinition(variableName, "-1");
  310. return true;
  311. }
  312. bool HandleInsertCommand(std::vector<std::string> const& args,
  313. cmExecutionStatus& status)
  314. {
  315. if (args.size() < 4) {
  316. status.SetError("sub-command INSERT requires at least three arguments.");
  317. return false;
  318. }
  319. const std::string& listName = args[1];
  320. // expand the variable
  321. int item = atoi(args[2].c_str());
  322. std::vector<std::string> varArgsExpanded;
  323. if ((!GetList(varArgsExpanded, listName, status.GetMakefile()) ||
  324. varArgsExpanded.empty()) &&
  325. item != 0) {
  326. std::ostringstream str;
  327. str << "index: " << item << " out of range (0, 0)";
  328. status.SetError(str.str());
  329. return false;
  330. }
  331. if (!varArgsExpanded.empty()) {
  332. size_t nitem = varArgsExpanded.size();
  333. if (item < 0) {
  334. item = static_cast<int>(nitem) + item;
  335. }
  336. if (item < 0 || nitem < static_cast<size_t>(item)) {
  337. std::ostringstream str;
  338. str << "index: " << item << " out of range (-" << varArgsExpanded.size()
  339. << ", " << varArgsExpanded.size() << ")";
  340. status.SetError(str.str());
  341. return false;
  342. }
  343. }
  344. varArgsExpanded.insert(varArgsExpanded.begin() + item, args.begin() + 3,
  345. args.end());
  346. std::string value = cmJoin(varArgsExpanded, ";");
  347. status.GetMakefile().AddDefinition(listName, value);
  348. return true;
  349. }
  350. bool HandleJoinCommand(std::vector<std::string> const& args,
  351. cmExecutionStatus& status)
  352. {
  353. if (args.size() != 4) {
  354. std::ostringstream error;
  355. error << "sub-command JOIN requires three arguments (" << args.size() - 1
  356. << " found).";
  357. status.SetError(error.str());
  358. return false;
  359. }
  360. const std::string& listName = args[1];
  361. const std::string& glue = args[2];
  362. const std::string& variableName = args[3];
  363. // expand the variable
  364. std::vector<std::string> varArgsExpanded;
  365. if (!GetList(varArgsExpanded, listName, status.GetMakefile())) {
  366. status.GetMakefile().AddDefinition(variableName, "");
  367. return true;
  368. }
  369. std::string value =
  370. cmJoin(cmMakeRange(varArgsExpanded.begin(), varArgsExpanded.end()), glue);
  371. status.GetMakefile().AddDefinition(variableName, value);
  372. return true;
  373. }
  374. bool HandleRemoveItemCommand(std::vector<std::string> const& args,
  375. cmExecutionStatus& status)
  376. {
  377. if (args.size() < 3) {
  378. status.SetError("sub-command REMOVE_ITEM requires two or more arguments.");
  379. return false;
  380. }
  381. const std::string& listName = args[1];
  382. // expand the variable
  383. std::vector<std::string> varArgsExpanded;
  384. if (!GetList(varArgsExpanded, listName, status.GetMakefile())) {
  385. return true;
  386. }
  387. std::vector<std::string> remove(args.begin() + 2, args.end());
  388. std::sort(remove.begin(), remove.end());
  389. auto remEnd = std::unique(remove.begin(), remove.end());
  390. auto remBegin = remove.begin();
  391. auto argsEnd =
  392. cmRemoveMatching(varArgsExpanded, cmMakeRange(remBegin, remEnd));
  393. auto argsBegin = varArgsExpanded.cbegin();
  394. std::string value = cmJoin(cmMakeRange(argsBegin, argsEnd), ";");
  395. status.GetMakefile().AddDefinition(listName, value);
  396. return true;
  397. }
  398. bool HandleReverseCommand(std::vector<std::string> const& args,
  399. cmExecutionStatus& status)
  400. {
  401. assert(args.size() >= 2);
  402. if (args.size() > 2) {
  403. status.SetError("sub-command REVERSE only takes one argument.");
  404. return false;
  405. }
  406. const std::string& listName = args[1];
  407. // expand the variable
  408. std::vector<std::string> varArgsExpanded;
  409. if (!GetList(varArgsExpanded, listName, status.GetMakefile())) {
  410. return true;
  411. }
  412. std::string value = cmJoin(cmReverseRange(varArgsExpanded), ";");
  413. status.GetMakefile().AddDefinition(listName, value);
  414. return true;
  415. }
  416. bool HandleRemoveDuplicatesCommand(std::vector<std::string> const& args,
  417. cmExecutionStatus& status)
  418. {
  419. assert(args.size() >= 2);
  420. if (args.size() > 2) {
  421. status.SetError("sub-command REMOVE_DUPLICATES only takes one argument.");
  422. return false;
  423. }
  424. const std::string& listName = args[1];
  425. // expand the variable
  426. std::vector<std::string> varArgsExpanded;
  427. if (!GetList(varArgsExpanded, listName, status.GetMakefile())) {
  428. return true;
  429. }
  430. auto argsEnd = cmRemoveDuplicates(varArgsExpanded);
  431. auto argsBegin = varArgsExpanded.cbegin();
  432. std::string value = cmJoin(cmMakeRange(argsBegin, argsEnd), ";");
  433. status.GetMakefile().AddDefinition(listName, value);
  434. return true;
  435. }
  436. // Helpers for list(TRANSFORM <list> ...)
  437. using transform_type = std::function<std::string(const std::string&)>;
  438. class transform_error : public std::runtime_error
  439. {
  440. public:
  441. transform_error(const std::string& error)
  442. : std::runtime_error(error)
  443. {
  444. }
  445. };
  446. class TransformSelector
  447. {
  448. public:
  449. virtual ~TransformSelector() = default;
  450. std::string Tag;
  451. virtual bool Validate(std::size_t count = 0) = 0;
  452. virtual bool InSelection(const std::string&) = 0;
  453. virtual void Transform(std::vector<std::string>& list,
  454. const transform_type& transform)
  455. {
  456. std::transform(list.begin(), list.end(), list.begin(), transform);
  457. }
  458. protected:
  459. TransformSelector(std::string&& tag)
  460. : Tag(std::move(tag))
  461. {
  462. }
  463. };
  464. class TransformNoSelector : public TransformSelector
  465. {
  466. public:
  467. TransformNoSelector()
  468. : TransformSelector("NO SELECTOR")
  469. {
  470. }
  471. bool Validate(std::size_t) override { return true; }
  472. bool InSelection(const std::string&) override { return true; }
  473. };
  474. class TransformSelectorRegex : public TransformSelector
  475. {
  476. public:
  477. TransformSelectorRegex(const std::string& regex)
  478. : TransformSelector("REGEX")
  479. , Regex(regex)
  480. {
  481. }
  482. bool Validate(std::size_t) override { return this->Regex.is_valid(); }
  483. bool InSelection(const std::string& value) override
  484. {
  485. return this->Regex.find(value);
  486. }
  487. cmsys::RegularExpression Regex;
  488. };
  489. class TransformSelectorIndexes : public TransformSelector
  490. {
  491. public:
  492. std::vector<int> Indexes;
  493. bool InSelection(const std::string&) override { return true; }
  494. void Transform(std::vector<std::string>& list,
  495. const transform_type& transform) override
  496. {
  497. this->Validate(list.size());
  498. for (auto index : this->Indexes) {
  499. list[index] = transform(list[index]);
  500. }
  501. }
  502. protected:
  503. TransformSelectorIndexes(std::string&& tag)
  504. : TransformSelector(std::move(tag))
  505. {
  506. }
  507. TransformSelectorIndexes(std::string&& tag, std::vector<int>&& indexes)
  508. : TransformSelector(std::move(tag))
  509. , Indexes(indexes)
  510. {
  511. }
  512. int NormalizeIndex(int index, std::size_t count)
  513. {
  514. if (index < 0) {
  515. index = static_cast<int>(count) + index;
  516. }
  517. if (index < 0 || count <= static_cast<std::size_t>(index)) {
  518. std::ostringstream str;
  519. str << "sub-command TRANSFORM, selector " << this->Tag
  520. << ", index: " << index << " out of range (-" << count << ", "
  521. << count - 1 << ").";
  522. throw transform_error(str.str());
  523. }
  524. return index;
  525. }
  526. };
  527. class TransformSelectorAt : public TransformSelectorIndexes
  528. {
  529. public:
  530. TransformSelectorAt(std::vector<int>&& indexes)
  531. : TransformSelectorIndexes("AT", std::move(indexes))
  532. {
  533. }
  534. bool Validate(std::size_t count) override
  535. {
  536. decltype(Indexes) indexes;
  537. for (auto index : Indexes) {
  538. indexes.push_back(this->NormalizeIndex(index, count));
  539. }
  540. this->Indexes = std::move(indexes);
  541. return true;
  542. }
  543. };
  544. class TransformSelectorFor : public TransformSelectorIndexes
  545. {
  546. public:
  547. TransformSelectorFor(int start, int stop, int step)
  548. : TransformSelectorIndexes("FOR")
  549. , Start(start)
  550. , Stop(stop)
  551. , Step(step)
  552. {
  553. }
  554. bool Validate(std::size_t count) override
  555. {
  556. this->Start = this->NormalizeIndex(this->Start, count);
  557. this->Stop = this->NormalizeIndex(this->Stop, count);
  558. // compute indexes
  559. auto size = (this->Stop - this->Start + 1) / this->Step;
  560. if ((this->Stop - this->Start + 1) % this->Step != 0) {
  561. size += 1;
  562. }
  563. this->Indexes.resize(size);
  564. auto start = this->Start;
  565. auto step = this->Step;
  566. std::generate(this->Indexes.begin(), this->Indexes.end(),
  567. [&start, step]() -> int {
  568. auto r = start;
  569. start += step;
  570. return r;
  571. });
  572. return true;
  573. }
  574. private:
  575. int Start, Stop, Step;
  576. };
  577. class TransformAction
  578. {
  579. public:
  580. virtual ~TransformAction() = default;
  581. virtual std::string Transform(const std::string& input) = 0;
  582. };
  583. class TransformReplace : public TransformAction
  584. {
  585. public:
  586. TransformReplace(const std::vector<std::string>& arguments,
  587. cmMakefile* makefile)
  588. : ReplaceHelper(arguments[0], arguments[1], makefile)
  589. {
  590. makefile->ClearMatches();
  591. if (!this->ReplaceHelper.IsRegularExpressionValid()) {
  592. std::ostringstream error;
  593. error
  594. << "sub-command TRANSFORM, action REPLACE: Failed to compile regex \""
  595. << arguments[0] << "\".";
  596. throw transform_error(error.str());
  597. }
  598. if (!this->ReplaceHelper.IsReplaceExpressionValid()) {
  599. std::ostringstream error;
  600. error << "sub-command TRANSFORM, action REPLACE: "
  601. << this->ReplaceHelper.GetError() << ".";
  602. throw transform_error(error.str());
  603. }
  604. }
  605. std::string Transform(const std::string& input) override
  606. {
  607. // Scan through the input for all matches.
  608. std::string output;
  609. if (!this->ReplaceHelper.Replace(input, output)) {
  610. std::ostringstream error;
  611. error << "sub-command TRANSFORM, action REPLACE: "
  612. << this->ReplaceHelper.GetError() << ".";
  613. throw transform_error(error.str());
  614. }
  615. return output;
  616. }
  617. private:
  618. cmStringReplaceHelper ReplaceHelper;
  619. };
  620. bool HandleTransformCommand(std::vector<std::string> const& args,
  621. cmExecutionStatus& status)
  622. {
  623. if (args.size() < 3) {
  624. status.SetError(
  625. "sub-command TRANSFORM requires an action to be specified.");
  626. return false;
  627. }
  628. // Structure collecting all elements of the command
  629. struct Command
  630. {
  631. Command(const std::string& listName)
  632. : ListName(listName)
  633. , OutputName(listName)
  634. {
  635. }
  636. std::string Name;
  637. std::string ListName;
  638. std::vector<std::string> Arguments;
  639. std::unique_ptr<TransformAction> Action;
  640. std::unique_ptr<TransformSelector> Selector;
  641. std::string OutputName;
  642. } command(args[1]);
  643. // Descriptor of action
  644. // Arity: number of arguments required for the action
  645. // Transform: lambda function implementing the action
  646. struct ActionDescriptor
  647. {
  648. ActionDescriptor(std::string name)
  649. : Name(std::move(name))
  650. {
  651. }
  652. ActionDescriptor(std::string name, int arity, transform_type transform)
  653. : Name(std::move(name))
  654. , Arity(arity)
  655. #if defined(__GNUC__) && __GNUC__ == 6 && defined(__aarch64__)
  656. // std::function move constructor miscompiles on this architecture
  657. , Transform(transform)
  658. #else
  659. , Transform(std::move(transform))
  660. #endif
  661. {
  662. }
  663. operator const std::string&() const { return Name; }
  664. std::string Name;
  665. int Arity = 0;
  666. transform_type Transform;
  667. };
  668. // Build a set of supported actions.
  669. std::set<ActionDescriptor,
  670. std::function<bool(const std::string&, const std::string&)>>
  671. descriptors(
  672. [](const std::string& x, const std::string& y) { return x < y; });
  673. descriptors = { { "APPEND", 1,
  674. [&command](const std::string& s) -> std::string {
  675. if (command.Selector->InSelection(s)) {
  676. return s + command.Arguments[0];
  677. }
  678. return s;
  679. } },
  680. { "PREPEND", 1,
  681. [&command](const std::string& s) -> std::string {
  682. if (command.Selector->InSelection(s)) {
  683. return command.Arguments[0] + s;
  684. }
  685. return s;
  686. } },
  687. { "TOUPPER", 0,
  688. [&command](const std::string& s) -> std::string {
  689. if (command.Selector->InSelection(s)) {
  690. return cmSystemTools::UpperCase(s);
  691. }
  692. return s;
  693. } },
  694. { "TOLOWER", 0,
  695. [&command](const std::string& s) -> std::string {
  696. if (command.Selector->InSelection(s)) {
  697. return cmSystemTools::LowerCase(s);
  698. }
  699. return s;
  700. } },
  701. { "STRIP", 0,
  702. [&command](const std::string& s) -> std::string {
  703. if (command.Selector->InSelection(s)) {
  704. return cmTrimWhitespace(s);
  705. }
  706. return s;
  707. } },
  708. { "GENEX_STRIP", 0,
  709. [&command](const std::string& s) -> std::string {
  710. if (command.Selector->InSelection(s)) {
  711. return cmGeneratorExpression::Preprocess(
  712. s,
  713. cmGeneratorExpression::StripAllGeneratorExpressions);
  714. }
  715. return s;
  716. } },
  717. { "REPLACE", 2,
  718. [&command](const std::string& s) -> std::string {
  719. if (command.Selector->InSelection(s)) {
  720. return command.Action->Transform(s);
  721. }
  722. return s;
  723. } } };
  724. using size_type = std::vector<std::string>::size_type;
  725. size_type index = 2;
  726. // Parse all possible function parameters
  727. auto descriptor = descriptors.find(args[index]);
  728. if (descriptor == descriptors.end()) {
  729. std::ostringstream error;
  730. error << " sub-command TRANSFORM, " << args[index] << " invalid action.";
  731. status.SetError(error.str());
  732. return false;
  733. }
  734. // Action arguments
  735. index += 1;
  736. if (args.size() < index + descriptor->Arity) {
  737. std::ostringstream error;
  738. error << "sub-command TRANSFORM, action " << descriptor->Name
  739. << " expects " << descriptor->Arity << " argument(s).";
  740. status.SetError(error.str());
  741. return false;
  742. }
  743. command.Name = descriptor->Name;
  744. index += descriptor->Arity;
  745. if (descriptor->Arity > 0) {
  746. command.Arguments =
  747. std::vector<std::string>(args.begin() + 3, args.begin() + index);
  748. }
  749. if (command.Name == "REPLACE") {
  750. try {
  751. command.Action = cm::make_unique<TransformReplace>(
  752. command.Arguments, &status.GetMakefile());
  753. } catch (const transform_error& e) {
  754. status.SetError(e.what());
  755. return false;
  756. }
  757. }
  758. const std::string REGEX{ "REGEX" };
  759. const std::string AT{ "AT" };
  760. const std::string FOR{ "FOR" };
  761. const std::string OUTPUT_VARIABLE{ "OUTPUT_VARIABLE" };
  762. // handle optional arguments
  763. while (args.size() > index) {
  764. if ((args[index] == REGEX || args[index] == AT || args[index] == FOR) &&
  765. command.Selector) {
  766. std::ostringstream error;
  767. error << "sub-command TRANSFORM, selector already specified ("
  768. << command.Selector->Tag << ").";
  769. status.SetError(error.str());
  770. return false;
  771. }
  772. // REGEX selector
  773. if (args[index] == REGEX) {
  774. if (args.size() == ++index) {
  775. status.SetError("sub-command TRANSFORM, selector REGEX expects "
  776. "'regular expression' argument.");
  777. return false;
  778. }
  779. command.Selector = cm::make_unique<TransformSelectorRegex>(args[index]);
  780. if (!command.Selector->Validate()) {
  781. std::ostringstream error;
  782. error << "sub-command TRANSFORM, selector REGEX failed to compile "
  783. "regex \"";
  784. error << args[index] << "\".";
  785. status.SetError(error.str());
  786. return false;
  787. }
  788. index += 1;
  789. continue;
  790. }
  791. // AT selector
  792. if (args[index] == AT) {
  793. // get all specified indexes
  794. std::vector<int> indexes;
  795. while (args.size() > ++index) {
  796. std::size_t pos;
  797. int value;
  798. try {
  799. value = std::stoi(args[index], &pos);
  800. if (pos != args[index].length()) {
  801. // this is not a number, stop processing
  802. break;
  803. }
  804. indexes.push_back(value);
  805. } catch (const std::invalid_argument&) {
  806. // this is not a number, stop processing
  807. break;
  808. }
  809. }
  810. if (indexes.empty()) {
  811. status.SetError(
  812. "sub-command TRANSFORM, selector AT expects at least one "
  813. "numeric value.");
  814. return false;
  815. }
  816. command.Selector =
  817. cm::make_unique<TransformSelectorAt>(std::move(indexes));
  818. continue;
  819. }
  820. // FOR selector
  821. if (args[index] == FOR) {
  822. if (args.size() <= ++index + 1) {
  823. status.SetError(
  824. "sub-command TRANSFORM, selector FOR expects, at least,"
  825. " two arguments.");
  826. return false;
  827. }
  828. int start = 0;
  829. int stop = 0;
  830. int step = 1;
  831. bool valid = true;
  832. try {
  833. std::size_t pos;
  834. start = std::stoi(args[index], &pos);
  835. if (pos != args[index].length()) {
  836. // this is not a number
  837. valid = false;
  838. } else {
  839. stop = std::stoi(args[++index], &pos);
  840. if (pos != args[index].length()) {
  841. // this is not a number
  842. valid = false;
  843. }
  844. }
  845. } catch (const std::invalid_argument&) {
  846. // this is not numbers
  847. valid = false;
  848. }
  849. if (!valid) {
  850. status.SetError("sub-command TRANSFORM, selector FOR expects, "
  851. "at least, two numeric values.");
  852. return false;
  853. }
  854. // try to read a third numeric value for step
  855. if (args.size() > ++index) {
  856. try {
  857. std::size_t pos;
  858. step = std::stoi(args[index], &pos);
  859. if (pos != args[index].length()) {
  860. // this is not a number
  861. step = 1;
  862. } else {
  863. index += 1;
  864. }
  865. } catch (const std::invalid_argument&) {
  866. // this is not number, ignore exception
  867. }
  868. }
  869. if (step < 0) {
  870. status.SetError("sub-command TRANSFORM, selector FOR expects "
  871. "non negative numeric value for <step>.");
  872. }
  873. command.Selector =
  874. cm::make_unique<TransformSelectorFor>(start, stop, step);
  875. continue;
  876. }
  877. // output variable
  878. if (args[index] == OUTPUT_VARIABLE) {
  879. if (args.size() == ++index) {
  880. status.SetError("sub-command TRANSFORM, OUTPUT_VARIABLE "
  881. "expects variable name argument.");
  882. return false;
  883. }
  884. command.OutputName = args[index++];
  885. continue;
  886. }
  887. std::ostringstream error;
  888. error << "sub-command TRANSFORM, '"
  889. << cmJoin(cmMakeRange(args).advance(index), " ")
  890. << "': unexpected argument(s).";
  891. status.SetError(error.str());
  892. return false;
  893. }
  894. // expand the list variable
  895. std::vector<std::string> varArgsExpanded;
  896. if (!GetList(varArgsExpanded, command.ListName, status.GetMakefile())) {
  897. status.GetMakefile().AddDefinition(command.OutputName, "");
  898. return true;
  899. }
  900. if (!command.Selector) {
  901. // no selector specified, apply transformation to all elements
  902. command.Selector = cm::make_unique<TransformNoSelector>();
  903. }
  904. try {
  905. command.Selector->Transform(varArgsExpanded, descriptor->Transform);
  906. } catch (const transform_error& e) {
  907. status.SetError(e.what());
  908. return false;
  909. }
  910. status.GetMakefile().AddDefinition(command.OutputName,
  911. cmJoin(varArgsExpanded, ";"));
  912. return true;
  913. }
  914. class cmStringSorter
  915. {
  916. public:
  917. enum class Order
  918. {
  919. UNINITIALIZED,
  920. ASCENDING,
  921. DESCENDING,
  922. };
  923. enum class Compare
  924. {
  925. UNINITIALIZED,
  926. STRING,
  927. FILE_BASENAME,
  928. };
  929. enum class CaseSensitivity
  930. {
  931. UNINITIALIZED,
  932. SENSITIVE,
  933. INSENSITIVE,
  934. };
  935. protected:
  936. using StringFilter = std::string (*)(const std::string&);
  937. StringFilter GetCompareFilter(Compare compare)
  938. {
  939. return (compare == Compare::FILE_BASENAME) ? cmSystemTools::GetFilenameName
  940. : nullptr;
  941. }
  942. StringFilter GetCaseFilter(CaseSensitivity sensitivity)
  943. {
  944. return (sensitivity == CaseSensitivity::INSENSITIVE)
  945. ? cmSystemTools::LowerCase
  946. : nullptr;
  947. }
  948. public:
  949. cmStringSorter(Compare compare, CaseSensitivity caseSensitivity,
  950. Order desc = Order::ASCENDING)
  951. : filters{ GetCompareFilter(compare), GetCaseFilter(caseSensitivity) }
  952. , descending(desc == Order::DESCENDING)
  953. {
  954. }
  955. std::string ApplyFilter(const std::string& argument)
  956. {
  957. std::string result = argument;
  958. for (auto filter : filters) {
  959. if (filter != nullptr) {
  960. result = filter(result);
  961. }
  962. }
  963. return result;
  964. }
  965. bool operator()(const std::string& a, const std::string& b)
  966. {
  967. std::string af = ApplyFilter(a);
  968. std::string bf = ApplyFilter(b);
  969. bool result;
  970. if (descending) {
  971. result = bf < af;
  972. } else {
  973. result = af < bf;
  974. }
  975. return result;
  976. }
  977. protected:
  978. StringFilter filters[2] = { nullptr, nullptr };
  979. bool descending;
  980. };
  981. bool HandleSortCommand(std::vector<std::string> const& args,
  982. cmExecutionStatus& status)
  983. {
  984. assert(args.size() >= 2);
  985. if (args.size() > 8) {
  986. status.SetError("sub-command SORT only takes up to six arguments.");
  987. return false;
  988. }
  989. auto sortCompare = cmStringSorter::Compare::UNINITIALIZED;
  990. auto sortCaseSensitivity = cmStringSorter::CaseSensitivity::UNINITIALIZED;
  991. auto sortOrder = cmStringSorter::Order::UNINITIALIZED;
  992. size_t argumentIndex = 2;
  993. const std::string messageHint = "sub-command SORT ";
  994. while (argumentIndex < args.size()) {
  995. const std::string option = args[argumentIndex++];
  996. if (option == "COMPARE") {
  997. if (sortCompare != cmStringSorter::Compare::UNINITIALIZED) {
  998. std::string error = cmStrCat(messageHint, "option \"", option,
  999. "\" has been specified multiple times.");
  1000. status.SetError(error);
  1001. return false;
  1002. }
  1003. if (argumentIndex < args.size()) {
  1004. const std::string argument = args[argumentIndex++];
  1005. if (argument == "STRING") {
  1006. sortCompare = cmStringSorter::Compare::STRING;
  1007. } else if (argument == "FILE_BASENAME") {
  1008. sortCompare = cmStringSorter::Compare::FILE_BASENAME;
  1009. } else {
  1010. std::string error =
  1011. cmStrCat(messageHint, "value \"", argument, "\" for option \"",
  1012. option, "\" is invalid.");
  1013. status.SetError(error);
  1014. return false;
  1015. }
  1016. } else {
  1017. status.SetError(cmStrCat(messageHint, "missing argument for option \"",
  1018. option, "\"."));
  1019. return false;
  1020. }
  1021. } else if (option == "CASE") {
  1022. if (sortCaseSensitivity !=
  1023. cmStringSorter::CaseSensitivity::UNINITIALIZED) {
  1024. status.SetError(cmStrCat(messageHint, "option \"", option,
  1025. "\" has been specified multiple times."));
  1026. return false;
  1027. }
  1028. if (argumentIndex < args.size()) {
  1029. const std::string argument = args[argumentIndex++];
  1030. if (argument == "SENSITIVE") {
  1031. sortCaseSensitivity = cmStringSorter::CaseSensitivity::SENSITIVE;
  1032. } else if (argument == "INSENSITIVE") {
  1033. sortCaseSensitivity = cmStringSorter::CaseSensitivity::INSENSITIVE;
  1034. } else {
  1035. status.SetError(cmStrCat(messageHint, "value \"", argument,
  1036. "\" for option \"", option,
  1037. "\" is invalid."));
  1038. return false;
  1039. }
  1040. } else {
  1041. status.SetError(cmStrCat(messageHint, "missing argument for option \"",
  1042. option, "\"."));
  1043. return false;
  1044. }
  1045. } else if (option == "ORDER") {
  1046. if (sortOrder != cmStringSorter::Order::UNINITIALIZED) {
  1047. status.SetError(cmStrCat(messageHint, "option \"", option,
  1048. "\" has been specified multiple times."));
  1049. return false;
  1050. }
  1051. if (argumentIndex < args.size()) {
  1052. const std::string argument = args[argumentIndex++];
  1053. if (argument == "ASCENDING") {
  1054. sortOrder = cmStringSorter::Order::ASCENDING;
  1055. } else if (argument == "DESCENDING") {
  1056. sortOrder = cmStringSorter::Order::DESCENDING;
  1057. } else {
  1058. status.SetError(cmStrCat(messageHint, "value \"", argument,
  1059. "\" for option \"", option,
  1060. "\" is invalid."));
  1061. return false;
  1062. }
  1063. } else {
  1064. status.SetError(cmStrCat(messageHint, "missing argument for option \"",
  1065. option, "\"."));
  1066. return false;
  1067. }
  1068. } else {
  1069. status.SetError(
  1070. cmStrCat(messageHint, "option \"", option, "\" is unknown."));
  1071. return false;
  1072. }
  1073. }
  1074. // set Default Values if Option is not given
  1075. if (sortCompare == cmStringSorter::Compare::UNINITIALIZED) {
  1076. sortCompare = cmStringSorter::Compare::STRING;
  1077. }
  1078. if (sortCaseSensitivity == cmStringSorter::CaseSensitivity::UNINITIALIZED) {
  1079. sortCaseSensitivity = cmStringSorter::CaseSensitivity::SENSITIVE;
  1080. }
  1081. if (sortOrder == cmStringSorter::Order::UNINITIALIZED) {
  1082. sortOrder = cmStringSorter::Order::ASCENDING;
  1083. }
  1084. const std::string& listName = args[1];
  1085. // expand the variable
  1086. std::vector<std::string> varArgsExpanded;
  1087. if (!GetList(varArgsExpanded, listName, status.GetMakefile())) {
  1088. return true;
  1089. }
  1090. if ((sortCompare == cmStringSorter::Compare::STRING) &&
  1091. (sortCaseSensitivity == cmStringSorter::CaseSensitivity::SENSITIVE) &&
  1092. (sortOrder == cmStringSorter::Order::ASCENDING)) {
  1093. std::sort(varArgsExpanded.begin(), varArgsExpanded.end());
  1094. } else {
  1095. cmStringSorter sorter(sortCompare, sortCaseSensitivity, sortOrder);
  1096. std::sort(varArgsExpanded.begin(), varArgsExpanded.end(), sorter);
  1097. }
  1098. std::string value = cmJoin(varArgsExpanded, ";");
  1099. status.GetMakefile().AddDefinition(listName, value);
  1100. return true;
  1101. }
  1102. bool HandleSublistCommand(std::vector<std::string> const& args,
  1103. cmExecutionStatus& status)
  1104. {
  1105. if (args.size() != 5) {
  1106. std::ostringstream error;
  1107. error << "sub-command SUBLIST requires four arguments (" << args.size() - 1
  1108. << " found).";
  1109. status.SetError(error.str());
  1110. return false;
  1111. }
  1112. const std::string& listName = args[1];
  1113. const std::string& variableName = args.back();
  1114. // expand the variable
  1115. std::vector<std::string> varArgsExpanded;
  1116. if (!GetList(varArgsExpanded, listName, status.GetMakefile()) ||
  1117. varArgsExpanded.empty()) {
  1118. status.GetMakefile().AddDefinition(variableName, "");
  1119. return true;
  1120. }
  1121. const int start = atoi(args[2].c_str());
  1122. const int length = atoi(args[3].c_str());
  1123. using size_type = decltype(varArgsExpanded)::size_type;
  1124. if (start < 0 || size_type(start) >= varArgsExpanded.size()) {
  1125. std::ostringstream error;
  1126. error << "begin index: " << start << " is out of range 0 - "
  1127. << varArgsExpanded.size() - 1;
  1128. status.SetError(error.str());
  1129. return false;
  1130. }
  1131. if (length < -1) {
  1132. std::ostringstream error;
  1133. error << "length: " << length << " should be -1 or greater";
  1134. status.SetError(error.str());
  1135. return false;
  1136. }
  1137. const size_type end =
  1138. (length == -1 || size_type(start + length) > varArgsExpanded.size())
  1139. ? varArgsExpanded.size()
  1140. : size_type(start + length);
  1141. std::vector<std::string> sublist(varArgsExpanded.begin() + start,
  1142. varArgsExpanded.begin() + end);
  1143. status.GetMakefile().AddDefinition(variableName, cmJoin(sublist, ";"));
  1144. return true;
  1145. }
  1146. bool HandleRemoveAtCommand(std::vector<std::string> const& args,
  1147. cmExecutionStatus& status)
  1148. {
  1149. if (args.size() < 3) {
  1150. status.SetError("sub-command REMOVE_AT requires at least "
  1151. "two arguments.");
  1152. return false;
  1153. }
  1154. const std::string& listName = args[1];
  1155. // expand the variable
  1156. std::vector<std::string> varArgsExpanded;
  1157. if (!GetList(varArgsExpanded, listName, status.GetMakefile()) ||
  1158. varArgsExpanded.empty()) {
  1159. std::ostringstream str;
  1160. str << "index: ";
  1161. for (size_t i = 1; i < args.size(); ++i) {
  1162. str << args[i];
  1163. if (i != args.size() - 1) {
  1164. str << ", ";
  1165. }
  1166. }
  1167. str << " out of range (0, 0)";
  1168. status.SetError(str.str());
  1169. return false;
  1170. }
  1171. size_t cc;
  1172. std::vector<size_t> removed;
  1173. size_t nitem = varArgsExpanded.size();
  1174. for (cc = 2; cc < args.size(); ++cc) {
  1175. int item = atoi(args[cc].c_str());
  1176. if (item < 0) {
  1177. item = static_cast<int>(nitem) + item;
  1178. }
  1179. if (item < 0 || nitem <= static_cast<size_t>(item)) {
  1180. std::ostringstream str;
  1181. str << "index: " << item << " out of range (-" << nitem << ", "
  1182. << nitem - 1 << ")";
  1183. status.SetError(str.str());
  1184. return false;
  1185. }
  1186. removed.push_back(static_cast<size_t>(item));
  1187. }
  1188. std::sort(removed.begin(), removed.end());
  1189. auto remEnd = std::unique(removed.begin(), removed.end());
  1190. auto remBegin = removed.begin();
  1191. auto argsEnd =
  1192. cmRemoveIndices(varArgsExpanded, cmMakeRange(remBegin, remEnd));
  1193. auto argsBegin = varArgsExpanded.cbegin();
  1194. std::string value = cmJoin(cmMakeRange(argsBegin, argsEnd), ";");
  1195. status.GetMakefile().AddDefinition(listName, value);
  1196. return true;
  1197. }
  1198. bool HandleFilterCommand(std::vector<std::string> const& args,
  1199. cmExecutionStatus& status)
  1200. {
  1201. if (args.size() < 2) {
  1202. status.SetError("sub-command FILTER requires a list to be specified.");
  1203. return false;
  1204. }
  1205. if (args.size() < 3) {
  1206. status.SetError(
  1207. "sub-command FILTER requires an operator to be specified.");
  1208. return false;
  1209. }
  1210. if (args.size() < 4) {
  1211. status.SetError("sub-command FILTER requires a mode to be specified.");
  1212. return false;
  1213. }
  1214. const std::string& op = args[2];
  1215. bool includeMatches;
  1216. if (op == "INCLUDE") {
  1217. includeMatches = true;
  1218. } else if (op == "EXCLUDE") {
  1219. includeMatches = false;
  1220. } else {
  1221. status.SetError("sub-command FILTER does not recognize operator " + op);
  1222. return false;
  1223. }
  1224. const std::string& listName = args[1];
  1225. // expand the variable
  1226. std::vector<std::string> varArgsExpanded;
  1227. if (!GetList(varArgsExpanded, listName, status.GetMakefile())) {
  1228. return true;
  1229. }
  1230. const std::string& mode = args[3];
  1231. if (mode == "REGEX") {
  1232. if (args.size() != 5) {
  1233. status.SetError("sub-command FILTER, mode REGEX "
  1234. "requires five arguments.");
  1235. return false;
  1236. }
  1237. return FilterRegex(args, includeMatches, listName, varArgsExpanded,
  1238. status);
  1239. }
  1240. status.SetError("sub-command FILTER does not recognize mode " + mode);
  1241. return false;
  1242. }
  1243. class MatchesRegex
  1244. {
  1245. public:
  1246. MatchesRegex(cmsys::RegularExpression& in_regex, bool in_includeMatches)
  1247. : regex(in_regex)
  1248. , includeMatches(in_includeMatches)
  1249. {
  1250. }
  1251. bool operator()(const std::string& target)
  1252. {
  1253. return regex.find(target) ^ includeMatches;
  1254. }
  1255. private:
  1256. cmsys::RegularExpression& regex;
  1257. const bool includeMatches;
  1258. };
  1259. bool FilterRegex(std::vector<std::string> const& args, bool includeMatches,
  1260. std::string const& listName,
  1261. std::vector<std::string>& varArgsExpanded,
  1262. cmExecutionStatus& status)
  1263. {
  1264. const std::string& pattern = args[4];
  1265. cmsys::RegularExpression regex(pattern);
  1266. if (!regex.is_valid()) {
  1267. std::string error =
  1268. cmStrCat("sub-command FILTER, mode REGEX failed to compile regex \"",
  1269. pattern, "\".");
  1270. status.SetError(error);
  1271. return false;
  1272. }
  1273. auto argsBegin = varArgsExpanded.begin();
  1274. auto argsEnd = varArgsExpanded.end();
  1275. auto newArgsEnd =
  1276. std::remove_if(argsBegin, argsEnd, MatchesRegex(regex, includeMatches));
  1277. std::string value = cmJoin(cmMakeRange(argsBegin, newArgsEnd), ";");
  1278. status.GetMakefile().AddDefinition(listName, value);
  1279. return true;
  1280. }
  1281. } // namespace
  1282. bool cmListCommand(std::vector<std::string> const& args,
  1283. cmExecutionStatus& status)
  1284. {
  1285. if (args.size() < 2) {
  1286. status.SetError("must be called with at least two arguments.");
  1287. return false;
  1288. }
  1289. static cmSubcommandTable const subcommand{
  1290. { "LENGTH"_s, HandleLengthCommand },
  1291. { "GET"_s, HandleGetCommand },
  1292. { "APPEND"_s, HandleAppendCommand },
  1293. { "PREPEND"_s, HandlePrependCommand },
  1294. { "POP_BACK"_s, HandlePopBackCommand },
  1295. { "POP_FRONT"_s, HandlePopFrontCommand },
  1296. { "FIND"_s, HandleFindCommand },
  1297. { "INSERT"_s, HandleInsertCommand },
  1298. { "JOIN"_s, HandleJoinCommand },
  1299. { "REMOVE_AT"_s, HandleRemoveAtCommand },
  1300. { "REMOVE_ITEM"_s, HandleRemoveItemCommand },
  1301. { "REMOVE_DUPLICATES"_s, HandleRemoveDuplicatesCommand },
  1302. { "TRANSFORM"_s, HandleTransformCommand },
  1303. { "SORT"_s, HandleSortCommand },
  1304. { "SUBLIST"_s, HandleSublistCommand },
  1305. { "REVERSE"_s, HandleReverseCommand },
  1306. { "FILTER"_s, HandleFilterCommand },
  1307. };
  1308. return subcommand(args[0], args, status);
  1309. }