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