cmSystemTools.cxx 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmSystemTools.h"
  11. #include <ctype.h>
  12. #include <errno.h>
  13. #include <time.h>
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #ifdef __QNX__
  17. # include <malloc.h> /* for malloc/free on QNX */
  18. #endif
  19. #include <cmsys/RegularExpression.hxx>
  20. #include <cmsys/Directory.hxx>
  21. #include <cmsys/System.h>
  22. #if defined(CMAKE_BUILD_WITH_CMAKE)
  23. # include <cmsys/Terminal.h>
  24. #endif
  25. #include <cmsys/stl/algorithm>
  26. #if defined(_WIN32)
  27. # include <windows.h>
  28. #else
  29. # include <sys/types.h>
  30. # include <unistd.h>
  31. # include <utime.h>
  32. # include <sys/wait.h>
  33. #endif
  34. #include <sys/stat.h>
  35. #if defined(_WIN32) && \
  36. (defined(_MSC_VER) || defined(__WATCOMC__) || \
  37. defined(__BORLANDC__) || defined(__MINGW32__))
  38. # include <io.h>
  39. #endif
  40. #if defined(CMAKE_BUILD_WITH_CMAKE)
  41. # include <libtar/libtar.h>
  42. # include <memory> // auto_ptr
  43. # include <fcntl.h>
  44. # include <cm_zlib.h>
  45. # include <cmsys/MD5.h>
  46. #endif
  47. #if defined(CMAKE_USE_ELF_PARSER)
  48. # include "cmELF.h"
  49. #endif
  50. class cmSystemToolsFileTime
  51. {
  52. public:
  53. #if defined(_WIN32) && !defined(__CYGWIN__)
  54. FILETIME timeCreation;
  55. FILETIME timeLastAccess;
  56. FILETIME timeLastWrite;
  57. #else
  58. struct utimbuf timeBuf;
  59. #endif
  60. };
  61. #if defined(__sgi) && !defined(__GNUC__)
  62. # pragma set woff 1375 /* base class destructor not virtual */
  63. #endif
  64. #if !defined(HAVE_ENVIRON_NOT_REQUIRE_PROTOTYPE)
  65. // For GetEnvironmentVariables
  66. # if defined(_WIN32)
  67. extern __declspec( dllimport ) char** environ;
  68. # else
  69. extern char** environ;
  70. # endif
  71. #endif
  72. #ifdef _WIN32
  73. class cmSystemToolsWindowsHandle
  74. {
  75. public:
  76. cmSystemToolsWindowsHandle(HANDLE h): handle_(h) {}
  77. ~cmSystemToolsWindowsHandle()
  78. {
  79. if(this->handle_ != INVALID_HANDLE_VALUE)
  80. {
  81. CloseHandle(this->handle_);
  82. }
  83. }
  84. operator bool() const { return this->handle_ != INVALID_HANDLE_VALUE; }
  85. bool operator !() const { return this->handle_ == INVALID_HANDLE_VALUE; }
  86. operator HANDLE() const { return this->handle_; }
  87. private:
  88. HANDLE handle_;
  89. };
  90. #endif
  91. bool cmSystemTools::s_RunCommandHideConsole = false;
  92. bool cmSystemTools::s_DisableRunCommandOutput = false;
  93. bool cmSystemTools::s_ErrorOccured = false;
  94. bool cmSystemTools::s_FatalErrorOccured = false;
  95. bool cmSystemTools::s_DisableMessages = false;
  96. bool cmSystemTools::s_ForceUnixPaths = false;
  97. std::string cmSystemTools::s_Windows9xComspecSubstitute = "command.com";
  98. void cmSystemTools::SetWindows9xComspecSubstitute(const char* str)
  99. {
  100. if ( str )
  101. {
  102. cmSystemTools::s_Windows9xComspecSubstitute = str;
  103. }
  104. }
  105. const char* cmSystemTools::GetWindows9xComspecSubstitute()
  106. {
  107. return cmSystemTools::s_Windows9xComspecSubstitute.c_str();
  108. }
  109. void (*cmSystemTools::s_ErrorCallback)(const char*, const char*,
  110. bool&, void*);
  111. void (*cmSystemTools::s_StdoutCallback)(const char*, int len, void*);
  112. void* cmSystemTools::s_ErrorCallbackClientData = 0;
  113. void* cmSystemTools::s_StdoutCallbackClientData = 0;
  114. // replace replace with with as many times as it shows up in source.
  115. // write the result into source.
  116. #if defined(_WIN32) && !defined(__CYGWIN__)
  117. void cmSystemTools::ExpandRegistryValues(std::string& source, KeyWOW64 view)
  118. {
  119. // Regular expression to match anything inside [...] that begins in HKEY.
  120. // Note that there is a special rule for regular expressions to match a
  121. // close square-bracket inside a list delimited by square brackets.
  122. // The "[^]]" part of this expression will match any character except
  123. // a close square-bracket. The ']' character must be the first in the
  124. // list of characters inside the [^...] block of the expression.
  125. cmsys::RegularExpression regEntry("\\[(HKEY[^]]*)\\]");
  126. // check for black line or comment
  127. while (regEntry.find(source))
  128. {
  129. // the arguments are the second match
  130. std::string key = regEntry.match(1);
  131. std::string val;
  132. if (ReadRegistryValue(key.c_str(), val, view))
  133. {
  134. std::string reg = "[";
  135. reg += key + "]";
  136. cmSystemTools::ReplaceString(source, reg.c_str(), val.c_str());
  137. }
  138. else
  139. {
  140. std::string reg = "[";
  141. reg += key + "]";
  142. cmSystemTools::ReplaceString(source, reg.c_str(), "/registry");
  143. }
  144. }
  145. }
  146. #else
  147. void cmSystemTools::ExpandRegistryValues(std::string& source, KeyWOW64)
  148. {
  149. cmsys::RegularExpression regEntry("\\[(HKEY[^]]*)\\]");
  150. while (regEntry.find(source))
  151. {
  152. // the arguments are the second match
  153. std::string key = regEntry.match(1);
  154. std::string val;
  155. std::string reg = "[";
  156. reg += key + "]";
  157. cmSystemTools::ReplaceString(source, reg.c_str(), "/registry");
  158. }
  159. }
  160. #endif
  161. std::string cmSystemTools::EscapeQuotes(const char* str)
  162. {
  163. std::string result = "";
  164. for(const char* ch = str; *ch != '\0'; ++ch)
  165. {
  166. if(*ch == '"')
  167. {
  168. result += '\\';
  169. }
  170. result += *ch;
  171. }
  172. return result;
  173. }
  174. std::string cmSystemTools::EscapeSpaces(const char* str)
  175. {
  176. #if defined(_WIN32) && !defined(__CYGWIN__)
  177. bool useDoubleQ = true;
  178. #else
  179. bool useDoubleQ = false;
  180. #endif
  181. if(cmSystemTools::s_ForceUnixPaths)
  182. {
  183. useDoubleQ = false;
  184. }
  185. if(useDoubleQ)
  186. {
  187. std::string result;
  188. // if there are spaces
  189. std::string temp = str;
  190. if (temp.find(" ") != std::string::npos &&
  191. temp.find("\"")==std::string::npos)
  192. {
  193. result = "\"";
  194. result += str;
  195. result += "\"";
  196. return result;
  197. }
  198. return str;
  199. }
  200. else
  201. {
  202. std::string result = "";
  203. for(const char* ch = str; *ch != '\0'; ++ch)
  204. {
  205. if(*ch == ' ')
  206. {
  207. result += '\\';
  208. }
  209. result += *ch;
  210. }
  211. return result;
  212. }
  213. }
  214. void cmSystemTools::Error(const char* m1, const char* m2,
  215. const char* m3, const char* m4)
  216. {
  217. std::string message = "CMake Error: ";
  218. if(m1)
  219. {
  220. message += m1;
  221. }
  222. if(m2)
  223. {
  224. message += m2;
  225. }
  226. if(m3)
  227. {
  228. message += m3;
  229. }
  230. if(m4)
  231. {
  232. message += m4;
  233. }
  234. cmSystemTools::s_ErrorOccured = true;
  235. cmSystemTools::Message(message.c_str(),"Error");
  236. }
  237. void cmSystemTools::SetErrorCallback(ErrorCallback f, void* clientData)
  238. {
  239. s_ErrorCallback = f;
  240. s_ErrorCallbackClientData = clientData;
  241. }
  242. void cmSystemTools::SetStdoutCallback(StdoutCallback f, void* clientData)
  243. {
  244. s_StdoutCallback = f;
  245. s_StdoutCallbackClientData = clientData;
  246. }
  247. void cmSystemTools::Stdout(const char* s)
  248. {
  249. if(s_StdoutCallback)
  250. {
  251. (*s_StdoutCallback)(s, static_cast<int>(strlen(s)),
  252. s_StdoutCallbackClientData);
  253. }
  254. else
  255. {
  256. std::cout << s;
  257. std::cout.flush();
  258. }
  259. }
  260. void cmSystemTools::Stdout(const char* s, int length)
  261. {
  262. if(s_StdoutCallback)
  263. {
  264. (*s_StdoutCallback)(s, length, s_StdoutCallbackClientData);
  265. }
  266. else
  267. {
  268. std::cout.write(s, length);
  269. std::cout.flush();
  270. }
  271. }
  272. void cmSystemTools::Message(const char* m1, const char *title)
  273. {
  274. if(s_DisableMessages)
  275. {
  276. return;
  277. }
  278. if(s_ErrorCallback)
  279. {
  280. (*s_ErrorCallback)(m1, title, s_DisableMessages,
  281. s_ErrorCallbackClientData);
  282. return;
  283. }
  284. else
  285. {
  286. std::cerr << m1 << std::endl << std::flush;
  287. }
  288. }
  289. void cmSystemTools::ReportLastSystemError(const char* msg)
  290. {
  291. std::string m = msg;
  292. m += ": System Error: ";
  293. m += Superclass::GetLastSystemError();
  294. cmSystemTools::Error(m.c_str());
  295. }
  296. bool cmSystemTools::IsOn(const char* val)
  297. {
  298. if (!val)
  299. {
  300. return false;
  301. }
  302. std::basic_string<char> v = val;
  303. for(std::basic_string<char>::iterator c = v.begin();
  304. c != v.end(); c++)
  305. {
  306. *c = toupper(*c);
  307. }
  308. return (v == "ON" || v == "1" || v == "YES" || v == "TRUE" || v == "Y");
  309. }
  310. bool cmSystemTools::IsNOTFOUND(const char* val)
  311. {
  312. size_t len = strlen(val);
  313. const char* notfound = "-NOTFOUND";
  314. const size_t lenNotFound = 9;
  315. if(len < lenNotFound-1)
  316. {
  317. return false;
  318. }
  319. if(len == lenNotFound-1)
  320. {
  321. return ( strcmp(val, "NOTFOUND") == 0);
  322. }
  323. return ((strncmp((val + (len - lenNotFound)), notfound, lenNotFound) == 0));
  324. }
  325. bool cmSystemTools::IsOff(const char* val)
  326. {
  327. if (!val || strlen(val) == 0)
  328. {
  329. return true;
  330. }
  331. std::basic_string<char> v = val;
  332. for(std::basic_string<char>::iterator c = v.begin();
  333. c != v.end(); c++)
  334. {
  335. *c = toupper(*c);
  336. }
  337. return (v == "OFF" || v == "0" || v == "NO" || v == "FALSE" ||
  338. v == "N" || cmSystemTools::IsNOTFOUND(v.c_str()) || v == "IGNORE");
  339. }
  340. //----------------------------------------------------------------------------
  341. void cmSystemTools::ParseWindowsCommandLine(const char* command,
  342. std::vector<std::string>& args)
  343. {
  344. // See the MSDN document "Parsing C Command-Line Arguments" at
  345. // http://msdn2.microsoft.com/en-us/library/a1y7w461.aspx for rules
  346. // of parsing the windows command line.
  347. bool in_argument = false;
  348. bool in_quotes = false;
  349. int backslashes = 0;
  350. std::string arg;
  351. for(const char* c = command;*c; ++c)
  352. {
  353. if(*c == '\\')
  354. {
  355. ++backslashes;
  356. in_argument = true;
  357. }
  358. else if(*c == '"')
  359. {
  360. int backslash_pairs = backslashes >> 1;
  361. int backslash_escaped = backslashes & 1;
  362. arg.append(backslash_pairs, '\\');
  363. backslashes = 0;
  364. if(backslash_escaped)
  365. {
  366. /* An odd number of backslashes precede this quote.
  367. It is escaped. */
  368. arg.append(1, '"');
  369. }
  370. else
  371. {
  372. /* An even number of backslashes precede this quote.
  373. It is not escaped. */
  374. in_quotes = !in_quotes;
  375. }
  376. in_argument = true;
  377. }
  378. else
  379. {
  380. arg.append(backslashes, '\\');
  381. backslashes = 0;
  382. if(isspace(*c))
  383. {
  384. if(in_quotes)
  385. {
  386. arg.append(1, *c);
  387. }
  388. else if(in_argument)
  389. {
  390. args.push_back(arg);
  391. arg = "";
  392. in_argument = false;
  393. }
  394. }
  395. else
  396. {
  397. in_argument = true;
  398. arg.append(1, *c);
  399. }
  400. }
  401. }
  402. arg.append(backslashes, '\\');
  403. if(in_argument)
  404. {
  405. args.push_back(arg);
  406. }
  407. }
  408. //----------------------------------------------------------------------------
  409. class cmSystemToolsArgV
  410. {
  411. char** ArgV;
  412. public:
  413. cmSystemToolsArgV(char** argv): ArgV(argv) {}
  414. ~cmSystemToolsArgV()
  415. {
  416. for(char** arg = this->ArgV; arg && *arg; ++arg)
  417. {
  418. free(*arg);
  419. }
  420. free(this->ArgV);
  421. }
  422. void Store(std::vector<std::string>& args) const
  423. {
  424. for(char** arg = this->ArgV; arg && *arg; ++arg)
  425. {
  426. args.push_back(*arg);
  427. }
  428. }
  429. };
  430. //----------------------------------------------------------------------------
  431. void cmSystemTools::ParseUnixCommandLine(const char* command,
  432. std::vector<std::string>& args)
  433. {
  434. // Invoke the underlying parser.
  435. cmSystemToolsArgV argv = cmsysSystem_Parse_CommandForUnix(command, 0);
  436. argv.Store(args);
  437. }
  438. std::string cmSystemTools::EscapeWindowsShellArgument(const char* arg,
  439. int shell_flags)
  440. {
  441. char local_buffer[1024];
  442. char* buffer = local_buffer;
  443. int size = cmsysSystem_Shell_GetArgumentSizeForWindows(arg, shell_flags);
  444. if(size > 1024)
  445. {
  446. buffer = new char[size];
  447. }
  448. cmsysSystem_Shell_GetArgumentForWindows(arg, buffer, shell_flags);
  449. std::string result(buffer);
  450. if(buffer != local_buffer)
  451. {
  452. delete [] buffer;
  453. }
  454. return result;
  455. }
  456. std::vector<cmStdString> cmSystemTools::ParseArguments(const char* command)
  457. {
  458. std::vector<cmStdString> args;
  459. std::string arg;
  460. bool win_path = false;
  461. if ((command[0] != '/' && command[1] == ':' && command[2] == '\\') ||
  462. (command[0] == '\"' && command[1] != '/' && command[2] == ':'
  463. && command[3] == '\\') ||
  464. (command[0] == '\'' && command[1] != '/' && command[2] == ':'
  465. && command[3] == '\\') ||
  466. (command[0] == '\\' && command[1] == '\\'))
  467. {
  468. win_path = true;
  469. }
  470. // Split the command into an argv array.
  471. for(const char* c = command; *c;)
  472. {
  473. // Skip over whitespace.
  474. while(*c == ' ' || *c == '\t')
  475. {
  476. ++c;
  477. }
  478. arg = "";
  479. if(*c == '"')
  480. {
  481. // Parse a quoted argument.
  482. ++c;
  483. while(*c && *c != '"')
  484. {
  485. arg.append(1, *c);
  486. ++c;
  487. }
  488. if(*c)
  489. {
  490. ++c;
  491. }
  492. args.push_back(arg);
  493. }
  494. else if(*c == '\'')
  495. {
  496. // Parse a quoted argument.
  497. ++c;
  498. while(*c && *c != '\'')
  499. {
  500. arg.append(1, *c);
  501. ++c;
  502. }
  503. if(*c)
  504. {
  505. ++c;
  506. }
  507. args.push_back(arg);
  508. }
  509. else if(*c)
  510. {
  511. // Parse an unquoted argument.
  512. while(*c && *c != ' ' && *c != '\t')
  513. {
  514. if(*c == '\\' && !win_path)
  515. {
  516. ++c;
  517. if(*c)
  518. {
  519. arg.append(1, *c);
  520. ++c;
  521. }
  522. }
  523. else
  524. {
  525. arg.append(1, *c);
  526. ++c;
  527. }
  528. }
  529. args.push_back(arg);
  530. }
  531. }
  532. return args;
  533. }
  534. bool cmSystemTools::RunSingleCommand(std::vector<cmStdString>const& command,
  535. std::string* output ,
  536. int* retVal , const char* dir ,
  537. bool verbose ,
  538. double timeout )
  539. {
  540. std::vector<const char*> argv;
  541. for(std::vector<cmStdString>::const_iterator a = command.begin();
  542. a != command.end(); ++a)
  543. {
  544. argv.push_back(a->c_str());
  545. }
  546. argv.push_back(0);
  547. if ( output )
  548. {
  549. *output = "";
  550. }
  551. cmsysProcess* cp = cmsysProcess_New();
  552. cmsysProcess_SetCommand(cp, &*argv.begin());
  553. cmsysProcess_SetWorkingDirectory(cp, dir);
  554. if(cmSystemTools::GetRunCommandHideConsole())
  555. {
  556. cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
  557. }
  558. cmsysProcess_SetTimeout(cp, timeout);
  559. cmsysProcess_Execute(cp);
  560. std::vector<char> tempOutput;
  561. char* data;
  562. int length;
  563. if ( output || verbose )
  564. {
  565. while(cmsysProcess_WaitForData(cp, &data, &length, 0))
  566. {
  567. if(output || verbose)
  568. {
  569. // Translate NULL characters in the output into valid text.
  570. // Visual Studio 7 puts these characters in the output of its
  571. // build process.
  572. for(int i=0; i < length; ++i)
  573. {
  574. if(data[i] == '\0')
  575. {
  576. data[i] = ' ';
  577. }
  578. }
  579. }
  580. if ( output )
  581. {
  582. tempOutput.insert(tempOutput.end(), data, data+length);
  583. }
  584. if(verbose)
  585. {
  586. cmSystemTools::Stdout(data, length);
  587. }
  588. }
  589. }
  590. cmsysProcess_WaitForExit(cp, 0);
  591. if ( output && tempOutput.begin() != tempOutput.end())
  592. {
  593. output->append(&*tempOutput.begin(), tempOutput.size());
  594. }
  595. bool result = true;
  596. if(cmsysProcess_GetState(cp) == cmsysProcess_State_Exited)
  597. {
  598. if ( retVal )
  599. {
  600. *retVal = cmsysProcess_GetExitValue(cp);
  601. }
  602. else
  603. {
  604. if ( cmsysProcess_GetExitValue(cp) != 0 )
  605. {
  606. result = false;
  607. }
  608. }
  609. }
  610. else if(cmsysProcess_GetState(cp) == cmsysProcess_State_Exception)
  611. {
  612. const char* exception_str = cmsysProcess_GetExceptionString(cp);
  613. if ( verbose )
  614. {
  615. std::cerr << exception_str << std::endl;
  616. }
  617. if ( output )
  618. {
  619. output->append(exception_str, strlen(exception_str));
  620. }
  621. result = false;
  622. }
  623. else if(cmsysProcess_GetState(cp) == cmsysProcess_State_Error)
  624. {
  625. const char* error_str = cmsysProcess_GetErrorString(cp);
  626. if ( verbose )
  627. {
  628. std::cerr << error_str << std::endl;
  629. }
  630. if ( output )
  631. {
  632. output->append(error_str, strlen(error_str));
  633. }
  634. result = false;
  635. }
  636. else if(cmsysProcess_GetState(cp) == cmsysProcess_State_Expired)
  637. {
  638. const char* error_str = "Process terminated due to timeout\n";
  639. if ( verbose )
  640. {
  641. std::cerr << error_str << std::endl;
  642. }
  643. if ( output )
  644. {
  645. output->append(error_str, strlen(error_str));
  646. }
  647. result = false;
  648. }
  649. cmsysProcess_Delete(cp);
  650. return result;
  651. }
  652. bool cmSystemTools::RunSingleCommand(
  653. const char* command,
  654. std::string* output,
  655. int *retVal,
  656. const char* dir,
  657. bool verbose,
  658. double timeout)
  659. {
  660. if(s_DisableRunCommandOutput)
  661. {
  662. verbose = false;
  663. }
  664. std::vector<cmStdString> args = cmSystemTools::ParseArguments(command);
  665. if(args.size() < 1)
  666. {
  667. return false;
  668. }
  669. return cmSystemTools::RunSingleCommand(args, output,retVal,
  670. dir, verbose, timeout);
  671. }
  672. bool cmSystemTools::RunCommand(const char* command,
  673. std::string& output,
  674. const char* dir,
  675. bool verbose,
  676. int timeout)
  677. {
  678. int dummy;
  679. return cmSystemTools::RunCommand(command, output, dummy,
  680. dir, verbose, timeout);
  681. }
  682. #if defined(WIN32) && !defined(__CYGWIN__)
  683. #include "cmWin32ProcessExecution.h"
  684. // use this for shell commands like echo and dir
  685. bool RunCommandViaWin32(const char* command,
  686. const char* dir,
  687. std::string& output,
  688. int& retVal,
  689. bool verbose,
  690. int timeout)
  691. {
  692. #if defined(__BORLANDC__)
  693. return
  694. cmWin32ProcessExecution::
  695. BorlandRunCommand(command, dir, output,
  696. retVal,
  697. verbose, timeout,
  698. cmSystemTools::GetRunCommandHideConsole());
  699. #else // Visual studio
  700. ::SetLastError(ERROR_SUCCESS);
  701. if ( ! command )
  702. {
  703. cmSystemTools::Error("No command specified");
  704. return false;
  705. }
  706. cmWin32ProcessExecution resProc;
  707. if(cmSystemTools::GetRunCommandHideConsole())
  708. {
  709. resProc.SetHideWindows(true);
  710. }
  711. if ( cmSystemTools::GetWindows9xComspecSubstitute() )
  712. {
  713. resProc.SetConsoleSpawn(cmSystemTools::GetWindows9xComspecSubstitute() );
  714. }
  715. if ( !resProc.StartProcess(command, dir, verbose) )
  716. {
  717. output = resProc.GetOutput();
  718. if(verbose)
  719. {
  720. cmSystemTools::Stdout(output.c_str());
  721. }
  722. return false;
  723. }
  724. resProc.Wait(timeout);
  725. output = resProc.GetOutput();
  726. retVal = resProc.GetExitValue();
  727. return true;
  728. #endif
  729. }
  730. // use this for shell commands like echo and dir
  731. bool RunCommandViaSystem(const char* command,
  732. const char* dir,
  733. std::string& output,
  734. int& retVal,
  735. bool verbose)
  736. {
  737. std::cout << "@@ " << command << std::endl;
  738. std::string commandInDir;
  739. if(dir)
  740. {
  741. commandInDir = "cd ";
  742. commandInDir += cmSystemTools::ConvertToOutputPath(dir);
  743. commandInDir += " && ";
  744. commandInDir += command;
  745. }
  746. else
  747. {
  748. commandInDir = command;
  749. }
  750. command = commandInDir.c_str();
  751. std::string commandToFile = command;
  752. commandToFile += " > ";
  753. std::string tempFile;
  754. tempFile += _tempnam(0, "cmake");
  755. commandToFile += tempFile;
  756. retVal = system(commandToFile.c_str());
  757. std::ifstream fin(tempFile.c_str());
  758. if(!fin)
  759. {
  760. if(verbose)
  761. {
  762. std::string errormsg = "RunCommand produced no output: command: \"";
  763. errormsg += command;
  764. errormsg += "\"";
  765. errormsg += "\nOutput file: ";
  766. errormsg += tempFile;
  767. cmSystemTools::Error(errormsg.c_str());
  768. }
  769. fin.close();
  770. cmSystemTools::RemoveFile(tempFile.c_str());
  771. return false;
  772. }
  773. bool multiLine = false;
  774. std::string line;
  775. while(cmSystemTools::GetLineFromStream(fin, line))
  776. {
  777. output += line;
  778. if(multiLine)
  779. {
  780. output += "\n";
  781. }
  782. multiLine = true;
  783. }
  784. fin.close();
  785. cmSystemTools::RemoveFile(tempFile.c_str());
  786. return true;
  787. }
  788. #else // We have popen
  789. // BeOS seems to return from a successful pclose() before the process has
  790. // legitimately exited, or at least before SIGCHLD is thrown...the signal may
  791. // come quite some time after pclose returns! This causes havoc with later
  792. // parts of CMake that expect to catch the signal from other child processes,
  793. // so we explicitly wait to catch it here. This should be safe to do with
  794. // popen() so long as we don't actually collect the zombie process ourselves.
  795. #ifdef __BEOS__
  796. #include <signal.h>
  797. #undef SIGBUS // this is the same as SIGSEGV on BeOS and causes issues below.
  798. static volatile bool beos_seen_signal = false;
  799. static void beos_popen_workaround(int sig)
  800. {
  801. beos_seen_signal = true;
  802. }
  803. #endif
  804. bool RunCommandViaPopen(const char* command,
  805. const char* dir,
  806. std::string& output,
  807. int& retVal,
  808. bool verbose,
  809. int /*timeout*/)
  810. {
  811. // if only popen worked on windows.....
  812. std::string commandInDir;
  813. if(dir)
  814. {
  815. commandInDir = "cd \"";
  816. commandInDir += dir;
  817. commandInDir += "\" && ";
  818. commandInDir += command;
  819. }
  820. else
  821. {
  822. commandInDir = command;
  823. }
  824. #ifndef __VMS
  825. commandInDir += " 2>&1";
  826. #endif
  827. command = commandInDir.c_str();
  828. const int BUFFER_SIZE = 4096;
  829. char buffer[BUFFER_SIZE];
  830. if(verbose)
  831. {
  832. cmSystemTools::Stdout("running ");
  833. cmSystemTools::Stdout(command);
  834. cmSystemTools::Stdout("\n");
  835. }
  836. fflush(stdout);
  837. fflush(stderr);
  838. #ifdef __BEOS__
  839. beos_seen_signal = false;
  840. signal(SIGCHLD, beos_popen_workaround);
  841. #endif
  842. FILE* cpipe = popen(command, "r");
  843. if(!cpipe)
  844. {
  845. #ifdef __BEOS__
  846. signal(SIGCHLD, SIG_DFL);
  847. #endif
  848. return false;
  849. }
  850. fgets(buffer, BUFFER_SIZE, cpipe);
  851. while(!feof(cpipe))
  852. {
  853. if(verbose)
  854. {
  855. cmSystemTools::Stdout(buffer);
  856. }
  857. output += buffer;
  858. buffer[0] = 0;
  859. fgets(buffer, BUFFER_SIZE, cpipe);
  860. }
  861. retVal = pclose(cpipe);
  862. #ifdef __BEOS__
  863. for (int i = 0; (!beos_seen_signal) && (i < 3); i++)
  864. {
  865. ::sleep(1); // signals should interrupt this...
  866. }
  867. if (!beos_seen_signal)
  868. {
  869. signal(SIGCHLD, SIG_DFL); // oh well, didn't happen. Go on anyhow.
  870. }
  871. #endif
  872. if (WIFEXITED(retVal))
  873. {
  874. retVal = WEXITSTATUS(retVal);
  875. return true;
  876. }
  877. if (WIFSIGNALED(retVal))
  878. {
  879. retVal = WTERMSIG(retVal);
  880. cmOStringStream error;
  881. error << "\nProcess terminated due to ";
  882. switch (retVal)
  883. {
  884. #ifdef SIGKILL
  885. case SIGKILL:
  886. error << "SIGKILL";
  887. break;
  888. #endif
  889. #ifdef SIGFPE
  890. case SIGFPE:
  891. error << "SIGFPE";
  892. break;
  893. #endif
  894. #ifndef __HAIKU__
  895. #ifdef SIGBUS
  896. case SIGBUS:
  897. error << "SIGBUS";
  898. break;
  899. #endif
  900. #endif
  901. #ifdef SIGSEGV
  902. case SIGSEGV:
  903. error << "SIGSEGV";
  904. break;
  905. #endif
  906. default:
  907. error << "signal " << retVal;
  908. break;
  909. }
  910. output += error.str();
  911. }
  912. return false;
  913. }
  914. #endif // endif WIN32 not CYGWIN
  915. // run a command unix uses popen (easy)
  916. // windows uses system and ShortPath
  917. bool cmSystemTools::RunCommand(const char* command,
  918. std::string& output,
  919. int &retVal,
  920. const char* dir,
  921. bool verbose,
  922. int timeout)
  923. {
  924. if(s_DisableRunCommandOutput)
  925. {
  926. verbose = false;
  927. }
  928. #if defined(WIN32) && !defined(__CYGWIN__)
  929. // if the command does not start with a quote, then
  930. // try to find the program, and if the program can not be
  931. // found use system to run the command as it must be a built in
  932. // shell command like echo or dir
  933. int count = 0;
  934. if(command[0] == '\"')
  935. {
  936. // count the number of quotes
  937. for(const char* s = command; *s != 0; ++s)
  938. {
  939. if(*s == '\"')
  940. {
  941. count++;
  942. if(count > 2)
  943. {
  944. break;
  945. }
  946. }
  947. }
  948. // if there are more than two double quotes use
  949. // GetShortPathName, the cmd.exe program in windows which
  950. // is used by system fails to execute if there are more than
  951. // one set of quotes in the arguments
  952. if(count > 2)
  953. {
  954. cmsys::RegularExpression quoted("^\"([^\"]*)\"[ \t](.*)");
  955. if(quoted.find(command))
  956. {
  957. std::string shortCmd;
  958. std::string cmd = quoted.match(1);
  959. std::string args = quoted.match(2);
  960. if(! cmSystemTools::FileExists(cmd.c_str()) )
  961. {
  962. shortCmd = cmd;
  963. }
  964. else if(!cmSystemTools::GetShortPath(cmd.c_str(), shortCmd))
  965. {
  966. cmSystemTools::Error("GetShortPath failed for " , cmd.c_str());
  967. return false;
  968. }
  969. shortCmd += " ";
  970. shortCmd += args;
  971. //return RunCommandViaSystem(shortCmd.c_str(), dir,
  972. // output, retVal, verbose);
  973. //return WindowsRunCommand(shortCmd.c_str(), dir,
  974. //output, retVal, verbose);
  975. return RunCommandViaWin32(shortCmd.c_str(), dir,
  976. output, retVal, verbose, timeout);
  977. }
  978. else
  979. {
  980. cmSystemTools::Error("Could not parse command line with quotes ",
  981. command);
  982. }
  983. }
  984. }
  985. // if there is only one set of quotes or no quotes then just run the command
  986. //return RunCommandViaSystem(command, dir, output, retVal, verbose);
  987. //return WindowsRunCommand(command, dir, output, retVal, verbose);
  988. return ::RunCommandViaWin32(command, dir, output, retVal, verbose, timeout);
  989. #else
  990. return ::RunCommandViaPopen(command, dir, output, retVal, verbose, timeout);
  991. #endif
  992. }
  993. bool cmSystemTools::DoesFileExistWithExtensions(
  994. const char* name,
  995. const std::vector<std::string>& headerExts)
  996. {
  997. std::string hname;
  998. for( std::vector<std::string>::const_iterator ext = headerExts.begin();
  999. ext != headerExts.end(); ++ext )
  1000. {
  1001. hname = name;
  1002. hname += ".";
  1003. hname += *ext;
  1004. if(cmSystemTools::FileExists(hname.c_str()))
  1005. {
  1006. return true;
  1007. }
  1008. }
  1009. return false;
  1010. }
  1011. std::string cmSystemTools::FileExistsInParentDirectories(const char* fname,
  1012. const char* directory, const char* toplevel)
  1013. {
  1014. std::string file = fname;
  1015. cmSystemTools::ConvertToUnixSlashes(file);
  1016. std::string dir = directory;
  1017. cmSystemTools::ConvertToUnixSlashes(dir);
  1018. std::string prevDir;
  1019. while(dir != prevDir)
  1020. {
  1021. std::string path = dir + "/" + file;
  1022. if ( cmSystemTools::FileExists(path.c_str()) )
  1023. {
  1024. return path;
  1025. }
  1026. if ( dir.size() < strlen(toplevel) )
  1027. {
  1028. break;
  1029. }
  1030. prevDir = dir;
  1031. dir = cmSystemTools::GetParentDirectory(dir.c_str());
  1032. }
  1033. return "";
  1034. }
  1035. bool cmSystemTools::cmCopyFile(const char* source, const char* destination)
  1036. {
  1037. return Superclass::CopyFileAlways(source, destination);
  1038. }
  1039. bool cmSystemTools::CopyFileIfDifferent(const char* source,
  1040. const char* destination)
  1041. {
  1042. return Superclass::CopyFileIfDifferent(source, destination);
  1043. }
  1044. //----------------------------------------------------------------------------
  1045. bool cmSystemTools::RenameFile(const char* oldname, const char* newname)
  1046. {
  1047. #ifdef _WIN32
  1048. /* On Windows the move functions will not replace existing files.
  1049. Check if the destination exists. */
  1050. struct stat newFile;
  1051. if(stat(newname, &newFile) == 0)
  1052. {
  1053. /* The destination exists. We have to replace it carefully. The
  1054. MoveFileEx function does what we need but is not available on
  1055. Win9x. */
  1056. OSVERSIONINFO osv;
  1057. DWORD attrs;
  1058. /* Make sure the destination is not read only. */
  1059. attrs = GetFileAttributes(newname);
  1060. if(attrs & FILE_ATTRIBUTE_READONLY)
  1061. {
  1062. SetFileAttributes(newname, attrs & ~FILE_ATTRIBUTE_READONLY);
  1063. }
  1064. /* Check the windows version number. */
  1065. osv.dwOSVersionInfoSize = sizeof(osv);
  1066. GetVersionEx(&osv);
  1067. if(osv.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
  1068. {
  1069. /* This is Win9x. There is no MoveFileEx implementation. We
  1070. cannot quite rename the file atomically. Just delete the
  1071. destination and then move the file. */
  1072. DeleteFile(newname);
  1073. return MoveFile(oldname, newname) != 0;
  1074. }
  1075. else
  1076. {
  1077. /* This is not Win9x. Use the MoveFileEx implementation. */
  1078. return MoveFileEx(oldname, newname, MOVEFILE_REPLACE_EXISTING) != 0;
  1079. }
  1080. }
  1081. else
  1082. {
  1083. /* The destination does not exist. Just move the file. */
  1084. return MoveFile(oldname, newname) != 0;
  1085. }
  1086. #else
  1087. /* On UNIX we have an OS-provided call to do this atomically. */
  1088. return rename(oldname, newname) == 0;
  1089. #endif
  1090. }
  1091. bool cmSystemTools::ComputeFileMD5(const char* source, char* md5out)
  1092. {
  1093. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1094. if(!cmSystemTools::FileExists(source))
  1095. {
  1096. return false;
  1097. }
  1098. // Open files
  1099. #if defined(_WIN32) || defined(__CYGWIN__)
  1100. cmsys_ios::ifstream fin(source, cmsys_ios::ios::binary | cmsys_ios::ios::in);
  1101. #else
  1102. cmsys_ios::ifstream fin(source);
  1103. #endif
  1104. if(!fin)
  1105. {
  1106. return false;
  1107. }
  1108. cmsysMD5* md5 = cmsysMD5_New();
  1109. cmsysMD5_Initialize(md5);
  1110. // Should be efficient enough on most system:
  1111. const int bufferSize = 4096;
  1112. char buffer[bufferSize];
  1113. // This copy loop is very sensitive on certain platforms with
  1114. // slightly broken stream libraries (like HPUX). Normally, it is
  1115. // incorrect to not check the error condition on the fin.read()
  1116. // before using the data, but the fin.gcount() will be zero if an
  1117. // error occurred. Therefore, the loop should be safe everywhere.
  1118. while(fin)
  1119. {
  1120. fin.read(buffer, bufferSize);
  1121. if(fin.gcount())
  1122. {
  1123. cmsysMD5_Append(md5, reinterpret_cast<unsigned char const*>(buffer),
  1124. fin.gcount());
  1125. }
  1126. }
  1127. cmsysMD5_FinalizeHex(md5, md5out);
  1128. cmsysMD5_Delete(md5);
  1129. fin.close();
  1130. return true;
  1131. #else
  1132. (void)source;
  1133. (void)md5out;
  1134. cmSystemTools::Message("md5sum not supported in bootstrapping mode","Error");
  1135. return false;
  1136. #endif
  1137. }
  1138. std::string cmSystemTools::ComputeStringMD5(const char* input)
  1139. {
  1140. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1141. char md5out[32];
  1142. cmsysMD5* md5 = cmsysMD5_New();
  1143. cmsysMD5_Initialize(md5);
  1144. cmsysMD5_Append(md5, reinterpret_cast<unsigned char const*>(input), -1);
  1145. cmsysMD5_FinalizeHex(md5, md5out);
  1146. cmsysMD5_Delete(md5);
  1147. return std::string(md5out, 32);
  1148. #else
  1149. (void)input;
  1150. cmSystemTools::Message("md5sum not supported in bootstrapping mode","Error");
  1151. return "";
  1152. #endif
  1153. }
  1154. void cmSystemTools::Glob(const char *directory, const char *regexp,
  1155. std::vector<std::string>& files)
  1156. {
  1157. cmsys::Directory d;
  1158. cmsys::RegularExpression reg(regexp);
  1159. if (d.Load(directory))
  1160. {
  1161. size_t numf;
  1162. unsigned int i;
  1163. numf = d.GetNumberOfFiles();
  1164. for (i = 0; i < numf; i++)
  1165. {
  1166. std::string fname = d.GetFile(i);
  1167. if (reg.find(fname))
  1168. {
  1169. files.push_back(fname);
  1170. }
  1171. }
  1172. }
  1173. }
  1174. void cmSystemTools::GlobDirs(const char *fullPath,
  1175. std::vector<std::string>& files)
  1176. {
  1177. std::string path = fullPath;
  1178. std::string::size_type pos = path.find("/*");
  1179. if(pos == std::string::npos)
  1180. {
  1181. files.push_back(fullPath);
  1182. return;
  1183. }
  1184. std::string startPath = path.substr(0, pos);
  1185. std::string finishPath = path.substr(pos+2);
  1186. cmsys::Directory d;
  1187. if (d.Load(startPath.c_str()))
  1188. {
  1189. for (unsigned int i = 0; i < d.GetNumberOfFiles(); ++i)
  1190. {
  1191. if((std::string(d.GetFile(i)) != ".")
  1192. && (std::string(d.GetFile(i)) != ".."))
  1193. {
  1194. std::string fname = startPath;
  1195. fname +="/";
  1196. fname += d.GetFile(i);
  1197. if(cmSystemTools::FileIsDirectory(fname.c_str()))
  1198. {
  1199. fname += finishPath;
  1200. cmSystemTools::GlobDirs(fname.c_str(), files);
  1201. }
  1202. }
  1203. }
  1204. }
  1205. }
  1206. void cmSystemTools::ExpandList(std::vector<std::string> const& arguments,
  1207. std::vector<std::string>& newargs)
  1208. {
  1209. std::vector<std::string>::const_iterator i;
  1210. for(i = arguments.begin();i != arguments.end(); ++i)
  1211. {
  1212. cmSystemTools::ExpandListArgument(*i, newargs);
  1213. }
  1214. }
  1215. void cmSystemTools::ExpandListArgument(const std::string& arg,
  1216. std::vector<std::string>& newargs,
  1217. bool emptyArgs)
  1218. {
  1219. // If argument is empty, it is an empty list.
  1220. if(arg.length() == 0 && !emptyArgs)
  1221. {
  1222. return;
  1223. }
  1224. // if there are no ; in the name then just copy the current string
  1225. if(arg.find(';') == std::string::npos)
  1226. {
  1227. newargs.push_back(arg);
  1228. return;
  1229. }
  1230. std::vector<char> newArgVec;
  1231. // Break the string at non-escaped semicolons not nested in [].
  1232. int squareNesting = 0;
  1233. for(const char* c = arg.c_str(); *c; ++c)
  1234. {
  1235. switch(*c)
  1236. {
  1237. case '\\':
  1238. {
  1239. // We only want to allow escaping of semicolons. Other
  1240. // escapes should not be processed here.
  1241. ++c;
  1242. if(*c == ';')
  1243. {
  1244. newArgVec.push_back(*c);
  1245. }
  1246. else
  1247. {
  1248. newArgVec.push_back('\\');
  1249. if(*c)
  1250. {
  1251. newArgVec.push_back(*c);
  1252. }
  1253. else
  1254. {
  1255. // Terminate the loop properly.
  1256. --c;
  1257. }
  1258. }
  1259. } break;
  1260. case '[':
  1261. {
  1262. ++squareNesting;
  1263. newArgVec.push_back(*c);
  1264. } break;
  1265. case ']':
  1266. {
  1267. --squareNesting;
  1268. newArgVec.push_back(*c);
  1269. } break;
  1270. case ';':
  1271. {
  1272. // Break the string here if we are not nested inside square
  1273. // brackets.
  1274. if(squareNesting == 0)
  1275. {
  1276. if ( newArgVec.size() || emptyArgs )
  1277. {
  1278. // Add the last argument if the string is not empty.
  1279. newArgVec.push_back(0);
  1280. newargs.push_back(&*newArgVec.begin());
  1281. newArgVec.clear();
  1282. }
  1283. }
  1284. else
  1285. {
  1286. newArgVec.push_back(*c);
  1287. }
  1288. } break;
  1289. default:
  1290. {
  1291. // Just append this character.
  1292. newArgVec.push_back(*c);
  1293. } break;
  1294. }
  1295. }
  1296. if ( newArgVec.size() || emptyArgs )
  1297. {
  1298. // Add the last argument if the string is not empty.
  1299. newArgVec.push_back(0);
  1300. newargs.push_back(&*newArgVec.begin());
  1301. }
  1302. }
  1303. bool cmSystemTools::SimpleGlob(const cmStdString& glob,
  1304. std::vector<cmStdString>& files,
  1305. int type /* = 0 */)
  1306. {
  1307. files.clear();
  1308. if ( glob[glob.size()-1] != '*' )
  1309. {
  1310. return false;
  1311. }
  1312. std::string path = cmSystemTools::GetFilenamePath(glob);
  1313. std::string ppath = cmSystemTools::GetFilenameName(glob);
  1314. ppath = ppath.substr(0, ppath.size()-1);
  1315. if ( path.size() == 0 )
  1316. {
  1317. path = "/";
  1318. }
  1319. bool res = false;
  1320. cmsys::Directory d;
  1321. if (d.Load(path.c_str()))
  1322. {
  1323. for (unsigned int i = 0; i < d.GetNumberOfFiles(); ++i)
  1324. {
  1325. if((std::string(d.GetFile(i)) != ".")
  1326. && (std::string(d.GetFile(i)) != ".."))
  1327. {
  1328. std::string fname = path;
  1329. if ( path[path.size()-1] != '/' )
  1330. {
  1331. fname +="/";
  1332. }
  1333. fname += d.GetFile(i);
  1334. std::string sfname = d.GetFile(i);
  1335. if ( type > 0 && cmSystemTools::FileIsDirectory(fname.c_str()) )
  1336. {
  1337. continue;
  1338. }
  1339. if ( type < 0 && !cmSystemTools::FileIsDirectory(fname.c_str()) )
  1340. {
  1341. continue;
  1342. }
  1343. if ( sfname.size() >= ppath.size() &&
  1344. sfname.substr(0, ppath.size()) ==
  1345. ppath )
  1346. {
  1347. files.push_back(fname);
  1348. res = true;
  1349. }
  1350. }
  1351. }
  1352. }
  1353. return res;
  1354. }
  1355. cmSystemTools::FileFormat cmSystemTools::GetFileFormat(const char* cext)
  1356. {
  1357. if ( ! cext || *cext == 0 )
  1358. {
  1359. return cmSystemTools::NO_FILE_FORMAT;
  1360. }
  1361. //std::string ext = cmSystemTools::LowerCase(cext);
  1362. std::string ext = cext;
  1363. if ( ext == "c" || ext == ".c" ||
  1364. ext == "m" || ext == ".m"
  1365. ) { return cmSystemTools::C_FILE_FORMAT; }
  1366. if (
  1367. ext == "C" || ext == ".C" ||
  1368. ext == "M" || ext == ".M" ||
  1369. ext == "c++" || ext == ".c++" ||
  1370. ext == "cc" || ext == ".cc" ||
  1371. ext == "cpp" || ext == ".cpp" ||
  1372. ext == "cxx" || ext == ".cxx" ||
  1373. ext == "mm" || ext == ".mm"
  1374. ) { return cmSystemTools::CXX_FILE_FORMAT; }
  1375. if (
  1376. ext == "f" || ext == ".f" ||
  1377. ext == "F" || ext == ".F" ||
  1378. ext == "f77" || ext == ".f77" ||
  1379. ext == "f90" || ext == ".f90" ||
  1380. ext == "for" || ext == ".for" ||
  1381. ext == "f95" || ext == ".f95"
  1382. ) { return cmSystemTools::FORTRAN_FILE_FORMAT; }
  1383. if ( ext == "java" || ext == ".java" )
  1384. { return cmSystemTools::JAVA_FILE_FORMAT; }
  1385. if (
  1386. ext == "H" || ext == ".H" ||
  1387. ext == "h" || ext == ".h" ||
  1388. ext == "h++" || ext == ".h++" ||
  1389. ext == "hm" || ext == ".hm" ||
  1390. ext == "hpp" || ext == ".hpp" ||
  1391. ext == "hxx" || ext == ".hxx" ||
  1392. ext == "in" || ext == ".in" ||
  1393. ext == "txx" || ext == ".txx"
  1394. ) { return cmSystemTools::HEADER_FILE_FORMAT; }
  1395. if ( ext == "rc" || ext == ".rc" )
  1396. { return cmSystemTools::RESOURCE_FILE_FORMAT; }
  1397. if ( ext == "def" || ext == ".def" )
  1398. { return cmSystemTools::DEFINITION_FILE_FORMAT; }
  1399. if ( ext == "lib" || ext == ".lib" ||
  1400. ext == "a" || ext == ".a")
  1401. { return cmSystemTools::STATIC_LIBRARY_FILE_FORMAT; }
  1402. if ( ext == "o" || ext == ".o" ||
  1403. ext == "obj" || ext == ".obj")
  1404. { return cmSystemTools::OBJECT_FILE_FORMAT; }
  1405. #ifdef __APPLE__
  1406. if ( ext == "dylib" || ext == ".dylib" )
  1407. { return cmSystemTools::SHARED_LIBRARY_FILE_FORMAT; }
  1408. if ( ext == "so" || ext == ".so" ||
  1409. ext == "bundle" || ext == ".bundle" )
  1410. { return cmSystemTools::MODULE_FILE_FORMAT; }
  1411. #else // __APPLE__
  1412. if ( ext == "so" || ext == ".so" ||
  1413. ext == "sl" || ext == ".sl" ||
  1414. ext == "dll" || ext == ".dll" )
  1415. { return cmSystemTools::SHARED_LIBRARY_FILE_FORMAT; }
  1416. #endif // __APPLE__
  1417. return cmSystemTools::UNKNOWN_FILE_FORMAT;
  1418. }
  1419. bool cmSystemTools::Split(const char* s, std::vector<cmStdString>& l)
  1420. {
  1421. std::vector<std::string> temp;
  1422. bool res = Superclass::Split(s, temp);
  1423. for(std::vector<std::string>::const_iterator i = temp.begin();
  1424. i != temp.end(); ++i)
  1425. {
  1426. l.push_back(*i);
  1427. }
  1428. return res;
  1429. }
  1430. std::string cmSystemTools::ConvertToOutputPath(const char* path)
  1431. {
  1432. #if defined(_WIN32) && !defined(__CYGWIN__)
  1433. if(s_ForceUnixPaths)
  1434. {
  1435. return cmSystemTools::ConvertToUnixOutputPath(path);
  1436. }
  1437. return cmSystemTools::ConvertToWindowsOutputPath(path);
  1438. #else
  1439. return cmSystemTools::ConvertToUnixOutputPath(path);
  1440. #endif
  1441. }
  1442. void cmSystemTools::ConvertToOutputSlashes(std::string& path)
  1443. {
  1444. #if defined(_WIN32) && !defined(__CYGWIN__)
  1445. if(!s_ForceUnixPaths)
  1446. {
  1447. // Convert to windows slashes.
  1448. std::string::size_type pos = 0;
  1449. while((pos = path.find('/', pos)) != std::string::npos)
  1450. {
  1451. path[pos++] = '\\';
  1452. }
  1453. }
  1454. #else
  1455. static_cast<void>(path);
  1456. #endif
  1457. }
  1458. std::string cmSystemTools::ConvertToRunCommandPath(const char* path)
  1459. {
  1460. #if defined(_WIN32) && !defined(__CYGWIN__)
  1461. return cmSystemTools::ConvertToWindowsOutputPath(path);
  1462. #else
  1463. return cmSystemTools::ConvertToUnixOutputPath(path);
  1464. #endif
  1465. }
  1466. bool cmSystemTools::StringEndsWith(const char* str1, const char* str2)
  1467. {
  1468. if ( !str1 || !str2 || strlen(str1) < strlen(str2) )
  1469. {
  1470. return 0;
  1471. }
  1472. return !strncmp(str1 + (strlen(str1)-strlen(str2)), str2, strlen(str2));
  1473. }
  1474. // compute the relative path from here to there
  1475. std::string cmSystemTools::RelativePath(const char* local, const char* remote)
  1476. {
  1477. if(!cmSystemTools::FileIsFullPath(local))
  1478. {
  1479. cmSystemTools::Error("RelativePath must be passed a full path to local: ",
  1480. local);
  1481. }
  1482. if(!cmSystemTools::FileIsFullPath(remote))
  1483. {
  1484. cmSystemTools::Error
  1485. ("RelativePath must be passed a full path to remote: ", remote);
  1486. }
  1487. return cmsys::SystemTools::RelativePath(local, remote);
  1488. }
  1489. #ifdef CMAKE_BUILD_WITH_CMAKE
  1490. //----------------------------------------------------------------------
  1491. bool cmSystemTools::UnsetEnv(const char* value)
  1492. {
  1493. #if !defined(HAVE_UNSETENV)
  1494. std::string var = value;
  1495. var += "=";
  1496. return cmSystemTools::PutEnv(var.c_str());
  1497. #else
  1498. unsetenv(value);
  1499. return true;
  1500. #endif
  1501. }
  1502. //----------------------------------------------------------------------
  1503. std::vector<std::string> cmSystemTools::GetEnvironmentVariables()
  1504. {
  1505. std::vector<std::string> env;
  1506. int cc;
  1507. for ( cc = 0; environ[cc]; ++ cc )
  1508. {
  1509. env.push_back(environ[cc]);
  1510. }
  1511. return env;
  1512. }
  1513. //----------------------------------------------------------------------
  1514. std::vector<std::string> cmSystemTools::AppendEnv(
  1515. std::vector<std::string>* env)
  1516. {
  1517. std::vector<std::string> origEnv = GetEnvironmentVariables();
  1518. if (env && env->size()>0)
  1519. {
  1520. std::vector<std::string>::const_iterator eit;
  1521. for (eit = env->begin(); eit!= env->end(); ++eit)
  1522. {
  1523. PutEnv(eit->c_str());
  1524. }
  1525. }
  1526. return origEnv;
  1527. }
  1528. //----------------------------------------------------------------------
  1529. void cmSystemTools::RestoreEnv(const std::vector<std::string>& env)
  1530. {
  1531. std::vector<std::string>::const_iterator eit;
  1532. // First clear everything in the current environment:
  1533. //
  1534. std::vector<std::string> currentEnv = GetEnvironmentVariables();
  1535. for (eit = currentEnv.begin(); eit!= currentEnv.end(); ++eit)
  1536. {
  1537. std::string var(*eit);
  1538. std::string::size_type pos = var.find("=");
  1539. if (pos != std::string::npos)
  1540. {
  1541. var = var.substr(0, pos);
  1542. }
  1543. UnsetEnv(var.c_str());
  1544. }
  1545. // Then put back each entry from the original environment:
  1546. //
  1547. for (eit = env.begin(); eit!= env.end(); ++eit)
  1548. {
  1549. PutEnv(eit->c_str());
  1550. }
  1551. }
  1552. #endif
  1553. void cmSystemTools::EnableVSConsoleOutput()
  1554. {
  1555. // Visual Studio 8 2005 (devenv.exe or VCExpress.exe) will not
  1556. // display output to the console unless this environment variable is
  1557. // set. We need it to capture the output of these build tools.
  1558. // Note for future work that one could pass "/out \\.\pipe\NAME" to
  1559. // either of these executables where NAME is created with
  1560. // CreateNamedPipe. This would bypass the internal buffering of the
  1561. // output and allow it to be captured on the fly.
  1562. #ifdef _WIN32
  1563. cmSystemTools::PutEnv("vsconsoleoutput=1");
  1564. #endif
  1565. }
  1566. bool cmSystemTools::IsPathToFramework(const char* path)
  1567. {
  1568. if(cmSystemTools::FileIsFullPath(path))
  1569. {
  1570. std::string libname = path;
  1571. if(libname.find(".framework") == libname.size()+1-sizeof(".framework"))
  1572. {
  1573. return true;
  1574. }
  1575. }
  1576. return false;
  1577. }
  1578. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1579. struct cmSystemToolsGZStruct
  1580. {
  1581. gzFile GZFile;
  1582. };
  1583. extern "C" {
  1584. int cmSystemToolsGZStructOpen(void* call_data, const char *pathname,
  1585. int oflags, mode_t mode);
  1586. int cmSystemToolsGZStructClose(void* call_data);
  1587. ssize_t cmSystemToolsGZStructRead(void* call_data, void* buf, size_t count);
  1588. ssize_t cmSystemToolsGZStructWrite(void* call_data, const void* buf,
  1589. size_t count);
  1590. }
  1591. int cmSystemToolsGZStructOpen(void* call_data, const char *pathname,
  1592. int oflags, mode_t mode)
  1593. {
  1594. const char *gzoflags;
  1595. int fd;
  1596. cmSystemToolsGZStruct* gzf = static_cast<cmSystemToolsGZStruct*>(call_data);
  1597. switch (oflags & O_ACCMODE)
  1598. {
  1599. case O_WRONLY:
  1600. gzoflags = "wb";
  1601. break;
  1602. case O_RDONLY:
  1603. gzoflags = "rb";
  1604. break;
  1605. default:
  1606. case O_RDWR:
  1607. errno = EINVAL;
  1608. return -1;
  1609. }
  1610. fd = open(pathname, oflags, mode);
  1611. if (fd == -1)
  1612. {
  1613. return -1;
  1614. }
  1615. // no fchmod on BeOS 5...do pathname instead.
  1616. #if defined(__BEOS__) && !defined(__ZETA__) && !defined(__HAIKU__)
  1617. if ((oflags & O_CREAT) && chmod(pathname, mode))
  1618. {
  1619. return -1;
  1620. }
  1621. #elif !defined(_WIN32) || defined(__CYGWIN__)
  1622. if ((oflags & O_CREAT) && fchmod(fd, mode))
  1623. {
  1624. return -1;
  1625. }
  1626. #endif
  1627. gzf->GZFile = gzdopen(fd, gzoflags);
  1628. if (!gzf->GZFile)
  1629. {
  1630. errno = ENOMEM;
  1631. return -1;
  1632. }
  1633. return fd;
  1634. }
  1635. int cmSystemToolsGZStructClose(void* call_data)
  1636. {
  1637. cmSystemToolsGZStruct* gzf = static_cast<cmSystemToolsGZStruct*>(call_data);
  1638. return gzclose(gzf->GZFile);
  1639. }
  1640. ssize_t cmSystemToolsGZStructRead(void* call_data, void* buf, size_t count)
  1641. {
  1642. cmSystemToolsGZStruct* gzf = static_cast<cmSystemToolsGZStruct*>(call_data);
  1643. return gzread(gzf->GZFile, buf, static_cast<int>(count));
  1644. }
  1645. ssize_t cmSystemToolsGZStructWrite(void* call_data, const void* buf,
  1646. size_t count)
  1647. {
  1648. cmSystemToolsGZStruct* gzf = static_cast<cmSystemToolsGZStruct*>(call_data);
  1649. return gzwrite(gzf->GZFile, (void*)buf, static_cast<int>(count));
  1650. }
  1651. #endif
  1652. bool cmSystemTools::CreateTar(const char* outFileName,
  1653. const std::vector<cmStdString>& files,
  1654. bool gzip, bool verbose)
  1655. {
  1656. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1657. TAR *t;
  1658. char buf[TAR_MAXPATHLEN];
  1659. char pathname[TAR_MAXPATHLEN];
  1660. cmSystemToolsGZStruct gzs;
  1661. tartype_t gztype = {
  1662. (openfunc_t)cmSystemToolsGZStructOpen,
  1663. (closefunc_t)cmSystemToolsGZStructClose,
  1664. (readfunc_t)cmSystemToolsGZStructRead,
  1665. (writefunc_t)cmSystemToolsGZStructWrite,
  1666. &gzs
  1667. };
  1668. // Ok, this libtar is not const safe. for now use auto_ptr hack
  1669. char* realName = new char[ strlen(outFileName) + 1 ];
  1670. std::auto_ptr<char> realNamePtr(realName);
  1671. strcpy(realName, outFileName);
  1672. int options = 0;
  1673. if(verbose)
  1674. {
  1675. options |= TAR_VERBOSE;
  1676. }
  1677. #ifdef __CYGWIN__
  1678. options |= TAR_GNU;
  1679. #endif
  1680. if (tar_open(&t, realName,
  1681. (gzip? &gztype : NULL),
  1682. O_WRONLY | O_CREAT, 0644,
  1683. options) == -1)
  1684. {
  1685. cmSystemTools::Error("Problem with tar_open(): ", strerror(errno));
  1686. return false;
  1687. }
  1688. std::vector<cmStdString>::const_iterator it;
  1689. for (it = files.begin(); it != files.end(); ++ it )
  1690. {
  1691. strncpy(pathname, it->c_str(), sizeof(pathname));
  1692. pathname[sizeof(pathname)-1] = 0;
  1693. strncpy(buf, pathname, sizeof(buf));
  1694. buf[sizeof(buf)-1] = 0;
  1695. if (tar_append_tree(t, buf, pathname) != 0)
  1696. {
  1697. cmOStringStream ostr;
  1698. ostr << "Problem with tar_append_tree(\"" << buf << "\", \""
  1699. << pathname << "\"): "
  1700. << strerror(errno);
  1701. cmSystemTools::Error(ostr.str().c_str());
  1702. tar_close(t);
  1703. return false;
  1704. }
  1705. }
  1706. if (tar_append_eof(t) != 0)
  1707. {
  1708. cmSystemTools::Error("Problem with tar_append_eof(): ", strerror(errno));
  1709. tar_close(t);
  1710. return false;
  1711. }
  1712. if (tar_close(t) != 0)
  1713. {
  1714. cmSystemTools::Error("Problem with tar_close(): ", strerror(errno));
  1715. return false;
  1716. }
  1717. return true;
  1718. #else
  1719. (void)outFileName;
  1720. (void)files;
  1721. (void)gzip;
  1722. (void)verbose;
  1723. return false;
  1724. #endif
  1725. }
  1726. bool cmSystemTools::ExtractTar(const char* outFileName,
  1727. const std::vector<cmStdString>& files,
  1728. bool gzip, bool verbose)
  1729. {
  1730. (void)files;
  1731. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1732. TAR *t;
  1733. cmSystemToolsGZStruct gzs;
  1734. tartype_t gztype = {
  1735. cmSystemToolsGZStructOpen,
  1736. cmSystemToolsGZStructClose,
  1737. cmSystemToolsGZStructRead,
  1738. cmSystemToolsGZStructWrite,
  1739. &gzs
  1740. };
  1741. // Ok, this libtar is not const safe. for now use auto_ptr hack
  1742. char* realName = new char[ strlen(outFileName) + 1 ];
  1743. std::auto_ptr<char> realNamePtr(realName);
  1744. strcpy(realName, outFileName);
  1745. if (tar_open(&t, realName,
  1746. (gzip? &gztype : NULL),
  1747. O_RDONLY
  1748. #ifdef _WIN32
  1749. | O_BINARY
  1750. #endif
  1751. , 0,
  1752. (verbose?TAR_VERBOSE:0)
  1753. | 0) == -1)
  1754. {
  1755. cmSystemTools::Error("Problem with tar_open(): ", strerror(errno));
  1756. return false;
  1757. }
  1758. if (tar_extract_all(t, 0) != 0)
  1759. {
  1760. cmSystemTools::Error("Problem with tar_extract_all(): ", strerror(errno));
  1761. return false;
  1762. }
  1763. if (tar_close(t) != 0)
  1764. {
  1765. cmSystemTools::Error("Problem with tar_close(): ", strerror(errno));
  1766. return false;
  1767. }
  1768. return true;
  1769. #else
  1770. (void)outFileName;
  1771. (void)gzip;
  1772. (void)verbose;
  1773. return false;
  1774. #endif
  1775. }
  1776. bool cmSystemTools::ListTar(const char* outFileName,
  1777. std::vector<cmStdString>& files, bool gzip,
  1778. bool verbose)
  1779. {
  1780. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1781. TAR *t;
  1782. cmSystemToolsGZStruct gzs;
  1783. tartype_t gztype = {
  1784. cmSystemToolsGZStructOpen,
  1785. cmSystemToolsGZStructClose,
  1786. cmSystemToolsGZStructRead,
  1787. cmSystemToolsGZStructWrite,
  1788. &gzs
  1789. };
  1790. // Ok, this libtar is not const safe. for now use auto_ptr hack
  1791. char* realName = new char[ strlen(outFileName) + 1 ];
  1792. std::auto_ptr<char> realNamePtr(realName);
  1793. strcpy(realName, outFileName);
  1794. if (tar_open(&t, realName,
  1795. (gzip? &gztype : NULL),
  1796. O_RDONLY
  1797. #ifdef _WIN32
  1798. | O_BINARY
  1799. #endif
  1800. , 0,
  1801. (verbose?TAR_VERBOSE:0)
  1802. | 0) == -1)
  1803. {
  1804. cmSystemTools::Error("Problem with tar_open(): ", strerror(errno));
  1805. return false;
  1806. }
  1807. while ((th_read(t)) == 0)
  1808. {
  1809. const char* filename = th_get_pathname(t);
  1810. files.push_back(filename);
  1811. if ( verbose )
  1812. {
  1813. th_print_long_ls(t);
  1814. }
  1815. else
  1816. {
  1817. std::cout << filename << std::endl;
  1818. }
  1819. #ifdef DEBUG
  1820. th_print(t);
  1821. #endif
  1822. if (TH_ISREG(t) && tar_skip_regfile(t) != 0)
  1823. {
  1824. cmSystemTools::Error("Problem with tar_skip_regfile(): ",
  1825. strerror(errno));
  1826. return false;
  1827. }
  1828. }
  1829. if (tar_close(t) != 0)
  1830. {
  1831. cmSystemTools::Error("Problem with tar_close(): ", strerror(errno));
  1832. return false;
  1833. }
  1834. return true;
  1835. #else
  1836. (void)outFileName;
  1837. (void)files;
  1838. (void)gzip;
  1839. (void)verbose;
  1840. return false;
  1841. #endif
  1842. }
  1843. int cmSystemTools::WaitForLine(cmsysProcess* process, std::string& line,
  1844. double timeout,
  1845. std::vector<char>& out,
  1846. std::vector<char>& err)
  1847. {
  1848. line = "";
  1849. std::vector<char>::iterator outiter = out.begin();
  1850. std::vector<char>::iterator erriter = err.begin();
  1851. while(1)
  1852. {
  1853. // Check for a newline in stdout.
  1854. for(;outiter != out.end(); ++outiter)
  1855. {
  1856. if((*outiter == '\r') && ((outiter+1) == out.end()))
  1857. {
  1858. break;
  1859. }
  1860. else if(*outiter == '\n' || *outiter == '\0')
  1861. {
  1862. int length = outiter-out.begin();
  1863. if(length > 1 && *(outiter-1) == '\r')
  1864. {
  1865. --length;
  1866. }
  1867. if(length > 0)
  1868. {
  1869. line.append(&out[0], length);
  1870. }
  1871. out.erase(out.begin(), outiter+1);
  1872. return cmsysProcess_Pipe_STDOUT;
  1873. }
  1874. }
  1875. // Check for a newline in stderr.
  1876. for(;erriter != err.end(); ++erriter)
  1877. {
  1878. if((*erriter == '\r') && ((erriter+1) == err.end()))
  1879. {
  1880. break;
  1881. }
  1882. else if(*erriter == '\n' || *erriter == '\0')
  1883. {
  1884. int length = erriter-err.begin();
  1885. if(length > 1 && *(erriter-1) == '\r')
  1886. {
  1887. --length;
  1888. }
  1889. if(length > 0)
  1890. {
  1891. line.append(&err[0], length);
  1892. }
  1893. err.erase(err.begin(), erriter+1);
  1894. return cmsysProcess_Pipe_STDERR;
  1895. }
  1896. }
  1897. // No newlines found. Wait for more data from the process.
  1898. int length;
  1899. char* data;
  1900. int pipe = cmsysProcess_WaitForData(process, &data, &length, &timeout);
  1901. if(pipe == cmsysProcess_Pipe_Timeout)
  1902. {
  1903. // Timeout has been exceeded.
  1904. return pipe;
  1905. }
  1906. else if(pipe == cmsysProcess_Pipe_STDOUT)
  1907. {
  1908. // Append to the stdout buffer.
  1909. std::vector<char>::size_type size = out.size();
  1910. out.insert(out.end(), data, data+length);
  1911. outiter = out.begin()+size;
  1912. }
  1913. else if(pipe == cmsysProcess_Pipe_STDERR)
  1914. {
  1915. // Append to the stderr buffer.
  1916. std::vector<char>::size_type size = err.size();
  1917. err.insert(err.end(), data, data+length);
  1918. erriter = err.begin()+size;
  1919. }
  1920. else if(pipe == cmsysProcess_Pipe_None)
  1921. {
  1922. // Both stdout and stderr pipes have broken. Return leftover data.
  1923. if(!out.empty())
  1924. {
  1925. line.append(&out[0], outiter-out.begin());
  1926. out.erase(out.begin(), out.end());
  1927. return cmsysProcess_Pipe_STDOUT;
  1928. }
  1929. else if(!err.empty())
  1930. {
  1931. line.append(&err[0], erriter-err.begin());
  1932. err.erase(err.begin(), err.end());
  1933. return cmsysProcess_Pipe_STDERR;
  1934. }
  1935. else
  1936. {
  1937. return cmsysProcess_Pipe_None;
  1938. }
  1939. }
  1940. }
  1941. }
  1942. void cmSystemTools::DoNotInheritStdPipes()
  1943. {
  1944. #ifdef _WIN32
  1945. // Check to see if we are attached to a console
  1946. // if so, then do not stop the inherited pipes
  1947. // or stdout and stderr will not show up in dos
  1948. // shell windows
  1949. CONSOLE_SCREEN_BUFFER_INFO hOutInfo;
  1950. HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  1951. if(GetConsoleScreenBufferInfo(hOut, &hOutInfo))
  1952. {
  1953. return;
  1954. }
  1955. {
  1956. HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
  1957. DuplicateHandle(GetCurrentProcess(), out,
  1958. GetCurrentProcess(), &out, 0, FALSE,
  1959. DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE);
  1960. SetStdHandle(STD_OUTPUT_HANDLE, out);
  1961. }
  1962. {
  1963. HANDLE out = GetStdHandle(STD_ERROR_HANDLE);
  1964. DuplicateHandle(GetCurrentProcess(), out,
  1965. GetCurrentProcess(), &out, 0, FALSE,
  1966. DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE);
  1967. SetStdHandle(STD_ERROR_HANDLE, out);
  1968. }
  1969. #endif
  1970. }
  1971. //----------------------------------------------------------------------------
  1972. bool cmSystemTools::CopyFileTime(const char* fromFile, const char* toFile)
  1973. {
  1974. #if defined(_WIN32) && !defined(__CYGWIN__)
  1975. cmSystemToolsWindowsHandle hFrom =
  1976. CreateFile(fromFile, GENERIC_READ, FILE_SHARE_READ, 0,
  1977. OPEN_EXISTING, 0, 0);
  1978. cmSystemToolsWindowsHandle hTo =
  1979. CreateFile(toFile, GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
  1980. if(!hFrom || !hTo)
  1981. {
  1982. return false;
  1983. }
  1984. FILETIME timeCreation;
  1985. FILETIME timeLastAccess;
  1986. FILETIME timeLastWrite;
  1987. if(!GetFileTime(hFrom, &timeCreation, &timeLastAccess, &timeLastWrite))
  1988. {
  1989. return false;
  1990. }
  1991. if(!SetFileTime(hTo, &timeCreation, &timeLastAccess, &timeLastWrite))
  1992. {
  1993. return false;
  1994. }
  1995. #else
  1996. struct stat fromStat;
  1997. if(stat(fromFile, &fromStat) < 0)
  1998. {
  1999. return false;
  2000. }
  2001. struct utimbuf buf;
  2002. buf.actime = fromStat.st_atime;
  2003. buf.modtime = fromStat.st_mtime;
  2004. if(utime(toFile, &buf) < 0)
  2005. {
  2006. return false;
  2007. }
  2008. #endif
  2009. return true;
  2010. }
  2011. //----------------------------------------------------------------------------
  2012. cmSystemToolsFileTime* cmSystemTools::FileTimeNew()
  2013. {
  2014. return new cmSystemToolsFileTime;
  2015. }
  2016. //----------------------------------------------------------------------------
  2017. void cmSystemTools::FileTimeDelete(cmSystemToolsFileTime* t)
  2018. {
  2019. delete t;
  2020. }
  2021. //----------------------------------------------------------------------------
  2022. bool cmSystemTools::FileTimeGet(const char* fname, cmSystemToolsFileTime* t)
  2023. {
  2024. #if defined(_WIN32) && !defined(__CYGWIN__)
  2025. cmSystemToolsWindowsHandle h =
  2026. CreateFile(fname, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
  2027. if(!h)
  2028. {
  2029. return false;
  2030. }
  2031. if(!GetFileTime(h, &t->timeCreation, &t->timeLastAccess, &t->timeLastWrite))
  2032. {
  2033. return false;
  2034. }
  2035. #else
  2036. struct stat st;
  2037. if(stat(fname, &st) < 0)
  2038. {
  2039. return false;
  2040. }
  2041. t->timeBuf.actime = st.st_atime;
  2042. t->timeBuf.modtime = st.st_mtime;
  2043. #endif
  2044. return true;
  2045. }
  2046. //----------------------------------------------------------------------------
  2047. bool cmSystemTools::FileTimeSet(const char* fname, cmSystemToolsFileTime* t)
  2048. {
  2049. #if defined(_WIN32) && !defined(__CYGWIN__)
  2050. cmSystemToolsWindowsHandle h =
  2051. CreateFile(fname, GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
  2052. if(!h)
  2053. {
  2054. return false;
  2055. }
  2056. if(!SetFileTime(h, &t->timeCreation, &t->timeLastAccess, &t->timeLastWrite))
  2057. {
  2058. return false;
  2059. }
  2060. #else
  2061. if(utime(fname, &t->timeBuf) < 0)
  2062. {
  2063. return false;
  2064. }
  2065. #endif
  2066. return true;
  2067. }
  2068. //----------------------------------------------------------------------------
  2069. static std::string cmSystemToolsExecutableDirectory;
  2070. void cmSystemTools::FindExecutableDirectory(const char* argv0)
  2071. {
  2072. #if defined(_WIN32) && !defined(__CYGWIN__)
  2073. (void)argv0; // ignore this on windows
  2074. char modulepath[_MAX_PATH];
  2075. ::GetModuleFileName(NULL, modulepath, sizeof(modulepath));
  2076. cmSystemToolsExecutableDirectory =
  2077. cmSystemTools::GetFilenamePath(modulepath);
  2078. return;
  2079. #else
  2080. std::string errorMsg;
  2081. std::string exe;
  2082. if(cmSystemTools::FindProgramPath(argv0, exe, errorMsg))
  2083. {
  2084. // remove symlinks
  2085. exe = cmSystemTools::GetRealPath(exe.c_str());
  2086. cmSystemToolsExecutableDirectory =
  2087. cmSystemTools::GetFilenamePath(exe.c_str());
  2088. }
  2089. else
  2090. {
  2091. // ???
  2092. }
  2093. #endif
  2094. }
  2095. //----------------------------------------------------------------------------
  2096. const char* cmSystemTools::GetExecutableDirectory()
  2097. {
  2098. return cmSystemToolsExecutableDirectory.c_str();
  2099. }
  2100. //----------------------------------------------------------------------------
  2101. #if defined(CMAKE_BUILD_WITH_CMAKE)
  2102. void cmSystemTools::MakefileColorEcho(int color, const char* message,
  2103. bool newline, bool enabled)
  2104. {
  2105. // On some platforms (an MSYS prompt) cmsysTerminal may not be able
  2106. // to determine whether the stream is displayed on a tty. In this
  2107. // case it assumes no unless we tell it otherwise. Since we want
  2108. // color messages to be displayed for users we will assume yes.
  2109. // However, we can test for some situations when the answer is most
  2110. // likely no.
  2111. int assumeTTY = cmsysTerminal_Color_AssumeTTY;
  2112. if(cmSystemTools::GetEnv("DART_TEST_FROM_DART") ||
  2113. cmSystemTools::GetEnv("DASHBOARD_TEST_FROM_CTEST") ||
  2114. cmSystemTools::GetEnv("CTEST_INTERACTIVE_DEBUG_MODE"))
  2115. {
  2116. // Avoid printing color escapes during dashboard builds.
  2117. assumeTTY = 0;
  2118. }
  2119. if(enabled)
  2120. {
  2121. cmsysTerminal_cfprintf(color | assumeTTY, stdout, "%s%s",
  2122. message, newline? "\n" : "");
  2123. }
  2124. else
  2125. {
  2126. // Color is disabled. Print without color.
  2127. fprintf(stdout, "%s%s", message, newline? "\n" : "");
  2128. }
  2129. }
  2130. #endif
  2131. //----------------------------------------------------------------------------
  2132. bool cmSystemTools::GuessLibrarySOName(std::string const& fullPath,
  2133. std::string& soname)
  2134. {
  2135. // For ELF shared libraries use a real parser to get the correct
  2136. // soname.
  2137. #if defined(CMAKE_USE_ELF_PARSER)
  2138. cmELF elf(fullPath.c_str());
  2139. if(elf)
  2140. {
  2141. return elf.GetSOName(soname);
  2142. }
  2143. #endif
  2144. // If the file is not a symlink we have no guess for its soname.
  2145. if(!cmSystemTools::FileIsSymlink(fullPath.c_str()))
  2146. {
  2147. return false;
  2148. }
  2149. if(!cmSystemTools::ReadSymlink(fullPath.c_str(), soname))
  2150. {
  2151. return false;
  2152. }
  2153. // If the symlink has a path component we have no guess for the soname.
  2154. if(!cmSystemTools::GetFilenamePath(soname).empty())
  2155. {
  2156. return false;
  2157. }
  2158. // If the symlink points at an extended version of the same name
  2159. // assume it is the soname.
  2160. std::string name = cmSystemTools::GetFilenameName(fullPath);
  2161. if(soname.length() > name.length() &&
  2162. soname.substr(0, name.length()) == name)
  2163. {
  2164. return true;
  2165. }
  2166. return false;
  2167. }
  2168. //----------------------------------------------------------------------------
  2169. #if defined(CMAKE_USE_ELF_PARSER)
  2170. std::string::size_type cmSystemToolsFindRPath(std::string const& have,
  2171. std::string const& want)
  2172. {
  2173. // Search for the desired rpath.
  2174. std::string::size_type pos = have.find(want);
  2175. // If the path is not present we are done.
  2176. if(pos == std::string::npos)
  2177. {
  2178. return pos;
  2179. }
  2180. // Build a regex to match a properly separated path instance.
  2181. std::string regex_str = "(^|:)(";
  2182. for(std::string::const_iterator i = want.begin(); i != want.end(); ++i)
  2183. {
  2184. int ch = *i;
  2185. if(!(('a' <= ch && ch <= 'z') ||
  2186. ('A' <= ch && ch <= 'Z') ||
  2187. ('0' <= ch && ch <= '9')))
  2188. {
  2189. // Escape the non-alphanumeric character.
  2190. regex_str += "\\";
  2191. }
  2192. // Store the character.
  2193. regex_str.append(1, static_cast<char>(ch));
  2194. }
  2195. regex_str += ")(:|$)";
  2196. // Look for the separated path.
  2197. cmsys::RegularExpression regex(regex_str.c_str());
  2198. if(regex.find(have))
  2199. {
  2200. // Return the position of the path portion.
  2201. return regex.start(2);
  2202. }
  2203. else
  2204. {
  2205. // The desired rpath was not found.
  2206. return std::string::npos;
  2207. }
  2208. }
  2209. #endif
  2210. #if defined(CMAKE_USE_ELF_PARSER)
  2211. struct cmSystemToolsRPathInfo
  2212. {
  2213. unsigned long Position;
  2214. unsigned long Size;
  2215. std::string Name;
  2216. std::string Value;
  2217. };
  2218. #endif
  2219. //----------------------------------------------------------------------------
  2220. bool cmSystemTools::ChangeRPath(std::string const& file,
  2221. std::string const& oldRPath,
  2222. std::string const& newRPath,
  2223. std::string* emsg,
  2224. bool* changed)
  2225. {
  2226. #if defined(CMAKE_USE_ELF_PARSER)
  2227. if(changed)
  2228. {
  2229. *changed = false;
  2230. }
  2231. int rp_count = 0;
  2232. cmSystemToolsRPathInfo rp[2];
  2233. {
  2234. // Parse the ELF binary.
  2235. cmELF elf(file.c_str());
  2236. // Get the RPATH and RUNPATH entries from it.
  2237. int se_count = 0;
  2238. cmELF::StringEntry const* se[2] = {0, 0};
  2239. const char* se_name[2] = {0, 0};
  2240. if(cmELF::StringEntry const* se_rpath = elf.GetRPath())
  2241. {
  2242. se[se_count] = se_rpath;
  2243. se_name[se_count] = "RPATH";
  2244. ++se_count;
  2245. }
  2246. if(cmELF::StringEntry const* se_runpath = elf.GetRunPath())
  2247. {
  2248. se[se_count] = se_runpath;
  2249. se_name[se_count] = "RUNPATH";
  2250. ++se_count;
  2251. }
  2252. if(se_count == 0)
  2253. {
  2254. if(newRPath.empty())
  2255. {
  2256. // The new rpath is empty and there is no rpath anyway so it is
  2257. // okay.
  2258. return true;
  2259. }
  2260. else
  2261. {
  2262. if(emsg)
  2263. {
  2264. *emsg = "No valid ELF RPATH or RUNPATH entry exists in the file; ";
  2265. *emsg += elf.GetErrorMessage();
  2266. }
  2267. return false;
  2268. }
  2269. }
  2270. for(int i=0; i < se_count; ++i)
  2271. {
  2272. // If both RPATH and RUNPATH refer to the same string literal it
  2273. // needs to be changed only once.
  2274. if(rp_count && rp[0].Position == se[i]->Position)
  2275. {
  2276. continue;
  2277. }
  2278. // Make sure the current rpath contains the old rpath.
  2279. std::string::size_type pos =
  2280. cmSystemToolsFindRPath(se[i]->Value, oldRPath);
  2281. if(pos == std::string::npos)
  2282. {
  2283. // If it contains the new rpath instead then it is okay.
  2284. if(cmSystemToolsFindRPath(se[i]->Value, newRPath) != std::string::npos)
  2285. {
  2286. continue;
  2287. }
  2288. if(emsg)
  2289. {
  2290. cmOStringStream e;
  2291. e << "The current " << se_name[i] << " is:\n"
  2292. << " " << se[i]->Value << "\n"
  2293. << "which does not contain:\n"
  2294. << " " << oldRPath << "\n"
  2295. << "as was expected.";
  2296. *emsg = e.str();
  2297. }
  2298. return false;
  2299. }
  2300. // Store information about the entry in the file.
  2301. rp[rp_count].Position = se[i]->Position;
  2302. rp[rp_count].Size = se[i]->Size;
  2303. rp[rp_count].Name = se_name[i];
  2304. // Construct the new value which preserves the part of the path
  2305. // not being changed.
  2306. rp[rp_count].Value = se[i]->Value.substr(0, pos);
  2307. rp[rp_count].Value += newRPath;
  2308. rp[rp_count].Value += se[i]->Value.substr(pos+oldRPath.length(),
  2309. oldRPath.npos);
  2310. // Make sure there is enough room to store the new rpath and at
  2311. // least one null terminator.
  2312. if(rp[rp_count].Size < rp[rp_count].Value.length()+1)
  2313. {
  2314. if(emsg)
  2315. {
  2316. *emsg = "The replacement path is too long for the ";
  2317. *emsg += se_name[i];
  2318. *emsg += " entry.";
  2319. }
  2320. return false;
  2321. }
  2322. // This entry is ready for update.
  2323. ++rp_count;
  2324. }
  2325. }
  2326. // If no runtime path needs to be changed, we are done.
  2327. if(rp_count == 0)
  2328. {
  2329. return true;
  2330. }
  2331. {
  2332. // Open the file for update.
  2333. std::ofstream f(file.c_str(),
  2334. std::ios::in | std::ios::out | std::ios::binary);
  2335. if(!f)
  2336. {
  2337. if(emsg)
  2338. {
  2339. *emsg = "Error opening file for update.";
  2340. }
  2341. return false;
  2342. }
  2343. // Store the new RPATH and RUNPATH strings.
  2344. for(int i=0; i < rp_count; ++i)
  2345. {
  2346. // Seek to the RPATH position.
  2347. if(!f.seekp(rp[i].Position))
  2348. {
  2349. if(emsg)
  2350. {
  2351. *emsg = "Error seeking to ";
  2352. *emsg += rp[i].Name;
  2353. *emsg += " position.";
  2354. }
  2355. return false;
  2356. }
  2357. // Write the new rpath. Follow it with enough null terminators to
  2358. // fill the string table entry.
  2359. f << rp[i].Value;
  2360. for(unsigned long j=rp[i].Value.length(); j < rp[i].Size; ++j)
  2361. {
  2362. f << '\0';
  2363. }
  2364. // Make sure it wrote correctly.
  2365. if(!f)
  2366. {
  2367. if(emsg)
  2368. {
  2369. *emsg = "Error writing the new ";
  2370. *emsg += rp[i].Name;
  2371. *emsg += " string to the file.";
  2372. }
  2373. return false;
  2374. }
  2375. }
  2376. }
  2377. // Everything was updated successfully.
  2378. if(changed)
  2379. {
  2380. *changed = true;
  2381. }
  2382. return true;
  2383. #else
  2384. (void)file;
  2385. (void)oldRPath;
  2386. (void)newRPath;
  2387. (void)emsg;
  2388. (void)changed;
  2389. return false;
  2390. #endif
  2391. }
  2392. //----------------------------------------------------------------------------
  2393. bool cmSystemTools::RemoveRPath(std::string const& file, std::string* emsg,
  2394. bool* removed)
  2395. {
  2396. #if defined(CMAKE_USE_ELF_PARSER)
  2397. if(removed)
  2398. {
  2399. *removed = false;
  2400. }
  2401. int zeroCount = 0;
  2402. unsigned long zeroPosition[2] = {0,0};
  2403. unsigned long zeroSize[2] = {0,0};
  2404. unsigned long bytesBegin = 0;
  2405. std::vector<char> bytes;
  2406. {
  2407. // Parse the ELF binary.
  2408. cmELF elf(file.c_str());
  2409. // Get the RPATH and RUNPATH entries from it and sort them by index
  2410. // in the dynamic section header.
  2411. int se_count = 0;
  2412. cmELF::StringEntry const* se[2] = {0, 0};
  2413. if(cmELF::StringEntry const* se_rpath = elf.GetRPath())
  2414. {
  2415. se[se_count++] = se_rpath;
  2416. }
  2417. if(cmELF::StringEntry const* se_runpath = elf.GetRunPath())
  2418. {
  2419. se[se_count++] = se_runpath;
  2420. }
  2421. if(se_count == 0)
  2422. {
  2423. // There is no RPATH or RUNPATH anyway.
  2424. return true;
  2425. }
  2426. if(se_count == 2 && se[1]->IndexInSection < se[0]->IndexInSection)
  2427. {
  2428. cmsys_stl::swap(se[0], se[1]);
  2429. }
  2430. // Get the size of the dynamic section header.
  2431. unsigned int count = elf.GetDynamicEntryCount();
  2432. if(count == 0)
  2433. {
  2434. // This should happen only for invalid ELF files where a DT_NULL
  2435. // appears before the end of the table.
  2436. if(emsg)
  2437. {
  2438. *emsg = "DYNAMIC section contains a DT_NULL before the end.";
  2439. }
  2440. return false;
  2441. }
  2442. // Save information about the string entries to be zeroed.
  2443. zeroCount = se_count;
  2444. for(int i=0; i < se_count; ++i)
  2445. {
  2446. zeroPosition[i] = se[i]->Position;
  2447. zeroSize[i] = se[i]->Size;
  2448. }
  2449. // Get the range of file positions corresponding to each entry and
  2450. // the rest of the table after them.
  2451. unsigned long entryBegin[3] = {0,0,0};
  2452. unsigned long entryEnd[2] = {0,0};
  2453. for(int i=0; i < se_count; ++i)
  2454. {
  2455. entryBegin[i] = elf.GetDynamicEntryPosition(se[i]->IndexInSection);
  2456. entryEnd[i] = elf.GetDynamicEntryPosition(se[i]->IndexInSection+1);
  2457. }
  2458. entryBegin[se_count] = elf.GetDynamicEntryPosition(count);
  2459. // The data are to be written over the old table entries starting at
  2460. // the first one being removed.
  2461. bytesBegin = entryBegin[0];
  2462. unsigned long bytesEnd = entryBegin[se_count];
  2463. // Allocate a buffer to hold the part of the file to be written.
  2464. // Initialize it with zeros.
  2465. bytes.resize(bytesEnd - bytesBegin, 0);
  2466. // Read the part of the DYNAMIC section header that will move.
  2467. // The remainder of the buffer will be left with zeros which
  2468. // represent a DT_NULL entry.
  2469. char* data = &bytes[0];
  2470. for(int i=0; i < se_count; ++i)
  2471. {
  2472. // Read data between the entries being removed.
  2473. unsigned long sz = entryBegin[i+1] - entryEnd[i];
  2474. if(sz > 0 && !elf.ReadBytes(entryEnd[i], sz, data))
  2475. {
  2476. if(emsg)
  2477. {
  2478. *emsg = "Failed to read DYNAMIC section header.";
  2479. }
  2480. return false;
  2481. }
  2482. data += sz;
  2483. }
  2484. }
  2485. // Open the file for update.
  2486. std::ofstream f(file.c_str(),
  2487. std::ios::in | std::ios::out | std::ios::binary);
  2488. if(!f)
  2489. {
  2490. if(emsg)
  2491. {
  2492. *emsg = "Error opening file for update.";
  2493. }
  2494. return false;
  2495. }
  2496. // Write the new DYNAMIC table header.
  2497. if(!f.seekp(bytesBegin))
  2498. {
  2499. if(emsg)
  2500. {
  2501. *emsg = "Error seeking to DYNAMIC table header for RPATH.";
  2502. }
  2503. return false;
  2504. }
  2505. if(!f.write(&bytes[0], bytes.size()))
  2506. {
  2507. if(emsg)
  2508. {
  2509. *emsg = "Error replacing DYNAMIC table header.";
  2510. }
  2511. return false;
  2512. }
  2513. // Fill the RPATH and RUNPATH strings with zero bytes.
  2514. for(int i=0; i < zeroCount; ++i)
  2515. {
  2516. if(!f.seekp(zeroPosition[i]))
  2517. {
  2518. if(emsg)
  2519. {
  2520. *emsg = "Error seeking to RPATH position.";
  2521. }
  2522. return false;
  2523. }
  2524. for(unsigned long j=0; j < zeroSize[i]; ++j)
  2525. {
  2526. f << '\0';
  2527. }
  2528. if(!f)
  2529. {
  2530. if(emsg)
  2531. {
  2532. *emsg = "Error writing the empty rpath string to the file.";
  2533. }
  2534. return false;
  2535. }
  2536. }
  2537. // Everything was updated successfully.
  2538. if(removed)
  2539. {
  2540. *removed = true;
  2541. }
  2542. return true;
  2543. #else
  2544. (void)file;
  2545. (void)emsg;
  2546. (void)removed;
  2547. return false;
  2548. #endif
  2549. }
  2550. //----------------------------------------------------------------------------
  2551. bool cmSystemTools::CheckRPath(std::string const& file,
  2552. std::string const& newRPath)
  2553. {
  2554. #if defined(CMAKE_USE_ELF_PARSER)
  2555. // Parse the ELF binary.
  2556. cmELF elf(file.c_str());
  2557. // Get the RPATH or RUNPATH entry from it.
  2558. cmELF::StringEntry const* se = elf.GetRPath();
  2559. if(!se)
  2560. {
  2561. se = elf.GetRunPath();
  2562. }
  2563. // Make sure the current rpath contains the new rpath.
  2564. if(newRPath.empty())
  2565. {
  2566. if(!se)
  2567. {
  2568. return true;
  2569. }
  2570. }
  2571. else
  2572. {
  2573. if(se &&
  2574. cmSystemToolsFindRPath(se->Value, newRPath) != std::string::npos)
  2575. {
  2576. return true;
  2577. }
  2578. }
  2579. return false;
  2580. #else
  2581. (void)file;
  2582. (void)newRPath;
  2583. return false;
  2584. #endif
  2585. }