cmSystemTools.cxx 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584
  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. #include <io.h>
  32. #define _unlink unlink
  33. #else
  34. #include <sys/types.h>
  35. #include <fcntl.h>
  36. #include <unistd.h>
  37. #endif
  38. #include <sys/stat.h>
  39. #if defined(CMAKE_BUILD_WITH_CMAKE)
  40. # include <libtar/libtar.h>
  41. # include <memory> // auto_ptr
  42. # include <fcntl.h>
  43. # include <cmzlib/zlib.h>
  44. #endif
  45. #if defined(__sgi) && !defined(__GNUC__)
  46. # pragma set woff 1375 /* base class destructor not virtual */
  47. #endif
  48. bool cmSystemTools::s_RunCommandHideConsole = false;
  49. bool cmSystemTools::s_DisableRunCommandOutput = false;
  50. bool cmSystemTools::s_ErrorOccured = false;
  51. bool cmSystemTools::s_FatalErrorOccured = false;
  52. bool cmSystemTools::s_DisableMessages = false;
  53. bool cmSystemTools::s_ForceUnixPaths = false;
  54. std::string cmSystemTools::s_Windows9xComspecSubstitute = "command.com";
  55. void cmSystemTools::SetWindows9xComspecSubstitute(const char* str)
  56. {
  57. if ( str )
  58. {
  59. cmSystemTools::s_Windows9xComspecSubstitute = str;
  60. }
  61. }
  62. const char* cmSystemTools::GetWindows9xComspecSubstitute()
  63. {
  64. return cmSystemTools::s_Windows9xComspecSubstitute.c_str();
  65. }
  66. void (*cmSystemTools::s_ErrorCallback)(const char*, const char*, bool&, void*);
  67. void (*cmSystemTools::s_StdoutCallback)(const char*, int len, void*);
  68. void* cmSystemTools::s_ErrorCallbackClientData = 0;
  69. void* cmSystemTools::s_StdoutCallbackClientData = 0;
  70. // replace replace with with as many times as it shows up in source.
  71. // write the result into source.
  72. #if defined(_WIN32) && !defined(__CYGWIN__)
  73. void cmSystemTools::ExpandRegistryValues(std::string& source)
  74. {
  75. // Regular expression to match anything inside [...] that begins in HKEY.
  76. // Note that there is a special rule for regular expressions to match a
  77. // close square-bracket inside a list delimited by square brackets.
  78. // The "[^]]" part of this expression will match any character except
  79. // a close square-bracket. The ']' character must be the first in the
  80. // list of characters inside the [^...] block of the expression.
  81. cmsys::RegularExpression regEntry("\\[(HKEY[^]]*)\\]");
  82. // check for black line or comment
  83. while (regEntry.find(source))
  84. {
  85. // the arguments are the second match
  86. std::string key = regEntry.match(1);
  87. std::string val;
  88. if (ReadRegistryValue(key.c_str(), val))
  89. {
  90. std::string reg = "[";
  91. reg += key + "]";
  92. cmSystemTools::ReplaceString(source, reg.c_str(), val.c_str());
  93. }
  94. else
  95. {
  96. std::string reg = "[";
  97. reg += key + "]";
  98. cmSystemTools::ReplaceString(source, reg.c_str(), "/registry");
  99. }
  100. }
  101. }
  102. #else
  103. void cmSystemTools::ExpandRegistryValues(std::string&)
  104. {
  105. }
  106. #endif
  107. std::string cmSystemTools::EscapeQuotes(const char* str)
  108. {
  109. std::string result = "";
  110. for(const char* ch = str; *ch != '\0'; ++ch)
  111. {
  112. if(*ch == '"')
  113. {
  114. result += '\\';
  115. }
  116. result += *ch;
  117. }
  118. return result;
  119. }
  120. std::string cmSystemTools::EscapeSpaces(const char* str)
  121. {
  122. #if defined(_WIN32) && !defined(__CYGWIN__)
  123. bool useDoubleQ = true;
  124. #else
  125. bool useDoubleQ = false;
  126. #endif
  127. if(cmSystemTools::s_ForceUnixPaths)
  128. {
  129. useDoubleQ = false;
  130. }
  131. if(useDoubleQ)
  132. {
  133. std::string result;
  134. // if there are spaces
  135. std::string temp = str;
  136. if (temp.find(" ") != std::string::npos &&
  137. temp.find("\"")==std::string::npos)
  138. {
  139. result = "\"";
  140. result += str;
  141. result += "\"";
  142. return result;
  143. }
  144. return str;
  145. }
  146. else
  147. {
  148. std::string result = "";
  149. for(const char* ch = str; *ch != '\0'; ++ch)
  150. {
  151. if(*ch == ' ')
  152. {
  153. result += '\\';
  154. }
  155. result += *ch;
  156. }
  157. return result;
  158. }
  159. }
  160. std::string cmSystemTools::RemoveEscapes(const char* s)
  161. {
  162. std::string result = "";
  163. for(const char* ch = s; *ch; ++ch)
  164. {
  165. if(*ch == '\\' && *(ch+1) != ';')
  166. {
  167. ++ch;
  168. switch (*ch)
  169. {
  170. case '\\': result.insert(result.end(), '\\'); break;
  171. case '"': result.insert(result.end(), '"'); break;
  172. case ' ': result.insert(result.end(), ' '); break;
  173. case 't': result.insert(result.end(), '\t'); break;
  174. case 'n': result.insert(result.end(), '\n'); break;
  175. case 'r': result.insert(result.end(), '\r'); break;
  176. case '#': result.insert(result.end(), '#'); break;
  177. case '(': result.insert(result.end(), '('); break;
  178. case ')': result.insert(result.end(), ')'); break;
  179. case '0': result.insert(result.end(), '\0'); break;
  180. case '\0':
  181. {
  182. cmSystemTools::Error("Trailing backslash in argument:\n", s);
  183. return result;
  184. }
  185. default:
  186. {
  187. std::string chStr(1, *ch);
  188. cmSystemTools::Error("Invalid escape sequence \\", chStr.c_str(),
  189. "\nin argument ", s);
  190. }
  191. }
  192. }
  193. else
  194. {
  195. result.insert(result.end(), *ch);
  196. }
  197. }
  198. return result;
  199. }
  200. void cmSystemTools::Error(const char* m1, const char* m2,
  201. const char* m3, const char* m4)
  202. {
  203. std::string message = "CMake Error: ";
  204. if(m1)
  205. {
  206. message += m1;
  207. }
  208. if(m2)
  209. {
  210. message += m2;
  211. }
  212. if(m3)
  213. {
  214. message += m3;
  215. }
  216. if(m4)
  217. {
  218. message += m4;
  219. }
  220. cmSystemTools::s_ErrorOccured = true;
  221. cmSystemTools::Message(message.c_str(),"Error");
  222. }
  223. void cmSystemTools::SetErrorCallback(ErrorCallback f, void* clientData)
  224. {
  225. s_ErrorCallback = f;
  226. s_ErrorCallbackClientData = clientData;
  227. }
  228. void cmSystemTools::SetStdoutCallback(StdoutCallback f, void* clientData)
  229. {
  230. s_StdoutCallback = f;
  231. s_StdoutCallbackClientData = clientData;
  232. }
  233. void cmSystemTools::Stdout(const char* s)
  234. {
  235. if(s_StdoutCallback)
  236. {
  237. (*s_StdoutCallback)(s, strlen(s), s_StdoutCallbackClientData);
  238. }
  239. else
  240. {
  241. std::cout << s;
  242. std::cout.flush();
  243. }
  244. }
  245. void cmSystemTools::Stdout(const char* s, int length)
  246. {
  247. if(s_StdoutCallback)
  248. {
  249. (*s_StdoutCallback)(s, length, s_StdoutCallbackClientData);
  250. }
  251. else
  252. {
  253. std::cout.write(s, length);
  254. std::cout.flush();
  255. }
  256. }
  257. void cmSystemTools::Message(const char* m1, const char *title)
  258. {
  259. if(s_DisableMessages)
  260. {
  261. return;
  262. }
  263. if(s_ErrorCallback)
  264. {
  265. (*s_ErrorCallback)(m1, title, s_DisableMessages, s_ErrorCallbackClientData);
  266. return;
  267. }
  268. else
  269. {
  270. std::cerr << m1 << std::endl << std::flush;
  271. }
  272. }
  273. void cmSystemTools::ReportLastSystemError(const char* msg)
  274. {
  275. std::string m = msg;
  276. m += ": System Error: ";
  277. m += Superclass::GetLastSystemError();
  278. cmSystemTools::Error(m.c_str());
  279. }
  280. bool cmSystemTools::IsOn(const char* val)
  281. {
  282. if (!val)
  283. {
  284. return false;
  285. }
  286. std::basic_string<char> v = val;
  287. for(std::basic_string<char>::iterator c = v.begin();
  288. c != v.end(); c++)
  289. {
  290. *c = toupper(*c);
  291. }
  292. return (v == "ON" || v == "1" || v == "YES" || v == "TRUE" || v == "Y");
  293. }
  294. bool cmSystemTools::IsNOTFOUND(const char* val)
  295. {
  296. int len = strlen(val);
  297. const char* notfound = "-NOTFOUND";
  298. const int lenNotFound = 9;
  299. if(len < lenNotFound-1)
  300. {
  301. return false;
  302. }
  303. if(len == lenNotFound-1)
  304. {
  305. return ( strcmp(val, "NOTFOUND") == 0);
  306. }
  307. return ((strncmp((val + (len - lenNotFound)), notfound, lenNotFound) == 0));
  308. }
  309. bool cmSystemTools::IsOff(const char* val)
  310. {
  311. if (!val || strlen(val) == 0)
  312. {
  313. return true;
  314. }
  315. std::basic_string<char> v = val;
  316. for(std::basic_string<char>::iterator c = v.begin();
  317. c != v.end(); c++)
  318. {
  319. *c = toupper(*c);
  320. }
  321. return (v == "OFF" || v == "0" || v == "NO" || v == "FALSE" ||
  322. v == "N" || cmSystemTools::IsNOTFOUND(v.c_str()) || v == "IGNORE");
  323. }
  324. std::vector<cmStdString> cmSystemTools::ParseArguments(const char* command)
  325. {
  326. std::vector<cmStdString> args;
  327. std::string arg;
  328. bool win_path = false;
  329. if ( command[0] != '/' && command[1] == ':' && command[2] == '\\' ||
  330. command[0] == '\"' && command[1] != '/' && command[2] == ':' && command[3] == '\\' ||
  331. command[0] == '\'' && command[1] != '/' && command[2] == ':' && command[3] == '\\' ||
  332. command[0] == '\\' && command[1] == '\\')
  333. {
  334. win_path = true;
  335. }
  336. // Split the command into an argv array.
  337. for(const char* c = command; *c;)
  338. {
  339. // Skip over whitespace.
  340. while(*c == ' ' || *c == '\t')
  341. {
  342. ++c;
  343. }
  344. arg = "";
  345. if(*c == '"')
  346. {
  347. // Parse a quoted argument.
  348. ++c;
  349. while(*c && *c != '"')
  350. {
  351. arg.append(1, *c);
  352. ++c;
  353. }
  354. if(*c)
  355. {
  356. ++c;
  357. }
  358. args.push_back(arg);
  359. }
  360. else if(*c == '\'')
  361. {
  362. // Parse a quoted argument.
  363. ++c;
  364. while(*c && *c != '\'')
  365. {
  366. arg.append(1, *c);
  367. ++c;
  368. }
  369. if(*c)
  370. {
  371. ++c;
  372. }
  373. args.push_back(arg);
  374. }
  375. else if(*c)
  376. {
  377. // Parse an unquoted argument.
  378. while(*c && *c != ' ' && *c != '\t')
  379. {
  380. if(*c == '\\' && !win_path)
  381. {
  382. ++c;
  383. if(*c)
  384. {
  385. arg.append(1, *c);
  386. ++c;
  387. }
  388. }
  389. else
  390. {
  391. arg.append(1, *c);
  392. ++c;
  393. }
  394. }
  395. args.push_back(arg);
  396. }
  397. }
  398. return args;
  399. }
  400. bool cmSystemTools::RunSingleCommand(
  401. const char* command,
  402. std::string* output,
  403. int *retVal,
  404. const char* dir,
  405. bool verbose,
  406. double timeout)
  407. {
  408. if(s_DisableRunCommandOutput)
  409. {
  410. verbose = false;
  411. }
  412. std::vector<cmStdString> args = cmSystemTools::ParseArguments(command);
  413. if(args.size() < 1)
  414. {
  415. return false;
  416. }
  417. std::vector<const char*> argv;
  418. for(std::vector<cmStdString>::const_iterator a = args.begin();
  419. a != args.end(); ++a)
  420. {
  421. argv.push_back(a->c_str());
  422. }
  423. argv.push_back(0);
  424. if ( output )
  425. {
  426. *output = "";
  427. }
  428. cmsysProcess* cp = cmsysProcess_New();
  429. cmsysProcess_SetCommand(cp, &*argv.begin());
  430. cmsysProcess_SetWorkingDirectory(cp, dir);
  431. if(cmSystemTools::GetRunCommandHideConsole())
  432. {
  433. cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
  434. }
  435. cmsysProcess_SetTimeout(cp, timeout);
  436. cmsysProcess_Execute(cp);
  437. std::vector<char> tempOutput;
  438. char* data;
  439. int length;
  440. if ( output || verbose )
  441. {
  442. while(cmsysProcess_WaitForData(cp, &data, &length, 0))
  443. {
  444. if(output || verbose)
  445. {
  446. // Translate NULL characters in the output into valid text.
  447. // Visual Studio 7 puts these characters in the output of its
  448. // build process.
  449. for(int i=0; i < length; ++i)
  450. {
  451. if(data[i] == '\0')
  452. {
  453. data[i] = ' ';
  454. }
  455. }
  456. }
  457. if ( output )
  458. {
  459. tempOutput.insert(tempOutput.end(), data, data+length);
  460. }
  461. if(verbose)
  462. {
  463. cmSystemTools::Stdout(data, length);
  464. }
  465. }
  466. }
  467. cmsysProcess_WaitForExit(cp, 0);
  468. if ( output && tempOutput.begin() != tempOutput.end())
  469. {
  470. output->append(&*tempOutput.begin(), tempOutput.size());
  471. }
  472. bool result = true;
  473. if(cmsysProcess_GetState(cp) == cmsysProcess_State_Exited)
  474. {
  475. if ( retVal )
  476. {
  477. *retVal = cmsysProcess_GetExitValue(cp);
  478. }
  479. else
  480. {
  481. if ( cmsysProcess_GetExitValue(cp) != 0 )
  482. {
  483. result = false;
  484. }
  485. }
  486. }
  487. else if(cmsysProcess_GetState(cp) == cmsysProcess_State_Exception)
  488. {
  489. const char* exception_str = cmsysProcess_GetExceptionString(cp);
  490. if ( verbose )
  491. {
  492. std::cerr << exception_str << std::endl;
  493. }
  494. if ( output )
  495. {
  496. output->append(exception_str, strlen(exception_str));
  497. }
  498. result = false;
  499. }
  500. else if(cmsysProcess_GetState(cp) == cmsysProcess_State_Error)
  501. {
  502. const char* error_str = cmsysProcess_GetErrorString(cp);
  503. if ( verbose )
  504. {
  505. std::cerr << error_str << std::endl;
  506. }
  507. if ( output )
  508. {
  509. output->append(error_str, strlen(error_str));
  510. }
  511. result = false;
  512. }
  513. else if(cmsysProcess_GetState(cp) == cmsysProcess_State_Expired)
  514. {
  515. const char* error_str = "Process terminated due to timeout\n";
  516. if ( verbose )
  517. {
  518. std::cerr << error_str << std::endl;
  519. }
  520. if ( output )
  521. {
  522. output->append(error_str, strlen(error_str));
  523. }
  524. result = false;
  525. }
  526. cmsysProcess_Delete(cp);
  527. return result;
  528. }
  529. bool cmSystemTools::RunCommand(const char* command,
  530. std::string& output,
  531. const char* dir,
  532. bool verbose,
  533. int timeout)
  534. {
  535. int dummy;
  536. return cmSystemTools::RunCommand(command, output, dummy,
  537. dir, verbose, timeout);
  538. }
  539. #if defined(WIN32) && !defined(__CYGWIN__)
  540. #include "cmWin32ProcessExecution.h"
  541. // use this for shell commands like echo and dir
  542. bool RunCommandViaWin32(const char* command,
  543. const char* dir,
  544. std::string& output,
  545. int& retVal,
  546. bool verbose,
  547. int timeout)
  548. {
  549. #if defined(__BORLANDC__)
  550. return cmWin32ProcessExecution::BorlandRunCommand(command, dir, output,
  551. retVal,
  552. verbose, timeout,
  553. cmSystemTools::GetRunCommandHideConsole());
  554. #else // Visual studio
  555. ::SetLastError(ERROR_SUCCESS);
  556. if ( ! command )
  557. {
  558. cmSystemTools::Error("No command specified");
  559. return false;
  560. }
  561. cmWin32ProcessExecution resProc;
  562. if(cmSystemTools::GetRunCommandHideConsole())
  563. {
  564. resProc.SetHideWindows(true);
  565. }
  566. if ( cmSystemTools::GetWindows9xComspecSubstitute() )
  567. {
  568. resProc.SetConsoleSpawn(cmSystemTools::GetWindows9xComspecSubstitute() );
  569. }
  570. if ( !resProc.StartProcess(command, dir, verbose) )
  571. {
  572. output = resProc.GetOutput();
  573. if(verbose)
  574. {
  575. cmSystemTools::Stdout(output.c_str());
  576. }
  577. return false;
  578. }
  579. resProc.Wait(timeout);
  580. output = resProc.GetOutput();
  581. retVal = resProc.GetExitValue();
  582. return true;
  583. #endif
  584. }
  585. // use this for shell commands like echo and dir
  586. bool RunCommandViaSystem(const char* command,
  587. const char* dir,
  588. std::string& output,
  589. int& retVal,
  590. bool verbose)
  591. {
  592. std::cout << "@@ " << command << std::endl;
  593. std::string commandInDir;
  594. if(dir)
  595. {
  596. commandInDir = "cd ";
  597. commandInDir += cmSystemTools::ConvertToOutputPath(dir);
  598. commandInDir += " && ";
  599. commandInDir += command;
  600. }
  601. else
  602. {
  603. commandInDir = command;
  604. }
  605. command = commandInDir.c_str();
  606. std::string commandToFile = command;
  607. commandToFile += " > ";
  608. std::string tempFile;
  609. tempFile += _tempnam(0, "cmake");
  610. commandToFile += tempFile;
  611. retVal = system(commandToFile.c_str());
  612. std::ifstream fin(tempFile.c_str());
  613. if(!fin)
  614. {
  615. if(verbose)
  616. {
  617. std::string errormsg = "RunCommand produced no output: command: \"";
  618. errormsg += command;
  619. errormsg += "\"";
  620. errormsg += "\nOutput file: ";
  621. errormsg += tempFile;
  622. cmSystemTools::Error(errormsg.c_str());
  623. }
  624. fin.close();
  625. cmSystemTools::RemoveFile(tempFile.c_str());
  626. return false;
  627. }
  628. bool multiLine = false;
  629. std::string line;
  630. while(cmSystemTools::GetLineFromStream(fin, line))
  631. {
  632. output += line;
  633. if(multiLine)
  634. {
  635. output += "\n";
  636. }
  637. multiLine = true;
  638. }
  639. fin.close();
  640. cmSystemTools::RemoveFile(tempFile.c_str());
  641. return true;
  642. }
  643. #else // We have popen
  644. bool RunCommandViaPopen(const char* command,
  645. const char* dir,
  646. std::string& output,
  647. int& retVal,
  648. bool verbose,
  649. int /*timeout*/)
  650. {
  651. // if only popen worked on windows.....
  652. std::string commandInDir;
  653. if(dir)
  654. {
  655. commandInDir = "cd \"";
  656. commandInDir += dir;
  657. commandInDir += "\" && ";
  658. commandInDir += command;
  659. }
  660. else
  661. {
  662. commandInDir = command;
  663. }
  664. commandInDir += " 2>&1";
  665. command = commandInDir.c_str();
  666. const int BUFFER_SIZE = 4096;
  667. char buffer[BUFFER_SIZE];
  668. if(verbose)
  669. {
  670. cmSystemTools::Stdout("running ");
  671. cmSystemTools::Stdout(command);
  672. cmSystemTools::Stdout("\n");
  673. }
  674. fflush(stdout);
  675. fflush(stderr);
  676. FILE* cpipe = popen(command, "r");
  677. if(!cpipe)
  678. {
  679. return false;
  680. }
  681. fgets(buffer, BUFFER_SIZE, cpipe);
  682. while(!feof(cpipe))
  683. {
  684. if(verbose)
  685. {
  686. cmSystemTools::Stdout(buffer);
  687. }
  688. output += buffer;
  689. fgets(buffer, BUFFER_SIZE, cpipe);
  690. }
  691. retVal = pclose(cpipe);
  692. if (WIFEXITED(retVal))
  693. {
  694. retVal = WEXITSTATUS(retVal);
  695. return true;
  696. }
  697. if (WIFSIGNALED(retVal))
  698. {
  699. retVal = WTERMSIG(retVal);
  700. cmOStringStream error;
  701. error << "\nProcess terminated due to ";
  702. switch (retVal)
  703. {
  704. #ifdef SIGKILL
  705. case SIGKILL:
  706. error << "SIGKILL";
  707. break;
  708. #endif
  709. #ifdef SIGFPE
  710. case SIGFPE:
  711. error << "SIGFPE";
  712. break;
  713. #endif
  714. #ifdef SIGBUS
  715. case SIGBUS:
  716. error << "SIGBUS";
  717. break;
  718. #endif
  719. #ifdef SIGSEGV
  720. case SIGSEGV:
  721. error << "SIGSEGV";
  722. break;
  723. #endif
  724. default:
  725. error << "signal " << retVal;
  726. break;
  727. }
  728. output += error.str();
  729. }
  730. return false;
  731. }
  732. #endif // endif WIN32 not CYGWIN
  733. // run a command unix uses popen (easy)
  734. // windows uses system and ShortPath
  735. bool cmSystemTools::RunCommand(const char* command,
  736. std::string& output,
  737. int &retVal,
  738. const char* dir,
  739. bool verbose,
  740. int timeout)
  741. {
  742. if(s_DisableRunCommandOutput)
  743. {
  744. verbose = false;
  745. }
  746. #if defined(WIN32) && !defined(__CYGWIN__)
  747. // if the command does not start with a quote, then
  748. // try to find the program, and if the program can not be
  749. // found use system to run the command as it must be a built in
  750. // shell command like echo or dir
  751. int count = 0;
  752. if(command[0] == '\"')
  753. {
  754. // count the number of quotes
  755. for(const char* s = command; *s != 0; ++s)
  756. {
  757. if(*s == '\"')
  758. {
  759. count++;
  760. if(count > 2)
  761. {
  762. break;
  763. }
  764. }
  765. }
  766. // if there are more than two double quotes use
  767. // GetShortPathName, the cmd.exe program in windows which
  768. // is used by system fails to execute if there are more than
  769. // one set of quotes in the arguments
  770. if(count > 2)
  771. {
  772. cmsys::RegularExpression quoted("^\"([^\"]*)\"[ \t](.*)");
  773. if(quoted.find(command))
  774. {
  775. std::string shortCmd;
  776. std::string cmd = quoted.match(1);
  777. std::string args = quoted.match(2);
  778. if(! cmSystemTools::FileExists(cmd.c_str()) )
  779. {
  780. shortCmd = cmd;
  781. }
  782. else if(!cmSystemTools::GetShortPath(cmd.c_str(), shortCmd))
  783. {
  784. cmSystemTools::Error("GetShortPath failed for " , cmd.c_str());
  785. return false;
  786. }
  787. shortCmd += " ";
  788. shortCmd += args;
  789. //return RunCommandViaSystem(shortCmd.c_str(), dir,
  790. // output, retVal, verbose);
  791. //return WindowsRunCommand(shortCmd.c_str(), dir,
  792. //output, retVal, verbose);
  793. return RunCommandViaWin32(shortCmd.c_str(), dir,
  794. output, retVal, verbose, timeout);
  795. }
  796. else
  797. {
  798. cmSystemTools::Error("Could not parse command line with quotes ",
  799. command);
  800. }
  801. }
  802. }
  803. // if there is only one set of quotes or no quotes then just run the command
  804. //return RunCommandViaSystem(command, dir, output, retVal, verbose);
  805. //return WindowsRunCommand(command, dir, output, retVal, verbose);
  806. return ::RunCommandViaWin32(command, dir, output, retVal, verbose, timeout);
  807. #else
  808. return ::RunCommandViaPopen(command, dir, output, retVal, verbose, timeout);
  809. #endif
  810. }
  811. bool cmSystemTools::DoesFileExistWithExtensions(
  812. const char* name,
  813. const std::vector<std::string>& headerExts)
  814. {
  815. std::string hname;
  816. for( std::vector<std::string>::const_iterator ext = headerExts.begin();
  817. ext != headerExts.end(); ++ext )
  818. {
  819. hname = name;
  820. hname += ".";
  821. hname += *ext;
  822. if(cmSystemTools::FileExists(hname.c_str()))
  823. {
  824. return true;
  825. }
  826. }
  827. return false;
  828. }
  829. bool cmSystemTools::cmCopyFile(const char* source, const char* destination)
  830. {
  831. return Superclass::CopyFileAlways(source, destination);
  832. }
  833. bool cmSystemTools::CopyFileIfDifferent(const char* source,
  834. const char* destination)
  835. {
  836. return Superclass::CopyFileIfDifferent(source, destination);
  837. }
  838. void cmSystemTools::Glob(const char *directory, const char *regexp,
  839. std::vector<std::string>& files)
  840. {
  841. cmsys::Directory d;
  842. cmsys::RegularExpression reg(regexp);
  843. if (d.Load(directory))
  844. {
  845. size_t numf;
  846. unsigned int i;
  847. numf = d.GetNumberOfFiles();
  848. for (i = 0; i < numf; i++)
  849. {
  850. std::string fname = d.GetFile(i);
  851. if (reg.find(fname))
  852. {
  853. files.push_back(fname);
  854. }
  855. }
  856. }
  857. }
  858. void cmSystemTools::GlobDirs(const char *fullPath,
  859. std::vector<std::string>& files)
  860. {
  861. std::string path = fullPath;
  862. std::string::size_type pos = path.find("/*");
  863. if(pos == std::string::npos)
  864. {
  865. files.push_back(fullPath);
  866. return;
  867. }
  868. std::string startPath = path.substr(0, pos);
  869. std::string finishPath = path.substr(pos+2);
  870. cmsys::Directory d;
  871. if (d.Load(startPath.c_str()))
  872. {
  873. for (unsigned int i = 0; i < d.GetNumberOfFiles(); ++i)
  874. {
  875. if((std::string(d.GetFile(i)) != ".")
  876. && (std::string(d.GetFile(i)) != ".."))
  877. {
  878. std::string fname = startPath;
  879. fname +="/";
  880. fname += d.GetFile(i);
  881. if(cmSystemTools::FileIsDirectory(fname.c_str()))
  882. {
  883. fname += finishPath;
  884. cmSystemTools::GlobDirs(fname.c_str(), files);
  885. }
  886. }
  887. }
  888. }
  889. }
  890. void cmSystemTools::ExpandList(std::vector<std::string> const& arguments,
  891. std::vector<std::string>& newargs)
  892. {
  893. std::vector<std::string>::const_iterator i;
  894. for(i = arguments.begin();i != arguments.end(); ++i)
  895. {
  896. cmSystemTools::ExpandListArgument(*i, newargs);
  897. }
  898. }
  899. void cmSystemTools::ExpandListArgument(const std::string& arg,
  900. std::vector<std::string>& newargs)
  901. {
  902. // If argument is empty, it is an empty list.
  903. if(arg.length() == 0)
  904. {
  905. return;
  906. }
  907. // if there are no ; in the name then just copy the current string
  908. if(arg.find(';') == std::string::npos)
  909. {
  910. newargs.push_back(arg);
  911. return;
  912. }
  913. std::vector<char> newArgVec;
  914. // Break the string at non-escaped semicolons not nested in [].
  915. int squareNesting = 0;
  916. for(const char* c = arg.c_str(); *c; ++c)
  917. {
  918. switch(*c)
  919. {
  920. case '\\':
  921. {
  922. // We only want to allow escaping of semicolons. Other
  923. // escapes should not be processed here.
  924. ++c;
  925. if(*c == ';')
  926. {
  927. newArgVec.push_back(*c);
  928. }
  929. else
  930. {
  931. newArgVec.push_back('\\');
  932. if(*c)
  933. {
  934. newArgVec.push_back(*c);
  935. }
  936. }
  937. } break;
  938. case '[':
  939. {
  940. ++squareNesting;
  941. newArgVec.push_back(*c);
  942. } break;
  943. case ']':
  944. {
  945. --squareNesting;
  946. newArgVec.push_back(*c);
  947. } break;
  948. case ';':
  949. {
  950. // Break the string here if we are not nested inside square
  951. // brackets.
  952. if(squareNesting == 0)
  953. {
  954. if ( newArgVec.size() )
  955. {
  956. // Add the last argument if the string is not empty.
  957. newArgVec.push_back(0);
  958. newargs.push_back(&*newArgVec.begin());
  959. newArgVec.clear();
  960. }
  961. }
  962. else
  963. {
  964. newArgVec.push_back(*c);
  965. }
  966. } break;
  967. default:
  968. {
  969. // Just append this character.
  970. newArgVec.push_back(*c);
  971. } break;
  972. }
  973. }
  974. if ( newArgVec.size() )
  975. {
  976. // Add the last argument if the string is not empty.
  977. newArgVec.push_back(0);
  978. newargs.push_back(&*newArgVec.begin());
  979. }
  980. }
  981. bool cmSystemTools::SimpleGlob(const cmStdString& glob,
  982. std::vector<cmStdString>& files,
  983. int type /* = 0 */)
  984. {
  985. files.clear();
  986. if ( glob[glob.size()-1] != '*' )
  987. {
  988. return false;
  989. }
  990. std::string path = cmSystemTools::GetFilenamePath(glob);
  991. std::string ppath = cmSystemTools::GetFilenameName(glob);
  992. ppath = ppath.substr(0, ppath.size()-1);
  993. if ( path.size() == 0 )
  994. {
  995. path = "/";
  996. }
  997. bool res = false;
  998. cmsys::Directory d;
  999. if (d.Load(path.c_str()))
  1000. {
  1001. for (unsigned int i = 0; i < d.GetNumberOfFiles(); ++i)
  1002. {
  1003. if((std::string(d.GetFile(i)) != ".")
  1004. && (std::string(d.GetFile(i)) != ".."))
  1005. {
  1006. std::string fname = path;
  1007. if ( path[path.size()-1] != '/' )
  1008. {
  1009. fname +="/";
  1010. }
  1011. fname += d.GetFile(i);
  1012. std::string sfname = d.GetFile(i);
  1013. if ( type > 0 && cmSystemTools::FileIsDirectory(fname.c_str()) )
  1014. {
  1015. continue;
  1016. }
  1017. if ( type < 0 && !cmSystemTools::FileIsDirectory(fname.c_str()) )
  1018. {
  1019. continue;
  1020. }
  1021. if ( sfname.size() >= ppath.size() &&
  1022. sfname.substr(0, ppath.size()) ==
  1023. ppath )
  1024. {
  1025. files.push_back(fname);
  1026. res = true;
  1027. }
  1028. }
  1029. }
  1030. }
  1031. return res;
  1032. }
  1033. cmSystemTools::FileFormat cmSystemTools::GetFileFormat(const char* cext)
  1034. {
  1035. if ( ! cext || *cext == 0 )
  1036. {
  1037. return cmSystemTools::NO_FILE_FORMAT;
  1038. }
  1039. //std::string ext = cmSystemTools::LowerCase(cext);
  1040. std::string ext = cext;
  1041. if ( ext == "c" || ext == ".c" ) { return cmSystemTools::C_FILE_FORMAT; }
  1042. if (
  1043. ext == "C" || ext == ".C" ||
  1044. ext == "M" || ext == ".M" ||
  1045. ext == "c++" || ext == ".c++" ||
  1046. ext == "cc" || ext == ".cc" ||
  1047. ext == "cpp" || ext == ".cpp" ||
  1048. ext == "cxx" || ext == ".cxx" ||
  1049. ext == "m" || ext == ".m" ||
  1050. ext == "mm" || ext == ".mm"
  1051. ) { return cmSystemTools::CXX_FILE_FORMAT; }
  1052. if (
  1053. ext == "f" || ext == ".f" ||
  1054. ext == "F" || ext == ".F" ||
  1055. ext == "f77" || ext == ".f77" ||
  1056. ext == "f90" || ext == ".f90" ||
  1057. ext == "for" || ext == ".for" ||
  1058. ext == "f95" || ext == ".f95"
  1059. ) { return cmSystemTools::FORTRAN_FILE_FORMAT; }
  1060. if ( ext == "java" || ext == ".java" ) { return cmSystemTools::JAVA_FILE_FORMAT; }
  1061. if (
  1062. ext == "H" || ext == ".H" ||
  1063. ext == "h" || ext == ".h" ||
  1064. ext == "h++" || ext == ".h++" ||
  1065. ext == "hm" || ext == ".hm" ||
  1066. ext == "hpp" || ext == ".hpp" ||
  1067. ext == "hxx" || ext == ".hxx" ||
  1068. ext == "in" || ext == ".in" ||
  1069. ext == "txx" || ext == ".txx"
  1070. ) { return cmSystemTools::HEADER_FILE_FORMAT; }
  1071. if ( ext == "rc" || ext == ".rc" ) { return cmSystemTools::RESOURCE_FILE_FORMAT; }
  1072. if ( ext == "def" || ext == ".def" ) { return cmSystemTools::DEFINITION_FILE_FORMAT; }
  1073. if ( ext == "lib" || ext == ".lib" ||
  1074. ext == "a" || ext == ".a") { return cmSystemTools::STATIC_LIBRARY_FILE_FORMAT; }
  1075. if ( ext == "o" || ext == ".o" ||
  1076. ext == "obj" || ext == ".obj") { return cmSystemTools::OBJECT_FILE_FORMAT; }
  1077. #ifdef __APPLE__
  1078. if ( ext == "dylib" || ext == ".dylib" )
  1079. { return cmSystemTools::SHARED_LIBRARY_FILE_FORMAT; }
  1080. if ( ext == "so" || ext == ".so" ||
  1081. ext == "bundle" || ext == ".bundle" )
  1082. { return cmSystemTools::MODULE_FILE_FORMAT; }
  1083. #else // __APPLE__
  1084. if ( ext == "so" || ext == ".so" ||
  1085. ext == "sl" || ext == ".sl" ||
  1086. ext == "dll" || ext == ".dll" )
  1087. { return cmSystemTools::SHARED_LIBRARY_FILE_FORMAT; }
  1088. #endif // __APPLE__
  1089. return cmSystemTools::UNKNOWN_FILE_FORMAT;
  1090. }
  1091. bool cmSystemTools::Split(const char* s, std::vector<cmStdString>& l)
  1092. {
  1093. std::vector<std::string> temp;
  1094. bool res = Superclass::Split(s, temp);
  1095. for(std::vector<std::string>::const_iterator i = temp.begin();
  1096. i != temp.end(); ++i)
  1097. {
  1098. l.push_back(*i);
  1099. }
  1100. return res;
  1101. }
  1102. std::string cmSystemTools::ConvertToOutputPath(const char* path)
  1103. {
  1104. #if defined(_WIN32) && !defined(__CYGWIN__)
  1105. if(s_ForceUnixPaths)
  1106. {
  1107. return cmSystemTools::ConvertToUnixOutputPath(path);
  1108. }
  1109. return cmSystemTools::ConvertToWindowsOutputPath(path);
  1110. #else
  1111. return cmSystemTools::ConvertToUnixOutputPath(path);
  1112. #endif
  1113. }
  1114. std::string cmSystemTools::ConvertToRunCommandPath(const char* path)
  1115. {
  1116. #if defined(_WIN32) && !defined(__CYGWIN__)
  1117. return cmSystemTools::ConvertToWindowsOutputPath(path);
  1118. #else
  1119. return cmSystemTools::ConvertToUnixOutputPath(path);
  1120. #endif
  1121. }
  1122. bool cmSystemTools::StringEndsWith(const char* str1, const char* str2)
  1123. {
  1124. if ( !str1 || !str2 || strlen(str1) < strlen(str2) )
  1125. {
  1126. return 0;
  1127. }
  1128. return !strncmp(str1 + (strlen(str1)-strlen(str2)), str2, strlen(str2));
  1129. }
  1130. #if defined(_WIN32) && !defined(__CYGWIN__)
  1131. bool cmSystemTools::CreateSymlink(const char*, const char*)
  1132. {
  1133. // Should we create a copy here?
  1134. return false;
  1135. }
  1136. #else
  1137. bool cmSystemTools::CreateSymlink(const char* origName, const char* newName)
  1138. {
  1139. return (symlink(origName, newName) >= 0);
  1140. }
  1141. #endif
  1142. // compute the relative path from here to there
  1143. std::string cmSystemTools::RelativePath(const char* local, const char* remote)
  1144. {
  1145. if(!cmSystemTools::FileIsFullPath(local))
  1146. {
  1147. cmSystemTools::Error("RelativePath must be passed a full path to local: ", local);
  1148. }
  1149. if(!cmSystemTools::FileIsFullPath(remote))
  1150. {
  1151. cmSystemTools::Error("RelativePath must be passed a full path to remote: ", remote);
  1152. }
  1153. return cmsys::SystemTools::RelativePath(local, remote);
  1154. }
  1155. class cmDeletingCharVector : public std::vector<char*>
  1156. {
  1157. public:
  1158. ~cmDeletingCharVector()
  1159. {
  1160. for(std::vector<char*>::iterator i = this->begin();
  1161. i != this->end(); ++i)
  1162. {
  1163. delete []*i;
  1164. }
  1165. }
  1166. };
  1167. bool cmSystemTools::PutEnv(const char* value)
  1168. {
  1169. static cmDeletingCharVector localEnvironment;
  1170. char* envVar = new char[strlen(value)+1];
  1171. strcpy(envVar, value);
  1172. int ret = putenv(envVar);
  1173. // save the pointer in the static vector so that it can
  1174. // be deleted on exit
  1175. localEnvironment.push_back(envVar);
  1176. return ret == 0;
  1177. }
  1178. std::string cmSystemTools::MakeXMLSafe(const char* str)
  1179. {
  1180. std::vector<char> result;
  1181. result.reserve(500);
  1182. const char* pos = str;
  1183. for ( ;*pos; ++pos)
  1184. {
  1185. char ch = *pos;
  1186. if ( (ch > 126 || ch < 32) && ch != 9 && ch != 10 && ch != 13 && ch != '\r' )
  1187. {
  1188. char buffer[33];
  1189. sprintf(buffer, "&lt;%d&gt;", (int)ch);
  1190. //sprintf(buffer, "&#x%0x;", (unsigned int)ch);
  1191. result.insert(result.end(), buffer, buffer+strlen(buffer));
  1192. }
  1193. else
  1194. {
  1195. const char* const encodedChars[] = {
  1196. "&amp;",
  1197. "&lt;",
  1198. "&gt;"
  1199. };
  1200. switch ( ch )
  1201. {
  1202. case '&':
  1203. result.insert(result.end(), encodedChars[0], encodedChars[0]+5);
  1204. break;
  1205. case '<':
  1206. result.insert(result.end(), encodedChars[1], encodedChars[1]+4);
  1207. break;
  1208. case '>':
  1209. result.insert(result.end(), encodedChars[2], encodedChars[2]+4);
  1210. break;
  1211. case '\n':
  1212. result.push_back('\n');
  1213. break;
  1214. case '\r': break; // Ignore \r
  1215. default:
  1216. result.push_back(ch);
  1217. }
  1218. }
  1219. }
  1220. return std::string(&*result.begin(), result.size());
  1221. }
  1222. bool cmSystemTools::IsPathToFramework(const char* path)
  1223. {
  1224. if(cmSystemTools::FileIsFullPath(path))
  1225. {
  1226. std::string libname = path;
  1227. if(libname.find(".framework") == libname.size()+1-sizeof(".framework"))
  1228. {
  1229. return true;
  1230. }
  1231. }
  1232. return false;
  1233. }
  1234. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1235. struct cmSystemToolsGZStruct
  1236. {
  1237. gzFile GZFile;
  1238. };
  1239. extern "C" {
  1240. int cmSystemToolsGZStructOpen(void* call_data, const char *pathname, int oflags, mode_t mode);
  1241. int cmSystemToolsGZStructClose(void* call_data);
  1242. ssize_t cmSystemToolsGZStructRead(void* call_data, void* buf, size_t count);
  1243. ssize_t cmSystemToolsGZStructWrite(void* call_data, const void* buf, size_t count);
  1244. }
  1245. int cmSystemToolsGZStructOpen(void* call_data, const char *pathname, int oflags, mode_t mode)
  1246. {
  1247. const char *gzoflags;
  1248. int fd;
  1249. cmSystemToolsGZStruct* gzf = static_cast<cmSystemToolsGZStruct*>(call_data);
  1250. switch (oflags & O_ACCMODE)
  1251. {
  1252. case O_WRONLY:
  1253. gzoflags = "wb";
  1254. break;
  1255. case O_RDONLY:
  1256. gzoflags = "rb";
  1257. break;
  1258. default:
  1259. case O_RDWR:
  1260. errno = EINVAL;
  1261. return -1;
  1262. }
  1263. fd = open(pathname, oflags, mode);
  1264. if (fd == -1)
  1265. {
  1266. return -1;
  1267. }
  1268. #if !defined(_WIN32) || defined(__CYGWIN__)
  1269. if ((oflags & O_CREAT) && fchmod(fd, mode))
  1270. {
  1271. return -1;
  1272. }
  1273. #endif
  1274. gzf->GZFile = cm_zlib_gzdopen(fd, gzoflags);
  1275. if (!gzf->GZFile)
  1276. {
  1277. errno = ENOMEM;
  1278. return -1;
  1279. }
  1280. return fd;
  1281. }
  1282. int cmSystemToolsGZStructClose(void* call_data)
  1283. {
  1284. cmSystemToolsGZStruct* gzf = static_cast<cmSystemToolsGZStruct*>(call_data);
  1285. return cm_zlib_gzclose(gzf->GZFile);
  1286. }
  1287. ssize_t cmSystemToolsGZStructRead(void* call_data, void* buf, size_t count)
  1288. {
  1289. cmSystemToolsGZStruct* gzf = static_cast<cmSystemToolsGZStruct*>(call_data);
  1290. return cm_zlib_gzread(gzf->GZFile, buf, count);
  1291. }
  1292. ssize_t cmSystemToolsGZStructWrite(void* call_data, const void* buf, size_t count)
  1293. {
  1294. cmSystemToolsGZStruct* gzf = static_cast<cmSystemToolsGZStruct*>(call_data);
  1295. return cm_zlib_gzwrite(gzf->GZFile, (void*)buf, count);
  1296. }
  1297. #endif
  1298. bool cmSystemTools::CreateTar(const char* outFileName, const std::vector<cmStdString>& files, bool gzip, bool verbose)
  1299. {
  1300. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1301. TAR *t;
  1302. char buf[TAR_MAXPATHLEN];
  1303. char pathname[TAR_MAXPATHLEN];
  1304. cmSystemToolsGZStruct gzs;
  1305. tartype_t gztype = {
  1306. (openfunc_t)cmSystemToolsGZStructOpen,
  1307. (closefunc_t)cmSystemToolsGZStructClose,
  1308. (readfunc_t)cmSystemToolsGZStructRead,
  1309. (writefunc_t)cmSystemToolsGZStructWrite,
  1310. &gzs
  1311. };
  1312. // Ok, this libtar is not const safe. for now use auto_ptr hack
  1313. char* realName = new char[ strlen(outFileName) + 1 ];
  1314. std::auto_ptr<char> realNamePtr(realName);
  1315. strcpy(realName, outFileName);
  1316. if (tar_open(&t, realName,
  1317. (gzip? &gztype : NULL),
  1318. O_WRONLY | O_CREAT, 0644,
  1319. (verbose?TAR_VERBOSE:0)
  1320. | 0) == -1)
  1321. {
  1322. cmSystemTools::Error("Problem with tar_open(): ", strerror(errno));
  1323. return false;
  1324. }
  1325. std::vector<cmStdString>::const_iterator it;
  1326. for (it = files.begin(); it != files.end(); ++ it )
  1327. {
  1328. strncpy(pathname, it->c_str(), sizeof(pathname));
  1329. pathname[sizeof(pathname)-1] = 0;
  1330. strncpy(buf, pathname, sizeof(buf));
  1331. buf[sizeof(buf)-1] = 0;
  1332. if (tar_append_tree(t, buf, pathname) != 0)
  1333. {
  1334. cmOStringStream ostr;
  1335. ostr << "Problem with tar_append_tree(\"" << buf << "\", \"" << pathname << "\"): "
  1336. << strerror(errno);
  1337. cmSystemTools::Error(ostr.str().c_str());
  1338. tar_close(t);
  1339. return false;
  1340. }
  1341. }
  1342. if (tar_append_eof(t) != 0)
  1343. {
  1344. cmSystemTools::Error("Problem with tar_append_eof(): ", strerror(errno));
  1345. tar_close(t);
  1346. return false;
  1347. }
  1348. if (tar_close(t) != 0)
  1349. {
  1350. cmSystemTools::Error("Problem with tar_close(): ", strerror(errno));
  1351. return false;
  1352. }
  1353. return true;
  1354. #else
  1355. return false;
  1356. #endif
  1357. }
  1358. bool cmSystemTools::ExtractTar(const char* outFileName, const std::vector<cmStdString>& files, bool gzip, bool verbose)
  1359. {
  1360. (void)files;
  1361. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1362. TAR *t;
  1363. cmSystemToolsGZStruct gzs;
  1364. tartype_t gztype = {
  1365. cmSystemToolsGZStructOpen,
  1366. cmSystemToolsGZStructClose,
  1367. cmSystemToolsGZStructRead,
  1368. cmSystemToolsGZStructWrite,
  1369. &gzs
  1370. };
  1371. // Ok, this libtar is not const safe. for now use auto_ptr hack
  1372. char* realName = new char[ strlen(outFileName) + 1 ];
  1373. std::auto_ptr<char> realNamePtr(realName);
  1374. strcpy(realName, outFileName);
  1375. if (tar_open(&t, realName,
  1376. (gzip? &gztype : NULL),
  1377. O_RDONLY
  1378. #ifdef _WIN32
  1379. | O_BINARY
  1380. #endif
  1381. , 0,
  1382. (verbose?TAR_VERBOSE:0)
  1383. | 0) == -1)
  1384. {
  1385. cmSystemTools::Error("Problem with tar_open(): ", strerror(errno));
  1386. return false;
  1387. }
  1388. if (tar_extract_all(t, 0) != 0)
  1389. {
  1390. cmSystemTools::Error("Problem with tar_extract_all(): ", strerror(errno));
  1391. return false;
  1392. }
  1393. if (tar_close(t) != 0)
  1394. {
  1395. cmSystemTools::Error("Problem with tar_close(): ", strerror(errno));
  1396. return false;
  1397. }
  1398. return true;
  1399. #else
  1400. return false;
  1401. #endif
  1402. }
  1403. bool cmSystemTools::ListTar(const char* outFileName, std::vector<cmStdString>& files, bool gzip, bool verbose)
  1404. {
  1405. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1406. TAR *t;
  1407. cmSystemToolsGZStruct gzs;
  1408. tartype_t gztype = {
  1409. cmSystemToolsGZStructOpen,
  1410. cmSystemToolsGZStructClose,
  1411. cmSystemToolsGZStructRead,
  1412. cmSystemToolsGZStructWrite,
  1413. &gzs
  1414. };
  1415. // Ok, this libtar is not const safe. for now use auto_ptr hack
  1416. char* realName = new char[ strlen(outFileName) + 1 ];
  1417. std::auto_ptr<char> realNamePtr(realName);
  1418. strcpy(realName, outFileName);
  1419. if (tar_open(&t, realName,
  1420. (gzip? &gztype : NULL),
  1421. O_RDONLY
  1422. #ifdef _WIN32
  1423. | O_BINARY
  1424. #endif
  1425. , 0,
  1426. (verbose?TAR_VERBOSE:0)
  1427. | 0) == -1)
  1428. {
  1429. cmSystemTools::Error("Problem with tar_open(): ", strerror(errno));
  1430. return false;
  1431. }
  1432. while ((th_read(t)) == 0)
  1433. {
  1434. const char* filename = th_get_pathname(t);
  1435. files.push_back(filename);
  1436. if ( verbose )
  1437. {
  1438. th_print_long_ls(t);
  1439. }
  1440. else
  1441. {
  1442. std::cout << filename << std::endl;
  1443. }
  1444. #ifdef DEBUG
  1445. th_print(t);
  1446. #endif
  1447. if (TH_ISREG(t) && tar_skip_regfile(t) != 0)
  1448. {
  1449. cmSystemTools::Error("Problem with tar_skip_regfile(): ", strerror(errno));
  1450. return false;
  1451. }
  1452. }
  1453. if (tar_close(t) != 0)
  1454. {
  1455. cmSystemTools::Error("Problem with tar_close(): ", strerror(errno));
  1456. return false;
  1457. }
  1458. return true;
  1459. #else
  1460. return false;
  1461. #endif
  1462. }