cmSystemTools.cxx 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639
  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. (void)outFileName;
  1394. (void)files;
  1395. (void)gzip;
  1396. (void)verbose;
  1397. return false;
  1398. #endif
  1399. }
  1400. bool cmSystemTools::ExtractTar(const char* outFileName,
  1401. const std::vector<cmStdString>& files,
  1402. bool gzip, bool verbose)
  1403. {
  1404. (void)files;
  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. if (tar_extract_all(t, 0) != 0)
  1433. {
  1434. cmSystemTools::Error("Problem with tar_extract_all(): ", strerror(errno));
  1435. return false;
  1436. }
  1437. if (tar_close(t) != 0)
  1438. {
  1439. cmSystemTools::Error("Problem with tar_close(): ", strerror(errno));
  1440. return false;
  1441. }
  1442. return true;
  1443. #else
  1444. (void)outFileName;
  1445. (void)gzip;
  1446. (void)verbose;
  1447. return false;
  1448. #endif
  1449. }
  1450. bool cmSystemTools::ListTar(const char* outFileName,
  1451. std::vector<cmStdString>& files, bool gzip,
  1452. bool verbose)
  1453. {
  1454. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1455. TAR *t;
  1456. cmSystemToolsGZStruct gzs;
  1457. tartype_t gztype = {
  1458. cmSystemToolsGZStructOpen,
  1459. cmSystemToolsGZStructClose,
  1460. cmSystemToolsGZStructRead,
  1461. cmSystemToolsGZStructWrite,
  1462. &gzs
  1463. };
  1464. // Ok, this libtar is not const safe. for now use auto_ptr hack
  1465. char* realName = new char[ strlen(outFileName) + 1 ];
  1466. std::auto_ptr<char> realNamePtr(realName);
  1467. strcpy(realName, outFileName);
  1468. if (tar_open(&t, realName,
  1469. (gzip? &gztype : NULL),
  1470. O_RDONLY
  1471. #ifdef _WIN32
  1472. | O_BINARY
  1473. #endif
  1474. , 0,
  1475. (verbose?TAR_VERBOSE:0)
  1476. | 0) == -1)
  1477. {
  1478. cmSystemTools::Error("Problem with tar_open(): ", strerror(errno));
  1479. return false;
  1480. }
  1481. while ((th_read(t)) == 0)
  1482. {
  1483. const char* filename = th_get_pathname(t);
  1484. files.push_back(filename);
  1485. if ( verbose )
  1486. {
  1487. th_print_long_ls(t);
  1488. }
  1489. else
  1490. {
  1491. std::cout << filename << std::endl;
  1492. }
  1493. #ifdef DEBUG
  1494. th_print(t);
  1495. #endif
  1496. if (TH_ISREG(t) && tar_skip_regfile(t) != 0)
  1497. {
  1498. cmSystemTools::Error("Problem with tar_skip_regfile(): ",
  1499. strerror(errno));
  1500. return false;
  1501. }
  1502. }
  1503. if (tar_close(t) != 0)
  1504. {
  1505. cmSystemTools::Error("Problem with tar_close(): ", strerror(errno));
  1506. return false;
  1507. }
  1508. return true;
  1509. #else
  1510. (void)outFileName;
  1511. (void)files;
  1512. (void)gzip;
  1513. (void)verbose;
  1514. return false;
  1515. #endif
  1516. }