cmSystemTools.cxx 26 KB

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