cmStringCommand.cxx 26 KB

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