cmStringCommand.cxx 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #define _SCL_SECURE_NO_WARNINGS
  4. #include "cmStringCommand.h"
  5. #include "cmsys/RegularExpression.hxx"
  6. #include <algorithm>
  7. #include <ctype.h>
  8. #include <iterator>
  9. #include <memory>
  10. #include <sstream>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include "cmAlgorithms.h"
  14. #include "cmCryptoHash.h"
  15. #include "cmExecutionStatus.h"
  16. #include "cmGeneratorExpression.h"
  17. #include "cmMakefile.h"
  18. #include "cmMessageType.h"
  19. #include "cmRange.h"
  20. #include "cmStringAlgorithms.h"
  21. #include "cmStringReplaceHelper.h"
  22. #include "cmSystemTools.h"
  23. #include "cmTimestamp.h"
  24. #include "cmUuid.h"
  25. namespace {
  26. bool HandleConfigureCommand(std::vector<std::string> const& args,
  27. cmExecutionStatus& status);
  28. bool HandleAsciiCommand(std::vector<std::string> const& args,
  29. cmExecutionStatus& status);
  30. bool HandleRegexCommand(std::vector<std::string> const& args,
  31. cmExecutionStatus& status);
  32. bool RegexMatch(std::vector<std::string> const& args,
  33. cmExecutionStatus& status);
  34. bool RegexMatchAll(std::vector<std::string> const& args,
  35. cmExecutionStatus& status);
  36. bool RegexReplace(std::vector<std::string> const& args,
  37. cmExecutionStatus& status);
  38. bool HandleHashCommand(std::vector<std::string> const& args,
  39. cmExecutionStatus& status);
  40. bool HandleToUpperLowerCommand(std::vector<std::string> const& args,
  41. bool toUpper, cmExecutionStatus& status);
  42. bool HandleCompareCommand(std::vector<std::string> const& args,
  43. cmExecutionStatus& status);
  44. bool HandleReplaceCommand(std::vector<std::string> const& args,
  45. cmExecutionStatus& status);
  46. bool HandleLengthCommand(std::vector<std::string> const& args,
  47. cmExecutionStatus& status);
  48. bool HandleSubstringCommand(std::vector<std::string> const& args,
  49. cmExecutionStatus& status);
  50. bool HandleAppendCommand(std::vector<std::string> const& args,
  51. cmExecutionStatus& status);
  52. bool HandlePrependCommand(std::vector<std::string> const& args,
  53. cmExecutionStatus& status);
  54. bool HandleConcatCommand(std::vector<std::string> const& args,
  55. cmExecutionStatus& status);
  56. bool HandleJoinCommand(std::vector<std::string> const& args,
  57. cmExecutionStatus& status);
  58. bool HandleStripCommand(std::vector<std::string> const& args,
  59. cmExecutionStatus& status);
  60. bool HandleRepeatCommand(std::vector<std::string> const& args,
  61. cmMakefile& makefile);
  62. bool HandleRandomCommand(std::vector<std::string> const& args,
  63. cmExecutionStatus& status);
  64. bool HandleFindCommand(std::vector<std::string> const& args,
  65. cmExecutionStatus& status);
  66. bool HandleTimestampCommand(std::vector<std::string> const& args,
  67. cmExecutionStatus& status);
  68. bool HandleMakeCIdentifierCommand(std::vector<std::string> const& args,
  69. cmExecutionStatus& status);
  70. bool HandleGenexStripCommand(std::vector<std::string> const& args,
  71. cmExecutionStatus& status);
  72. bool HandleUuidCommand(std::vector<std::string> const& args,
  73. cmExecutionStatus& status);
  74. bool joinImpl(std::vector<std::string> const& args, std::string const& glue,
  75. size_t varIdx, cmMakefile& makefile);
  76. }
  77. bool cmStringCommand(std::vector<std::string> const& args,
  78. cmExecutionStatus& status)
  79. {
  80. if (args.empty()) {
  81. status.SetError("must be called with at least one argument.");
  82. return false;
  83. }
  84. const std::string& subCommand = args[0];
  85. if (subCommand == "REGEX") {
  86. return HandleRegexCommand(args, status);
  87. }
  88. if (subCommand == "REPLACE") {
  89. return HandleReplaceCommand(args, status);
  90. }
  91. if (subCommand == "MD5" || subCommand == "SHA1" || subCommand == "SHA224" ||
  92. subCommand == "SHA256" || subCommand == "SHA384" ||
  93. subCommand == "SHA512" || subCommand == "SHA3_224" ||
  94. subCommand == "SHA3_256" || subCommand == "SHA3_384" ||
  95. subCommand == "SHA3_512") {
  96. return HandleHashCommand(args, status);
  97. }
  98. if (subCommand == "TOLOWER") {
  99. return HandleToUpperLowerCommand(args, false, status);
  100. }
  101. if (subCommand == "TOUPPER") {
  102. return HandleToUpperLowerCommand(args, true, status);
  103. }
  104. if (subCommand == "COMPARE") {
  105. return HandleCompareCommand(args, status);
  106. }
  107. if (subCommand == "ASCII") {
  108. return HandleAsciiCommand(args, status);
  109. }
  110. if (subCommand == "CONFIGURE") {
  111. return HandleConfigureCommand(args, status);
  112. }
  113. if (subCommand == "LENGTH") {
  114. return HandleLengthCommand(args, status);
  115. }
  116. if (subCommand == "APPEND") {
  117. return HandleAppendCommand(args, status);
  118. }
  119. if (subCommand == "PREPEND") {
  120. return HandlePrependCommand(args, status);
  121. }
  122. if (subCommand == "CONCAT") {
  123. return HandleConcatCommand(args, status);
  124. }
  125. if (subCommand == "JOIN") {
  126. return HandleJoinCommand(args, status);
  127. }
  128. if (subCommand == "SUBSTRING") {
  129. return HandleSubstringCommand(args, status);
  130. }
  131. if (subCommand == "STRIP") {
  132. return HandleStripCommand(args, status);
  133. }
  134. if (subCommand == "REPEAT") {
  135. return HandleRepeatCommand(args, status.GetMakefile());
  136. }
  137. if (subCommand == "RANDOM") {
  138. return HandleRandomCommand(args, status);
  139. }
  140. if (subCommand == "FIND") {
  141. return HandleFindCommand(args, status);
  142. }
  143. if (subCommand == "TIMESTAMP") {
  144. return HandleTimestampCommand(args, status);
  145. }
  146. if (subCommand == "MAKE_C_IDENTIFIER") {
  147. return HandleMakeCIdentifierCommand(args, status);
  148. }
  149. if (subCommand == "GENEX_STRIP") {
  150. return HandleGenexStripCommand(args, status);
  151. }
  152. if (subCommand == "UUID") {
  153. return HandleUuidCommand(args, status);
  154. }
  155. std::string e = "does not recognize sub-command " + subCommand;
  156. status.SetError(e);
  157. return false;
  158. }
  159. namespace {
  160. bool HandleHashCommand(std::vector<std::string> const& args,
  161. cmExecutionStatus& status)
  162. {
  163. #if !defined(CMAKE_BOOTSTRAP)
  164. if (args.size() != 3) {
  165. std::ostringstream e;
  166. e << args[0] << " requires an output variable and an input string";
  167. status.SetError(e.str());
  168. return false;
  169. }
  170. std::unique_ptr<cmCryptoHash> hash(cmCryptoHash::New(args[0]));
  171. if (hash) {
  172. std::string out = hash->HashString(args[2]);
  173. status.GetMakefile().AddDefinition(args[1], out);
  174. return true;
  175. }
  176. return false;
  177. #else
  178. std::ostringstream e;
  179. e << args[0] << " not available during bootstrap";
  180. status.SetError(e.str().c_str());
  181. return false;
  182. #endif
  183. }
  184. bool HandleToUpperLowerCommand(std::vector<std::string> const& args,
  185. bool toUpper, cmExecutionStatus& status)
  186. {
  187. if (args.size() < 3) {
  188. status.SetError("no output variable specified");
  189. return false;
  190. }
  191. std::string const& outvar = args[2];
  192. std::string output;
  193. if (toUpper) {
  194. output = cmSystemTools::UpperCase(args[1]);
  195. } else {
  196. output = cmSystemTools::LowerCase(args[1]);
  197. }
  198. // Store the output in the provided variable.
  199. status.GetMakefile().AddDefinition(outvar, output);
  200. return true;
  201. }
  202. bool HandleAsciiCommand(std::vector<std::string> const& args,
  203. cmExecutionStatus& status)
  204. {
  205. if (args.size() < 3) {
  206. status.SetError("No output variable specified");
  207. return false;
  208. }
  209. std::string::size_type cc;
  210. std::string const& outvar = args.back();
  211. std::string output;
  212. for (cc = 1; cc < args.size() - 1; cc++) {
  213. int ch = atoi(args[cc].c_str());
  214. if (ch > 0 && ch < 256) {
  215. output += static_cast<char>(ch);
  216. } else {
  217. std::string error = "Character with code ";
  218. error += args[cc];
  219. error += " does not exist.";
  220. status.SetError(error);
  221. return false;
  222. }
  223. }
  224. // Store the output in the provided variable.
  225. status.GetMakefile().AddDefinition(outvar, output);
  226. return true;
  227. }
  228. bool HandleConfigureCommand(std::vector<std::string> const& args,
  229. cmExecutionStatus& status)
  230. {
  231. if (args.size() < 2) {
  232. status.SetError("No input string specified.");
  233. return false;
  234. }
  235. if (args.size() < 3) {
  236. status.SetError("No output variable specified.");
  237. return false;
  238. }
  239. // Parse options.
  240. bool escapeQuotes = false;
  241. bool atOnly = false;
  242. for (unsigned int i = 3; i < args.size(); ++i) {
  243. if (args[i] == "@ONLY") {
  244. atOnly = true;
  245. } else if (args[i] == "ESCAPE_QUOTES") {
  246. escapeQuotes = true;
  247. } else {
  248. std::ostringstream err;
  249. err << "Unrecognized argument \"" << args[i] << "\"";
  250. status.SetError(err.str());
  251. return false;
  252. }
  253. }
  254. // Configure the string.
  255. std::string output;
  256. status.GetMakefile().ConfigureString(args[1], output, atOnly, escapeQuotes);
  257. // Store the output in the provided variable.
  258. status.GetMakefile().AddDefinition(args[2], output);
  259. return true;
  260. }
  261. bool HandleRegexCommand(std::vector<std::string> const& args,
  262. cmExecutionStatus& status)
  263. {
  264. if (args.size() < 2) {
  265. status.SetError("sub-command REGEX requires a mode to be specified.");
  266. return false;
  267. }
  268. std::string const& mode = args[1];
  269. if (mode == "MATCH") {
  270. if (args.size() < 5) {
  271. status.SetError("sub-command REGEX, mode MATCH needs "
  272. "at least 5 arguments total to command.");
  273. return false;
  274. }
  275. return RegexMatch(args, status);
  276. }
  277. if (mode == "MATCHALL") {
  278. if (args.size() < 5) {
  279. status.SetError("sub-command REGEX, mode MATCHALL needs "
  280. "at least 5 arguments total to command.");
  281. return false;
  282. }
  283. return RegexMatchAll(args, status);
  284. }
  285. if (mode == "REPLACE") {
  286. if (args.size() < 6) {
  287. status.SetError("sub-command REGEX, mode REPLACE needs "
  288. "at least 6 arguments total to command.");
  289. return false;
  290. }
  291. return RegexReplace(args, status);
  292. }
  293. std::string e = "sub-command REGEX does not recognize mode " + mode;
  294. status.SetError(e);
  295. return false;
  296. }
  297. bool RegexMatch(std::vector<std::string> const& args,
  298. cmExecutionStatus& status)
  299. {
  300. //"STRING(REGEX MATCH <regular_expression> <output variable>
  301. // <input> [<input>...])\n";
  302. std::string const& regex = args[2];
  303. std::string const& outvar = args[3];
  304. status.GetMakefile().ClearMatches();
  305. // Compile the regular expression.
  306. cmsys::RegularExpression re;
  307. if (!re.compile(regex.c_str())) {
  308. std::string e =
  309. "sub-command REGEX, mode MATCH failed to compile regex \"" + regex +
  310. "\".";
  311. status.SetError(e);
  312. return false;
  313. }
  314. // Concatenate all the last arguments together.
  315. std::string input = cmJoin(cmMakeRange(args).advance(4), std::string());
  316. // Scan through the input for all matches.
  317. std::string output;
  318. if (re.find(input)) {
  319. status.GetMakefile().StoreMatches(re);
  320. std::string::size_type l = re.start();
  321. std::string::size_type r = re.end();
  322. if (r - l == 0) {
  323. std::string e = "sub-command REGEX, mode MATCH regex \"" + regex +
  324. "\" matched an empty string.";
  325. status.SetError(e);
  326. return false;
  327. }
  328. output = input.substr(l, r - l);
  329. }
  330. // Store the output in the provided variable.
  331. status.GetMakefile().AddDefinition(outvar, output);
  332. return true;
  333. }
  334. bool RegexMatchAll(std::vector<std::string> const& args,
  335. cmExecutionStatus& status)
  336. {
  337. //"STRING(REGEX MATCHALL <regular_expression> <output variable> <input>
  338. // [<input>...])\n";
  339. std::string const& regex = args[2];
  340. std::string const& outvar = args[3];
  341. status.GetMakefile().ClearMatches();
  342. // Compile the regular expression.
  343. cmsys::RegularExpression re;
  344. if (!re.compile(regex.c_str())) {
  345. std::string e =
  346. "sub-command REGEX, mode MATCHALL failed to compile regex \"" + regex +
  347. "\".";
  348. status.SetError(e);
  349. return false;
  350. }
  351. // Concatenate all the last arguments together.
  352. std::string input = cmJoin(cmMakeRange(args).advance(4), std::string());
  353. // Scan through the input for all matches.
  354. std::string output;
  355. const char* p = input.c_str();
  356. while (re.find(p)) {
  357. status.GetMakefile().ClearMatches();
  358. status.GetMakefile().StoreMatches(re);
  359. std::string::size_type l = re.start();
  360. std::string::size_type r = re.end();
  361. if (r - l == 0) {
  362. std::string e = "sub-command REGEX, mode MATCHALL regex \"" + regex +
  363. "\" matched an empty string.";
  364. status.SetError(e);
  365. return false;
  366. }
  367. if (!output.empty()) {
  368. output += ";";
  369. }
  370. output += std::string(p + l, r - l);
  371. p += r;
  372. }
  373. // Store the output in the provided variable.
  374. status.GetMakefile().AddDefinition(outvar, output);
  375. return true;
  376. }
  377. bool RegexReplace(std::vector<std::string> const& args,
  378. cmExecutionStatus& status)
  379. {
  380. //"STRING(REGEX REPLACE <regular_expression> <replace_expression>
  381. // <output variable> <input> [<input>...])\n"
  382. std::string const& regex = args[2];
  383. std::string const& replace = args[3];
  384. std::string const& outvar = args[4];
  385. cmStringReplaceHelper replaceHelper(regex, replace, &status.GetMakefile());
  386. if (!replaceHelper.IsReplaceExpressionValid()) {
  387. status.SetError(
  388. "sub-command REGEX, mode REPLACE: " + replaceHelper.GetError() + ".");
  389. return false;
  390. }
  391. status.GetMakefile().ClearMatches();
  392. if (!replaceHelper.IsRegularExpressionValid()) {
  393. std::string e =
  394. "sub-command REGEX, mode REPLACE failed to compile regex \"" + regex +
  395. "\".";
  396. status.SetError(e);
  397. return false;
  398. }
  399. // Concatenate all the last arguments together.
  400. const std::string input =
  401. cmJoin(cmMakeRange(args).advance(5), std::string());
  402. std::string output;
  403. if (!replaceHelper.Replace(input, output)) {
  404. status.SetError(
  405. "sub-command REGEX, mode REPLACE: " + replaceHelper.GetError() + ".");
  406. return false;
  407. }
  408. // Store the output in the provided variable.
  409. status.GetMakefile().AddDefinition(outvar, output);
  410. return true;
  411. }
  412. bool HandleFindCommand(std::vector<std::string> const& args,
  413. cmExecutionStatus& status)
  414. {
  415. // check if all required parameters were passed
  416. if (args.size() < 4 || args.size() > 5) {
  417. status.SetError("sub-command FIND requires 3 or 4 parameters.");
  418. return false;
  419. }
  420. // check if the reverse flag was set or not
  421. bool reverseMode = false;
  422. if (args.size() == 5 && args[4] == "REVERSE") {
  423. reverseMode = true;
  424. }
  425. // if we have 5 arguments the last one must be REVERSE
  426. if (args.size() == 5 && args[4] != "REVERSE") {
  427. status.SetError("sub-command FIND: unknown last parameter");
  428. return false;
  429. }
  430. // local parameter names.
  431. const std::string& sstring = args[1];
  432. const std::string& schar = args[2];
  433. const std::string& outvar = args[3];
  434. // ensure that the user cannot accidentally specify REVERSE as a variable
  435. if (outvar == "REVERSE") {
  436. status.SetError("sub-command FIND does not allow one to select REVERSE as "
  437. "the output variable. "
  438. "Maybe you missed the actual output variable?");
  439. return false;
  440. }
  441. // try to find the character and return its position
  442. size_t pos;
  443. if (!reverseMode) {
  444. pos = sstring.find(schar);
  445. } else {
  446. pos = sstring.rfind(schar);
  447. }
  448. if (std::string::npos != pos) {
  449. std::ostringstream s;
  450. s << pos;
  451. status.GetMakefile().AddDefinition(outvar, s.str());
  452. return true;
  453. }
  454. // the character was not found, but this is not really an error
  455. status.GetMakefile().AddDefinition(outvar, "-1");
  456. return true;
  457. }
  458. bool HandleCompareCommand(std::vector<std::string> const& args,
  459. cmExecutionStatus& status)
  460. {
  461. if (args.size() < 2) {
  462. status.SetError("sub-command COMPARE requires a mode to be specified.");
  463. return false;
  464. }
  465. std::string const& mode = args[1];
  466. if ((mode == "EQUAL") || (mode == "NOTEQUAL") || (mode == "LESS") ||
  467. (mode == "LESS_EQUAL") || (mode == "GREATER") ||
  468. (mode == "GREATER_EQUAL")) {
  469. if (args.size() < 5) {
  470. std::string e = "sub-command COMPARE, mode ";
  471. e += mode;
  472. e += " needs at least 5 arguments total to command.";
  473. status.SetError(e);
  474. return false;
  475. }
  476. const std::string& left = args[2];
  477. const std::string& right = args[3];
  478. const std::string& outvar = args[4];
  479. bool result;
  480. if (mode == "LESS") {
  481. result = (left < right);
  482. } else if (mode == "LESS_EQUAL") {
  483. result = (left <= right);
  484. } else if (mode == "GREATER") {
  485. result = (left > right);
  486. } else if (mode == "GREATER_EQUAL") {
  487. result = (left >= right);
  488. } else if (mode == "EQUAL") {
  489. result = (left == right);
  490. } else // if(mode == "NOTEQUAL")
  491. {
  492. result = !(left == right);
  493. }
  494. if (result) {
  495. status.GetMakefile().AddDefinition(outvar, "1");
  496. } else {
  497. status.GetMakefile().AddDefinition(outvar, "0");
  498. }
  499. return true;
  500. }
  501. std::string e = "sub-command COMPARE does not recognize mode " + mode;
  502. status.SetError(e);
  503. return false;
  504. }
  505. bool HandleReplaceCommand(std::vector<std::string> const& args,
  506. cmExecutionStatus& status)
  507. {
  508. if (args.size() < 5) {
  509. status.SetError("sub-command REPLACE requires at least four arguments.");
  510. return false;
  511. }
  512. const std::string& matchExpression = args[1];
  513. const std::string& replaceExpression = args[2];
  514. const std::string& variableName = args[3];
  515. std::string input = cmJoin(cmMakeRange(args).advance(4), std::string());
  516. cmsys::SystemTools::ReplaceString(input, matchExpression.c_str(),
  517. replaceExpression.c_str());
  518. status.GetMakefile().AddDefinition(variableName, input);
  519. return true;
  520. }
  521. bool HandleSubstringCommand(std::vector<std::string> const& args,
  522. cmExecutionStatus& status)
  523. {
  524. if (args.size() != 5) {
  525. status.SetError("sub-command SUBSTRING requires four arguments.");
  526. return false;
  527. }
  528. const std::string& stringValue = args[1];
  529. int begin = atoi(args[2].c_str());
  530. int end = atoi(args[3].c_str());
  531. const std::string& variableName = args[4];
  532. size_t stringLength = stringValue.size();
  533. int intStringLength = static_cast<int>(stringLength);
  534. if (begin < 0 || begin > intStringLength) {
  535. std::ostringstream ostr;
  536. ostr << "begin index: " << begin << " is out of range 0 - "
  537. << stringLength;
  538. status.SetError(ostr.str());
  539. return false;
  540. }
  541. if (end < -1) {
  542. std::ostringstream ostr;
  543. ostr << "end index: " << end << " should be -1 or greater";
  544. status.SetError(ostr.str());
  545. return false;
  546. }
  547. status.GetMakefile().AddDefinition(variableName,
  548. stringValue.substr(begin, end));
  549. return true;
  550. }
  551. bool HandleLengthCommand(std::vector<std::string> const& args,
  552. cmExecutionStatus& status)
  553. {
  554. if (args.size() != 3) {
  555. status.SetError("sub-command LENGTH requires two arguments.");
  556. return false;
  557. }
  558. const std::string& stringValue = args[1];
  559. const std::string& variableName = args[2];
  560. size_t length = stringValue.size();
  561. char buffer[1024];
  562. sprintf(buffer, "%d", static_cast<int>(length));
  563. status.GetMakefile().AddDefinition(variableName, buffer);
  564. return true;
  565. }
  566. bool HandleAppendCommand(std::vector<std::string> const& args,
  567. cmExecutionStatus& status)
  568. {
  569. if (args.size() < 2) {
  570. status.SetError("sub-command APPEND requires at least one argument.");
  571. return false;
  572. }
  573. // Skip if nothing to append.
  574. if (args.size() < 3) {
  575. return true;
  576. }
  577. const std::string& variable = args[1];
  578. std::string value;
  579. const char* oldValue = status.GetMakefile().GetDefinition(variable);
  580. if (oldValue) {
  581. value = oldValue;
  582. }
  583. value += cmJoin(cmMakeRange(args).advance(2), std::string());
  584. status.GetMakefile().AddDefinition(variable, value);
  585. return true;
  586. }
  587. bool HandlePrependCommand(std::vector<std::string> const& args,
  588. cmExecutionStatus& status)
  589. {
  590. if (args.size() < 2) {
  591. status.SetError("sub-command PREPEND requires at least one argument.");
  592. return false;
  593. }
  594. // Skip if nothing to prepend.
  595. if (args.size() < 3) {
  596. return true;
  597. }
  598. const std::string& variable = args[1];
  599. std::string value = cmJoin(cmMakeRange(args).advance(2), std::string());
  600. const char* oldValue = status.GetMakefile().GetDefinition(variable);
  601. if (oldValue) {
  602. value += oldValue;
  603. }
  604. status.GetMakefile().AddDefinition(variable, value);
  605. return true;
  606. }
  607. bool HandleConcatCommand(std::vector<std::string> const& args,
  608. cmExecutionStatus& status)
  609. {
  610. if (args.size() < 2) {
  611. status.SetError("sub-command CONCAT requires at least one argument.");
  612. return false;
  613. }
  614. return joinImpl(args, std::string(), 1, status.GetMakefile());
  615. }
  616. bool HandleJoinCommand(std::vector<std::string> const& args,
  617. cmExecutionStatus& status)
  618. {
  619. if (args.size() < 3) {
  620. status.SetError("sub-command JOIN requires at least two arguments.");
  621. return false;
  622. }
  623. return joinImpl(args, args[1], 2, status.GetMakefile());
  624. }
  625. bool joinImpl(std::vector<std::string> const& args, std::string const& glue,
  626. const size_t varIdx, cmMakefile& makefile)
  627. {
  628. std::string const& variableName = args[varIdx];
  629. // NOTE Items to concat/join placed right after the variable for
  630. // both `CONCAT` and `JOIN` sub-commands.
  631. std::string value = cmJoin(cmMakeRange(args).advance(varIdx + 1), glue);
  632. makefile.AddDefinition(variableName, value);
  633. return true;
  634. }
  635. bool HandleMakeCIdentifierCommand(std::vector<std::string> const& args,
  636. cmExecutionStatus& status)
  637. {
  638. if (args.size() != 3) {
  639. status.SetError("sub-command MAKE_C_IDENTIFIER requires two arguments.");
  640. return false;
  641. }
  642. const std::string& input = args[1];
  643. const std::string& variableName = args[2];
  644. status.GetMakefile().AddDefinition(variableName,
  645. cmSystemTools::MakeCidentifier(input));
  646. return true;
  647. }
  648. bool HandleGenexStripCommand(std::vector<std::string> const& args,
  649. cmExecutionStatus& status)
  650. {
  651. if (args.size() != 3) {
  652. status.SetError("sub-command GENEX_STRIP requires two arguments.");
  653. return false;
  654. }
  655. const std::string& input = args[1];
  656. std::string result = cmGeneratorExpression::Preprocess(
  657. input, cmGeneratorExpression::StripAllGeneratorExpressions);
  658. const std::string& variableName = args[2];
  659. status.GetMakefile().AddDefinition(variableName, result);
  660. return true;
  661. }
  662. bool HandleStripCommand(std::vector<std::string> const& args,
  663. cmExecutionStatus& status)
  664. {
  665. if (args.size() != 3) {
  666. status.SetError("sub-command STRIP requires two arguments.");
  667. return false;
  668. }
  669. const std::string& stringValue = args[1];
  670. const std::string& variableName = args[2];
  671. size_t inStringLength = stringValue.size();
  672. size_t startPos = inStringLength + 1;
  673. size_t endPos = 0;
  674. const char* ptr = stringValue.c_str();
  675. size_t cc;
  676. for (cc = 0; cc < inStringLength; ++cc) {
  677. if (!isspace(*ptr)) {
  678. if (startPos > inStringLength) {
  679. startPos = cc;
  680. }
  681. endPos = cc;
  682. }
  683. ++ptr;
  684. }
  685. size_t outLength = 0;
  686. // if the input string didn't contain any non-space characters, return
  687. // an empty string
  688. if (startPos > inStringLength) {
  689. outLength = 0;
  690. startPos = 0;
  691. } else {
  692. outLength = endPos - startPos + 1;
  693. }
  694. status.GetMakefile().AddDefinition(variableName,
  695. stringValue.substr(startPos, outLength));
  696. return true;
  697. }
  698. bool HandleRepeatCommand(std::vector<std::string> const& args,
  699. cmMakefile& makefile)
  700. {
  701. // `string(REPEAT "<str>" <times> OUTPUT_VARIABLE)`
  702. enum ArgPos : std::size_t
  703. {
  704. SUB_COMMAND,
  705. VALUE,
  706. TIMES,
  707. OUTPUT_VARIABLE,
  708. TOTAL_ARGS
  709. };
  710. if (args.size() != ArgPos::TOTAL_ARGS) {
  711. makefile.IssueMessage(MessageType::FATAL_ERROR,
  712. "sub-command REPEAT requires three arguments.");
  713. return true;
  714. }
  715. unsigned long times;
  716. if (!cmStrToULong(args[ArgPos::TIMES], &times)) {
  717. makefile.IssueMessage(MessageType::FATAL_ERROR,
  718. "repeat count is not a positive number.");
  719. return true;
  720. }
  721. const auto& stringValue = args[ArgPos::VALUE];
  722. const auto& variableName = args[ArgPos::OUTPUT_VARIABLE];
  723. const auto inStringLength = stringValue.size();
  724. std::string result;
  725. switch (inStringLength) {
  726. case 0u:
  727. // Nothing to do for zero length input strings
  728. break;
  729. case 1u:
  730. // NOTE If the string to repeat consists of the only character,
  731. // use the appropriate constructor.
  732. result = std::string(times, stringValue[0]);
  733. break;
  734. default:
  735. result = std::string(inStringLength * times, char{});
  736. for (auto i = 0u; i < times; ++i) {
  737. std::copy(cm::cbegin(stringValue), cm::cend(stringValue),
  738. &result[i * inStringLength]);
  739. }
  740. break;
  741. }
  742. makefile.AddDefinition(variableName, result);
  743. return true;
  744. }
  745. bool HandleRandomCommand(std::vector<std::string> const& args,
  746. cmExecutionStatus& status)
  747. {
  748. if (args.size() < 2 || args.size() == 3 || args.size() == 5) {
  749. status.SetError("sub-command RANDOM requires at least one argument.");
  750. return false;
  751. }
  752. static bool seeded = false;
  753. bool force_seed = false;
  754. unsigned int seed = 0;
  755. int length = 5;
  756. const char cmStringCommandDefaultAlphabet[] = "qwertyuiopasdfghjklzxcvbnm"
  757. "QWERTYUIOPASDFGHJKLZXCVBNM"
  758. "0123456789";
  759. std::string alphabet;
  760. if (args.size() > 3) {
  761. size_t i = 1;
  762. size_t stopAt = args.size() - 2;
  763. for (; i < stopAt; ++i) {
  764. if (args[i] == "LENGTH") {
  765. ++i;
  766. length = atoi(args[i].c_str());
  767. } else if (args[i] == "ALPHABET") {
  768. ++i;
  769. alphabet = args[i];
  770. } else if (args[i] == "RANDOM_SEED") {
  771. ++i;
  772. seed = static_cast<unsigned int>(atoi(args[i].c_str()));
  773. force_seed = true;
  774. }
  775. }
  776. }
  777. if (alphabet.empty()) {
  778. alphabet = cmStringCommandDefaultAlphabet;
  779. }
  780. double sizeofAlphabet = static_cast<double>(alphabet.size());
  781. if (sizeofAlphabet < 1) {
  782. status.SetError("sub-command RANDOM invoked with bad alphabet.");
  783. return false;
  784. }
  785. if (length < 1) {
  786. status.SetError("sub-command RANDOM invoked with bad length.");
  787. return false;
  788. }
  789. const std::string& variableName = args.back();
  790. std::vector<char> result;
  791. if (!seeded || force_seed) {
  792. seeded = true;
  793. srand(force_seed ? seed : cmSystemTools::RandomSeed());
  794. }
  795. const char* alphaPtr = alphabet.c_str();
  796. for (int cc = 0; cc < length; cc++) {
  797. int idx = static_cast<int>(sizeofAlphabet * rand() / (RAND_MAX + 1.0));
  798. result.push_back(*(alphaPtr + idx));
  799. }
  800. result.push_back(0);
  801. status.GetMakefile().AddDefinition(variableName, result.data());
  802. return true;
  803. }
  804. bool HandleTimestampCommand(std::vector<std::string> const& args,
  805. cmExecutionStatus& status)
  806. {
  807. if (args.size() < 2) {
  808. status.SetError("sub-command TIMESTAMP requires at least one argument.");
  809. return false;
  810. }
  811. if (args.size() > 4) {
  812. status.SetError("sub-command TIMESTAMP takes at most three arguments.");
  813. return false;
  814. }
  815. unsigned int argsIndex = 1;
  816. const std::string& outputVariable = args[argsIndex++];
  817. std::string formatString;
  818. if (args.size() > argsIndex && args[argsIndex] != "UTC") {
  819. formatString = args[argsIndex++];
  820. }
  821. bool utcFlag = false;
  822. if (args.size() > argsIndex) {
  823. if (args[argsIndex] == "UTC") {
  824. utcFlag = true;
  825. } else {
  826. std::string e = " TIMESTAMP sub-command does not recognize option " +
  827. args[argsIndex] + ".";
  828. status.SetError(e);
  829. return false;
  830. }
  831. }
  832. cmTimestamp timestamp;
  833. std::string result = timestamp.CurrentTime(formatString, utcFlag);
  834. status.GetMakefile().AddDefinition(outputVariable, result);
  835. return true;
  836. }
  837. bool HandleUuidCommand(std::vector<std::string> const& args,
  838. cmExecutionStatus& status)
  839. {
  840. #if !defined(CMAKE_BOOTSTRAP)
  841. unsigned int argsIndex = 1;
  842. if (args.size() < 2) {
  843. status.SetError("UUID sub-command requires an output variable.");
  844. return false;
  845. }
  846. const std::string& outputVariable = args[argsIndex++];
  847. std::string uuidNamespaceString;
  848. std::string uuidName;
  849. std::string uuidType;
  850. bool uuidUpperCase = false;
  851. while (args.size() > argsIndex) {
  852. if (args[argsIndex] == "NAMESPACE") {
  853. ++argsIndex;
  854. if (argsIndex >= args.size()) {
  855. status.SetError("UUID sub-command, NAMESPACE requires a value.");
  856. return false;
  857. }
  858. uuidNamespaceString = args[argsIndex++];
  859. } else if (args[argsIndex] == "NAME") {
  860. ++argsIndex;
  861. if (argsIndex >= args.size()) {
  862. status.SetError("UUID sub-command, NAME requires a value.");
  863. return false;
  864. }
  865. uuidName = args[argsIndex++];
  866. } else if (args[argsIndex] == "TYPE") {
  867. ++argsIndex;
  868. if (argsIndex >= args.size()) {
  869. status.SetError("UUID sub-command, TYPE requires a value.");
  870. return false;
  871. }
  872. uuidType = args[argsIndex++];
  873. } else if (args[argsIndex] == "UPPER") {
  874. ++argsIndex;
  875. uuidUpperCase = true;
  876. } else {
  877. std::string e =
  878. "UUID sub-command does not recognize option " + args[argsIndex] + ".";
  879. status.SetError(e);
  880. return false;
  881. }
  882. }
  883. std::string uuid;
  884. cmUuid uuidGenerator;
  885. std::vector<unsigned char> uuidNamespace;
  886. if (!uuidGenerator.StringToBinary(uuidNamespaceString, uuidNamespace)) {
  887. status.SetError("UUID sub-command, malformed NAMESPACE UUID.");
  888. return false;
  889. }
  890. if (uuidType == "MD5") {
  891. uuid = uuidGenerator.FromMd5(uuidNamespace, uuidName);
  892. } else if (uuidType == "SHA1") {
  893. uuid = uuidGenerator.FromSha1(uuidNamespace, uuidName);
  894. } else {
  895. std::string e = "UUID sub-command, unknown TYPE '" + uuidType + "'.";
  896. status.SetError(e);
  897. return false;
  898. }
  899. if (uuid.empty()) {
  900. status.SetError("UUID sub-command, generation failed.");
  901. return false;
  902. }
  903. if (uuidUpperCase) {
  904. uuid = cmSystemTools::UpperCase(uuid);
  905. }
  906. status.GetMakefile().AddDefinition(outputVariable, uuid);
  907. return true;
  908. #else
  909. std::ostringstream e;
  910. e << args[0] << " not available during bootstrap";
  911. status.SetError(e.str().c_str());
  912. return false;
  913. #endif
  914. }
  915. }