cmSystemTools.cxx 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129
  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, &data, &length, 0))
  365. {
  366. if ( output )
  367. {
  368. tempOutput.insert(tempOutput.end(), data, data+length);
  369. }
  370. if(verbose)
  371. {
  372. std::cout.write(data, length);
  373. }
  374. }
  375. cmsysProcess_WaitForExit(cp, 0);
  376. if ( output )
  377. {
  378. output->append(&*tempOutput.begin(), tempOutput.size());
  379. }
  380. bool result = true;
  381. if(cmsysProcess_GetState(cp) == cmsysProcess_State_Exited)
  382. {
  383. if ( retVal )
  384. {
  385. *retVal = cmsysProcess_GetExitValue(cp);
  386. }
  387. else
  388. {
  389. if ( cmsysProcess_GetExitValue(cp) != 0 )
  390. {
  391. result = false;
  392. }
  393. }
  394. }
  395. else
  396. {
  397. std::cerr << cmsysProcess_GetErrorString(cp) << "\n";
  398. result = false;
  399. }
  400. cmsysProcess_Delete(cp);
  401. return result;
  402. }
  403. bool cmSystemTools::RunCommand(const char* command,
  404. std::string& output,
  405. const char* dir,
  406. bool verbose,
  407. int timeout)
  408. {
  409. int dummy;
  410. return cmSystemTools::RunCommand(command, output, dummy,
  411. dir, verbose, timeout);
  412. }
  413. #if defined(WIN32) && !defined(__CYGWIN__)
  414. #include "cmWin32ProcessExecution.h"
  415. // use this for shell commands like echo and dir
  416. bool RunCommandViaWin32(const char* command,
  417. const char* dir,
  418. std::string& output,
  419. int& retVal,
  420. bool verbose,
  421. int timeout)
  422. {
  423. #if defined(__BORLANDC__)
  424. return cmWin32ProcessExecution::BorlandRunCommand(command, dir, output,
  425. retVal,
  426. verbose, timeout,
  427. cmSystemTools::GetRunCommandHideConsole());
  428. #else // Visual studio
  429. ::SetLastError(ERROR_SUCCESS);
  430. if ( ! command )
  431. {
  432. cmSystemTools::Error("No command specified");
  433. return false;
  434. }
  435. cmWin32ProcessExecution resProc;
  436. if(cmSystemTools::GetRunCommandHideConsole())
  437. {
  438. resProc.SetHideWindows(true);
  439. }
  440. if ( cmSystemTools::GetWindows9xComspecSubstitute() )
  441. {
  442. resProc.SetConsoleSpawn(cmSystemTools::GetWindows9xComspecSubstitute() );
  443. }
  444. if ( !resProc.StartProcess(command, dir, verbose) )
  445. {
  446. return false;
  447. }
  448. resProc.Wait(timeout);
  449. output = resProc.GetOutput();
  450. retVal = resProc.GetExitValue();
  451. return true;
  452. #endif
  453. }
  454. // use this for shell commands like echo and dir
  455. bool RunCommandViaSystem(const char* command,
  456. const char* dir,
  457. std::string& output,
  458. int& retVal,
  459. bool verbose)
  460. {
  461. std::cout << "@@ " << command << std::endl;
  462. std::string commandInDir;
  463. if(dir)
  464. {
  465. commandInDir = "cd ";
  466. commandInDir += cmSystemTools::ConvertToOutputPath(dir);
  467. commandInDir += " && ";
  468. commandInDir += command;
  469. }
  470. else
  471. {
  472. commandInDir = command;
  473. }
  474. command = commandInDir.c_str();
  475. std::string commandToFile = command;
  476. commandToFile += " > ";
  477. std::string tempFile;
  478. tempFile += _tempnam(0, "cmake");
  479. commandToFile += tempFile;
  480. retVal = system(commandToFile.c_str());
  481. std::ifstream fin(tempFile.c_str());
  482. if(!fin)
  483. {
  484. if(verbose)
  485. {
  486. std::string errormsg = "RunCommand produced no output: command: \"";
  487. errormsg += command;
  488. errormsg += "\"";
  489. errormsg += "\nOutput file: ";
  490. errormsg += tempFile;
  491. cmSystemTools::Error(errormsg.c_str());
  492. }
  493. fin.close();
  494. cmSystemTools::RemoveFile(tempFile.c_str());
  495. return false;
  496. }
  497. bool multiLine = false;
  498. std::string line;
  499. while(cmSystemTools::GetLineFromStream(fin, line))
  500. {
  501. output += line;
  502. if(multiLine)
  503. {
  504. output += "\n";
  505. }
  506. multiLine = true;
  507. }
  508. fin.close();
  509. cmSystemTools::RemoveFile(tempFile.c_str());
  510. return true;
  511. }
  512. #else // We have popen
  513. bool RunCommandViaPopen(const char* command,
  514. const char* dir,
  515. std::string& output,
  516. int& retVal,
  517. bool verbose,
  518. int /*timeout*/)
  519. {
  520. // if only popen worked on windows.....
  521. std::string commandInDir;
  522. if(dir)
  523. {
  524. commandInDir = "cd \"";
  525. commandInDir += dir;
  526. commandInDir += "\" && ";
  527. commandInDir += command;
  528. }
  529. else
  530. {
  531. commandInDir = command;
  532. }
  533. commandInDir += " 2>&1";
  534. command = commandInDir.c_str();
  535. const int BUFFER_SIZE = 4096;
  536. char buffer[BUFFER_SIZE];
  537. if(verbose)
  538. {
  539. std::cout << "running " << command << std::endl;
  540. }
  541. fflush(stdout);
  542. fflush(stderr);
  543. FILE* cpipe = popen(command, "r");
  544. if(!cpipe)
  545. {
  546. return false;
  547. }
  548. fgets(buffer, BUFFER_SIZE, cpipe);
  549. while(!feof(cpipe))
  550. {
  551. if(verbose)
  552. {
  553. std::cout << buffer << std::flush;
  554. }
  555. output += buffer;
  556. fgets(buffer, BUFFER_SIZE, cpipe);
  557. }
  558. retVal = pclose(cpipe);
  559. if (WIFEXITED(retVal))
  560. {
  561. retVal = WEXITSTATUS(retVal);
  562. return true;
  563. }
  564. if (WIFSIGNALED(retVal))
  565. {
  566. retVal = WTERMSIG(retVal);
  567. cmOStringStream error;
  568. error << "\nProcess terminated due to ";
  569. switch (retVal)
  570. {
  571. #ifdef SIGKILL
  572. case SIGKILL:
  573. error << "SIGKILL";
  574. break;
  575. #endif
  576. #ifdef SIGFPE
  577. case SIGFPE:
  578. error << "SIGFPE";
  579. break;
  580. #endif
  581. #ifdef SIGBUS
  582. case SIGBUS:
  583. error << "SIGBUS";
  584. break;
  585. #endif
  586. #ifdef SIGSEGV
  587. case SIGSEGV:
  588. error << "SIGSEGV";
  589. break;
  590. #endif
  591. default:
  592. error << "signal " << retVal;
  593. break;
  594. }
  595. output += error.str();
  596. }
  597. return false;
  598. }
  599. #endif // endif WIN32 not CYGWIN
  600. // run a command unix uses popen (easy)
  601. // windows uses system and ShortPath
  602. bool cmSystemTools::RunCommand(const char* command,
  603. std::string& output,
  604. int &retVal,
  605. const char* dir,
  606. bool verbose,
  607. int timeout)
  608. {
  609. if(s_DisableRunCommandOutput)
  610. {
  611. verbose = false;
  612. }
  613. #if defined(WIN32) && !defined(__CYGWIN__)
  614. // if the command does not start with a quote, then
  615. // try to find the program, and if the program can not be
  616. // found use system to run the command as it must be a built in
  617. // shell command like echo or dir
  618. int count = 0;
  619. if(command[0] == '\"')
  620. {
  621. // count the number of quotes
  622. for(const char* s = command; *s != 0; ++s)
  623. {
  624. if(*s == '\"')
  625. {
  626. count++;
  627. if(count > 2)
  628. {
  629. break;
  630. }
  631. }
  632. }
  633. // if there are more than two double quotes use
  634. // GetShortPathName, the cmd.exe program in windows which
  635. // is used by system fails to execute if there are more than
  636. // one set of quotes in the arguments
  637. if(count > 2)
  638. {
  639. cmsys::RegularExpression quoted("^\"([^\"]*)\"[ \t](.*)");
  640. if(quoted.find(command))
  641. {
  642. std::string shortCmd;
  643. std::string cmd = quoted.match(1);
  644. std::string args = quoted.match(2);
  645. if(! cmSystemTools::FileExists(cmd.c_str()) )
  646. {
  647. shortCmd = cmd;
  648. }
  649. else if(!cmSystemTools::GetShortPath(cmd.c_str(), shortCmd))
  650. {
  651. cmSystemTools::Error("GetShortPath failed for " , cmd.c_str());
  652. return false;
  653. }
  654. shortCmd += " ";
  655. shortCmd += args;
  656. //return RunCommandViaSystem(shortCmd.c_str(), dir,
  657. // output, retVal, verbose);
  658. //return WindowsRunCommand(shortCmd.c_str(), dir,
  659. //output, retVal, verbose);
  660. return RunCommandViaWin32(shortCmd.c_str(), dir,
  661. output, retVal, verbose, timeout);
  662. }
  663. else
  664. {
  665. cmSystemTools::Error("Could not parse command line with quotes ",
  666. command);
  667. }
  668. }
  669. }
  670. // if there is only one set of quotes or no quotes then just run the command
  671. //return RunCommandViaSystem(command, dir, output, retVal, verbose);
  672. //return WindowsRunCommand(command, dir, output, retVal, verbose);
  673. return ::RunCommandViaWin32(command, dir, output, retVal, verbose, timeout);
  674. #else
  675. return ::RunCommandViaPopen(command, dir, output, retVal, verbose, timeout);
  676. #endif
  677. }
  678. bool cmSystemTools::DoesFileExistWithExtensions(
  679. const char* name,
  680. const std::vector<std::string>& headerExts)
  681. {
  682. std::string hname;
  683. for( std::vector<std::string>::const_iterator ext = headerExts.begin();
  684. ext != headerExts.end(); ++ext )
  685. {
  686. hname = name;
  687. hname += ".";
  688. hname += *ext;
  689. if(cmSystemTools::FileExists(hname.c_str()))
  690. {
  691. return true;
  692. }
  693. }
  694. return false;
  695. }
  696. bool cmSystemTools::cmCopyFile(const char* source, const char* destination)
  697. {
  698. return Superclass::CopyFileAlways(source, destination);
  699. }
  700. void cmSystemTools::Glob(const char *directory, const char *regexp,
  701. std::vector<std::string>& files)
  702. {
  703. cmsys::Directory d;
  704. cmsys::RegularExpression reg(regexp);
  705. if (d.Load(directory))
  706. {
  707. size_t numf;
  708. unsigned int i;
  709. numf = d.GetNumberOfFiles();
  710. for (i = 0; i < numf; i++)
  711. {
  712. std::string fname = d.GetFile(i);
  713. if (reg.find(fname))
  714. {
  715. files.push_back(fname);
  716. }
  717. }
  718. }
  719. }
  720. void cmSystemTools::GlobDirs(const char *fullPath,
  721. std::vector<std::string>& files)
  722. {
  723. std::string path = fullPath;
  724. std::string::size_type pos = path.find("/*");
  725. if(pos == std::string::npos)
  726. {
  727. files.push_back(fullPath);
  728. return;
  729. }
  730. std::string startPath = path.substr(0, pos);
  731. std::string finishPath = path.substr(pos+2);
  732. cmsys::Directory d;
  733. if (d.Load(startPath.c_str()))
  734. {
  735. for (unsigned int i = 0; i < d.GetNumberOfFiles(); ++i)
  736. {
  737. if((std::string(d.GetFile(i)) != ".")
  738. && (std::string(d.GetFile(i)) != ".."))
  739. {
  740. std::string fname = startPath;
  741. fname +="/";
  742. fname += d.GetFile(i);
  743. if(cmSystemTools::FileIsDirectory(fname.c_str()))
  744. {
  745. fname += finishPath;
  746. cmSystemTools::GlobDirs(fname.c_str(), files);
  747. }
  748. }
  749. }
  750. }
  751. }
  752. void cmSystemTools::ExpandList(std::vector<std::string> const& arguments,
  753. std::vector<std::string>& newargs)
  754. {
  755. std::vector<std::string>::const_iterator i;
  756. for(i = arguments.begin();i != arguments.end(); ++i)
  757. {
  758. cmSystemTools::ExpandListArgument(*i, newargs);
  759. }
  760. }
  761. void cmSystemTools::ExpandListArgument(const std::string& arg,
  762. std::vector<std::string>& newargs)
  763. {
  764. std::string newarg;
  765. // If argument is empty, it is an empty list.
  766. if(arg.length() == 0)
  767. {
  768. return;
  769. }
  770. // if there are no ; in the name then just copy the current string
  771. if(arg.find(';') == std::string::npos)
  772. {
  773. newargs.push_back(arg);
  774. return;
  775. }
  776. // Break the string at non-escaped semicolons not nested in [].
  777. int squareNesting = 0;
  778. for(const char* c = arg.c_str(); *c; ++c)
  779. {
  780. switch(*c)
  781. {
  782. case '\\':
  783. {
  784. // We only want to allow escaping of semicolons. Other
  785. // escapes should not be processed here.
  786. ++c;
  787. if(*c == ';')
  788. {
  789. newarg += ';';
  790. }
  791. else
  792. {
  793. newarg += '\\';
  794. if(*c)
  795. {
  796. newarg += *c;
  797. }
  798. }
  799. } break;
  800. case '[':
  801. {
  802. ++squareNesting;
  803. newarg += '[';
  804. } break;
  805. case ']':
  806. {
  807. --squareNesting;
  808. newarg += ']';
  809. } break;
  810. case ';':
  811. {
  812. // Break the string here if we are not nested inside square
  813. // brackets.
  814. if(squareNesting == 0)
  815. {
  816. if(newarg.length())
  817. {
  818. // Add an argument if the string is not empty.
  819. newargs.push_back(newarg);
  820. newarg = "";
  821. }
  822. }
  823. else
  824. {
  825. newarg += ';';
  826. }
  827. } break;
  828. default:
  829. {
  830. // Just append this character.
  831. newarg += *c;
  832. } break;
  833. }
  834. }
  835. if(newarg.length())
  836. {
  837. // Add the last argument if the string is not empty.
  838. newargs.push_back(newarg);
  839. }
  840. }
  841. bool cmSystemTools::SimpleGlob(const cmStdString& glob,
  842. std::vector<cmStdString>& 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. }
  957. std::string cmSystemTools::ConvertToOutputPath(const char* path)
  958. {
  959. #if defined(_WIN32) && !defined(__CYGWIN__)
  960. if(s_ForceUnixPaths)
  961. {
  962. return cmSystemTools::ConvertToUnixOutputPath(path);
  963. }
  964. return cmSystemTools::ConvertToWindowsOutputPath(path);
  965. #else
  966. return cmSystemTools::ConvertToUnixOutputPath(path);
  967. #endif
  968. }
  969. bool cmSystemTools::StringEndsWith(const char* str1, const char* str2)
  970. {
  971. if ( !str1 || !str2 || strlen(str1) < strlen(str2) )
  972. {
  973. return 0;
  974. }
  975. return !strncmp(str1 + (strlen(str1)-strlen(str2)), str2, strlen(str2));
  976. }
  977. #if defined(_WIN32) && !defined(__CYGWIN__)
  978. bool cmSystemTools::CreateSymlink(const char*, const char*)
  979. {
  980. // Should we create a copy here?
  981. return false;
  982. }
  983. #else
  984. bool cmSystemTools::CreateSymlink(const char* origName, const char* newName)
  985. {
  986. return (symlink(origName, newName) >= 0);
  987. }
  988. #endif
  989. std::vector<std::string> cmSystemTools::SplitString(const char* p, char sep)
  990. {
  991. std::string path = p;
  992. std::vector<std::string> paths;
  993. std::string::size_type pos1 = 0;
  994. std::string::size_type pos2 = path.find(sep, pos1+1);
  995. while(pos2 != std::string::npos)
  996. {
  997. paths.push_back(path.substr(pos1+1, pos2-pos1-1));
  998. pos1 = pos2;
  999. pos2 = path.find(sep, pos1+1);
  1000. }
  1001. paths.push_back(path.substr(pos1+1, pos2-pos1-1));
  1002. return paths;
  1003. }
  1004. // compute the relative path from here to there
  1005. std::string cmSystemTools::RelativePath(const char* local, const char* remote)
  1006. {
  1007. #ifdef _WIN32
  1008. std::string lowerCaseLocal = cmSystemTools::LowerCase(std::string(local));
  1009. std::string lowerCaseRemote = cmSystemTools::LowerCase(std::string(remote));
  1010. remote = lowerCaseRemote.c_str();
  1011. local = lowerCaseLocal.c_str();
  1012. #endif
  1013. // check for driveletter: as the start of the path
  1014. // if not on the same drive then full path to remote must be used.
  1015. if(local[0] && local[0] != '/')
  1016. {
  1017. if(remote[0] && local[0] != remote[0])
  1018. {
  1019. return remote;
  1020. }
  1021. }
  1022. std::string relativePath; // result string
  1023. // split up both paths into arrays of strings using / as a separator
  1024. std::vector<std::string> fileSplit = cmSystemTools::SplitString(local);
  1025. std::vector<std::string> relativeSplit = cmSystemTools::SplitString(remote);
  1026. // count up how many mathing directory names there are from the start
  1027. int sameCount = 0;
  1028. while(sameCount < fileSplit.size()-1 && sameCount < relativeSplit.size()-1 &&
  1029. fileSplit[sameCount] == relativeSplit[sameCount])
  1030. {
  1031. sameCount++;
  1032. }
  1033. // put in sameCount number of ../ into the path
  1034. int i;
  1035. for(i = sameCount; i < fileSplit.size(); ++i)
  1036. {
  1037. relativePath += "../";
  1038. }
  1039. // now put the rest of path that did not match back
  1040. for(i = sameCount; i < relativeSplit.size()-1; ++i)
  1041. {
  1042. relativePath += relativeSplit[i] + "/";
  1043. }
  1044. relativePath += relativeSplit[i];
  1045. return relativePath;
  1046. }