cmSystemTools.cxx 25 KB

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