cmSystemTools.cxx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  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 <ctype.h>
  15. #include <errno.h>
  16. #include <time.h>
  17. #include <cmsys/RegularExpression.hxx>
  18. #include <cmsys/Directory.hxx>
  19. #include <cmsys/Process.h>
  20. // support for realpath call
  21. #ifndef _WIN32
  22. #include <limits.h>
  23. #include <stdlib.h>
  24. #include <sys/param.h>
  25. #include <sys/wait.h>
  26. #endif
  27. #if defined(_WIN32) && (defined(_MSC_VER) || defined(__BORLANDC__))
  28. #include <string.h>
  29. #include <windows.h>
  30. #include <direct.h>
  31. #define _unlink unlink
  32. #else
  33. #include <sys/types.h>
  34. #include <fcntl.h>
  35. #include <unistd.h>
  36. #endif
  37. bool cmSystemTools::s_RunCommandHideConsole = false;
  38. bool cmSystemTools::s_DisableRunCommandOutput = false;
  39. bool cmSystemTools::s_ErrorOccured = false;
  40. bool cmSystemTools::s_FatalErrorOccured = false;
  41. bool cmSystemTools::s_DisableMessages = false;
  42. bool cmSystemTools::s_ForceUnixPaths = 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. if(cmSystemTools::GetRunCommandHideConsole())
  356. {
  357. cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
  358. }
  359. cmsysProcess_SetTimeout(cp, timeout);
  360. cmsysProcess_Execute(cp);
  361. std::vector<char> tempOutput;
  362. char* data;
  363. int length;
  364. while(cmsysProcess_WaitForData(cp, &data, &length, 0))
  365. {
  366. if ( output )
  367. {
  368. tempOutput.insert(tempOutput.end(), data, data+length);
  369. }
  370. if(verbose)
  371. {
  372. std::cout.write(data, length);
  373. }
  374. }
  375. cmsysProcess_WaitForExit(cp, 0);
  376. if ( output )
  377. {
  378. output->append(&*tempOutput.begin(), tempOutput.size());
  379. }
  380. bool result = true;
  381. if(cmsysProcess_GetState(cp) == cmsysProcess_State_Exited)
  382. {
  383. if ( retVal )
  384. {
  385. *retVal = cmsysProcess_GetExitValue(cp);
  386. }
  387. else
  388. {
  389. if ( cmsysProcess_GetExitValue(cp) != 0 )
  390. {
  391. result = false;
  392. }
  393. }
  394. }
  395. else
  396. {
  397. result = false;
  398. }
  399. cmsysProcess_Delete(cp);
  400. return result;
  401. }
  402. bool cmSystemTools::RunCommand(const char* command,
  403. std::string& output,
  404. const char* dir,
  405. bool verbose,
  406. int timeout)
  407. {
  408. int dummy;
  409. return cmSystemTools::RunCommand(command, output, dummy,
  410. dir, verbose, timeout);
  411. }
  412. #if defined(WIN32) && !defined(__CYGWIN__)
  413. #include "cmWin32ProcessExecution.h"
  414. // use this for shell commands like echo and dir
  415. bool RunCommandViaWin32(const char* command,
  416. const char* dir,
  417. std::string& output,
  418. int& retVal,
  419. bool verbose,
  420. int timeout)
  421. {
  422. #if defined(__BORLANDC__)
  423. return cmWin32ProcessExecution::BorlandRunCommand(command, dir, output,
  424. retVal,
  425. verbose, timeout,
  426. cmSystemTools::GetRunCommandHideConsole());
  427. #else // Visual studio
  428. ::SetLastError(ERROR_SUCCESS);
  429. if ( ! command )
  430. {
  431. cmSystemTools::Error("No command specified");
  432. return false;
  433. }
  434. cmWin32ProcessExecution resProc;
  435. if(cmSystemTools::GetRunCommandHideConsole())
  436. {
  437. resProc.SetHideWindows(true);
  438. }
  439. if ( cmSystemTools::GetWindows9xComspecSubstitute() )
  440. {
  441. resProc.SetConsoleSpawn(cmSystemTools::GetWindows9xComspecSubstitute() );
  442. }
  443. if ( !resProc.StartProcess(command, dir, verbose) )
  444. {
  445. return false;
  446. }
  447. resProc.Wait(timeout);
  448. output = resProc.GetOutput();
  449. retVal = resProc.GetExitValue();
  450. return true;
  451. #endif
  452. }
  453. // use this for shell commands like echo and dir
  454. bool RunCommandViaSystem(const char* command,
  455. const char* dir,
  456. std::string& output,
  457. int& retVal,
  458. bool verbose)
  459. {
  460. std::cout << "@@ " << command << std::endl;
  461. std::string commandInDir;
  462. if(dir)
  463. {
  464. commandInDir = "cd ";
  465. commandInDir += cmSystemTools::ConvertToOutputPath(dir);
  466. commandInDir += " && ";
  467. commandInDir += command;
  468. }
  469. else
  470. {
  471. commandInDir = command;
  472. }
  473. command = commandInDir.c_str();
  474. std::string commandToFile = command;
  475. commandToFile += " > ";
  476. std::string tempFile;
  477. tempFile += _tempnam(0, "cmake");
  478. commandToFile += tempFile;
  479. retVal = system(commandToFile.c_str());
  480. std::ifstream fin(tempFile.c_str());
  481. if(!fin)
  482. {
  483. if(verbose)
  484. {
  485. std::string errormsg = "RunCommand produced no output: command: \"";
  486. errormsg += command;
  487. errormsg += "\"";
  488. errormsg += "\nOutput file: ";
  489. errormsg += tempFile;
  490. cmSystemTools::Error(errormsg.c_str());
  491. }
  492. fin.close();
  493. cmSystemTools::RemoveFile(tempFile.c_str());
  494. return false;
  495. }
  496. bool multiLine = false;
  497. std::string line;
  498. while(cmSystemTools::GetLineFromStream(fin, line))
  499. {
  500. output += line;
  501. if(multiLine)
  502. {
  503. output += "\n";
  504. }
  505. multiLine = true;
  506. }
  507. fin.close();
  508. cmSystemTools::RemoveFile(tempFile.c_str());
  509. return true;
  510. }
  511. #else // We have popen
  512. bool RunCommandViaPopen(const char* command,
  513. const char* dir,
  514. std::string& output,
  515. int& retVal,
  516. bool verbose,
  517. int /*timeout*/)
  518. {
  519. // if only popen worked on windows.....
  520. std::string commandInDir;
  521. if(dir)
  522. {
  523. commandInDir = "cd \"";
  524. commandInDir += dir;
  525. commandInDir += "\" && ";
  526. commandInDir += command;
  527. }
  528. else
  529. {
  530. commandInDir = command;
  531. }
  532. commandInDir += " 2>&1";
  533. command = commandInDir.c_str();
  534. const int BUFFER_SIZE = 4096;
  535. char buffer[BUFFER_SIZE];
  536. if(verbose)
  537. {
  538. std::cout << "running " << command << std::endl;
  539. }
  540. fflush(stdout);
  541. fflush(stderr);
  542. FILE* cpipe = popen(command, "r");
  543. if(!cpipe)
  544. {
  545. return false;
  546. }
  547. fgets(buffer, BUFFER_SIZE, cpipe);
  548. while(!feof(cpipe))
  549. {
  550. if(verbose)
  551. {
  552. std::cout << buffer << std::flush;
  553. }
  554. output += buffer;
  555. fgets(buffer, BUFFER_SIZE, cpipe);
  556. }
  557. retVal = pclose(cpipe);
  558. if (WIFEXITED(retVal))
  559. {
  560. retVal = WEXITSTATUS(retVal);
  561. return true;
  562. }
  563. if (WIFSIGNALED(retVal))
  564. {
  565. retVal = WTERMSIG(retVal);
  566. cmOStringStream error;
  567. error << "\nProcess terminated due to ";
  568. switch (retVal)
  569. {
  570. #ifdef SIGKILL
  571. case SIGKILL:
  572. error << "SIGKILL";
  573. break;
  574. #endif
  575. #ifdef SIGFPE
  576. case SIGFPE:
  577. error << "SIGFPE";
  578. break;
  579. #endif
  580. #ifdef SIGBUS
  581. case SIGBUS:
  582. error << "SIGBUS";
  583. break;
  584. #endif
  585. #ifdef SIGSEGV
  586. case SIGSEGV:
  587. error << "SIGSEGV";
  588. break;
  589. #endif
  590. default:
  591. error << "signal " << retVal;
  592. break;
  593. }
  594. output += error.str();
  595. }
  596. return false;
  597. }
  598. #endif // endif WIN32 not CYGWIN
  599. // run a command unix uses popen (easy)
  600. // windows uses system and ShortPath
  601. bool cmSystemTools::RunCommand(const char* command,
  602. std::string& output,
  603. int &retVal,
  604. const char* dir,
  605. bool verbose,
  606. int timeout)
  607. {
  608. if(s_DisableRunCommandOutput)
  609. {
  610. verbose = false;
  611. }
  612. #if defined(WIN32) && !defined(__CYGWIN__)
  613. // if the command does not start with a quote, then
  614. // try to find the program, and if the program can not be
  615. // found use system to run the command as it must be a built in
  616. // shell command like echo or dir
  617. int count = 0;
  618. if(command[0] == '\"')
  619. {
  620. // count the number of quotes
  621. for(const char* s = command; *s != 0; ++s)
  622. {
  623. if(*s == '\"')
  624. {
  625. count++;
  626. if(count > 2)
  627. {
  628. break;
  629. }
  630. }
  631. }
  632. // if there are more than two double quotes use
  633. // GetShortPathName, the cmd.exe program in windows which
  634. // is used by system fails to execute if there are more than
  635. // one set of quotes in the arguments
  636. if(count > 2)
  637. {
  638. cmsys::RegularExpression quoted("^\"([^\"]*)\"[ \t](.*)");
  639. if(quoted.find(command))
  640. {
  641. std::string shortCmd;
  642. std::string cmd = quoted.match(1);
  643. std::string args = quoted.match(2);
  644. if(! cmSystemTools::FileExists(cmd.c_str()) )
  645. {
  646. shortCmd = cmd;
  647. }
  648. else if(!cmSystemTools::GetShortPath(cmd.c_str(), shortCmd))
  649. {
  650. cmSystemTools::Error("GetShortPath failed for " , cmd.c_str());
  651. return false;
  652. }
  653. shortCmd += " ";
  654. shortCmd += args;
  655. //return RunCommandViaSystem(shortCmd.c_str(), dir,
  656. // output, retVal, verbose);
  657. //return WindowsRunCommand(shortCmd.c_str(), dir,
  658. //output, retVal, verbose);
  659. return RunCommandViaWin32(shortCmd.c_str(), dir,
  660. output, retVal, verbose, timeout);
  661. }
  662. else
  663. {
  664. cmSystemTools::Error("Could not parse command line with quotes ",
  665. command);
  666. }
  667. }
  668. }
  669. // if there is only one set of quotes or no quotes then just run the command
  670. //return RunCommandViaSystem(command, dir, output, retVal, verbose);
  671. //return WindowsRunCommand(command, dir, output, retVal, verbose);
  672. return ::RunCommandViaWin32(command, dir, output, retVal, verbose, timeout);
  673. #else
  674. return ::RunCommandViaPopen(command, dir, output, retVal, verbose, timeout);
  675. #endif
  676. }
  677. bool cmSystemTools::DoesFileExistWithExtensions(
  678. const char* name,
  679. const std::vector<std::string>& headerExts)
  680. {
  681. std::string hname;
  682. for( std::vector<std::string>::const_iterator ext = headerExts.begin();
  683. ext != headerExts.end(); ++ext )
  684. {
  685. hname = name;
  686. hname += ".";
  687. hname += *ext;
  688. if(cmSystemTools::FileExists(hname.c_str()))
  689. {
  690. return true;
  691. }
  692. }
  693. return false;
  694. }
  695. bool cmSystemTools::cmCopyFile(const char* source, const char* destination)
  696. {
  697. return Superclass::CopyFileAlways(source, destination);
  698. }
  699. void cmSystemTools::Glob(const char *directory, const char *regexp,
  700. std::vector<std::string>& files)
  701. {
  702. cmsys::Directory d;
  703. cmsys::RegularExpression reg(regexp);
  704. if (d.Load(directory))
  705. {
  706. size_t numf;
  707. unsigned int i;
  708. numf = d.GetNumberOfFiles();
  709. for (i = 0; i < numf; i++)
  710. {
  711. std::string fname = d.GetFile(i);
  712. if (reg.find(fname))
  713. {
  714. files.push_back(fname);
  715. }
  716. }
  717. }
  718. }
  719. void cmSystemTools::GlobDirs(const char *fullPath,
  720. std::vector<std::string>& files)
  721. {
  722. std::string path = fullPath;
  723. std::string::size_type pos = path.find("/*");
  724. if(pos == std::string::npos)
  725. {
  726. files.push_back(fullPath);
  727. return;
  728. }
  729. std::string startPath = path.substr(0, pos);
  730. std::string finishPath = path.substr(pos+2);
  731. cmsys::Directory d;
  732. if (d.Load(startPath.c_str()))
  733. {
  734. for (unsigned int i = 0; i < d.GetNumberOfFiles(); ++i)
  735. {
  736. if((std::string(d.GetFile(i)) != ".")
  737. && (std::string(d.GetFile(i)) != ".."))
  738. {
  739. std::string fname = startPath;
  740. fname +="/";
  741. fname += d.GetFile(i);
  742. if(cmSystemTools::FileIsDirectory(fname.c_str()))
  743. {
  744. fname += finishPath;
  745. cmSystemTools::GlobDirs(fname.c_str(), files);
  746. }
  747. }
  748. }
  749. }
  750. }
  751. void cmSystemTools::ExpandList(std::vector<std::string> const& arguments,
  752. std::vector<std::string>& newargs)
  753. {
  754. std::vector<std::string>::const_iterator i;
  755. for(i = arguments.begin();i != arguments.end(); ++i)
  756. {
  757. cmSystemTools::ExpandListArgument(*i, newargs);
  758. }
  759. }
  760. void cmSystemTools::ExpandListArgument(const std::string& arg,
  761. std::vector<std::string>& newargs)
  762. {
  763. std::string newarg;
  764. // If argument is empty, it is an empty list.
  765. if(arg.length() == 0)
  766. {
  767. return;
  768. }
  769. // if there are no ; in the name then just copy the current string
  770. if(arg.find(';') == std::string::npos)
  771. {
  772. newargs.push_back(arg);
  773. return;
  774. }
  775. // Break the string at non-escaped semicolons not nested in [].
  776. int squareNesting = 0;
  777. for(const char* c = arg.c_str(); *c; ++c)
  778. {
  779. switch(*c)
  780. {
  781. case '\\':
  782. {
  783. // We only want to allow escaping of semicolons. Other
  784. // escapes should not be processed here.
  785. ++c;
  786. if(*c == ';')
  787. {
  788. newarg += ';';
  789. }
  790. else
  791. {
  792. newarg += '\\';
  793. if(*c)
  794. {
  795. newarg += *c;
  796. }
  797. }
  798. } break;
  799. case '[':
  800. {
  801. ++squareNesting;
  802. newarg += '[';
  803. } break;
  804. case ']':
  805. {
  806. --squareNesting;
  807. newarg += ']';
  808. } break;
  809. case ';':
  810. {
  811. // Break the string here if we are not nested inside square
  812. // brackets.
  813. if(squareNesting == 0)
  814. {
  815. if(newarg.length())
  816. {
  817. // Add an argument if the string is not empty.
  818. newargs.push_back(newarg);
  819. newarg = "";
  820. }
  821. }
  822. else
  823. {
  824. newarg += ';';
  825. }
  826. } break;
  827. default:
  828. {
  829. // Just append this character.
  830. newarg += *c;
  831. } break;
  832. }
  833. }
  834. if(newarg.length())
  835. {
  836. // Add the last argument if the string is not empty.
  837. newargs.push_back(newarg);
  838. }
  839. }
  840. bool cmSystemTools::SimpleGlob(const std::string& glob,
  841. std::vector<std::string>& files,
  842. int type /* = 0 */)
  843. {
  844. files.clear();
  845. if ( glob[glob.size()-1] != '*' )
  846. {
  847. return false;
  848. }
  849. std::string path = cmSystemTools::GetFilenamePath(glob);
  850. std::string ppath = cmSystemTools::GetFilenameName(glob);
  851. ppath = ppath.substr(0, ppath.size()-1);
  852. if ( path.size() == 0 )
  853. {
  854. path = "/";
  855. }
  856. bool res = false;
  857. cmsys::Directory d;
  858. if (d.Load(path.c_str()))
  859. {
  860. for (unsigned int i = 0; i < d.GetNumberOfFiles(); ++i)
  861. {
  862. if((std::string(d.GetFile(i)) != ".")
  863. && (std::string(d.GetFile(i)) != ".."))
  864. {
  865. std::string fname = path;
  866. if ( path[path.size()-1] != '/' )
  867. {
  868. fname +="/";
  869. }
  870. fname += d.GetFile(i);
  871. std::string sfname = d.GetFile(i);
  872. if ( type > 0 && cmSystemTools::FileIsDirectory(fname.c_str()) )
  873. {
  874. continue;
  875. }
  876. if ( type < 0 && !cmSystemTools::FileIsDirectory(fname.c_str()) )
  877. {
  878. continue;
  879. }
  880. if ( sfname.size() >= ppath.size() &&
  881. sfname.substr(0, ppath.size()) ==
  882. ppath )
  883. {
  884. files.push_back(fname);
  885. res = true;
  886. }
  887. }
  888. }
  889. }
  890. return res;
  891. }
  892. cmSystemTools::FileFormat cmSystemTools::GetFileFormat(const char* cext)
  893. {
  894. if ( ! cext || *cext == 0 )
  895. {
  896. return cmSystemTools::NO_FILE_FORMAT;
  897. }
  898. //std::string ext = cmSystemTools::LowerCase(cext);
  899. std::string ext = cext;
  900. if ( ext == "c" || ext == ".c" ) { return cmSystemTools::C_FILE_FORMAT; }
  901. if (
  902. ext == "C" || ext == ".C" ||
  903. ext == "M" || ext == ".M" ||
  904. ext == "c++" || ext == ".c++" ||
  905. ext == "cc" || ext == ".cc" ||
  906. ext == "cpp" || ext == ".cpp" ||
  907. ext == "cxx" || ext == ".cxx" ||
  908. ext == "m" || ext == ".m" ||
  909. ext == "mm" || ext == ".mm"
  910. ) { return cmSystemTools::CXX_FILE_FORMAT; }
  911. if ( ext == "java" || ext == ".java" ) { return cmSystemTools::JAVA_FILE_FORMAT; }
  912. if (
  913. ext == "H" || ext == ".H" ||
  914. ext == "h" || ext == ".h" ||
  915. ext == "h++" || ext == ".h++" ||
  916. ext == "hm" || ext == ".hm" ||
  917. ext == "hpp" || ext == ".hpp" ||
  918. ext == "hxx" || ext == ".hxx" ||
  919. ext == "in" || ext == ".in" ||
  920. ext == "txx" || ext == ".txx"
  921. ) { return cmSystemTools::HEADER_FILE_FORMAT; }
  922. if ( ext == "rc" || ext == ".rc" ) { return cmSystemTools::RESOURCE_FILE_FORMAT; }
  923. if ( ext == "def" || ext == ".def" ) { return cmSystemTools::DEFINITION_FILE_FORMAT; }
  924. if ( ext == "lib" || ext == ".lib" ||
  925. ext == "a" || ext == ".a") { return cmSystemTools::STATIC_LIBRARY_FILE_FORMAT; }
  926. if ( ext == "o" || ext == ".o" ||
  927. ext == "obj" || ext == ".obj") { return cmSystemTools::OBJECT_FILE_FORMAT; }
  928. #ifdef __APPLE__
  929. if ( ext == "dylib" || ext == ".dylib" )
  930. { return cmSystemTools::SHARED_LIBRARY_FILE_FORMAT; }
  931. if ( ext == "so" || ext == ".so" ||
  932. ext == "bundle" || ext == ".bundle" )
  933. { return cmSystemTools::MODULE_FILE_FORMAT; }
  934. #else // __APPLE__
  935. if ( ext == "so" || ext == ".so" ||
  936. ext == "sl" || ext == ".sl" ||
  937. ext == "dll" || ext == ".dll" )
  938. { return cmSystemTools::SHARED_LIBRARY_FILE_FORMAT; }
  939. #endif // __APPLE__
  940. return cmSystemTools::UNKNOWN_FILE_FORMAT;
  941. }
  942. bool cmSystemTools::Split(const char* s, std::vector<cmStdString>& l)
  943. {
  944. std::vector<std::string> temp;
  945. if(!Superclass::Split(s, temp))
  946. {
  947. return false;
  948. }
  949. for(std::vector<std::string>::const_iterator i = temp.begin();
  950. i != temp.end(); ++i)
  951. {
  952. l.push_back(*i);
  953. }
  954. return true;
  955. }
  956. std::string cmSystemTools::ConvertToOutputPath(const char* path)
  957. {
  958. #if defined(_WIN32) && !defined(__CYGWIN__)
  959. if(s_ForceUnixPaths)
  960. {
  961. return cmSystemTools::ConvertToUnixOutputPath(path);
  962. }
  963. return cmSystemTools::ConvertToWindowsOutputPath(path);
  964. #else
  965. return cmSystemTools::ConvertToUnixOutputPath(path);
  966. #endif
  967. }
  968. bool cmSystemTools::StringEndsWith(const char* str1, const char* str2)
  969. {
  970. if ( !str1 || !str2 || strlen(str1) < strlen(str2) )
  971. {
  972. return 0;
  973. }
  974. return !strncmp(str1 + (strlen(str1)-strlen(str2)), str2, strlen(str2));
  975. }
  976. #if defined(_WIN32) && !defined(__CYGWIN__)
  977. bool cmSystemTools::CreateSymlink(const char*, const char*)
  978. {
  979. // Should we create a copy here?
  980. return false;
  981. }
  982. #else
  983. bool cmSystemTools::CreateSymlink(const char* origName, const char* newName)
  984. {
  985. return (symlink(origName, newName) >= 0);
  986. }
  987. #endif