cmListCommand.cxx 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504
  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. cmExpandList(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. cmExpandList(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. cmExpandList(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;
  610. auto step = this->Step;
  611. std::generate(this->Indexes.begin(), this->Indexes.end(),
  612. [&start, step]() -> int {
  613. auto r = start;
  614. start += step;
  615. return r;
  616. });
  617. return true;
  618. }
  619. private:
  620. int Start, Stop, Step;
  621. };
  622. class TransformAction
  623. {
  624. public:
  625. virtual ~TransformAction() = default;
  626. virtual std::string Transform(const std::string& input) = 0;
  627. };
  628. class TransformReplace : public TransformAction
  629. {
  630. public:
  631. TransformReplace(const std::vector<std::string>& arguments,
  632. cmMakefile* makefile)
  633. : ReplaceHelper(arguments[0], arguments[1], makefile)
  634. {
  635. makefile->ClearMatches();
  636. if (!this->ReplaceHelper.IsRegularExpressionValid()) {
  637. std::ostringstream error;
  638. error
  639. << "sub-command TRANSFORM, action REPLACE: Failed to compile regex \""
  640. << arguments[0] << "\".";
  641. throw transform_error(error.str());
  642. }
  643. if (!this->ReplaceHelper.IsReplaceExpressionValid()) {
  644. std::ostringstream error;
  645. error << "sub-command TRANSFORM, action REPLACE: "
  646. << this->ReplaceHelper.GetError() << ".";
  647. throw transform_error(error.str());
  648. }
  649. }
  650. std::string Transform(const std::string& input) override
  651. {
  652. // Scan through the input for all matches.
  653. std::string output;
  654. if (!this->ReplaceHelper.Replace(input, output)) {
  655. std::ostringstream error;
  656. error << "sub-command TRANSFORM, action REPLACE: "
  657. << this->ReplaceHelper.GetError() << ".";
  658. throw transform_error(error.str());
  659. }
  660. return output;
  661. }
  662. private:
  663. cmStringReplaceHelper ReplaceHelper;
  664. };
  665. }
  666. bool cmListCommand::HandleTransformCommand(
  667. std::vector<std::string> const& args)
  668. {
  669. if (args.size() < 3) {
  670. this->SetError(
  671. "sub-command TRANSFORM requires an action to be specified.");
  672. return false;
  673. }
  674. // Structure collecting all elements of the command
  675. struct Command
  676. {
  677. Command(const std::string& listName)
  678. : ListName(listName)
  679. , OutputName(listName)
  680. {
  681. }
  682. std::string Name;
  683. std::string ListName;
  684. std::vector<std::string> Arguments;
  685. std::unique_ptr<TransformAction> Action;
  686. std::unique_ptr<TransformSelector> Selector;
  687. std::string OutputName;
  688. } command(args[1]);
  689. // Descriptor of action
  690. // Arity: number of arguments required for the action
  691. // Transform: lambda function implementing the action
  692. struct ActionDescriptor
  693. {
  694. ActionDescriptor(std::string name)
  695. : Name(std::move(name))
  696. {
  697. }
  698. ActionDescriptor(std::string name, int arity, transform_type transform)
  699. : Name(std::move(name))
  700. , Arity(arity)
  701. #if defined(__GNUC__) && __GNUC__ == 6 && defined(__aarch64__)
  702. // std::function move constructor miscompiles on this architecture
  703. , Transform(transform)
  704. #else
  705. , Transform(std::move(transform))
  706. #endif
  707. {
  708. }
  709. operator const std::string&() const { return Name; }
  710. std::string Name;
  711. int Arity = 0;
  712. transform_type Transform;
  713. };
  714. // Build a set of supported actions.
  715. std::set<ActionDescriptor,
  716. std::function<bool(const std::string&, const std::string&)>>
  717. descriptors(
  718. [](const std::string& x, const std::string& y) { return x < y; });
  719. descriptors = { { "APPEND", 1,
  720. [&command](const std::string& s) -> std::string {
  721. if (command.Selector->InSelection(s)) {
  722. return s + command.Arguments[0];
  723. }
  724. return s;
  725. } },
  726. { "PREPEND", 1,
  727. [&command](const std::string& s) -> std::string {
  728. if (command.Selector->InSelection(s)) {
  729. return command.Arguments[0] + s;
  730. }
  731. return s;
  732. } },
  733. { "TOUPPER", 0,
  734. [&command](const std::string& s) -> std::string {
  735. if (command.Selector->InSelection(s)) {
  736. return cmSystemTools::UpperCase(s);
  737. }
  738. return s;
  739. } },
  740. { "TOLOWER", 0,
  741. [&command](const std::string& s) -> std::string {
  742. if (command.Selector->InSelection(s)) {
  743. return cmSystemTools::LowerCase(s);
  744. }
  745. return s;
  746. } },
  747. { "STRIP", 0,
  748. [&command](const std::string& s) -> std::string {
  749. if (command.Selector->InSelection(s)) {
  750. return cmTrimWhitespace(s);
  751. }
  752. return s;
  753. } },
  754. { "GENEX_STRIP", 0,
  755. [&command](const std::string& s) -> std::string {
  756. if (command.Selector->InSelection(s)) {
  757. return cmGeneratorExpression::Preprocess(
  758. s,
  759. cmGeneratorExpression::StripAllGeneratorExpressions);
  760. }
  761. return s;
  762. } },
  763. { "REPLACE", 2,
  764. [&command](const std::string& s) -> std::string {
  765. if (command.Selector->InSelection(s)) {
  766. return command.Action->Transform(s);
  767. }
  768. return s;
  769. } } };
  770. using size_type = std::vector<std::string>::size_type;
  771. size_type index = 2;
  772. // Parse all possible function parameters
  773. auto descriptor = descriptors.find(args[index]);
  774. if (descriptor == descriptors.end()) {
  775. std::ostringstream error;
  776. error << " sub-command TRANSFORM, " << args[index] << " invalid action.";
  777. this->SetError(error.str());
  778. return false;
  779. }
  780. // Action arguments
  781. index += 1;
  782. if (args.size() < index + descriptor->Arity) {
  783. std::ostringstream error;
  784. error << "sub-command TRANSFORM, action " << descriptor->Name
  785. << " expects " << descriptor->Arity << " argument(s).";
  786. this->SetError(error.str());
  787. return false;
  788. }
  789. command.Name = descriptor->Name;
  790. index += descriptor->Arity;
  791. if (descriptor->Arity > 0) {
  792. command.Arguments =
  793. std::vector<std::string>(args.begin() + 3, args.begin() + index);
  794. }
  795. if (command.Name == "REPLACE") {
  796. try {
  797. command.Action =
  798. cm::make_unique<TransformReplace>(command.Arguments, this->Makefile);
  799. } catch (const transform_error& e) {
  800. this->SetError(e.what());
  801. return false;
  802. }
  803. }
  804. const std::string REGEX{ "REGEX" };
  805. const std::string AT{ "AT" };
  806. const std::string FOR{ "FOR" };
  807. const std::string OUTPUT_VARIABLE{ "OUTPUT_VARIABLE" };
  808. // handle optional arguments
  809. while (args.size() > index) {
  810. if ((args[index] == REGEX || args[index] == AT || args[index] == FOR) &&
  811. command.Selector) {
  812. std::ostringstream error;
  813. error << "sub-command TRANSFORM, selector already specified ("
  814. << command.Selector->Tag << ").";
  815. this->SetError(error.str());
  816. return false;
  817. }
  818. // REGEX selector
  819. if (args[index] == REGEX) {
  820. if (args.size() == ++index) {
  821. this->SetError("sub-command TRANSFORM, selector REGEX expects "
  822. "'regular expression' argument.");
  823. return false;
  824. }
  825. command.Selector = cm::make_unique<TransformSelectorRegex>(args[index]);
  826. if (!command.Selector->Validate()) {
  827. std::ostringstream error;
  828. error << "sub-command TRANSFORM, selector REGEX failed to compile "
  829. "regex \"";
  830. error << args[index] << "\".";
  831. this->SetError(error.str());
  832. return false;
  833. }
  834. index += 1;
  835. continue;
  836. }
  837. // AT selector
  838. if (args[index] == AT) {
  839. // get all specified indexes
  840. std::vector<int> indexes;
  841. while (args.size() > ++index) {
  842. std::size_t pos;
  843. int value;
  844. try {
  845. value = std::stoi(args[index], &pos);
  846. if (pos != args[index].length()) {
  847. // this is not a number, stop processing
  848. break;
  849. }
  850. indexes.push_back(value);
  851. } catch (const std::invalid_argument&) {
  852. // this is not a number, stop processing
  853. break;
  854. }
  855. }
  856. if (indexes.empty()) {
  857. this->SetError(
  858. "sub-command TRANSFORM, selector AT expects at least one "
  859. "numeric value.");
  860. return false;
  861. }
  862. command.Selector =
  863. cm::make_unique<TransformSelectorAt>(std::move(indexes));
  864. continue;
  865. }
  866. // FOR selector
  867. if (args[index] == FOR) {
  868. if (args.size() <= ++index + 1) {
  869. this->SetError("sub-command TRANSFORM, selector FOR expects, at least,"
  870. " two arguments.");
  871. return false;
  872. }
  873. int start = 0;
  874. int stop = 0;
  875. int step = 1;
  876. bool valid = true;
  877. try {
  878. std::size_t pos;
  879. start = std::stoi(args[index], &pos);
  880. if (pos != args[index].length()) {
  881. // this is not a number
  882. valid = false;
  883. } else {
  884. stop = std::stoi(args[++index], &pos);
  885. if (pos != args[index].length()) {
  886. // this is not a number
  887. valid = false;
  888. }
  889. }
  890. } catch (const std::invalid_argument&) {
  891. // this is not numbers
  892. valid = false;
  893. }
  894. if (!valid) {
  895. this->SetError("sub-command TRANSFORM, selector FOR expects, "
  896. "at least, two numeric values.");
  897. return false;
  898. }
  899. // try to read a third numeric value for step
  900. if (args.size() > ++index) {
  901. try {
  902. std::size_t pos;
  903. step = std::stoi(args[index], &pos);
  904. if (pos != args[index].length()) {
  905. // this is not a number
  906. step = 1;
  907. } else {
  908. index += 1;
  909. }
  910. } catch (const std::invalid_argument&) {
  911. // this is not number, ignore exception
  912. }
  913. }
  914. if (step < 0) {
  915. this->SetError("sub-command TRANSFORM, selector FOR expects "
  916. "non negative numeric value for <step>.");
  917. }
  918. command.Selector =
  919. cm::make_unique<TransformSelectorFor>(start, stop, step);
  920. continue;
  921. }
  922. // output variable
  923. if (args[index] == OUTPUT_VARIABLE) {
  924. if (args.size() == ++index) {
  925. this->SetError("sub-command TRANSFORM, OUTPUT_VARIABLE "
  926. "expects variable name argument.");
  927. return false;
  928. }
  929. command.OutputName = args[index++];
  930. continue;
  931. }
  932. std::ostringstream error;
  933. error << "sub-command TRANSFORM, '"
  934. << cmJoin(cmMakeRange(args).advance(index), " ")
  935. << "': unexpected argument(s).";
  936. this->SetError(error.str());
  937. return false;
  938. }
  939. // expand the list variable
  940. std::vector<std::string> varArgsExpanded;
  941. if (!this->GetList(varArgsExpanded, command.ListName)) {
  942. this->Makefile->AddDefinition(command.OutputName, "");
  943. return true;
  944. }
  945. if (!command.Selector) {
  946. // no selector specified, apply transformation to all elements
  947. command.Selector = cm::make_unique<TransformNoSelector>();
  948. }
  949. try {
  950. command.Selector->Transform(varArgsExpanded, descriptor->Transform);
  951. } catch (const transform_error& e) {
  952. this->SetError(e.what());
  953. return false;
  954. }
  955. this->Makefile->AddDefinition(command.OutputName,
  956. cmJoin(varArgsExpanded, ";"));
  957. return true;
  958. }
  959. class cmStringSorter
  960. {
  961. public:
  962. enum class Order
  963. {
  964. UNINITIALIZED,
  965. ASCENDING,
  966. DESCENDING,
  967. };
  968. enum class Compare
  969. {
  970. UNINITIALIZED,
  971. STRING,
  972. FILE_BASENAME,
  973. };
  974. enum class CaseSensitivity
  975. {
  976. UNINITIALIZED,
  977. SENSITIVE,
  978. INSENSITIVE,
  979. };
  980. protected:
  981. typedef std::string (*StringFilter)(const std::string& in);
  982. StringFilter GetCompareFilter(Compare compare)
  983. {
  984. return (compare == Compare::FILE_BASENAME) ? cmSystemTools::GetFilenameName
  985. : nullptr;
  986. }
  987. StringFilter GetCaseFilter(CaseSensitivity sensitivity)
  988. {
  989. return (sensitivity == CaseSensitivity::INSENSITIVE)
  990. ? cmSystemTools::LowerCase
  991. : nullptr;
  992. }
  993. public:
  994. cmStringSorter(Compare compare, CaseSensitivity caseSensitivity,
  995. Order desc = Order::ASCENDING)
  996. : filters{ GetCompareFilter(compare), GetCaseFilter(caseSensitivity) }
  997. , descending(desc == Order::DESCENDING)
  998. {
  999. }
  1000. std::string ApplyFilter(const std::string& argument)
  1001. {
  1002. std::string result = argument;
  1003. for (auto filter : filters) {
  1004. if (filter != nullptr) {
  1005. result = filter(result);
  1006. }
  1007. }
  1008. return result;
  1009. }
  1010. bool operator()(const std::string& a, const std::string& b)
  1011. {
  1012. std::string af = ApplyFilter(a);
  1013. std::string bf = ApplyFilter(b);
  1014. bool result;
  1015. if (descending) {
  1016. result = bf < af;
  1017. } else {
  1018. result = af < bf;
  1019. }
  1020. return result;
  1021. }
  1022. protected:
  1023. StringFilter filters[2] = { nullptr, nullptr };
  1024. bool descending;
  1025. };
  1026. bool cmListCommand::HandleSortCommand(std::vector<std::string> const& args)
  1027. {
  1028. assert(args.size() >= 2);
  1029. if (args.size() > 8) {
  1030. this->SetError("sub-command SORT only takes up to six arguments.");
  1031. return false;
  1032. }
  1033. auto sortCompare = cmStringSorter::Compare::UNINITIALIZED;
  1034. auto sortCaseSensitivity = cmStringSorter::CaseSensitivity::UNINITIALIZED;
  1035. auto sortOrder = cmStringSorter::Order::UNINITIALIZED;
  1036. size_t argumentIndex = 2;
  1037. const std::string messageHint = "sub-command SORT ";
  1038. while (argumentIndex < args.size()) {
  1039. const std::string option = args[argumentIndex++];
  1040. if (option == "COMPARE") {
  1041. if (sortCompare != cmStringSorter::Compare::UNINITIALIZED) {
  1042. std::string error = cmStrCat(messageHint, "option \"", option,
  1043. "\" has been specified multiple times.");
  1044. this->SetError(error);
  1045. return false;
  1046. }
  1047. if (argumentIndex < args.size()) {
  1048. const std::string argument = args[argumentIndex++];
  1049. if (argument == "STRING") {
  1050. sortCompare = cmStringSorter::Compare::STRING;
  1051. } else if (argument == "FILE_BASENAME") {
  1052. sortCompare = cmStringSorter::Compare::FILE_BASENAME;
  1053. } else {
  1054. std::string error =
  1055. cmStrCat(messageHint, "value \"", argument, "\" for option \"",
  1056. option, "\" is invalid.");
  1057. this->SetError(error);
  1058. return false;
  1059. }
  1060. } else {
  1061. this->SetError(cmStrCat(messageHint, "missing argument for option \"",
  1062. option, "\"."));
  1063. return false;
  1064. }
  1065. } else if (option == "CASE") {
  1066. if (sortCaseSensitivity !=
  1067. cmStringSorter::CaseSensitivity::UNINITIALIZED) {
  1068. this->SetError(cmStrCat(messageHint, "option \"", option,
  1069. "\" has been specified multiple times."));
  1070. return false;
  1071. }
  1072. if (argumentIndex < args.size()) {
  1073. const std::string argument = args[argumentIndex++];
  1074. if (argument == "SENSITIVE") {
  1075. sortCaseSensitivity = cmStringSorter::CaseSensitivity::SENSITIVE;
  1076. } else if (argument == "INSENSITIVE") {
  1077. sortCaseSensitivity = cmStringSorter::CaseSensitivity::INSENSITIVE;
  1078. } else {
  1079. this->SetError(cmStrCat(messageHint, "value \"", argument,
  1080. "\" for option \"", option,
  1081. "\" is invalid."));
  1082. return false;
  1083. }
  1084. } else {
  1085. this->SetError(cmStrCat(messageHint, "missing argument for option \"",
  1086. option, "\"."));
  1087. return false;
  1088. }
  1089. } else if (option == "ORDER") {
  1090. if (sortOrder != cmStringSorter::Order::UNINITIALIZED) {
  1091. this->SetError(cmStrCat(messageHint, "option \"", option,
  1092. "\" has been specified multiple times."));
  1093. return false;
  1094. }
  1095. if (argumentIndex < args.size()) {
  1096. const std::string argument = args[argumentIndex++];
  1097. if (argument == "ASCENDING") {
  1098. sortOrder = cmStringSorter::Order::ASCENDING;
  1099. } else if (argument == "DESCENDING") {
  1100. sortOrder = cmStringSorter::Order::DESCENDING;
  1101. } else {
  1102. this->SetError(cmStrCat(messageHint, "value \"", argument,
  1103. "\" for option \"", option,
  1104. "\" is invalid."));
  1105. return false;
  1106. }
  1107. } else {
  1108. this->SetError(cmStrCat(messageHint, "missing argument for option \"",
  1109. option, "\"."));
  1110. return false;
  1111. }
  1112. } else {
  1113. this->SetError(
  1114. cmStrCat(messageHint, "option \"", option, "\" is unknown."));
  1115. return false;
  1116. }
  1117. }
  1118. // set Default Values if Option is not given
  1119. if (sortCompare == cmStringSorter::Compare::UNINITIALIZED) {
  1120. sortCompare = cmStringSorter::Compare::STRING;
  1121. }
  1122. if (sortCaseSensitivity == cmStringSorter::CaseSensitivity::UNINITIALIZED) {
  1123. sortCaseSensitivity = cmStringSorter::CaseSensitivity::SENSITIVE;
  1124. }
  1125. if (sortOrder == cmStringSorter::Order::UNINITIALIZED) {
  1126. sortOrder = cmStringSorter::Order::ASCENDING;
  1127. }
  1128. const std::string& listName = args[1];
  1129. // expand the variable
  1130. std::vector<std::string> varArgsExpanded;
  1131. if (!this->GetList(varArgsExpanded, listName)) {
  1132. return true;
  1133. }
  1134. if ((sortCompare == cmStringSorter::Compare::STRING) &&
  1135. (sortCaseSensitivity == cmStringSorter::CaseSensitivity::SENSITIVE) &&
  1136. (sortOrder == cmStringSorter::Order::ASCENDING)) {
  1137. std::sort(varArgsExpanded.begin(), varArgsExpanded.end());
  1138. } else {
  1139. cmStringSorter sorter(sortCompare, sortCaseSensitivity, sortOrder);
  1140. std::sort(varArgsExpanded.begin(), varArgsExpanded.end(), sorter);
  1141. }
  1142. std::string value = cmJoin(varArgsExpanded, ";");
  1143. this->Makefile->AddDefinition(listName, value);
  1144. return true;
  1145. }
  1146. bool cmListCommand::HandleSublistCommand(std::vector<std::string> const& args)
  1147. {
  1148. if (args.size() != 5) {
  1149. std::ostringstream error;
  1150. error << "sub-command SUBLIST requires four arguments (" << args.size() - 1
  1151. << " found).";
  1152. this->SetError(error.str());
  1153. return false;
  1154. }
  1155. const std::string& listName = args[1];
  1156. const std::string& variableName = args.back();
  1157. // expand the variable
  1158. std::vector<std::string> varArgsExpanded;
  1159. if (!this->GetList(varArgsExpanded, listName) || varArgsExpanded.empty()) {
  1160. this->Makefile->AddDefinition(variableName, "");
  1161. return true;
  1162. }
  1163. const int start = atoi(args[2].c_str());
  1164. const int length = atoi(args[3].c_str());
  1165. using size_type = decltype(varArgsExpanded)::size_type;
  1166. if (start < 0 || size_type(start) >= varArgsExpanded.size()) {
  1167. std::ostringstream error;
  1168. error << "begin index: " << start << " is out of range 0 - "
  1169. << varArgsExpanded.size() - 1;
  1170. this->SetError(error.str());
  1171. return false;
  1172. }
  1173. if (length < -1) {
  1174. std::ostringstream error;
  1175. error << "length: " << length << " should be -1 or greater";
  1176. this->SetError(error.str());
  1177. return false;
  1178. }
  1179. const size_type end =
  1180. (length == -1 || size_type(start + length) > varArgsExpanded.size())
  1181. ? varArgsExpanded.size()
  1182. : size_type(start + length);
  1183. std::vector<std::string> sublist(varArgsExpanded.begin() + start,
  1184. varArgsExpanded.begin() + end);
  1185. this->Makefile->AddDefinition(variableName, cmJoin(sublist, ";"));
  1186. return true;
  1187. }
  1188. bool cmListCommand::HandleRemoveAtCommand(std::vector<std::string> const& args)
  1189. {
  1190. if (args.size() < 3) {
  1191. this->SetError("sub-command REMOVE_AT requires at least "
  1192. "two arguments.");
  1193. return false;
  1194. }
  1195. const std::string& listName = args[1];
  1196. // expand the variable
  1197. std::vector<std::string> varArgsExpanded;
  1198. if (!this->GetList(varArgsExpanded, listName) || varArgsExpanded.empty()) {
  1199. std::ostringstream str;
  1200. str << "index: ";
  1201. for (size_t i = 1; i < args.size(); ++i) {
  1202. str << args[i];
  1203. if (i != args.size() - 1) {
  1204. str << ", ";
  1205. }
  1206. }
  1207. str << " out of range (0, 0)";
  1208. this->SetError(str.str());
  1209. return false;
  1210. }
  1211. size_t cc;
  1212. std::vector<size_t> removed;
  1213. size_t nitem = varArgsExpanded.size();
  1214. for (cc = 2; cc < args.size(); ++cc) {
  1215. int item = atoi(args[cc].c_str());
  1216. if (item < 0) {
  1217. item = static_cast<int>(nitem) + item;
  1218. }
  1219. if (item < 0 || nitem <= static_cast<size_t>(item)) {
  1220. std::ostringstream str;
  1221. str << "index: " << item << " out of range (-" << nitem << ", "
  1222. << nitem - 1 << ")";
  1223. this->SetError(str.str());
  1224. return false;
  1225. }
  1226. removed.push_back(static_cast<size_t>(item));
  1227. }
  1228. std::sort(removed.begin(), removed.end());
  1229. std::vector<size_t>::const_iterator remEnd =
  1230. std::unique(removed.begin(), removed.end());
  1231. std::vector<size_t>::const_iterator remBegin = removed.begin();
  1232. std::vector<std::string>::const_iterator argsEnd =
  1233. cmRemoveIndices(varArgsExpanded, cmMakeRange(remBegin, remEnd));
  1234. std::vector<std::string>::const_iterator argsBegin = varArgsExpanded.begin();
  1235. std::string value = cmJoin(cmMakeRange(argsBegin, argsEnd), ";");
  1236. this->Makefile->AddDefinition(listName, value);
  1237. return true;
  1238. }
  1239. bool cmListCommand::HandleFilterCommand(std::vector<std::string> const& args)
  1240. {
  1241. if (args.size() < 2) {
  1242. this->SetError("sub-command FILTER requires a list to be specified.");
  1243. return false;
  1244. }
  1245. if (args.size() < 3) {
  1246. this->SetError("sub-command FILTER requires an operator to be specified.");
  1247. return false;
  1248. }
  1249. if (args.size() < 4) {
  1250. this->SetError("sub-command FILTER requires a mode to be specified.");
  1251. return false;
  1252. }
  1253. const std::string& op = args[2];
  1254. bool includeMatches;
  1255. if (op == "INCLUDE") {
  1256. includeMatches = true;
  1257. } else if (op == "EXCLUDE") {
  1258. includeMatches = false;
  1259. } else {
  1260. this->SetError("sub-command FILTER does not recognize operator " + op);
  1261. return false;
  1262. }
  1263. const std::string& listName = args[1];
  1264. // expand the variable
  1265. std::vector<std::string> varArgsExpanded;
  1266. if (!this->GetList(varArgsExpanded, listName)) {
  1267. return true;
  1268. }
  1269. const std::string& mode = args[3];
  1270. if (mode == "REGEX") {
  1271. if (args.size() != 5) {
  1272. this->SetError("sub-command FILTER, mode REGEX "
  1273. "requires five arguments.");
  1274. return false;
  1275. }
  1276. return this->FilterRegex(args, includeMatches, listName, varArgsExpanded);
  1277. }
  1278. this->SetError("sub-command FILTER does not recognize mode " + mode);
  1279. return false;
  1280. }
  1281. class MatchesRegex
  1282. {
  1283. public:
  1284. MatchesRegex(cmsys::RegularExpression& in_regex, bool in_includeMatches)
  1285. : regex(in_regex)
  1286. , includeMatches(in_includeMatches)
  1287. {
  1288. }
  1289. bool operator()(const std::string& target)
  1290. {
  1291. return regex.find(target) ^ includeMatches;
  1292. }
  1293. private:
  1294. cmsys::RegularExpression& regex;
  1295. const bool includeMatches;
  1296. };
  1297. bool cmListCommand::FilterRegex(std::vector<std::string> const& args,
  1298. bool includeMatches,
  1299. std::string const& listName,
  1300. std::vector<std::string>& varArgsExpanded)
  1301. {
  1302. const std::string& pattern = args[4];
  1303. cmsys::RegularExpression regex(pattern);
  1304. if (!regex.is_valid()) {
  1305. std::string error = "sub-command FILTER, mode REGEX ";
  1306. error += "failed to compile regex \"";
  1307. error += pattern;
  1308. error += "\".";
  1309. this->SetError(error);
  1310. return false;
  1311. }
  1312. std::vector<std::string>::iterator argsBegin = varArgsExpanded.begin();
  1313. std::vector<std::string>::iterator argsEnd = varArgsExpanded.end();
  1314. std::vector<std::string>::iterator newArgsEnd =
  1315. std::remove_if(argsBegin, argsEnd, MatchesRegex(regex, includeMatches));
  1316. std::string value = cmJoin(cmMakeRange(argsBegin, newArgsEnd), ";");
  1317. this->Makefile->AddDefinition(listName, value);
  1318. return true;
  1319. }