cmSystemTools.cxx 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  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. std::string cmSystemTools::s_Windows9xComspecSubstitute = "command.com";
  43. void cmSystemTools::SetWindows9xComspecSubstitute(const char* str)
  44. {
  45. if ( str )
  46. {
  47. cmSystemTools::s_Windows9xComspecSubstitute = str;
  48. }
  49. }
  50. const char* cmSystemTools::GetWindows9xComspecSubstitute()
  51. {
  52. return cmSystemTools::s_Windows9xComspecSubstitute.c_str();
  53. }
  54. void (*cmSystemTools::s_ErrorCallback)(const char*, const char*, bool&, void*);
  55. void* cmSystemTools::s_ErrorCallbackClientData = 0;
  56. // replace replace with with as many times as it shows up in source.
  57. // write the result into source.
  58. #if defined(_WIN32) && !defined(__CYGWIN__)
  59. void cmSystemTools::ExpandRegistryValues(std::string& source)
  60. {
  61. // Regular expression to match anything inside [...] that begins in HKEY.
  62. // Note that there is a special rule for regular expressions to match a
  63. // close square-bracket inside a list delimited by square brackets.
  64. // The "[^]]" part of this expression will match any character except
  65. // a close square-bracket. The ']' character must be the first in the
  66. // list of characters inside the [^...] block of the expression.
  67. cmsys::RegularExpression regEntry("\\[(HKEY[^]]*)\\]");
  68. // check for black line or comment
  69. while (regEntry.find(source))
  70. {
  71. // the arguments are the second match
  72. std::string key = regEntry.match(1);
  73. std::string val;
  74. if (ReadRegistryValue(key.c_str(), val))
  75. {
  76. std::string reg = "[";
  77. reg += key + "]";
  78. cmSystemTools::ReplaceString(source, reg.c_str(), val.c_str());
  79. }
  80. else
  81. {
  82. std::string reg = "[";
  83. reg += key + "]";
  84. cmSystemTools::ReplaceString(source, reg.c_str(), "/registry");
  85. }
  86. }
  87. }
  88. #else
  89. void cmSystemTools::ExpandRegistryValues(std::string&)
  90. {
  91. }
  92. #endif
  93. std::string cmSystemTools::EscapeQuotes(const char* str)
  94. {
  95. std::string result = "";
  96. for(const char* ch = str; *ch != '\0'; ++ch)
  97. {
  98. if(*ch == '"')
  99. {
  100. result += '\\';
  101. }
  102. result += *ch;
  103. }
  104. return result;
  105. }
  106. std::string cmSystemTools::EscapeSpaces(const char* str)
  107. {
  108. #if defined(_WIN32) && !defined(__CYGWIN__)
  109. std::string result;
  110. // if there are spaces
  111. std::string temp = str;
  112. if (temp.find(" ") != std::string::npos &&
  113. temp.find("\"")==std::string::npos)
  114. {
  115. result = "\"";
  116. result += str;
  117. result += "\"";
  118. return result;
  119. }
  120. return str;
  121. #else
  122. std::string result = "";
  123. for(const char* ch = str; *ch != '\0'; ++ch)
  124. {
  125. if(*ch == ' ')
  126. {
  127. result += '\\';
  128. }
  129. result += *ch;
  130. }
  131. return result;
  132. #endif
  133. }
  134. std::string cmSystemTools::RemoveEscapes(const char* s)
  135. {
  136. std::string result = "";
  137. for(const char* ch = s; *ch; ++ch)
  138. {
  139. if(*ch == '\\' && *(ch+1) != ';')
  140. {
  141. ++ch;
  142. switch (*ch)
  143. {
  144. case '\\': result.insert(result.end(), '\\'); break;
  145. case '"': result.insert(result.end(), '"'); break;
  146. case ' ': result.insert(result.end(), ' '); break;
  147. case 't': result.insert(result.end(), '\t'); break;
  148. case 'n': result.insert(result.end(), '\n'); break;
  149. case 'r': result.insert(result.end(), '\r'); break;
  150. case '#': result.insert(result.end(), '#'); break;
  151. case '(': result.insert(result.end(), '('); break;
  152. case ')': result.insert(result.end(), ')'); break;
  153. case '0': result.insert(result.end(), '\0'); break;
  154. case '\0':
  155. {
  156. cmSystemTools::Error("Trailing backslash in argument:\n", s);
  157. return result;
  158. }
  159. default:
  160. {
  161. std::string chStr(1, *ch);
  162. cmSystemTools::Error("Invalid escape sequence \\", chStr.c_str(),
  163. "\nin argument ", s);
  164. }
  165. }
  166. }
  167. else
  168. {
  169. result.insert(result.end(), *ch);
  170. }
  171. }
  172. return result;
  173. }
  174. void cmSystemTools::Error(const char* m1, const char* m2,
  175. const char* m3, const char* m4)
  176. {
  177. std::string message = "CMake Error: ";
  178. if(m1)
  179. {
  180. message += m1;
  181. }
  182. if(m2)
  183. {
  184. message += m2;
  185. }
  186. if(m3)
  187. {
  188. message += m3;
  189. }
  190. if(m4)
  191. {
  192. message += m4;
  193. }
  194. cmSystemTools::s_ErrorOccured = true;
  195. cmSystemTools::Message(message.c_str(),"Error");
  196. }
  197. void cmSystemTools::SetErrorCallback(ErrorCallback f, void* clientData)
  198. {
  199. s_ErrorCallback = f;
  200. s_ErrorCallbackClientData = clientData;
  201. }
  202. void cmSystemTools::Message(const char* m1, const char *title)
  203. {
  204. if(s_DisableMessages)
  205. {
  206. return;
  207. }
  208. if(s_ErrorCallback)
  209. {
  210. (*s_ErrorCallback)(m1, title, s_DisableMessages, s_ErrorCallbackClientData);
  211. return;
  212. }
  213. else
  214. {
  215. std::cerr << m1 << std::endl << std::flush;
  216. }
  217. }
  218. void cmSystemTools::ReportLastSystemError(const char* msg)
  219. {
  220. std::string m = msg;
  221. m += ": System Error: ";
  222. m += Superclass::GetLastSystemError();
  223. cmSystemTools::Error(m.c_str());
  224. }
  225. bool cmSystemTools::IsOn(const char* val)
  226. {
  227. if (!val)
  228. {
  229. return false;
  230. }
  231. std::basic_string<char> v = val;
  232. for(std::basic_string<char>::iterator c = v.begin();
  233. c != v.end(); c++)
  234. {
  235. *c = toupper(*c);
  236. }
  237. return (v == "ON" || v == "1" || v == "YES" || v == "TRUE" || v == "Y");
  238. }
  239. bool cmSystemTools::IsNOTFOUND(const char* val)
  240. {
  241. cmsys::RegularExpression reg("-NOTFOUND$");
  242. if(reg.find(val))
  243. {
  244. return true;
  245. }
  246. return std::string("NOTFOUND") == val;
  247. }
  248. bool cmSystemTools::IsOff(const char* val)
  249. {
  250. if (!val || strlen(val) == 0)
  251. {
  252. return true;
  253. }
  254. std::basic_string<char> v = val;
  255. for(std::basic_string<char>::iterator c = v.begin();
  256. c != v.end(); c++)
  257. {
  258. *c = toupper(*c);
  259. }
  260. return (v == "OFF" || v == "0" || v == "NO" || v == "FALSE" ||
  261. v == "N" || cmSystemTools::IsNOTFOUND(v.c_str()) || v == "IGNORE");
  262. }
  263. std::vector<cmStdString> cmSystemTools::ParseArguments(const char* command)
  264. {
  265. std::vector<cmStdString> args;
  266. std::string arg;
  267. bool win_path = false;
  268. if ( command[0] != '/' && command[1] == ':' && command[2] == '\\' ||
  269. command[0] == '\"' && command[1] != '/' && command[2] == ':' && command[3] == '\\' ||
  270. command[0] == '\\' && command[1] == '\\')
  271. {
  272. win_path = true;
  273. }
  274. // Split the command into an argv array.
  275. for(const char* c = command; *c;)
  276. {
  277. // Skip over whitespace.
  278. while(*c == ' ' || *c == '\t')
  279. {
  280. ++c;
  281. }
  282. arg = "";
  283. if(*c == '"')
  284. {
  285. // Parse a quoted argument.
  286. ++c;
  287. while(*c && *c != '"')
  288. {
  289. arg.append(1, *c);
  290. ++c;
  291. }
  292. if(*c)
  293. {
  294. ++c;
  295. }
  296. args.push_back(arg);
  297. }
  298. else if(*c)
  299. {
  300. // Parse an unquoted argument.
  301. while(*c && *c != ' ' && *c != '\t')
  302. {
  303. if(*c == '\\' && !win_path)
  304. {
  305. ++c;
  306. if(*c)
  307. {
  308. arg.append(1, *c);
  309. ++c;
  310. }
  311. }
  312. else
  313. {
  314. arg.append(1, *c);
  315. ++c;
  316. }
  317. }
  318. args.push_back(arg);
  319. }
  320. }
  321. return args;
  322. }
  323. bool cmSystemTools::RunSingleCommand(
  324. const char* command,
  325. std::string* output,
  326. int *retVal,
  327. const char* dir,
  328. bool verbose,
  329. int timeout)
  330. {
  331. if(s_DisableRunCommandOutput)
  332. {
  333. verbose = false;
  334. }
  335. std::vector<cmStdString> args = cmSystemTools::ParseArguments(command);
  336. if(args.size() < 1)
  337. {
  338. return false;
  339. }
  340. std::vector<const char*> argv;
  341. for(std::vector<cmStdString>::const_iterator a = args.begin();
  342. a != args.end(); ++a)
  343. {
  344. argv.push_back(a->c_str());
  345. }
  346. argv.push_back(0);
  347. if ( output )
  348. {
  349. *output = "";
  350. }
  351. cmsysProcess* cp = cmsysProcess_New();
  352. cmsysProcess_SetCommand(cp, &*argv.begin());
  353. cmsysProcess_SetWorkingDirectory(cp, dir);
  354. cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
  355. cmsysProcess_SetTimeout(cp, timeout);
  356. cmsysProcess_Execute(cp);
  357. char* data;
  358. int length;
  359. while(cmsysProcess_WaitForData(cp, (cmsysProcess_Pipe_STDOUT |
  360. cmsysProcess_Pipe_STDERR),
  361. &data, &length, 0))
  362. {
  363. if ( output )
  364. {
  365. output->append(data, length);
  366. }
  367. if(verbose)
  368. {
  369. std::cout.write(data, length);
  370. }
  371. }
  372. cmsysProcess_WaitForExit(cp, 0);
  373. bool result = true;
  374. if(cmsysProcess_GetState(cp) == cmsysProcess_State_Exited)
  375. {
  376. if ( retVal )
  377. {
  378. *retVal = cmsysProcess_GetExitValue(cp);
  379. }
  380. else
  381. {
  382. if ( cmsysProcess_GetExitValue(cp) != 0 )
  383. {
  384. result = false;
  385. }
  386. }
  387. }
  388. else
  389. {
  390. result = false;
  391. }
  392. cmsysProcess_Delete(cp);
  393. return result;
  394. }
  395. bool cmSystemTools::RunCommand(const char* command,
  396. std::string& output,
  397. const char* dir,
  398. bool verbose,
  399. int timeout)
  400. {
  401. int dummy;
  402. return cmSystemTools::RunCommand(command, output, dummy,
  403. dir, verbose, timeout);
  404. }
  405. #if defined(WIN32) && !defined(__CYGWIN__)
  406. #include "cmWin32ProcessExecution.h"
  407. // use this for shell commands like echo and dir
  408. bool RunCommandViaWin32(const char* command,
  409. const char* dir,
  410. std::string& output,
  411. int& retVal,
  412. bool verbose,
  413. int timeout)
  414. {
  415. #if defined(__BORLANDC__)
  416. return cmWin32ProcessExecution::BorlandRunCommand(command, dir, output,
  417. retVal,
  418. verbose, timeout,
  419. cmSystemTools::GetRunCommandHideConsole());
  420. #else // Visual studio
  421. ::SetLastError(ERROR_SUCCESS);
  422. if ( ! command )
  423. {
  424. cmSystemTools::Error("No command specified");
  425. return false;
  426. }
  427. cmWin32ProcessExecution resProc;
  428. if(cmSystemTools::GetRunCommandHideConsole())
  429. {
  430. resProc.SetHideWindows(true);
  431. }
  432. if ( cmSystemTools::GetWindows9xComspecSubstitute() )
  433. {
  434. resProc.SetConsoleSpawn(cmSystemTools::GetWindows9xComspecSubstitute() );
  435. }
  436. if ( !resProc.StartProcess(command, dir, verbose) )
  437. {
  438. return false;
  439. }
  440. resProc.Wait(timeout);
  441. output = resProc.GetOutput();
  442. retVal = resProc.GetExitValue();
  443. return true;
  444. #endif
  445. }
  446. // use this for shell commands like echo and dir
  447. bool RunCommandViaSystem(const char* command,
  448. const char* dir,
  449. std::string& output,
  450. int& retVal,
  451. bool verbose)
  452. {
  453. std::cout << "@@ " << command << std::endl;
  454. std::string commandInDir;
  455. if(dir)
  456. {
  457. commandInDir = "cd ";
  458. commandInDir += cmSystemTools::ConvertToOutputPath(dir);
  459. commandInDir += " && ";
  460. commandInDir += command;
  461. }
  462. else
  463. {
  464. commandInDir = command;
  465. }
  466. command = commandInDir.c_str();
  467. std::string commandToFile = command;
  468. commandToFile += " > ";
  469. std::string tempFile;
  470. tempFile += _tempnam(0, "cmake");
  471. commandToFile += tempFile;
  472. retVal = system(commandToFile.c_str());
  473. std::ifstream fin(tempFile.c_str());
  474. if(!fin)
  475. {
  476. if(verbose)
  477. {
  478. std::string errormsg = "RunCommand produced no output: command: \"";
  479. errormsg += command;
  480. errormsg += "\"";
  481. errormsg += "\nOutput file: ";
  482. errormsg += tempFile;
  483. cmSystemTools::Error(errormsg.c_str());
  484. }
  485. fin.close();
  486. cmSystemTools::RemoveFile(tempFile.c_str());
  487. return false;
  488. }
  489. bool multiLine = false;
  490. std::string line;
  491. while(cmSystemTools::GetLineFromStream(fin, line))
  492. {
  493. output += line;
  494. if(multiLine)
  495. {
  496. output += "\n";
  497. }
  498. multiLine = true;
  499. }
  500. fin.close();
  501. cmSystemTools::RemoveFile(tempFile.c_str());
  502. return true;
  503. }
  504. #else // We have popen
  505. bool RunCommandViaPopen(const char* command,
  506. const char* dir,
  507. std::string& output,
  508. int& retVal,
  509. bool verbose,
  510. int /*timeout*/)
  511. {
  512. // if only popen worked on windows.....
  513. std::string commandInDir;
  514. if(dir)
  515. {
  516. commandInDir = "cd \"";
  517. commandInDir += dir;
  518. commandInDir += "\" && ";
  519. commandInDir += command;
  520. }
  521. else
  522. {
  523. commandInDir = command;
  524. }
  525. commandInDir += " 2>&1";
  526. command = commandInDir.c_str();
  527. const int BUFFER_SIZE = 4096;
  528. char buffer[BUFFER_SIZE];
  529. if(verbose)
  530. {
  531. std::cout << "running " << command << std::endl;
  532. }
  533. fflush(stdout);
  534. fflush(stderr);
  535. FILE* cpipe = popen(command, "r");
  536. if(!cpipe)
  537. {
  538. return false;
  539. }
  540. fgets(buffer, BUFFER_SIZE, cpipe);
  541. while(!feof(cpipe))
  542. {
  543. if(verbose)
  544. {
  545. std::cout << buffer << std::flush;
  546. }
  547. output += buffer;
  548. fgets(buffer, BUFFER_SIZE, cpipe);
  549. }
  550. retVal = pclose(cpipe);
  551. if (WIFEXITED(retVal))
  552. {
  553. retVal = WEXITSTATUS(retVal);
  554. return true;
  555. }
  556. if (WIFSIGNALED(retVal))
  557. {
  558. retVal = WTERMSIG(retVal);
  559. cmOStringStream error;
  560. error << "\nProcess terminated due to ";
  561. switch (retVal)
  562. {
  563. #ifdef SIGKILL
  564. case SIGKILL:
  565. error << "SIGKILL";
  566. break;
  567. #endif
  568. #ifdef SIGFPE
  569. case SIGFPE:
  570. error << "SIGFPE";
  571. break;
  572. #endif
  573. #ifdef SIGBUS
  574. case SIGBUS:
  575. error << "SIGBUS";
  576. break;
  577. #endif
  578. #ifdef SIGSEGV
  579. case SIGSEGV:
  580. error << "SIGSEGV";
  581. break;
  582. #endif
  583. default:
  584. error << "signal " << retVal;
  585. break;
  586. }
  587. output += error.str();
  588. }
  589. return false;
  590. }
  591. #endif // endif WIN32 not CYGWIN
  592. // run a command unix uses popen (easy)
  593. // windows uses system and ShortPath
  594. bool cmSystemTools::RunCommand(const char* command,
  595. std::string& output,
  596. int &retVal,
  597. const char* dir,
  598. bool verbose,
  599. int timeout)
  600. {
  601. if(s_DisableRunCommandOutput)
  602. {
  603. verbose = false;
  604. }
  605. #if defined(WIN32) && !defined(__CYGWIN__)
  606. // if the command does not start with a quote, then
  607. // try to find the program, and if the program can not be
  608. // found use system to run the command as it must be a built in
  609. // shell command like echo or dir
  610. int count = 0;
  611. if(command[0] == '\"')
  612. {
  613. // count the number of quotes
  614. for(const char* s = command; *s != 0; ++s)
  615. {
  616. if(*s == '\"')
  617. {
  618. count++;
  619. if(count > 2)
  620. {
  621. break;
  622. }
  623. }
  624. }
  625. // if there are more than two double quotes use
  626. // GetShortPathName, the cmd.exe program in windows which
  627. // is used by system fails to execute if there are more than
  628. // one set of quotes in the arguments
  629. if(count > 2)
  630. {
  631. cmsys::RegularExpression quoted("^\"([^\"]*)\"[ \t](.*)");
  632. if(quoted.find(command))
  633. {
  634. std::string shortCmd;
  635. std::string cmd = quoted.match(1);
  636. std::string args = quoted.match(2);
  637. if(! cmSystemTools::FileExists(cmd.c_str()) )
  638. {
  639. shortCmd = cmd;
  640. }
  641. else if(!cmSystemTools::GetShortPath(cmd.c_str(), shortCmd))
  642. {
  643. cmSystemTools::Error("GetShortPath failed for " , cmd.c_str());
  644. return false;
  645. }
  646. shortCmd += " ";
  647. shortCmd += args;
  648. //return RunCommandViaSystem(shortCmd.c_str(), dir,
  649. // output, retVal, verbose);
  650. //return WindowsRunCommand(shortCmd.c_str(), dir,
  651. //output, retVal, verbose);
  652. return RunCommandViaWin32(shortCmd.c_str(), dir,
  653. output, retVal, verbose, timeout);
  654. }
  655. else
  656. {
  657. cmSystemTools::Error("Could not parse command line with quotes ",
  658. command);
  659. }
  660. }
  661. }
  662. // if there is only one set of quotes or no quotes then just run the command
  663. //return RunCommandViaSystem(command, dir, output, retVal, verbose);
  664. //return WindowsRunCommand(command, dir, output, retVal, verbose);
  665. return ::RunCommandViaWin32(command, dir, output, retVal, verbose, timeout);
  666. #else
  667. return ::RunCommandViaPopen(command, dir, output, retVal, verbose, timeout);
  668. #endif
  669. }
  670. bool cmSystemTools::DoesFileExistWithExtensions(
  671. const char* name,
  672. const std::vector<std::string>& headerExts)
  673. {
  674. std::string hname;
  675. for( std::vector<std::string>::const_iterator ext = headerExts.begin();
  676. ext != headerExts.end(); ++ext )
  677. {
  678. hname = name;
  679. hname += ".";
  680. hname += *ext;
  681. if(cmSystemTools::FileExists(hname.c_str()))
  682. {
  683. return true;
  684. }
  685. }
  686. return false;
  687. }
  688. bool cmSystemTools::cmCopyFile(const char* source, const char* destination)
  689. {
  690. return Superclass::CopyFileAlways(source, destination);
  691. }
  692. void cmSystemTools::Glob(const char *directory, const char *regexp,
  693. std::vector<std::string>& files)
  694. {
  695. cmsys::Directory d;
  696. cmsys::RegularExpression reg(regexp);
  697. if (d.Load(directory))
  698. {
  699. size_t numf;
  700. unsigned int i;
  701. numf = d.GetNumberOfFiles();
  702. for (i = 0; i < numf; i++)
  703. {
  704. std::string fname = d.GetFile(i);
  705. if (reg.find(fname))
  706. {
  707. files.push_back(fname);
  708. }
  709. }
  710. }
  711. }
  712. void cmSystemTools::GlobDirs(const char *fullPath,
  713. std::vector<std::string>& files)
  714. {
  715. std::string path = fullPath;
  716. std::string::size_type pos = path.find("/*");
  717. if(pos == std::string::npos)
  718. {
  719. files.push_back(fullPath);
  720. return;
  721. }
  722. std::string startPath = path.substr(0, pos);
  723. std::string finishPath = path.substr(pos+2);
  724. cmsys::Directory d;
  725. if (d.Load(startPath.c_str()))
  726. {
  727. for (unsigned int i = 0; i < d.GetNumberOfFiles(); ++i)
  728. {
  729. if((std::string(d.GetFile(i)) != ".")
  730. && (std::string(d.GetFile(i)) != ".."))
  731. {
  732. std::string fname = startPath;
  733. fname +="/";
  734. fname += d.GetFile(i);
  735. if(cmSystemTools::FileIsDirectory(fname.c_str()))
  736. {
  737. fname += finishPath;
  738. cmSystemTools::GlobDirs(fname.c_str(), files);
  739. }
  740. }
  741. }
  742. }
  743. }
  744. void cmSystemTools::ExpandList(std::vector<std::string> const& arguments,
  745. std::vector<std::string>& newargs)
  746. {
  747. std::vector<std::string>::const_iterator i;
  748. for(i = arguments.begin();i != arguments.end(); ++i)
  749. {
  750. cmSystemTools::ExpandListArgument(*i, newargs);
  751. }
  752. }
  753. void cmSystemTools::ExpandListArgument(const std::string& arg,
  754. std::vector<std::string>& newargs)
  755. {
  756. std::string newarg;
  757. // If argument is empty, it is an empty list.
  758. if(arg.length() == 0)
  759. {
  760. return;
  761. }
  762. // if there are no ; in the name then just copy the current string
  763. if(arg.find(';') == std::string::npos)
  764. {
  765. newargs.push_back(arg);
  766. }
  767. else
  768. {
  769. std::string::size_type start = 0;
  770. std::string::size_type endpos = 0;
  771. const std::string::size_type size = arg.size();
  772. // break up ; separated sections of the string into separate strings
  773. while(endpos != size)
  774. {
  775. endpos = arg.find(';', start);
  776. if(endpos == std::string::npos)
  777. {
  778. endpos = arg.size();
  779. }
  780. else
  781. {
  782. // skip right over escaped ; ( \; )
  783. while((endpos != std::string::npos)
  784. && (endpos > 0)
  785. && ((arg)[endpos-1] == '\\') )
  786. {
  787. endpos = arg.find(';', endpos+1);
  788. }
  789. if(endpos == std::string::npos)
  790. {
  791. endpos = arg.size();
  792. }
  793. }
  794. std::string::size_type len = endpos - start;
  795. if (len > 0)
  796. {
  797. // check for a closing ] after the start position
  798. if(arg.find('[', start) == std::string::npos)
  799. {
  800. // if there is no [ in the string then keep it
  801. newarg = arg.substr(start, len);
  802. }
  803. else
  804. {
  805. int opencount = 0;
  806. int closecount = 0;
  807. for(std::string::size_type j = start; j < endpos; ++j)
  808. {
  809. if(arg.at(j) == '[')
  810. {
  811. ++opencount;
  812. }
  813. else if (arg.at(j) == ']')
  814. {
  815. ++closecount;
  816. }
  817. }
  818. if(opencount != closecount)
  819. {
  820. // skip this one
  821. endpos = arg.find(';', endpos+1);
  822. if(endpos == std::string::npos)
  823. {
  824. endpos = arg.size();
  825. }
  826. len = endpos - start;
  827. }
  828. newarg = arg.substr(start, len);
  829. }
  830. std::string::size_type pos = newarg.find("\\;");
  831. if(pos != std::string::npos)
  832. {
  833. newarg.erase(pos, 1);
  834. }
  835. newargs.push_back(newarg);
  836. }
  837. start = endpos+1;
  838. }
  839. }
  840. }
  841. bool cmSystemTools::SimpleGlob(const std::string& glob,
  842. std::vector<std::string>& files,
  843. int type /* = 0 */)
  844. {
  845. files.clear();
  846. if ( glob[glob.size()-1] != '*' )
  847. {
  848. return false;
  849. }
  850. std::string path = cmSystemTools::GetFilenamePath(glob);
  851. std::string ppath = cmSystemTools::GetFilenameName(glob);
  852. ppath = ppath.substr(0, ppath.size()-1);
  853. if ( path.size() == 0 )
  854. {
  855. path = "/";
  856. }
  857. bool res = false;
  858. cmsys::Directory d;
  859. if (d.Load(path.c_str()))
  860. {
  861. for (unsigned int i = 0; i < d.GetNumberOfFiles(); ++i)
  862. {
  863. if((std::string(d.GetFile(i)) != ".")
  864. && (std::string(d.GetFile(i)) != ".."))
  865. {
  866. std::string fname = path;
  867. if ( path[path.size()-1] != '/' )
  868. {
  869. fname +="/";
  870. }
  871. fname += d.GetFile(i);
  872. std::string sfname = d.GetFile(i);
  873. if ( type > 0 && cmSystemTools::FileIsDirectory(fname.c_str()) )
  874. {
  875. continue;
  876. }
  877. if ( type < 0 && !cmSystemTools::FileIsDirectory(fname.c_str()) )
  878. {
  879. continue;
  880. }
  881. if ( sfname.size() >= ppath.size() &&
  882. sfname.substr(0, ppath.size()) ==
  883. ppath )
  884. {
  885. files.push_back(fname);
  886. res = true;
  887. }
  888. }
  889. }
  890. }
  891. return res;
  892. }
  893. cmSystemTools::FileFormat cmSystemTools::GetFileFormat(const char* cext)
  894. {
  895. if ( ! cext || *cext == 0 )
  896. {
  897. return cmSystemTools::NO_FILE_FORMAT;
  898. }
  899. //std::string ext = cmSystemTools::LowerCase(cext);
  900. std::string ext = cext;
  901. if ( ext == "c" || ext == ".c" ) { return cmSystemTools::C_FILE_FORMAT; }
  902. if (
  903. ext == "C" || ext == ".C" ||
  904. ext == "M" || ext == ".M" ||
  905. ext == "c++" || ext == ".c++" ||
  906. ext == "cc" || ext == ".cc" ||
  907. ext == "cpp" || ext == ".cpp" ||
  908. ext == "cxx" || ext == ".cxx" ||
  909. ext == "m" || ext == ".m" ||
  910. ext == "mm" || ext == ".mm"
  911. ) { return cmSystemTools::CXX_FILE_FORMAT; }
  912. if ( ext == "java" || ext == ".java" ) { return cmSystemTools::JAVA_FILE_FORMAT; }
  913. if (
  914. ext == "H" || ext == ".H" ||
  915. ext == "h" || ext == ".h" ||
  916. ext == "h++" || ext == ".h++" ||
  917. ext == "hm" || ext == ".hm" ||
  918. ext == "hpp" || ext == ".hpp" ||
  919. ext == "hxx" || ext == ".hxx" ||
  920. ext == "in" || ext == ".in" ||
  921. ext == "txx" || ext == ".txx"
  922. ) { return cmSystemTools::HEADER_FILE_FORMAT; }
  923. if ( ext == "rc" || ext == ".rc" ) { return cmSystemTools::RESOURCE_FILE_FORMAT; }
  924. if ( ext == "def" || ext == ".def" ) { return cmSystemTools::DEFINITION_FILE_FORMAT; }
  925. if ( ext == "lib" || ext == ".lib" ||
  926. ext == "a" || ext == ".a") { return cmSystemTools::STATIC_LIBRARY_FILE_FORMAT; }
  927. if ( ext == "o" || ext == ".o" ||
  928. ext == "obj" || ext == ".obj") { return cmSystemTools::OBJECT_FILE_FORMAT; }
  929. #ifdef __APPLE__
  930. if ( ext == "dylib" || ext == ".dylib" )
  931. { return cmSystemTools::SHARED_LIBRARY_FILE_FORMAT; }
  932. if ( ext == "so" || ext == ".so" ||
  933. ext == "bundle" || ext == ".bundle" )
  934. { return cmSystemTools::MODULE_FILE_FORMAT; }
  935. #else // __APPLE__
  936. if ( ext == "so" || ext == ".so" ||
  937. ext == "sl" || ext == ".sl" ||
  938. ext == "dll" || ext == ".dll" )
  939. { return cmSystemTools::SHARED_LIBRARY_FILE_FORMAT; }
  940. #endif // __APPLE__
  941. return cmSystemTools::UNKNOWN_FILE_FORMAT;
  942. }
  943. bool cmSystemTools::Split(const char* s, std::vector<cmStdString>& l)
  944. {
  945. std::vector<std::string> temp;
  946. if(!Superclass::Split(s, temp))
  947. {
  948. return false;
  949. }
  950. for(std::vector<std::string>::const_iterator i = temp.begin();
  951. i != temp.end(); ++i)
  952. {
  953. l.push_back(*i);
  954. }
  955. return true;
  956. }