cmListCommand.cxx 43 KB

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