cmSystemTools.cxx 26 KB

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