cmSystemTools.cxx 29 KB

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