cmSystemTools.cxx 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  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_Exception)
  426. {
  427. std::cerr << cmsysProcess_GetExceptionString(cp) << "\n";
  428. result = false;
  429. }
  430. else if(cmsysProcess_GetState(cp) == cmsysProcess_State_Error)
  431. {
  432. std::cerr << cmsysProcess_GetErrorString(cp) << "\n";
  433. result = false;
  434. }
  435. else if(cmsysProcess_GetState(cp) == cmsysProcess_State_Expired)
  436. {
  437. std::cerr << "Process terminated due to timeout\n";
  438. result = false;
  439. }
  440. cmsysProcess_Delete(cp);
  441. return result;
  442. }
  443. bool cmSystemTools::RunCommand(const char* command,
  444. std::string& output,
  445. const char* dir,
  446. bool verbose,
  447. int timeout)
  448. {
  449. int dummy;
  450. return cmSystemTools::RunCommand(command, output, dummy,
  451. dir, verbose, timeout);
  452. }
  453. #if defined(WIN32) && !defined(__CYGWIN__)
  454. #include "cmWin32ProcessExecution.h"
  455. // use this for shell commands like echo and dir
  456. bool RunCommandViaWin32(const char* command,
  457. const char* dir,
  458. std::string& output,
  459. int& retVal,
  460. bool verbose,
  461. int timeout)
  462. {
  463. #if defined(__BORLANDC__)
  464. return cmWin32ProcessExecution::BorlandRunCommand(command, dir, output,
  465. retVal,
  466. verbose, timeout,
  467. cmSystemTools::GetRunCommandHideConsole());
  468. #else // Visual studio
  469. ::SetLastError(ERROR_SUCCESS);
  470. if ( ! command )
  471. {
  472. cmSystemTools::Error("No command specified");
  473. return false;
  474. }
  475. cmWin32ProcessExecution resProc;
  476. if(cmSystemTools::GetRunCommandHideConsole())
  477. {
  478. resProc.SetHideWindows(true);
  479. }
  480. if ( cmSystemTools::GetWindows9xComspecSubstitute() )
  481. {
  482. resProc.SetConsoleSpawn(cmSystemTools::GetWindows9xComspecSubstitute() );
  483. }
  484. if ( !resProc.StartProcess(command, dir, verbose) )
  485. {
  486. return false;
  487. }
  488. resProc.Wait(timeout);
  489. output = resProc.GetOutput();
  490. retVal = resProc.GetExitValue();
  491. return true;
  492. #endif
  493. }
  494. // use this for shell commands like echo and dir
  495. bool RunCommandViaSystem(const char* command,
  496. const char* dir,
  497. std::string& output,
  498. int& retVal,
  499. bool verbose)
  500. {
  501. std::cout << "@@ " << command << std::endl;
  502. std::string commandInDir;
  503. if(dir)
  504. {
  505. commandInDir = "cd ";
  506. commandInDir += cmSystemTools::ConvertToOutputPath(dir);
  507. commandInDir += " && ";
  508. commandInDir += command;
  509. }
  510. else
  511. {
  512. commandInDir = command;
  513. }
  514. command = commandInDir.c_str();
  515. std::string commandToFile = command;
  516. commandToFile += " > ";
  517. std::string tempFile;
  518. tempFile += _tempnam(0, "cmake");
  519. commandToFile += tempFile;
  520. retVal = system(commandToFile.c_str());
  521. std::ifstream fin(tempFile.c_str());
  522. if(!fin)
  523. {
  524. if(verbose)
  525. {
  526. std::string errormsg = "RunCommand produced no output: command: \"";
  527. errormsg += command;
  528. errormsg += "\"";
  529. errormsg += "\nOutput file: ";
  530. errormsg += tempFile;
  531. cmSystemTools::Error(errormsg.c_str());
  532. }
  533. fin.close();
  534. cmSystemTools::RemoveFile(tempFile.c_str());
  535. return false;
  536. }
  537. bool multiLine = false;
  538. std::string line;
  539. while(cmSystemTools::GetLineFromStream(fin, line))
  540. {
  541. output += line;
  542. if(multiLine)
  543. {
  544. output += "\n";
  545. }
  546. multiLine = true;
  547. }
  548. fin.close();
  549. cmSystemTools::RemoveFile(tempFile.c_str());
  550. return true;
  551. }
  552. #else // We have popen
  553. bool RunCommandViaPopen(const char* command,
  554. const char* dir,
  555. std::string& output,
  556. int& retVal,
  557. bool verbose,
  558. int /*timeout*/)
  559. {
  560. // if only popen worked on windows.....
  561. std::string commandInDir;
  562. if(dir)
  563. {
  564. commandInDir = "cd \"";
  565. commandInDir += dir;
  566. commandInDir += "\" && ";
  567. commandInDir += command;
  568. }
  569. else
  570. {
  571. commandInDir = command;
  572. }
  573. commandInDir += " 2>&1";
  574. command = commandInDir.c_str();
  575. const int BUFFER_SIZE = 4096;
  576. char buffer[BUFFER_SIZE];
  577. if(verbose)
  578. {
  579. cmSystemTools::Stdout("running ");
  580. cmSystemTools::Stdout(command);
  581. cmSystemTools::Stdout("\n");
  582. }
  583. fflush(stdout);
  584. fflush(stderr);
  585. FILE* cpipe = popen(command, "r");
  586. if(!cpipe)
  587. {
  588. return false;
  589. }
  590. fgets(buffer, BUFFER_SIZE, cpipe);
  591. while(!feof(cpipe))
  592. {
  593. if(verbose)
  594. {
  595. cmSystemTools::Stdout(buffer);
  596. }
  597. output += buffer;
  598. fgets(buffer, BUFFER_SIZE, cpipe);
  599. }
  600. retVal = pclose(cpipe);
  601. if (WIFEXITED(retVal))
  602. {
  603. retVal = WEXITSTATUS(retVal);
  604. return true;
  605. }
  606. if (WIFSIGNALED(retVal))
  607. {
  608. retVal = WTERMSIG(retVal);
  609. cmOStringStream error;
  610. error << "\nProcess terminated due to ";
  611. switch (retVal)
  612. {
  613. #ifdef SIGKILL
  614. case SIGKILL:
  615. error << "SIGKILL";
  616. break;
  617. #endif
  618. #ifdef SIGFPE
  619. case SIGFPE:
  620. error << "SIGFPE";
  621. break;
  622. #endif
  623. #ifdef SIGBUS
  624. case SIGBUS:
  625. error << "SIGBUS";
  626. break;
  627. #endif
  628. #ifdef SIGSEGV
  629. case SIGSEGV:
  630. error << "SIGSEGV";
  631. break;
  632. #endif
  633. default:
  634. error << "signal " << retVal;
  635. break;
  636. }
  637. output += error.str();
  638. }
  639. return false;
  640. }
  641. #endif // endif WIN32 not CYGWIN
  642. // run a command unix uses popen (easy)
  643. // windows uses system and ShortPath
  644. bool cmSystemTools::RunCommand(const char* command,
  645. std::string& output,
  646. int &retVal,
  647. const char* dir,
  648. bool verbose,
  649. int timeout)
  650. {
  651. if(s_DisableRunCommandOutput)
  652. {
  653. verbose = false;
  654. }
  655. #if defined(WIN32) && !defined(__CYGWIN__)
  656. // if the command does not start with a quote, then
  657. // try to find the program, and if the program can not be
  658. // found use system to run the command as it must be a built in
  659. // shell command like echo or dir
  660. int count = 0;
  661. if(command[0] == '\"')
  662. {
  663. // count the number of quotes
  664. for(const char* s = command; *s != 0; ++s)
  665. {
  666. if(*s == '\"')
  667. {
  668. count++;
  669. if(count > 2)
  670. {
  671. break;
  672. }
  673. }
  674. }
  675. // if there are more than two double quotes use
  676. // GetShortPathName, the cmd.exe program in windows which
  677. // is used by system fails to execute if there are more than
  678. // one set of quotes in the arguments
  679. if(count > 2)
  680. {
  681. cmsys::RegularExpression quoted("^\"([^\"]*)\"[ \t](.*)");
  682. if(quoted.find(command))
  683. {
  684. std::string shortCmd;
  685. std::string cmd = quoted.match(1);
  686. std::string args = quoted.match(2);
  687. if(! cmSystemTools::FileExists(cmd.c_str()) )
  688. {
  689. shortCmd = cmd;
  690. }
  691. else if(!cmSystemTools::GetShortPath(cmd.c_str(), shortCmd))
  692. {
  693. cmSystemTools::Error("GetShortPath failed for " , cmd.c_str());
  694. return false;
  695. }
  696. shortCmd += " ";
  697. shortCmd += args;
  698. //return RunCommandViaSystem(shortCmd.c_str(), dir,
  699. // output, retVal, verbose);
  700. //return WindowsRunCommand(shortCmd.c_str(), dir,
  701. //output, retVal, verbose);
  702. return RunCommandViaWin32(shortCmd.c_str(), dir,
  703. output, retVal, verbose, timeout);
  704. }
  705. else
  706. {
  707. cmSystemTools::Error("Could not parse command line with quotes ",
  708. command);
  709. }
  710. }
  711. }
  712. // if there is only one set of quotes or no quotes then just run the command
  713. //return RunCommandViaSystem(command, dir, output, retVal, verbose);
  714. //return WindowsRunCommand(command, dir, output, retVal, verbose);
  715. return ::RunCommandViaWin32(command, dir, output, retVal, verbose, timeout);
  716. #else
  717. return ::RunCommandViaPopen(command, dir, output, retVal, verbose, timeout);
  718. #endif
  719. }
  720. bool cmSystemTools::DoesFileExistWithExtensions(
  721. const char* name,
  722. const std::vector<std::string>& headerExts)
  723. {
  724. std::string hname;
  725. for( std::vector<std::string>::const_iterator ext = headerExts.begin();
  726. ext != headerExts.end(); ++ext )
  727. {
  728. hname = name;
  729. hname += ".";
  730. hname += *ext;
  731. if(cmSystemTools::FileExists(hname.c_str()))
  732. {
  733. return true;
  734. }
  735. }
  736. return false;
  737. }
  738. bool cmSystemTools::cmCopyFile(const char* source, const char* destination)
  739. {
  740. return Superclass::CopyFileAlways(source, destination);
  741. }
  742. void cmSystemTools::Glob(const char *directory, const char *regexp,
  743. std::vector<std::string>& files)
  744. {
  745. cmsys::Directory d;
  746. cmsys::RegularExpression reg(regexp);
  747. if (d.Load(directory))
  748. {
  749. size_t numf;
  750. unsigned int i;
  751. numf = d.GetNumberOfFiles();
  752. for (i = 0; i < numf; i++)
  753. {
  754. std::string fname = d.GetFile(i);
  755. if (reg.find(fname))
  756. {
  757. files.push_back(fname);
  758. }
  759. }
  760. }
  761. }
  762. void cmSystemTools::GlobDirs(const char *fullPath,
  763. std::vector<std::string>& files)
  764. {
  765. std::string path = fullPath;
  766. std::string::size_type pos = path.find("/*");
  767. if(pos == std::string::npos)
  768. {
  769. files.push_back(fullPath);
  770. return;
  771. }
  772. std::string startPath = path.substr(0, pos);
  773. std::string finishPath = path.substr(pos+2);
  774. cmsys::Directory d;
  775. if (d.Load(startPath.c_str()))
  776. {
  777. for (unsigned int i = 0; i < d.GetNumberOfFiles(); ++i)
  778. {
  779. if((std::string(d.GetFile(i)) != ".")
  780. && (std::string(d.GetFile(i)) != ".."))
  781. {
  782. std::string fname = startPath;
  783. fname +="/";
  784. fname += d.GetFile(i);
  785. if(cmSystemTools::FileIsDirectory(fname.c_str()))
  786. {
  787. fname += finishPath;
  788. cmSystemTools::GlobDirs(fname.c_str(), files);
  789. }
  790. }
  791. }
  792. }
  793. }
  794. void cmSystemTools::ExpandList(std::vector<std::string> const& arguments,
  795. std::vector<std::string>& newargs)
  796. {
  797. std::vector<std::string>::const_iterator i;
  798. for(i = arguments.begin();i != arguments.end(); ++i)
  799. {
  800. cmSystemTools::ExpandListArgument(*i, newargs);
  801. }
  802. }
  803. void cmSystemTools::ExpandListArgument(const std::string& arg,
  804. std::vector<std::string>& newargs)
  805. {
  806. std::string newarg;
  807. // If argument is empty, it is an empty list.
  808. if(arg.length() == 0)
  809. {
  810. return;
  811. }
  812. // if there are no ; in the name then just copy the current string
  813. if(arg.find(';') == std::string::npos)
  814. {
  815. newargs.push_back(arg);
  816. return;
  817. }
  818. // Break the string at non-escaped semicolons not nested in [].
  819. int squareNesting = 0;
  820. for(const char* c = arg.c_str(); *c; ++c)
  821. {
  822. switch(*c)
  823. {
  824. case '\\':
  825. {
  826. // We only want to allow escaping of semicolons. Other
  827. // escapes should not be processed here.
  828. ++c;
  829. if(*c == ';')
  830. {
  831. newarg += ';';
  832. }
  833. else
  834. {
  835. newarg += '\\';
  836. if(*c)
  837. {
  838. newarg += *c;
  839. }
  840. }
  841. } break;
  842. case '[':
  843. {
  844. ++squareNesting;
  845. newarg += '[';
  846. } break;
  847. case ']':
  848. {
  849. --squareNesting;
  850. newarg += ']';
  851. } break;
  852. case ';':
  853. {
  854. // Break the string here if we are not nested inside square
  855. // brackets.
  856. if(squareNesting == 0)
  857. {
  858. if(newarg.length())
  859. {
  860. // Add an argument if the string is not empty.
  861. newargs.push_back(newarg);
  862. newarg = "";
  863. }
  864. }
  865. else
  866. {
  867. newarg += ';';
  868. }
  869. } break;
  870. default:
  871. {
  872. // Just append this character.
  873. newarg += *c;
  874. } break;
  875. }
  876. }
  877. if(newarg.length())
  878. {
  879. // Add the last argument if the string is not empty.
  880. newargs.push_back(newarg);
  881. }
  882. }
  883. bool cmSystemTools::SimpleGlob(const cmStdString& glob,
  884. std::vector<cmStdString>& files,
  885. int type /* = 0 */)
  886. {
  887. files.clear();
  888. if ( glob[glob.size()-1] != '*' )
  889. {
  890. return false;
  891. }
  892. std::string path = cmSystemTools::GetFilenamePath(glob);
  893. std::string ppath = cmSystemTools::GetFilenameName(glob);
  894. ppath = ppath.substr(0, ppath.size()-1);
  895. if ( path.size() == 0 )
  896. {
  897. path = "/";
  898. }
  899. bool res = false;
  900. cmsys::Directory d;
  901. if (d.Load(path.c_str()))
  902. {
  903. for (unsigned int i = 0; i < d.GetNumberOfFiles(); ++i)
  904. {
  905. if((std::string(d.GetFile(i)) != ".")
  906. && (std::string(d.GetFile(i)) != ".."))
  907. {
  908. std::string fname = path;
  909. if ( path[path.size()-1] != '/' )
  910. {
  911. fname +="/";
  912. }
  913. fname += d.GetFile(i);
  914. std::string sfname = d.GetFile(i);
  915. if ( type > 0 && cmSystemTools::FileIsDirectory(fname.c_str()) )
  916. {
  917. continue;
  918. }
  919. if ( type < 0 && !cmSystemTools::FileIsDirectory(fname.c_str()) )
  920. {
  921. continue;
  922. }
  923. if ( sfname.size() >= ppath.size() &&
  924. sfname.substr(0, ppath.size()) ==
  925. ppath )
  926. {
  927. files.push_back(fname);
  928. res = true;
  929. }
  930. }
  931. }
  932. }
  933. return res;
  934. }
  935. cmSystemTools::FileFormat cmSystemTools::GetFileFormat(const char* cext)
  936. {
  937. if ( ! cext || *cext == 0 )
  938. {
  939. return cmSystemTools::NO_FILE_FORMAT;
  940. }
  941. //std::string ext = cmSystemTools::LowerCase(cext);
  942. std::string ext = cext;
  943. if ( ext == "c" || ext == ".c" ) { return cmSystemTools::C_FILE_FORMAT; }
  944. if (
  945. ext == "C" || ext == ".C" ||
  946. ext == "M" || ext == ".M" ||
  947. ext == "c++" || ext == ".c++" ||
  948. ext == "cc" || ext == ".cc" ||
  949. ext == "cpp" || ext == ".cpp" ||
  950. ext == "cxx" || ext == ".cxx" ||
  951. ext == "m" || ext == ".m" ||
  952. ext == "mm" || ext == ".mm"
  953. ) { return cmSystemTools::CXX_FILE_FORMAT; }
  954. if ( ext == "java" || ext == ".java" ) { return cmSystemTools::JAVA_FILE_FORMAT; }
  955. if (
  956. ext == "H" || ext == ".H" ||
  957. ext == "h" || ext == ".h" ||
  958. ext == "h++" || ext == ".h++" ||
  959. ext == "hm" || ext == ".hm" ||
  960. ext == "hpp" || ext == ".hpp" ||
  961. ext == "hxx" || ext == ".hxx" ||
  962. ext == "in" || ext == ".in" ||
  963. ext == "txx" || ext == ".txx"
  964. ) { return cmSystemTools::HEADER_FILE_FORMAT; }
  965. if ( ext == "rc" || ext == ".rc" ) { return cmSystemTools::RESOURCE_FILE_FORMAT; }
  966. if ( ext == "def" || ext == ".def" ) { return cmSystemTools::DEFINITION_FILE_FORMAT; }
  967. if ( ext == "lib" || ext == ".lib" ||
  968. ext == "a" || ext == ".a") { return cmSystemTools::STATIC_LIBRARY_FILE_FORMAT; }
  969. if ( ext == "o" || ext == ".o" ||
  970. ext == "obj" || ext == ".obj") { return cmSystemTools::OBJECT_FILE_FORMAT; }
  971. #ifdef __APPLE__
  972. if ( ext == "dylib" || ext == ".dylib" )
  973. { return cmSystemTools::SHARED_LIBRARY_FILE_FORMAT; }
  974. if ( ext == "so" || ext == ".so" ||
  975. ext == "bundle" || ext == ".bundle" )
  976. { return cmSystemTools::MODULE_FILE_FORMAT; }
  977. #else // __APPLE__
  978. if ( ext == "so" || ext == ".so" ||
  979. ext == "sl" || ext == ".sl" ||
  980. ext == "dll" || ext == ".dll" )
  981. { return cmSystemTools::SHARED_LIBRARY_FILE_FORMAT; }
  982. #endif // __APPLE__
  983. return cmSystemTools::UNKNOWN_FILE_FORMAT;
  984. }
  985. bool cmSystemTools::Split(const char* s, std::vector<cmStdString>& l)
  986. {
  987. std::vector<std::string> temp;
  988. if(!Superclass::Split(s, temp))
  989. {
  990. return false;
  991. }
  992. for(std::vector<std::string>::const_iterator i = temp.begin();
  993. i != temp.end(); ++i)
  994. {
  995. l.push_back(*i);
  996. }
  997. return true;
  998. }
  999. std::string cmSystemTools::ConvertToOutputPath(const char* path)
  1000. {
  1001. #if defined(_WIN32) && !defined(__CYGWIN__)
  1002. if(s_ForceUnixPaths)
  1003. {
  1004. return cmSystemTools::ConvertToUnixOutputPath(path);
  1005. }
  1006. return cmSystemTools::ConvertToWindowsOutputPath(path);
  1007. #else
  1008. return cmSystemTools::ConvertToUnixOutputPath(path);
  1009. #endif
  1010. }
  1011. bool cmSystemTools::StringEndsWith(const char* str1, const char* str2)
  1012. {
  1013. if ( !str1 || !str2 || strlen(str1) < strlen(str2) )
  1014. {
  1015. return 0;
  1016. }
  1017. return !strncmp(str1 + (strlen(str1)-strlen(str2)), str2, strlen(str2));
  1018. }
  1019. #if defined(_WIN32) && !defined(__CYGWIN__)
  1020. bool cmSystemTools::CreateSymlink(const char*, const char*)
  1021. {
  1022. // Should we create a copy here?
  1023. return false;
  1024. }
  1025. #else
  1026. bool cmSystemTools::CreateSymlink(const char* origName, const char* newName)
  1027. {
  1028. return (symlink(origName, newName) >= 0);
  1029. }
  1030. #endif
  1031. std::vector<cmStdString> cmSystemTools::SplitString(const char* p, char sep)
  1032. {
  1033. std::string path = p;
  1034. std::vector<cmStdString> paths;
  1035. std::string::size_type pos1 = 0;
  1036. std::string::size_type pos2 = path.find(sep, pos1+1);
  1037. while(pos2 != std::string::npos)
  1038. {
  1039. paths.push_back(path.substr(pos1, pos2-pos1));
  1040. pos1 = pos2+1;
  1041. pos2 = path.find(sep, pos1+1);
  1042. }
  1043. paths.push_back(path.substr(pos1, pos2-pos1));
  1044. return paths;
  1045. }
  1046. // compute the relative path from here to there
  1047. std::string cmSystemTools::RelativePath(const char* local, const char* remote)
  1048. {
  1049. #ifdef _WIN32
  1050. std::string lowerCaseLocal = cmSystemTools::LowerCase(std::string(local));
  1051. std::string lowerCaseRemote = cmSystemTools::LowerCase(std::string(remote));
  1052. remote = lowerCaseRemote.c_str();
  1053. local = lowerCaseLocal.c_str();
  1054. #endif
  1055. // check for driveletter: as the start of the path
  1056. // if not on the same drive then full path to remote must be used.
  1057. if(local[0] && local[0] != '/')
  1058. {
  1059. if(remote[0] && local[0] != remote[0])
  1060. {
  1061. return remote;
  1062. }
  1063. }
  1064. std::string relativePath; // result string
  1065. // split up both paths into arrays of strings using / as a separator
  1066. std::vector<cmStdString> fileSplit = cmSystemTools::SplitString(local);
  1067. std::vector<cmStdString> relativeSplit = cmSystemTools::SplitString(remote);
  1068. // count up how many mathing directory names there are from the start
  1069. unsigned int sameCount = 0;
  1070. while(sameCount < fileSplit.size()-1 && sameCount < relativeSplit.size()-1 &&
  1071. fileSplit[sameCount] == relativeSplit[sameCount])
  1072. {
  1073. sameCount++;
  1074. }
  1075. if(sameCount == 0)
  1076. {
  1077. return std::string(remote);
  1078. }
  1079. // put in sameCount number of ../ into the path
  1080. unsigned int i;
  1081. for(i = sameCount; i < fileSplit.size(); ++i)
  1082. {
  1083. relativePath += "../";
  1084. }
  1085. // now put the rest of path that did not match back
  1086. for(i = sameCount; i < relativeSplit.size()-1; ++i)
  1087. {
  1088. relativePath += relativeSplit[i] + "/";
  1089. }
  1090. relativePath += relativeSplit[i];
  1091. return relativePath;
  1092. }