cmSystemTools.cxx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmSystemTools.h"
  14. #include <stdio.h>
  15. #include <ctype.h>
  16. #include <errno.h>
  17. #include <time.h>
  18. #include <cmsys/RegularExpression.hxx>
  19. #include <cmsys/Directory.hxx>
  20. #include <cmsys/Process.h>
  21. // support for realpath call
  22. #ifndef _WIN32
  23. #include <limits.h>
  24. #include <stdlib.h>
  25. #include <sys/param.h>
  26. #include <sys/wait.h>
  27. #endif
  28. #if defined(_WIN32) && (defined(_MSC_VER) || defined(__BORLANDC__))
  29. #include <string.h>
  30. #include <windows.h>
  31. #include <direct.h>
  32. #define _unlink unlink
  33. #else
  34. #include <sys/types.h>
  35. #include <fcntl.h>
  36. #include <unistd.h>
  37. #endif
  38. bool cmSystemTools::s_RunCommandHideConsole = false;
  39. bool cmSystemTools::s_DisableRunCommandOutput = false;
  40. bool cmSystemTools::s_ErrorOccured = false;
  41. bool cmSystemTools::s_FatalErrorOccured = false;
  42. bool cmSystemTools::s_DisableMessages = false;
  43. std::string cmSystemTools::s_Windows9xComspecSubstitute = "command.com";
  44. void cmSystemTools::SetWindows9xComspecSubstitute(const char* str)
  45. {
  46. if ( str )
  47. {
  48. cmSystemTools::s_Windows9xComspecSubstitute = str;
  49. }
  50. }
  51. const char* cmSystemTools::GetWindows9xComspecSubstitute()
  52. {
  53. return cmSystemTools::s_Windows9xComspecSubstitute.c_str();
  54. }
  55. void (*cmSystemTools::s_ErrorCallback)(const char*, const char*, bool&, void*);
  56. void* cmSystemTools::s_ErrorCallbackClientData = 0;
  57. // replace replace with with as many times as it shows up in source.
  58. // write the result into source.
  59. #if defined(_WIN32) && !defined(__CYGWIN__)
  60. void cmSystemTools::ExpandRegistryValues(std::string& source)
  61. {
  62. // Regular expression to match anything inside [...] that begins in HKEY.
  63. // Note that there is a special rule for regular expressions to match a
  64. // close square-bracket inside a list delimited by square brackets.
  65. // The "[^]]" part of this expression will match any character except
  66. // a close square-bracket. The ']' character must be the first in the
  67. // list of characters inside the [^...] block of the expression.
  68. cmsys::RegularExpression regEntry("\\[(HKEY[^]]*)\\]");
  69. // check for black line or comment
  70. while (regEntry.find(source))
  71. {
  72. // the arguments are the second match
  73. std::string key = regEntry.match(1);
  74. std::string val;
  75. if (ReadRegistryValue(key.c_str(), val))
  76. {
  77. std::string reg = "[";
  78. reg += key + "]";
  79. cmSystemTools::ReplaceString(source, reg.c_str(), val.c_str());
  80. }
  81. else
  82. {
  83. std::string reg = "[";
  84. reg += key + "]";
  85. cmSystemTools::ReplaceString(source, reg.c_str(), "/registry");
  86. }
  87. }
  88. }
  89. #else
  90. void cmSystemTools::ExpandRegistryValues(std::string&)
  91. {
  92. }
  93. #endif
  94. std::string cmSystemTools::EscapeQuotes(const char* str)
  95. {
  96. std::string result = "";
  97. for(const char* ch = str; *ch != '\0'; ++ch)
  98. {
  99. if(*ch == '"')
  100. {
  101. result += '\\';
  102. }
  103. result += *ch;
  104. }
  105. return result;
  106. }
  107. std::string cmSystemTools::EscapeSpaces(const char* str)
  108. {
  109. #if defined(_WIN32) && !defined(__CYGWIN__)
  110. std::string result;
  111. // if there are spaces
  112. std::string temp = str;
  113. if (temp.find(" ") != std::string::npos &&
  114. temp.find("\"")==std::string::npos)
  115. {
  116. result = "\"";
  117. result += str;
  118. result += "\"";
  119. return result;
  120. }
  121. return str;
  122. #else
  123. std::string result = "";
  124. for(const char* ch = str; *ch != '\0'; ++ch)
  125. {
  126. if(*ch == ' ')
  127. {
  128. result += '\\';
  129. }
  130. result += *ch;
  131. }
  132. return result;
  133. #endif
  134. }
  135. std::string cmSystemTools::RemoveEscapes(const char* s)
  136. {
  137. std::string result = "";
  138. for(const char* ch = s; *ch; ++ch)
  139. {
  140. if(*ch == '\\' && *(ch+1) != ';')
  141. {
  142. ++ch;
  143. switch (*ch)
  144. {
  145. case '\\': result.insert(result.end(), '\\'); break;
  146. case '"': result.insert(result.end(), '"'); break;
  147. case ' ': result.insert(result.end(), ' '); break;
  148. case 't': result.insert(result.end(), '\t'); break;
  149. case 'n': result.insert(result.end(), '\n'); break;
  150. case 'r': result.insert(result.end(), '\r'); break;
  151. case '#': result.insert(result.end(), '#'); break;
  152. case '(': result.insert(result.end(), '('); break;
  153. case ')': result.insert(result.end(), ')'); break;
  154. case '0': result.insert(result.end(), '\0'); break;
  155. case '\0':
  156. {
  157. cmSystemTools::Error("Trailing backslash in argument:\n", s);
  158. return result;
  159. }
  160. default:
  161. {
  162. std::string chStr(1, *ch);
  163. cmSystemTools::Error("Invalid escape sequence \\", chStr.c_str(),
  164. "\nin argument ", s);
  165. }
  166. }
  167. }
  168. else
  169. {
  170. result.insert(result.end(), *ch);
  171. }
  172. }
  173. return result;
  174. }
  175. void cmSystemTools::Error(const char* m1, const char* m2,
  176. const char* m3, const char* m4)
  177. {
  178. std::string message = "CMake Error: ";
  179. if(m1)
  180. {
  181. message += m1;
  182. }
  183. if(m2)
  184. {
  185. message += m2;
  186. }
  187. if(m3)
  188. {
  189. message += m3;
  190. }
  191. if(m4)
  192. {
  193. message += m4;
  194. }
  195. cmSystemTools::s_ErrorOccured = true;
  196. cmSystemTools::Message(message.c_str(),"Error");
  197. }
  198. void cmSystemTools::SetErrorCallback(ErrorCallback f, void* clientData)
  199. {
  200. s_ErrorCallback = f;
  201. s_ErrorCallbackClientData = clientData;
  202. }
  203. void cmSystemTools::Message(const char* m1, const char *title)
  204. {
  205. if(s_DisableMessages)
  206. {
  207. return;
  208. }
  209. if(s_ErrorCallback)
  210. {
  211. (*s_ErrorCallback)(m1, title, s_DisableMessages, s_ErrorCallbackClientData);
  212. return;
  213. }
  214. else
  215. {
  216. std::cerr << m1 << std::endl << std::flush;
  217. }
  218. }
  219. void cmSystemTools::ReportLastSystemError(const char* msg)
  220. {
  221. std::string m = msg;
  222. m += ": System Error: ";
  223. m += Superclass::GetLastSystemError();
  224. cmSystemTools::Error(m.c_str());
  225. }
  226. bool cmSystemTools::IsOn(const char* val)
  227. {
  228. if (!val)
  229. {
  230. return false;
  231. }
  232. std::basic_string<char> v = val;
  233. for(std::basic_string<char>::iterator c = v.begin();
  234. c != v.end(); c++)
  235. {
  236. *c = toupper(*c);
  237. }
  238. return (v == "ON" || v == "1" || v == "YES" || v == "TRUE" || v == "Y");
  239. }
  240. bool cmSystemTools::IsNOTFOUND(const char* val)
  241. {
  242. cmsys::RegularExpression reg("-NOTFOUND$");
  243. if(reg.find(val))
  244. {
  245. return true;
  246. }
  247. return std::string("NOTFOUND") == val;
  248. }
  249. bool cmSystemTools::IsOff(const char* val)
  250. {
  251. if (!val || strlen(val) == 0)
  252. {
  253. return true;
  254. }
  255. std::basic_string<char> v = val;
  256. for(std::basic_string<char>::iterator c = v.begin();
  257. c != v.end(); c++)
  258. {
  259. *c = toupper(*c);
  260. }
  261. return (v == "OFF" || v == "0" || v == "NO" || v == "FALSE" ||
  262. v == "N" || cmSystemTools::IsNOTFOUND(v.c_str()) || v == "IGNORE");
  263. }
  264. std::vector<cmStdString> cmSystemTools::ParseArguments(const char* command)
  265. {
  266. std::vector<cmStdString> args;
  267. std::string arg;
  268. bool win_path = false;
  269. if ( command[0] != '/' && command[1] == ':' && command[2] == '\\' ||
  270. command[0] == '\"' && command[1] != '/' && command[2] == ':' && command[3] == '\\' ||
  271. command[0] == '\\' && command[1] == '\\')
  272. {
  273. win_path = true;
  274. }
  275. // Split the command into an argv array.
  276. for(const char* c = command; *c;)
  277. {
  278. // Skip over whitespace.
  279. while(*c == ' ' || *c == '\t')
  280. {
  281. ++c;
  282. }
  283. arg = "";
  284. if(*c == '"')
  285. {
  286. // Parse a quoted argument.
  287. ++c;
  288. while(*c && *c != '"')
  289. {
  290. arg.append(1, *c);
  291. ++c;
  292. }
  293. if(*c)
  294. {
  295. ++c;
  296. }
  297. args.push_back(arg);
  298. }
  299. else if(*c)
  300. {
  301. // Parse an unquoted argument.
  302. while(*c && *c != ' ' && *c != '\t')
  303. {
  304. if(*c == '\\' && !win_path)
  305. {
  306. ++c;
  307. if(*c)
  308. {
  309. arg.append(1, *c);
  310. ++c;
  311. }
  312. }
  313. else
  314. {
  315. arg.append(1, *c);
  316. ++c;
  317. }
  318. }
  319. args.push_back(arg);
  320. }
  321. }
  322. return args;
  323. }
  324. bool cmSystemTools::RunSingleCommand(
  325. const char* command,
  326. std::string* output,
  327. int *retVal,
  328. const char* dir,
  329. bool verbose,
  330. int timeout)
  331. {
  332. if(s_DisableRunCommandOutput)
  333. {
  334. verbose = false;
  335. }
  336. std::vector<cmStdString> args = cmSystemTools::ParseArguments(command);
  337. if(args.size() < 1)
  338. {
  339. return false;
  340. }
  341. std::vector<const char*> argv;
  342. for(std::vector<cmStdString>::const_iterator a = args.begin();
  343. a != args.end(); ++a)
  344. {
  345. argv.push_back(a->c_str());
  346. }
  347. argv.push_back(0);
  348. if ( output )
  349. {
  350. *output = "";
  351. }
  352. cmsysProcess* cp = cmsysProcess_New();
  353. cmsysProcess_SetCommand(cp, &*argv.begin());
  354. cmsysProcess_SetWorkingDirectory(cp, dir);
  355. cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
  356. cmsysProcess_SetTimeout(cp, timeout);
  357. cmsysProcess_Execute(cp);
  358. char* data;
  359. int length;
  360. while(cmsysProcess_WaitForData(cp, (cmsysProcess_Pipe_STDOUT |
  361. cmsysProcess_Pipe_STDERR),
  362. &data, &length, 0))
  363. {
  364. if ( output )
  365. {
  366. output->append(data, length);
  367. }
  368. if(verbose)
  369. {
  370. std::cout.write(data, length);
  371. }
  372. }
  373. cmsysProcess_WaitForExit(cp, 0);
  374. bool result = true;
  375. if(cmsysProcess_GetState(cp) == cmsysProcess_State_Exited)
  376. {
  377. if ( retVal )
  378. {
  379. *retVal = cmsysProcess_GetExitValue(cp);
  380. }
  381. else
  382. {
  383. if ( cmsysProcess_GetExitValue(cp) != 0 )
  384. {
  385. result = false;
  386. }
  387. }
  388. }
  389. else
  390. {
  391. result = false;
  392. }
  393. cmsysProcess_Delete(cp);
  394. return result;
  395. }
  396. bool cmSystemTools::RunCommand(const char* command,
  397. std::string& output,
  398. const char* dir,
  399. bool verbose,
  400. int timeout)
  401. {
  402. int dummy;
  403. return cmSystemTools::RunCommand(command, output, dummy,
  404. dir, verbose, timeout);
  405. }
  406. #if defined(WIN32) && !defined(__CYGWIN__)
  407. #include "cmWin32ProcessExecution.h"
  408. // use this for shell commands like echo and dir
  409. bool RunCommandViaWin32(const char* command,
  410. const char* dir,
  411. std::string& output,
  412. int& retVal,
  413. bool verbose,
  414. int timeout)
  415. {
  416. #if defined(__BORLANDC__)
  417. return cmWin32ProcessExecution::BorlandRunCommand(command, dir, output,
  418. retVal,
  419. verbose, timeout,
  420. cmSystemTools::GetRunCommandHideConsole());
  421. #else // Visual studio
  422. ::SetLastError(ERROR_SUCCESS);
  423. if ( ! command )
  424. {
  425. cmSystemTools::Error("No command specified");
  426. return false;
  427. }
  428. cmWin32ProcessExecution resProc;
  429. if(cmSystemTools::GetRunCommandHideConsole())
  430. {
  431. resProc.SetHideWindows(true);
  432. }
  433. if ( cmSystemTools::GetWindows9xComspecSubstitute() )
  434. {
  435. resProc.SetConsoleSpawn(cmSystemTools::GetWindows9xComspecSubstitute() );
  436. }
  437. if ( !resProc.StartProcess(command, dir, verbose) )
  438. {
  439. return false;
  440. }
  441. resProc.Wait(timeout);
  442. output = resProc.GetOutput();
  443. retVal = resProc.GetExitValue();
  444. return true;
  445. #endif
  446. }
  447. // use this for shell commands like echo and dir
  448. bool RunCommandViaSystem(const char* command,
  449. const char* dir,
  450. std::string& output,
  451. int& retVal,
  452. bool verbose)
  453. {
  454. std::cout << "@@ " << command << std::endl;
  455. std::string commandInDir;
  456. if(dir)
  457. {
  458. commandInDir = "cd ";
  459. commandInDir += cmSystemTools::ConvertToOutputPath(dir);
  460. commandInDir += " && ";
  461. commandInDir += command;
  462. }
  463. else
  464. {
  465. commandInDir = command;
  466. }
  467. command = commandInDir.c_str();
  468. std::string commandToFile = command;
  469. commandToFile += " > ";
  470. std::string tempFile;
  471. tempFile += _tempnam(0, "cmake");
  472. commandToFile += tempFile;
  473. retVal = system(commandToFile.c_str());
  474. std::ifstream fin(tempFile.c_str());
  475. if(!fin)
  476. {
  477. if(verbose)
  478. {
  479. std::string errormsg = "RunCommand produced no output: command: \"";
  480. errormsg += command;
  481. errormsg += "\"";
  482. errormsg += "\nOutput file: ";
  483. errormsg += tempFile;
  484. cmSystemTools::Error(errormsg.c_str());
  485. }
  486. fin.close();
  487. cmSystemTools::RemoveFile(tempFile.c_str());
  488. return false;
  489. }
  490. bool multiLine = false;
  491. std::string line;
  492. while(cmSystemTools::GetLineFromStream(fin, line))
  493. {
  494. output += line;
  495. if(multiLine)
  496. {
  497. output += "\n";
  498. }
  499. multiLine = true;
  500. }
  501. fin.close();
  502. cmSystemTools::RemoveFile(tempFile.c_str());
  503. return true;
  504. }
  505. #else // We have popen
  506. bool RunCommandViaPopen(const char* command,
  507. const char* dir,
  508. std::string& output,
  509. int& retVal,
  510. bool verbose,
  511. int /*timeout*/)
  512. {
  513. // if only popen worked on windows.....
  514. std::string commandInDir;
  515. if(dir)
  516. {
  517. commandInDir = "cd \"";
  518. commandInDir += dir;
  519. commandInDir += "\" && ";
  520. commandInDir += command;
  521. }
  522. else
  523. {
  524. commandInDir = command;
  525. }
  526. commandInDir += " 2>&1";
  527. command = commandInDir.c_str();
  528. const int BUFFER_SIZE = 4096;
  529. char buffer[BUFFER_SIZE];
  530. if(verbose)
  531. {
  532. std::cout << "running " << command << std::endl;
  533. }
  534. fflush(stdout);
  535. fflush(stderr);
  536. FILE* cpipe = popen(command, "r");
  537. if(!cpipe)
  538. {
  539. return false;
  540. }
  541. fgets(buffer, BUFFER_SIZE, cpipe);
  542. while(!feof(cpipe))
  543. {
  544. if(verbose)
  545. {
  546. std::cout << buffer << std::flush;
  547. }
  548. output += buffer;
  549. fgets(buffer, BUFFER_SIZE, cpipe);
  550. }
  551. retVal = pclose(cpipe);
  552. if (WIFEXITED(retVal))
  553. {
  554. retVal = WEXITSTATUS(retVal);
  555. return true;
  556. }
  557. if (WIFSIGNALED(retVal))
  558. {
  559. retVal = WTERMSIG(retVal);
  560. cmOStringStream error;
  561. error << "\nProcess terminated due to ";
  562. switch (retVal)
  563. {
  564. #ifdef SIGKILL
  565. case SIGKILL:
  566. error << "SIGKILL";
  567. break;
  568. #endif
  569. #ifdef SIGFPE
  570. case SIGFPE:
  571. error << "SIGFPE";
  572. break;
  573. #endif
  574. #ifdef SIGBUS
  575. case SIGBUS:
  576. error << "SIGBUS";
  577. break;
  578. #endif
  579. #ifdef SIGSEGV
  580. case SIGSEGV:
  581. error << "SIGSEGV";
  582. break;
  583. #endif
  584. default:
  585. error << "signal " << retVal;
  586. break;
  587. }
  588. output += error.str();
  589. }
  590. return false;
  591. }
  592. #endif // endif WIN32 not CYGWIN
  593. // run a command unix uses popen (easy)
  594. // windows uses system and ShortPath
  595. bool cmSystemTools::RunCommand(const char* command,
  596. std::string& output,
  597. int &retVal,
  598. const char* dir,
  599. bool verbose,
  600. int timeout)
  601. {
  602. if(s_DisableRunCommandOutput)
  603. {
  604. verbose = false;
  605. }
  606. #if defined(WIN32) && !defined(__CYGWIN__)
  607. // if the command does not start with a quote, then
  608. // try to find the program, and if the program can not be
  609. // found use system to run the command as it must be a built in
  610. // shell command like echo or dir
  611. int count = 0;
  612. if(command[0] == '\"')
  613. {
  614. // count the number of quotes
  615. for(const char* s = command; *s != 0; ++s)
  616. {
  617. if(*s == '\"')
  618. {
  619. count++;
  620. if(count > 2)
  621. {
  622. break;
  623. }
  624. }
  625. }
  626. // if there are more than two double quotes use
  627. // GetShortPathName, the cmd.exe program in windows which
  628. // is used by system fails to execute if there are more than
  629. // one set of quotes in the arguments
  630. if(count > 2)
  631. {
  632. cmsys::RegularExpression quoted("^\"([^\"]*)\"[ \t](.*)");
  633. if(quoted.find(command))
  634. {
  635. std::string shortCmd;
  636. std::string cmd = quoted.match(1);
  637. std::string args = quoted.match(2);
  638. if(! cmSystemTools::FileExists(cmd.c_str()) )
  639. {
  640. shortCmd = cmd;
  641. }
  642. else if(!cmSystemTools::GetShortPath(cmd.c_str(), shortCmd))
  643. {
  644. cmSystemTools::Error("GetShortPath failed for " , cmd.c_str());
  645. return false;
  646. }
  647. shortCmd += " ";
  648. shortCmd += args;
  649. //return RunCommandViaSystem(shortCmd.c_str(), dir,
  650. // output, retVal, verbose);
  651. //return WindowsRunCommand(shortCmd.c_str(), dir,
  652. //output, retVal, verbose);
  653. return RunCommandViaWin32(shortCmd.c_str(), dir,
  654. output, retVal, verbose, timeout);
  655. }
  656. else
  657. {
  658. cmSystemTools::Error("Could not parse command line with quotes ",
  659. command);
  660. }
  661. }
  662. }
  663. // if there is only one set of quotes or no quotes then just run the command
  664. //return RunCommandViaSystem(command, dir, output, retVal, verbose);
  665. //return WindowsRunCommand(command, dir, output, retVal, verbose);
  666. return ::RunCommandViaWin32(command, dir, output, retVal, verbose, timeout);
  667. #else
  668. return ::RunCommandViaPopen(command, dir, output, retVal, verbose, timeout);
  669. #endif
  670. }
  671. bool cmSystemTools::DoesFileExistWithExtensions(
  672. const char* name,
  673. const std::vector<std::string>& headerExts)
  674. {
  675. std::string hname;
  676. for( std::vector<std::string>::const_iterator ext = headerExts.begin();
  677. ext != headerExts.end(); ++ext )
  678. {
  679. hname = name;
  680. hname += ".";
  681. hname += *ext;
  682. if(cmSystemTools::FileExists(hname.c_str()))
  683. {
  684. return true;
  685. }
  686. }
  687. return false;
  688. }
  689. bool cmSystemTools::cmCopyFile(const char* source, const char* destination)
  690. {
  691. return Superclass::CopyFileAlways(source, destination);
  692. }
  693. void cmSystemTools::Glob(const char *directory, const char *regexp,
  694. std::vector<std::string>& files)
  695. {
  696. cmsys::Directory d;
  697. cmsys::RegularExpression reg(regexp);
  698. if (d.Load(directory))
  699. {
  700. size_t numf;
  701. unsigned int i;
  702. numf = d.GetNumberOfFiles();
  703. for (i = 0; i < numf; i++)
  704. {
  705. std::string fname = d.GetFile(i);
  706. if (reg.find(fname))
  707. {
  708. files.push_back(fname);
  709. }
  710. }
  711. }
  712. }
  713. void cmSystemTools::GlobDirs(const char *fullPath,
  714. std::vector<std::string>& files)
  715. {
  716. std::string path = fullPath;
  717. std::string::size_type pos = path.find("/*");
  718. if(pos == std::string::npos)
  719. {
  720. files.push_back(fullPath);
  721. return;
  722. }
  723. std::string startPath = path.substr(0, pos);
  724. std::string finishPath = path.substr(pos+2);
  725. cmsys::Directory d;
  726. if (d.Load(startPath.c_str()))
  727. {
  728. for (unsigned int i = 0; i < d.GetNumberOfFiles(); ++i)
  729. {
  730. if((std::string(d.GetFile(i)) != ".")
  731. && (std::string(d.GetFile(i)) != ".."))
  732. {
  733. std::string fname = startPath;
  734. fname +="/";
  735. fname += d.GetFile(i);
  736. if(cmSystemTools::FileIsDirectory(fname.c_str()))
  737. {
  738. fname += finishPath;
  739. cmSystemTools::GlobDirs(fname.c_str(), files);
  740. }
  741. }
  742. }
  743. }
  744. }
  745. void cmSystemTools::ExpandList(std::vector<std::string> const& arguments,
  746. std::vector<std::string>& newargs)
  747. {
  748. std::vector<std::string>::const_iterator i;
  749. for(i = arguments.begin();i != arguments.end(); ++i)
  750. {
  751. cmSystemTools::ExpandListArgument(*i, newargs);
  752. }
  753. }
  754. void cmSystemTools::ExpandListArgument(const std::string& arg,
  755. std::vector<std::string>& newargs)
  756. {
  757. std::string newarg;
  758. // If argument is empty, it is an empty list.
  759. if(arg.length() == 0)
  760. {
  761. return;
  762. }
  763. // if there are no ; in the name then just copy the current string
  764. if(arg.find(';') == std::string::npos)
  765. {
  766. newargs.push_back(arg);
  767. }
  768. else
  769. {
  770. std::string::size_type start = 0;
  771. std::string::size_type endpos = 0;
  772. const std::string::size_type size = arg.size();
  773. // break up ; separated sections of the string into separate strings
  774. while(endpos != size)
  775. {
  776. endpos = arg.find(';', start);
  777. if(endpos == std::string::npos)
  778. {
  779. endpos = arg.size();
  780. }
  781. else
  782. {
  783. // skip right over escaped ; ( \; )
  784. while((endpos != std::string::npos)
  785. && (endpos > 0)
  786. && ((arg)[endpos-1] == '\\') )
  787. {
  788. endpos = arg.find(';', endpos+1);
  789. }
  790. if(endpos == std::string::npos)
  791. {
  792. endpos = arg.size();
  793. }
  794. }
  795. std::string::size_type len = endpos - start;
  796. if (len > 0)
  797. {
  798. // check for a closing ] after the start position
  799. if(arg.find('[', start) == std::string::npos)
  800. {
  801. // if there is no [ in the string then keep it
  802. newarg = arg.substr(start, len);
  803. }
  804. else
  805. {
  806. int opencount = 0;
  807. int closecount = 0;
  808. for(std::string::size_type j = start; j < endpos; ++j)
  809. {
  810. if(arg.at(j) == '[')
  811. {
  812. ++opencount;
  813. }
  814. else if (arg.at(j) == ']')
  815. {
  816. ++closecount;
  817. }
  818. }
  819. if(opencount != closecount)
  820. {
  821. // skip this one
  822. endpos = arg.find(';', endpos+1);
  823. if(endpos == std::string::npos)
  824. {
  825. endpos = arg.size();
  826. }
  827. len = endpos - start;
  828. }
  829. newarg = arg.substr(start, len);
  830. }
  831. std::string::size_type pos = newarg.find("\\;");
  832. if(pos != std::string::npos)
  833. {
  834. newarg.erase(pos, 1);
  835. }
  836. newargs.push_back(newarg);
  837. }
  838. start = endpos+1;
  839. }
  840. }
  841. }
  842. bool cmSystemTools::SimpleGlob(const std::string& glob,
  843. std::vector<std::string>& files,
  844. int type /* = 0 */)
  845. {
  846. files.clear();
  847. if ( glob[glob.size()-1] != '*' )
  848. {
  849. return false;
  850. }
  851. std::string path = cmSystemTools::GetFilenamePath(glob);
  852. std::string ppath = cmSystemTools::GetFilenameName(glob);
  853. ppath = ppath.substr(0, ppath.size()-1);
  854. if ( path.size() == 0 )
  855. {
  856. path = "/";
  857. }
  858. bool res = false;
  859. cmsys::Directory d;
  860. if (d.Load(path.c_str()))
  861. {
  862. for (unsigned int i = 0; i < d.GetNumberOfFiles(); ++i)
  863. {
  864. if((std::string(d.GetFile(i)) != ".")
  865. && (std::string(d.GetFile(i)) != ".."))
  866. {
  867. std::string fname = path;
  868. if ( path[path.size()-1] != '/' )
  869. {
  870. fname +="/";
  871. }
  872. fname += d.GetFile(i);
  873. std::string sfname = d.GetFile(i);
  874. if ( type > 0 && cmSystemTools::FileIsDirectory(fname.c_str()) )
  875. {
  876. continue;
  877. }
  878. if ( type < 0 && !cmSystemTools::FileIsDirectory(fname.c_str()) )
  879. {
  880. continue;
  881. }
  882. if ( sfname.size() >= ppath.size() &&
  883. sfname.substr(0, ppath.size()) ==
  884. ppath )
  885. {
  886. files.push_back(fname);
  887. res = true;
  888. }
  889. }
  890. }
  891. }
  892. return res;
  893. }
  894. cmSystemTools::FileFormat cmSystemTools::GetFileFormat(const char* cext)
  895. {
  896. if ( ! cext || *cext == 0 )
  897. {
  898. return cmSystemTools::NO_FILE_FORMAT;
  899. }
  900. //std::string ext = cmSystemTools::LowerCase(cext);
  901. std::string ext = cext;
  902. if ( ext == "c" || ext == ".c" ) { return cmSystemTools::C_FILE_FORMAT; }
  903. if (
  904. ext == "C" || ext == ".C" ||
  905. ext == "M" || ext == ".M" ||
  906. ext == "c++" || ext == ".c++" ||
  907. ext == "cc" || ext == ".cc" ||
  908. ext == "cpp" || ext == ".cpp" ||
  909. ext == "cxx" || ext == ".cxx" ||
  910. ext == "m" || ext == ".m" ||
  911. ext == "mm" || ext == ".mm"
  912. ) { return cmSystemTools::CXX_FILE_FORMAT; }
  913. if ( ext == "java" || ext == ".java" ) { return cmSystemTools::JAVA_FILE_FORMAT; }
  914. if (
  915. ext == "H" || ext == ".H" ||
  916. ext == "h" || ext == ".h" ||
  917. ext == "h++" || ext == ".h++" ||
  918. ext == "hm" || ext == ".hm" ||
  919. ext == "hpp" || ext == ".hpp" ||
  920. ext == "hxx" || ext == ".hxx" ||
  921. ext == "in" || ext == ".in" ||
  922. ext == "txx" || ext == ".txx"
  923. ) { return cmSystemTools::HEADER_FILE_FORMAT; }
  924. if ( ext == "rc" || ext == ".rc" ) { return cmSystemTools::RESOURCE_FILE_FORMAT; }
  925. if ( ext == "def" || ext == ".def" ) { return cmSystemTools::DEFINITION_FILE_FORMAT; }
  926. if ( ext == "lib" || ext == ".lib" ||
  927. ext == "a" || ext == ".a") { return cmSystemTools::STATIC_LIBRARY_FILE_FORMAT; }
  928. if ( ext == "o" || ext == ".o" ||
  929. ext == "obj" || ext == ".obj") { return cmSystemTools::OBJECT_FILE_FORMAT; }
  930. #ifdef __APPLE__
  931. if ( ext == "dylib" || ext == ".dylib" )
  932. { return cmSystemTools::SHARED_LIBRARY_FILE_FORMAT; }
  933. if ( ext == "so" || ext == ".so" ||
  934. ext == "bundle" || ext == ".bundle" )
  935. { return cmSystemTools::MODULE_FILE_FORMAT; }
  936. #else // __APPLE__
  937. if ( ext == "so" || ext == ".so" ||
  938. ext == "sl" || ext == ".sl" ||
  939. ext == "dll" || ext == ".dll" )
  940. { return cmSystemTools::SHARED_LIBRARY_FILE_FORMAT; }
  941. #endif // __APPLE__
  942. return cmSystemTools::UNKNOWN_FILE_FORMAT;
  943. }
  944. bool cmSystemTools::Split(const char* s, std::vector<cmStdString>& l)
  945. {
  946. std::vector<std::string> temp;
  947. if(!Superclass::Split(s, temp))
  948. {
  949. return false;
  950. }
  951. for(std::vector<std::string>::const_iterator i = temp.begin();
  952. i != temp.end(); ++i)
  953. {
  954. l.push_back(*i);
  955. }
  956. return true;
  957. }