cmSystemTools.cxx 40 KB

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