cmSystemTools.cxx 27 KB

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