cmSystemTools.cxx 25 KB

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