cmStringCommand.cxx 27 KB

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