cmStringCommand.cxx 26 KB

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