cmListCommand.cxx 46 KB

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