cmSystemTools.cxx 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135
  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 <string.h>
  18. #include <cmsys/RegularExpression.hxx>
  19. #include <cmsys/Directory.hxx>
  20. #include <cmsys/System.h>
  21. #if defined(CMAKE_BUILD_WITH_CMAKE)
  22. # include <cmsys/Terminal.h>
  23. #endif
  24. #if defined(_WIN32)
  25. # include <windows.h>
  26. #else
  27. # include <sys/types.h>
  28. # include <unistd.h>
  29. # include <utime.h>
  30. # include <sys/wait.h>
  31. #endif
  32. #include <sys/stat.h>
  33. #if defined(_WIN32) && \
  34. (defined(_MSC_VER) || defined(__WATCOMC__) || \
  35. defined(__BORLANDC__) || defined(__MINGW32__))
  36. # include <io.h>
  37. #endif
  38. #if defined(CMAKE_BUILD_WITH_CMAKE)
  39. # include <libtar/libtar.h>
  40. # include <memory> // auto_ptr
  41. # include <fcntl.h>
  42. # include <cm_zlib.h>
  43. # include <cmsys/MD5.h>
  44. #endif
  45. #if defined(__sgi) && !defined(__GNUC__)
  46. # pragma set woff 1375 /* base class destructor not virtual */
  47. #endif
  48. #if !defined(HAVE_ENVIRON_NOT_REQUIRE_PROTOTYPE)
  49. // For GetEnvironmentVariables
  50. # if defined(_WIN32)
  51. extern __declspec( dllimport ) char** environ;
  52. # else
  53. extern char** environ;
  54. # endif
  55. #endif
  56. #ifdef _WIN32
  57. class cmSystemToolsWindowsHandle
  58. {
  59. public:
  60. cmSystemToolsWindowsHandle(HANDLE h): handle_(h) {}
  61. ~cmSystemToolsWindowsHandle()
  62. {
  63. if(this->handle_ != INVALID_HANDLE_VALUE)
  64. {
  65. CloseHandle(this->handle_);
  66. }
  67. }
  68. operator bool() const { return this->handle_ != INVALID_HANDLE_VALUE; }
  69. bool operator !() const { return this->handle_ == INVALID_HANDLE_VALUE; }
  70. operator HANDLE() const { return this->handle_; }
  71. private:
  72. HANDLE handle_;
  73. };
  74. #endif
  75. bool cmSystemTools::s_RunCommandHideConsole = false;
  76. bool cmSystemTools::s_DisableRunCommandOutput = false;
  77. bool cmSystemTools::s_ErrorOccured = false;
  78. bool cmSystemTools::s_FatalErrorOccured = false;
  79. bool cmSystemTools::s_DisableMessages = false;
  80. bool cmSystemTools::s_ForceUnixPaths = false;
  81. std::string cmSystemTools::s_Windows9xComspecSubstitute = "command.com";
  82. void cmSystemTools::SetWindows9xComspecSubstitute(const char* str)
  83. {
  84. if ( str )
  85. {
  86. cmSystemTools::s_Windows9xComspecSubstitute = str;
  87. }
  88. }
  89. const char* cmSystemTools::GetWindows9xComspecSubstitute()
  90. {
  91. return cmSystemTools::s_Windows9xComspecSubstitute.c_str();
  92. }
  93. void (*cmSystemTools::s_ErrorCallback)(const char*, const char*,
  94. bool&, void*);
  95. void (*cmSystemTools::s_StdoutCallback)(const char*, int len, void*);
  96. void* cmSystemTools::s_ErrorCallbackClientData = 0;
  97. void* cmSystemTools::s_StdoutCallbackClientData = 0;
  98. // replace replace with with as many times as it shows up in source.
  99. // write the result into source.
  100. #if defined(_WIN32) && !defined(__CYGWIN__)
  101. void cmSystemTools::ExpandRegistryValues(std::string& source)
  102. {
  103. // Regular expression to match anything inside [...] that begins in HKEY.
  104. // Note that there is a special rule for regular expressions to match a
  105. // close square-bracket inside a list delimited by square brackets.
  106. // The "[^]]" part of this expression will match any character except
  107. // a close square-bracket. The ']' character must be the first in the
  108. // list of characters inside the [^...] block of the expression.
  109. cmsys::RegularExpression regEntry("\\[(HKEY[^]]*)\\]");
  110. // check for black line or comment
  111. while (regEntry.find(source))
  112. {
  113. // the arguments are the second match
  114. std::string key = regEntry.match(1);
  115. std::string val;
  116. if (ReadRegistryValue(key.c_str(), val))
  117. {
  118. std::string reg = "[";
  119. reg += key + "]";
  120. cmSystemTools::ReplaceString(source, reg.c_str(), val.c_str());
  121. }
  122. else
  123. {
  124. std::string reg = "[";
  125. reg += key + "]";
  126. cmSystemTools::ReplaceString(source, reg.c_str(), "/registry");
  127. }
  128. }
  129. }
  130. #else
  131. void cmSystemTools::ExpandRegistryValues(std::string&)
  132. {
  133. }
  134. #endif
  135. std::string cmSystemTools::EscapeQuotes(const char* str)
  136. {
  137. std::string result = "";
  138. for(const char* ch = str; *ch != '\0'; ++ch)
  139. {
  140. if(*ch == '"')
  141. {
  142. result += '\\';
  143. }
  144. result += *ch;
  145. }
  146. return result;
  147. }
  148. std::string cmSystemTools::EscapeSpaces(const char* str)
  149. {
  150. #if defined(_WIN32) && !defined(__CYGWIN__)
  151. bool useDoubleQ = true;
  152. #else
  153. bool useDoubleQ = false;
  154. #endif
  155. if(cmSystemTools::s_ForceUnixPaths)
  156. {
  157. useDoubleQ = false;
  158. }
  159. if(useDoubleQ)
  160. {
  161. std::string result;
  162. // if there are spaces
  163. std::string temp = str;
  164. if (temp.find(" ") != std::string::npos &&
  165. temp.find("\"")==std::string::npos)
  166. {
  167. result = "\"";
  168. result += str;
  169. result += "\"";
  170. return result;
  171. }
  172. return str;
  173. }
  174. else
  175. {
  176. std::string result = "";
  177. for(const char* ch = str; *ch != '\0'; ++ch)
  178. {
  179. if(*ch == ' ')
  180. {
  181. result += '\\';
  182. }
  183. result += *ch;
  184. }
  185. return result;
  186. }
  187. }
  188. std::string cmSystemTools::RemoveEscapes(const char* s)
  189. {
  190. std::string result = "";
  191. for(const char* ch = s; *ch; ++ch)
  192. {
  193. if(*ch == '\\' && *(ch+1) != ';')
  194. {
  195. ++ch;
  196. switch (*ch)
  197. {
  198. case '\\': result.insert(result.end(), '\\'); break;
  199. case '"': result.insert(result.end(), '"'); break;
  200. case ' ': result.insert(result.end(), ' '); break;
  201. case 't': result.insert(result.end(), '\t'); break;
  202. case 'n': result.insert(result.end(), '\n'); break;
  203. case 'r': result.insert(result.end(), '\r'); break;
  204. case '#': result.insert(result.end(), '#'); break;
  205. case '(': result.insert(result.end(), '('); break;
  206. case ')': result.insert(result.end(), ')'); break;
  207. case '0': result.insert(result.end(), '\0'); break;
  208. case '\0':
  209. {
  210. cmSystemTools::Error("Trailing backslash in argument:\n", s);
  211. return result;
  212. }
  213. default:
  214. {
  215. std::string chStr(1, *ch);
  216. cmSystemTools::Error("Invalid escape sequence \\", chStr.c_str(),
  217. "\nin argument ", s);
  218. }
  219. }
  220. }
  221. else
  222. {
  223. result.insert(result.end(), *ch);
  224. }
  225. }
  226. return result;
  227. }
  228. void cmSystemTools::Error(const char* m1, const char* m2,
  229. const char* m3, const char* m4)
  230. {
  231. std::string message = "CMake Error: ";
  232. if(m1)
  233. {
  234. message += m1;
  235. }
  236. if(m2)
  237. {
  238. message += m2;
  239. }
  240. if(m3)
  241. {
  242. message += m3;
  243. }
  244. if(m4)
  245. {
  246. message += m4;
  247. }
  248. cmSystemTools::s_ErrorOccured = true;
  249. cmSystemTools::Message(message.c_str(),"Error");
  250. }
  251. void cmSystemTools::SetErrorCallback(ErrorCallback f, void* clientData)
  252. {
  253. s_ErrorCallback = f;
  254. s_ErrorCallbackClientData = clientData;
  255. }
  256. void cmSystemTools::SetStdoutCallback(StdoutCallback f, void* clientData)
  257. {
  258. s_StdoutCallback = f;
  259. s_StdoutCallbackClientData = clientData;
  260. }
  261. void cmSystemTools::Stdout(const char* s)
  262. {
  263. if(s_StdoutCallback)
  264. {
  265. (*s_StdoutCallback)(s, static_cast<int>(strlen(s)),
  266. s_StdoutCallbackClientData);
  267. }
  268. else
  269. {
  270. std::cout << s;
  271. std::cout.flush();
  272. }
  273. }
  274. void cmSystemTools::Stdout(const char* s, int length)
  275. {
  276. if(s_StdoutCallback)
  277. {
  278. (*s_StdoutCallback)(s, length, s_StdoutCallbackClientData);
  279. }
  280. else
  281. {
  282. std::cout.write(s, length);
  283. std::cout.flush();
  284. }
  285. }
  286. void cmSystemTools::Message(const char* m1, const char *title)
  287. {
  288. if(s_DisableMessages)
  289. {
  290. return;
  291. }
  292. if(s_ErrorCallback)
  293. {
  294. (*s_ErrorCallback)(m1, title, s_DisableMessages,
  295. s_ErrorCallbackClientData);
  296. return;
  297. }
  298. else
  299. {
  300. std::cerr << m1 << std::endl << std::flush;
  301. }
  302. }
  303. void cmSystemTools::ReportLastSystemError(const char* msg)
  304. {
  305. std::string m = msg;
  306. m += ": System Error: ";
  307. m += Superclass::GetLastSystemError();
  308. cmSystemTools::Error(m.c_str());
  309. }
  310. bool cmSystemTools::IsOn(const char* val)
  311. {
  312. if (!val)
  313. {
  314. return false;
  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 == "ON" || v == "1" || v == "YES" || v == "TRUE" || v == "Y");
  323. }
  324. bool cmSystemTools::IsNOTFOUND(const char* val)
  325. {
  326. size_t len = strlen(val);
  327. const char* notfound = "-NOTFOUND";
  328. const size_t lenNotFound = 9;
  329. if(len < lenNotFound-1)
  330. {
  331. return false;
  332. }
  333. if(len == lenNotFound-1)
  334. {
  335. return ( strcmp(val, "NOTFOUND") == 0);
  336. }
  337. return ((strncmp((val + (len - lenNotFound)), notfound, lenNotFound) == 0));
  338. }
  339. bool cmSystemTools::IsOff(const char* val)
  340. {
  341. if (!val || strlen(val) == 0)
  342. {
  343. return true;
  344. }
  345. std::basic_string<char> v = val;
  346. for(std::basic_string<char>::iterator c = v.begin();
  347. c != v.end(); c++)
  348. {
  349. *c = toupper(*c);
  350. }
  351. return (v == "OFF" || v == "0" || v == "NO" || v == "FALSE" ||
  352. v == "N" || cmSystemTools::IsNOTFOUND(v.c_str()) || v == "IGNORE");
  353. }
  354. //----------------------------------------------------------------------------
  355. void cmSystemTools::ParseWindowsCommandLine(const char* command,
  356. std::vector<std::string>& args)
  357. {
  358. // See the MSDN document "Parsing C Command-Line Arguments" at
  359. // http://msdn2.microsoft.com/en-us/library/a1y7w461.aspx for rules
  360. // of parsing the windows command line.
  361. bool in_argument = false;
  362. bool in_quotes = false;
  363. int backslashes = 0;
  364. std::string arg;
  365. for(const char* c = command;*c; ++c)
  366. {
  367. if(*c == '\\')
  368. {
  369. ++backslashes;
  370. in_argument = true;
  371. }
  372. else if(*c == '"')
  373. {
  374. int backslash_pairs = backslashes >> 1;
  375. int backslash_escaped = backslashes & 1;
  376. arg.append(backslash_pairs, '\\');
  377. backslashes = 0;
  378. if(backslash_escaped)
  379. {
  380. /* An odd number of backslashes precede this quote.
  381. It is escaped. */
  382. arg.append(1, '"');
  383. }
  384. else
  385. {
  386. /* An even number of backslashes precede this quote.
  387. It is not escaped. */
  388. in_quotes = !in_quotes;
  389. }
  390. in_argument = true;
  391. }
  392. else
  393. {
  394. arg.append(backslashes, '\\');
  395. backslashes = 0;
  396. if(isspace(*c))
  397. {
  398. if(in_quotes)
  399. {
  400. arg.append(1, *c);
  401. }
  402. else if(in_argument)
  403. {
  404. args.push_back(arg);
  405. arg = "";
  406. in_argument = false;
  407. }
  408. }
  409. else
  410. {
  411. in_argument = true;
  412. arg.append(1, *c);
  413. }
  414. }
  415. }
  416. arg.append(backslashes, '\\');
  417. if(in_argument)
  418. {
  419. args.push_back(arg);
  420. }
  421. }
  422. std::string cmSystemTools::EscapeWindowsShellArgument(const char* arg,
  423. int shell_flags)
  424. {
  425. char local_buffer[1024];
  426. char* buffer = local_buffer;
  427. int size = cmsysSystem_Shell_GetArgumentSizeForWindows(arg, shell_flags);
  428. if(size > 1024)
  429. {
  430. buffer = new char[size];
  431. }
  432. cmsysSystem_Shell_GetArgumentForWindows(arg, buffer, shell_flags);
  433. std::string result(buffer);
  434. if(buffer != local_buffer)
  435. {
  436. delete [] buffer;
  437. }
  438. return result;
  439. }
  440. std::vector<cmStdString> cmSystemTools::ParseArguments(const char* command)
  441. {
  442. std::vector<cmStdString> args;
  443. std::string arg;
  444. bool win_path = false;
  445. if ( command[0] != '/' && command[1] == ':' && command[2] == '\\' ||
  446. command[0] == '\"' && command[1] != '/' && command[2] == ':'
  447. && command[3] == '\\' ||
  448. command[0] == '\'' && command[1] != '/' && command[2] == ':'
  449. && command[3] == '\\' ||
  450. command[0] == '\\' && command[1] == '\\')
  451. {
  452. win_path = true;
  453. }
  454. // Split the command into an argv array.
  455. for(const char* c = command; *c;)
  456. {
  457. // Skip over whitespace.
  458. while(*c == ' ' || *c == '\t')
  459. {
  460. ++c;
  461. }
  462. arg = "";
  463. if(*c == '"')
  464. {
  465. // Parse a quoted argument.
  466. ++c;
  467. while(*c && *c != '"')
  468. {
  469. arg.append(1, *c);
  470. ++c;
  471. }
  472. if(*c)
  473. {
  474. ++c;
  475. }
  476. args.push_back(arg);
  477. }
  478. else if(*c == '\'')
  479. {
  480. // Parse a quoted argument.
  481. ++c;
  482. while(*c && *c != '\'')
  483. {
  484. arg.append(1, *c);
  485. ++c;
  486. }
  487. if(*c)
  488. {
  489. ++c;
  490. }
  491. args.push_back(arg);
  492. }
  493. else if(*c)
  494. {
  495. // Parse an unquoted argument.
  496. while(*c && *c != ' ' && *c != '\t')
  497. {
  498. if(*c == '\\' && !win_path)
  499. {
  500. ++c;
  501. if(*c)
  502. {
  503. arg.append(1, *c);
  504. ++c;
  505. }
  506. }
  507. else
  508. {
  509. arg.append(1, *c);
  510. ++c;
  511. }
  512. }
  513. args.push_back(arg);
  514. }
  515. }
  516. return args;
  517. }
  518. bool cmSystemTools::RunSingleCommand(std::vector<cmStdString>const& command,
  519. std::string* output ,
  520. int* retVal , const char* dir ,
  521. bool verbose ,
  522. double timeout )
  523. {
  524. std::vector<const char*> argv;
  525. for(std::vector<cmStdString>::const_iterator a = command.begin();
  526. a != command.end(); ++a)
  527. {
  528. argv.push_back(a->c_str());
  529. }
  530. argv.push_back(0);
  531. if ( output )
  532. {
  533. *output = "";
  534. }
  535. cmsysProcess* cp = cmsysProcess_New();
  536. cmsysProcess_SetCommand(cp, &*argv.begin());
  537. cmsysProcess_SetWorkingDirectory(cp, dir);
  538. if(cmSystemTools::GetRunCommandHideConsole())
  539. {
  540. cmsysProcess_SetOption(cp, cmsysProcess_Option_HideWindow, 1);
  541. }
  542. cmsysProcess_SetTimeout(cp, timeout);
  543. cmsysProcess_Execute(cp);
  544. std::vector<char> tempOutput;
  545. char* data;
  546. int length;
  547. if ( output || verbose )
  548. {
  549. while(cmsysProcess_WaitForData(cp, &data, &length, 0))
  550. {
  551. if(output || verbose)
  552. {
  553. // Translate NULL characters in the output into valid text.
  554. // Visual Studio 7 puts these characters in the output of its
  555. // build process.
  556. for(int i=0; i < length; ++i)
  557. {
  558. if(data[i] == '\0')
  559. {
  560. data[i] = ' ';
  561. }
  562. }
  563. }
  564. if ( output )
  565. {
  566. tempOutput.insert(tempOutput.end(), data, data+length);
  567. }
  568. if(verbose)
  569. {
  570. cmSystemTools::Stdout(data, length);
  571. }
  572. }
  573. }
  574. cmsysProcess_WaitForExit(cp, 0);
  575. if ( output && tempOutput.begin() != tempOutput.end())
  576. {
  577. output->append(&*tempOutput.begin(), tempOutput.size());
  578. }
  579. bool result = true;
  580. if(cmsysProcess_GetState(cp) == cmsysProcess_State_Exited)
  581. {
  582. if ( retVal )
  583. {
  584. *retVal = cmsysProcess_GetExitValue(cp);
  585. }
  586. else
  587. {
  588. if ( cmsysProcess_GetExitValue(cp) != 0 )
  589. {
  590. result = false;
  591. }
  592. }
  593. }
  594. else if(cmsysProcess_GetState(cp) == cmsysProcess_State_Exception)
  595. {
  596. const char* exception_str = cmsysProcess_GetExceptionString(cp);
  597. if ( verbose )
  598. {
  599. std::cerr << exception_str << std::endl;
  600. }
  601. if ( output )
  602. {
  603. output->append(exception_str, strlen(exception_str));
  604. }
  605. result = false;
  606. }
  607. else if(cmsysProcess_GetState(cp) == cmsysProcess_State_Error)
  608. {
  609. const char* error_str = cmsysProcess_GetErrorString(cp);
  610. if ( verbose )
  611. {
  612. std::cerr << error_str << std::endl;
  613. }
  614. if ( output )
  615. {
  616. output->append(error_str, strlen(error_str));
  617. }
  618. result = false;
  619. }
  620. else if(cmsysProcess_GetState(cp) == cmsysProcess_State_Expired)
  621. {
  622. const char* error_str = "Process terminated due to timeout\n";
  623. if ( verbose )
  624. {
  625. std::cerr << error_str << std::endl;
  626. }
  627. if ( output )
  628. {
  629. output->append(error_str, strlen(error_str));
  630. }
  631. result = false;
  632. }
  633. cmsysProcess_Delete(cp);
  634. return result;
  635. }
  636. bool cmSystemTools::RunSingleCommand(
  637. const char* command,
  638. std::string* output,
  639. int *retVal,
  640. const char* dir,
  641. bool verbose,
  642. double timeout)
  643. {
  644. if(s_DisableRunCommandOutput)
  645. {
  646. verbose = false;
  647. }
  648. std::vector<cmStdString> args = cmSystemTools::ParseArguments(command);
  649. if(args.size() < 1)
  650. {
  651. return false;
  652. }
  653. return cmSystemTools::RunSingleCommand(args, output,retVal,
  654. dir, verbose, timeout);
  655. }
  656. bool cmSystemTools::RunCommand(const char* command,
  657. std::string& output,
  658. const char* dir,
  659. bool verbose,
  660. int timeout)
  661. {
  662. int dummy;
  663. return cmSystemTools::RunCommand(command, output, dummy,
  664. dir, verbose, timeout);
  665. }
  666. #if defined(WIN32) && !defined(__CYGWIN__)
  667. #include "cmWin32ProcessExecution.h"
  668. // use this for shell commands like echo and dir
  669. bool RunCommandViaWin32(const char* command,
  670. const char* dir,
  671. std::string& output,
  672. int& retVal,
  673. bool verbose,
  674. int timeout)
  675. {
  676. #if defined(__BORLANDC__)
  677. return
  678. cmWin32ProcessExecution::
  679. BorlandRunCommand(command, dir, output,
  680. retVal,
  681. verbose, timeout,
  682. cmSystemTools::GetRunCommandHideConsole());
  683. #else // Visual studio
  684. ::SetLastError(ERROR_SUCCESS);
  685. if ( ! command )
  686. {
  687. cmSystemTools::Error("No command specified");
  688. return false;
  689. }
  690. cmWin32ProcessExecution resProc;
  691. if(cmSystemTools::GetRunCommandHideConsole())
  692. {
  693. resProc.SetHideWindows(true);
  694. }
  695. if ( cmSystemTools::GetWindows9xComspecSubstitute() )
  696. {
  697. resProc.SetConsoleSpawn(cmSystemTools::GetWindows9xComspecSubstitute() );
  698. }
  699. if ( !resProc.StartProcess(command, dir, verbose) )
  700. {
  701. output = resProc.GetOutput();
  702. if(verbose)
  703. {
  704. cmSystemTools::Stdout(output.c_str());
  705. }
  706. return false;
  707. }
  708. resProc.Wait(timeout);
  709. output = resProc.GetOutput();
  710. retVal = resProc.GetExitValue();
  711. return true;
  712. #endif
  713. }
  714. // use this for shell commands like echo and dir
  715. bool RunCommandViaSystem(const char* command,
  716. const char* dir,
  717. std::string& output,
  718. int& retVal,
  719. bool verbose)
  720. {
  721. std::cout << "@@ " << command << std::endl;
  722. std::string commandInDir;
  723. if(dir)
  724. {
  725. commandInDir = "cd ";
  726. commandInDir += cmSystemTools::ConvertToOutputPath(dir);
  727. commandInDir += " && ";
  728. commandInDir += command;
  729. }
  730. else
  731. {
  732. commandInDir = command;
  733. }
  734. command = commandInDir.c_str();
  735. std::string commandToFile = command;
  736. commandToFile += " > ";
  737. std::string tempFile;
  738. tempFile += _tempnam(0, "cmake");
  739. commandToFile += tempFile;
  740. retVal = system(commandToFile.c_str());
  741. std::ifstream fin(tempFile.c_str());
  742. if(!fin)
  743. {
  744. if(verbose)
  745. {
  746. std::string errormsg = "RunCommand produced no output: command: \"";
  747. errormsg += command;
  748. errormsg += "\"";
  749. errormsg += "\nOutput file: ";
  750. errormsg += tempFile;
  751. cmSystemTools::Error(errormsg.c_str());
  752. }
  753. fin.close();
  754. cmSystemTools::RemoveFile(tempFile.c_str());
  755. return false;
  756. }
  757. bool multiLine = false;
  758. std::string line;
  759. while(cmSystemTools::GetLineFromStream(fin, line))
  760. {
  761. output += line;
  762. if(multiLine)
  763. {
  764. output += "\n";
  765. }
  766. multiLine = true;
  767. }
  768. fin.close();
  769. cmSystemTools::RemoveFile(tempFile.c_str());
  770. return true;
  771. }
  772. #else // We have popen
  773. // BeOS seems to return from a successful pclose() before the process has
  774. // legitimately exited, or at least before SIGCHLD is thrown...the signal may
  775. // come quite some time after pclose returns! This causes havoc with later
  776. // parts of CMake that expect to catch the signal from other child processes,
  777. // so we explicitly wait to catch it here. This should be safe to do with
  778. // popen() so long as we don't actually collect the zombie process ourselves.
  779. #ifdef __BEOS__
  780. #include <signal.h>
  781. #undef SIGBUS // this is the same as SIGSEGV on BeOS and causes issues below.
  782. static volatile bool beos_seen_signal = false;
  783. static void beos_popen_workaround(int sig)
  784. {
  785. beos_seen_signal = true;
  786. }
  787. #endif
  788. bool RunCommandViaPopen(const char* command,
  789. const char* dir,
  790. std::string& output,
  791. int& retVal,
  792. bool verbose,
  793. int /*timeout*/)
  794. {
  795. // if only popen worked on windows.....
  796. std::string commandInDir;
  797. if(dir)
  798. {
  799. commandInDir = "cd \"";
  800. commandInDir += dir;
  801. commandInDir += "\" && ";
  802. commandInDir += command;
  803. }
  804. else
  805. {
  806. commandInDir = command;
  807. }
  808. commandInDir += " 2>&1";
  809. command = commandInDir.c_str();
  810. const int BUFFER_SIZE = 4096;
  811. char buffer[BUFFER_SIZE];
  812. if(verbose)
  813. {
  814. cmSystemTools::Stdout("running ");
  815. cmSystemTools::Stdout(command);
  816. cmSystemTools::Stdout("\n");
  817. }
  818. fflush(stdout);
  819. fflush(stderr);
  820. #ifdef __BEOS__
  821. beos_seen_signal = false;
  822. signal(SIGCHLD, beos_popen_workaround);
  823. #endif
  824. FILE* cpipe = popen(command, "r");
  825. if(!cpipe)
  826. {
  827. #ifdef __BEOS__
  828. signal(SIGCHLD, SIG_DFL);
  829. #endif
  830. return false;
  831. }
  832. fgets(buffer, BUFFER_SIZE, cpipe);
  833. while(!feof(cpipe))
  834. {
  835. if(verbose)
  836. {
  837. cmSystemTools::Stdout(buffer);
  838. }
  839. output += buffer;
  840. fgets(buffer, BUFFER_SIZE, cpipe);
  841. }
  842. retVal = pclose(cpipe);
  843. #ifdef __BEOS__
  844. for (int i = 0; (!beos_seen_signal) && (i < 3); i++)
  845. {
  846. ::sleep(1); // signals should interrupt this...
  847. }
  848. if (!beos_seen_signal)
  849. {
  850. signal(SIGCHLD, SIG_DFL); // oh well, didn't happen. Go on anyhow.
  851. }
  852. #endif
  853. if (WIFEXITED(retVal))
  854. {
  855. retVal = WEXITSTATUS(retVal);
  856. return true;
  857. }
  858. if (WIFSIGNALED(retVal))
  859. {
  860. retVal = WTERMSIG(retVal);
  861. cmOStringStream error;
  862. error << "\nProcess terminated due to ";
  863. switch (retVal)
  864. {
  865. #ifdef SIGKILL
  866. case SIGKILL:
  867. error << "SIGKILL";
  868. break;
  869. #endif
  870. #ifdef SIGFPE
  871. case SIGFPE:
  872. error << "SIGFPE";
  873. break;
  874. #endif
  875. #ifdef SIGBUS
  876. case SIGBUS:
  877. error << "SIGBUS";
  878. break;
  879. #endif
  880. #ifdef SIGSEGV
  881. case SIGSEGV:
  882. error << "SIGSEGV";
  883. break;
  884. #endif
  885. default:
  886. error << "signal " << retVal;
  887. break;
  888. }
  889. output += error.str();
  890. }
  891. return false;
  892. }
  893. #endif // endif WIN32 not CYGWIN
  894. // run a command unix uses popen (easy)
  895. // windows uses system and ShortPath
  896. bool cmSystemTools::RunCommand(const char* command,
  897. std::string& output,
  898. int &retVal,
  899. const char* dir,
  900. bool verbose,
  901. int timeout)
  902. {
  903. if(s_DisableRunCommandOutput)
  904. {
  905. verbose = false;
  906. }
  907. #if defined(WIN32) && !defined(__CYGWIN__)
  908. // if the command does not start with a quote, then
  909. // try to find the program, and if the program can not be
  910. // found use system to run the command as it must be a built in
  911. // shell command like echo or dir
  912. int count = 0;
  913. if(command[0] == '\"')
  914. {
  915. // count the number of quotes
  916. for(const char* s = command; *s != 0; ++s)
  917. {
  918. if(*s == '\"')
  919. {
  920. count++;
  921. if(count > 2)
  922. {
  923. break;
  924. }
  925. }
  926. }
  927. // if there are more than two double quotes use
  928. // GetShortPathName, the cmd.exe program in windows which
  929. // is used by system fails to execute if there are more than
  930. // one set of quotes in the arguments
  931. if(count > 2)
  932. {
  933. cmsys::RegularExpression quoted("^\"([^\"]*)\"[ \t](.*)");
  934. if(quoted.find(command))
  935. {
  936. std::string shortCmd;
  937. std::string cmd = quoted.match(1);
  938. std::string args = quoted.match(2);
  939. if(! cmSystemTools::FileExists(cmd.c_str()) )
  940. {
  941. shortCmd = cmd;
  942. }
  943. else if(!cmSystemTools::GetShortPath(cmd.c_str(), shortCmd))
  944. {
  945. cmSystemTools::Error("GetShortPath failed for " , cmd.c_str());
  946. return false;
  947. }
  948. shortCmd += " ";
  949. shortCmd += args;
  950. //return RunCommandViaSystem(shortCmd.c_str(), dir,
  951. // output, retVal, verbose);
  952. //return WindowsRunCommand(shortCmd.c_str(), dir,
  953. //output, retVal, verbose);
  954. return RunCommandViaWin32(shortCmd.c_str(), dir,
  955. output, retVal, verbose, timeout);
  956. }
  957. else
  958. {
  959. cmSystemTools::Error("Could not parse command line with quotes ",
  960. command);
  961. }
  962. }
  963. }
  964. // if there is only one set of quotes or no quotes then just run the command
  965. //return RunCommandViaSystem(command, dir, output, retVal, verbose);
  966. //return WindowsRunCommand(command, dir, output, retVal, verbose);
  967. return ::RunCommandViaWin32(command, dir, output, retVal, verbose, timeout);
  968. #else
  969. return ::RunCommandViaPopen(command, dir, output, retVal, verbose, timeout);
  970. #endif
  971. }
  972. bool cmSystemTools::DoesFileExistWithExtensions(
  973. const char* name,
  974. const std::vector<std::string>& headerExts)
  975. {
  976. std::string hname;
  977. for( std::vector<std::string>::const_iterator ext = headerExts.begin();
  978. ext != headerExts.end(); ++ext )
  979. {
  980. hname = name;
  981. hname += ".";
  982. hname += *ext;
  983. if(cmSystemTools::FileExists(hname.c_str()))
  984. {
  985. return true;
  986. }
  987. }
  988. return false;
  989. }
  990. bool cmSystemTools::cmCopyFile(const char* source, const char* destination)
  991. {
  992. return Superclass::CopyFileAlways(source, destination);
  993. }
  994. bool cmSystemTools::CopyFileIfDifferent(const char* source,
  995. const char* destination)
  996. {
  997. return Superclass::CopyFileIfDifferent(source, destination);
  998. }
  999. bool cmSystemTools::ComputeFileMD5(const char* source, char* md5out)
  1000. {
  1001. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1002. if(!cmSystemTools::FileExists(source))
  1003. {
  1004. return false;
  1005. }
  1006. // Open files
  1007. #if defined(_WIN32) || defined(__CYGWIN__)
  1008. cmsys_ios::ifstream fin(source, cmsys_ios::ios::binary | cmsys_ios::ios::in);
  1009. #else
  1010. cmsys_ios::ifstream fin(source);
  1011. #endif
  1012. if(!fin)
  1013. {
  1014. return false;
  1015. }
  1016. cmsysMD5* md5 = cmsysMD5_New();
  1017. cmsysMD5_Initialize(md5);
  1018. // Should be efficient enough on most system:
  1019. const int bufferSize = 4096;
  1020. char buffer[bufferSize];
  1021. // This copy loop is very sensitive on certain platforms with
  1022. // slightly broken stream libraries (like HPUX). Normally, it is
  1023. // incorrect to not check the error condition on the fin.read()
  1024. // before using the data, but the fin.gcount() will be zero if an
  1025. // error occurred. Therefore, the loop should be safe everywhere.
  1026. while(fin)
  1027. {
  1028. fin.read(buffer, bufferSize);
  1029. if(fin.gcount())
  1030. {
  1031. cmsysMD5_Append(md5, reinterpret_cast<unsigned char const*>(buffer),
  1032. fin.gcount());
  1033. }
  1034. }
  1035. cmsysMD5_FinalizeHex(md5, md5out);
  1036. cmsysMD5_Delete(md5);
  1037. fin.close();
  1038. return true;
  1039. #else
  1040. (void)source;
  1041. (void)md5out;
  1042. cmSystemTools::Message("md5sum not supported in bootstrapping mode","Error");
  1043. return false;
  1044. #endif
  1045. }
  1046. void cmSystemTools::Glob(const char *directory, const char *regexp,
  1047. std::vector<std::string>& files)
  1048. {
  1049. cmsys::Directory d;
  1050. cmsys::RegularExpression reg(regexp);
  1051. if (d.Load(directory))
  1052. {
  1053. size_t numf;
  1054. unsigned int i;
  1055. numf = d.GetNumberOfFiles();
  1056. for (i = 0; i < numf; i++)
  1057. {
  1058. std::string fname = d.GetFile(i);
  1059. if (reg.find(fname))
  1060. {
  1061. files.push_back(fname);
  1062. }
  1063. }
  1064. }
  1065. }
  1066. void cmSystemTools::GlobDirs(const char *fullPath,
  1067. std::vector<std::string>& files)
  1068. {
  1069. std::string path = fullPath;
  1070. std::string::size_type pos = path.find("/*");
  1071. if(pos == std::string::npos)
  1072. {
  1073. files.push_back(fullPath);
  1074. return;
  1075. }
  1076. std::string startPath = path.substr(0, pos);
  1077. std::string finishPath = path.substr(pos+2);
  1078. cmsys::Directory d;
  1079. if (d.Load(startPath.c_str()))
  1080. {
  1081. for (unsigned int i = 0; i < d.GetNumberOfFiles(); ++i)
  1082. {
  1083. if((std::string(d.GetFile(i)) != ".")
  1084. && (std::string(d.GetFile(i)) != ".."))
  1085. {
  1086. std::string fname = startPath;
  1087. fname +="/";
  1088. fname += d.GetFile(i);
  1089. if(cmSystemTools::FileIsDirectory(fname.c_str()))
  1090. {
  1091. fname += finishPath;
  1092. cmSystemTools::GlobDirs(fname.c_str(), files);
  1093. }
  1094. }
  1095. }
  1096. }
  1097. }
  1098. void cmSystemTools::ExpandList(std::vector<std::string> const& arguments,
  1099. std::vector<std::string>& newargs)
  1100. {
  1101. std::vector<std::string>::const_iterator i;
  1102. for(i = arguments.begin();i != arguments.end(); ++i)
  1103. {
  1104. cmSystemTools::ExpandListArgument(*i, newargs);
  1105. }
  1106. }
  1107. void cmSystemTools::ExpandListArgument(const std::string& arg,
  1108. std::vector<std::string>& newargs,
  1109. bool emptyArgs)
  1110. {
  1111. // If argument is empty, it is an empty list.
  1112. if(arg.length() == 0 && !emptyArgs)
  1113. {
  1114. return;
  1115. }
  1116. // if there are no ; in the name then just copy the current string
  1117. if(arg.find(';') == std::string::npos)
  1118. {
  1119. newargs.push_back(arg);
  1120. return;
  1121. }
  1122. std::vector<char> newArgVec;
  1123. // Break the string at non-escaped semicolons not nested in [].
  1124. int squareNesting = 0;
  1125. for(const char* c = arg.c_str(); *c; ++c)
  1126. {
  1127. switch(*c)
  1128. {
  1129. case '\\':
  1130. {
  1131. // We only want to allow escaping of semicolons. Other
  1132. // escapes should not be processed here.
  1133. ++c;
  1134. if(*c == ';')
  1135. {
  1136. newArgVec.push_back(*c);
  1137. }
  1138. else
  1139. {
  1140. newArgVec.push_back('\\');
  1141. if(*c)
  1142. {
  1143. newArgVec.push_back(*c);
  1144. }
  1145. else
  1146. {
  1147. // Terminate the loop properly.
  1148. --c;
  1149. }
  1150. }
  1151. } break;
  1152. case '[':
  1153. {
  1154. ++squareNesting;
  1155. newArgVec.push_back(*c);
  1156. } break;
  1157. case ']':
  1158. {
  1159. --squareNesting;
  1160. newArgVec.push_back(*c);
  1161. } break;
  1162. case ';':
  1163. {
  1164. // Break the string here if we are not nested inside square
  1165. // brackets.
  1166. if(squareNesting == 0)
  1167. {
  1168. if ( newArgVec.size() || emptyArgs )
  1169. {
  1170. // Add the last argument if the string is not empty.
  1171. newArgVec.push_back(0);
  1172. newargs.push_back(&*newArgVec.begin());
  1173. newArgVec.clear();
  1174. }
  1175. }
  1176. else
  1177. {
  1178. newArgVec.push_back(*c);
  1179. }
  1180. } break;
  1181. default:
  1182. {
  1183. // Just append this character.
  1184. newArgVec.push_back(*c);
  1185. } break;
  1186. }
  1187. }
  1188. if ( newArgVec.size() || emptyArgs )
  1189. {
  1190. // Add the last argument if the string is not empty.
  1191. newArgVec.push_back(0);
  1192. newargs.push_back(&*newArgVec.begin());
  1193. }
  1194. }
  1195. bool cmSystemTools::SimpleGlob(const cmStdString& glob,
  1196. std::vector<cmStdString>& files,
  1197. int type /* = 0 */)
  1198. {
  1199. files.clear();
  1200. if ( glob[glob.size()-1] != '*' )
  1201. {
  1202. return false;
  1203. }
  1204. std::string path = cmSystemTools::GetFilenamePath(glob);
  1205. std::string ppath = cmSystemTools::GetFilenameName(glob);
  1206. ppath = ppath.substr(0, ppath.size()-1);
  1207. if ( path.size() == 0 )
  1208. {
  1209. path = "/";
  1210. }
  1211. bool res = false;
  1212. cmsys::Directory d;
  1213. if (d.Load(path.c_str()))
  1214. {
  1215. for (unsigned int i = 0; i < d.GetNumberOfFiles(); ++i)
  1216. {
  1217. if((std::string(d.GetFile(i)) != ".")
  1218. && (std::string(d.GetFile(i)) != ".."))
  1219. {
  1220. std::string fname = path;
  1221. if ( path[path.size()-1] != '/' )
  1222. {
  1223. fname +="/";
  1224. }
  1225. fname += d.GetFile(i);
  1226. std::string sfname = d.GetFile(i);
  1227. if ( type > 0 && cmSystemTools::FileIsDirectory(fname.c_str()) )
  1228. {
  1229. continue;
  1230. }
  1231. if ( type < 0 && !cmSystemTools::FileIsDirectory(fname.c_str()) )
  1232. {
  1233. continue;
  1234. }
  1235. if ( sfname.size() >= ppath.size() &&
  1236. sfname.substr(0, ppath.size()) ==
  1237. ppath )
  1238. {
  1239. files.push_back(fname);
  1240. res = true;
  1241. }
  1242. }
  1243. }
  1244. }
  1245. return res;
  1246. }
  1247. cmSystemTools::FileFormat cmSystemTools::GetFileFormat(const char* cext)
  1248. {
  1249. if ( ! cext || *cext == 0 )
  1250. {
  1251. return cmSystemTools::NO_FILE_FORMAT;
  1252. }
  1253. //std::string ext = cmSystemTools::LowerCase(cext);
  1254. std::string ext = cext;
  1255. if ( ext == "c" || ext == ".c" ) { return cmSystemTools::C_FILE_FORMAT; }
  1256. if (
  1257. ext == "C" || ext == ".C" ||
  1258. ext == "M" || ext == ".M" ||
  1259. ext == "c++" || ext == ".c++" ||
  1260. ext == "cc" || ext == ".cc" ||
  1261. ext == "cpp" || ext == ".cpp" ||
  1262. ext == "cxx" || ext == ".cxx" ||
  1263. ext == "m" || ext == ".m" ||
  1264. ext == "mm" || ext == ".mm"
  1265. ) { return cmSystemTools::CXX_FILE_FORMAT; }
  1266. if (
  1267. ext == "f" || ext == ".f" ||
  1268. ext == "F" || ext == ".F" ||
  1269. ext == "f77" || ext == ".f77" ||
  1270. ext == "f90" || ext == ".f90" ||
  1271. ext == "for" || ext == ".for" ||
  1272. ext == "f95" || ext == ".f95"
  1273. ) { return cmSystemTools::FORTRAN_FILE_FORMAT; }
  1274. if ( ext == "java" || ext == ".java" )
  1275. { return cmSystemTools::JAVA_FILE_FORMAT; }
  1276. if (
  1277. ext == "H" || ext == ".H" ||
  1278. ext == "h" || ext == ".h" ||
  1279. ext == "h++" || ext == ".h++" ||
  1280. ext == "hm" || ext == ".hm" ||
  1281. ext == "hpp" || ext == ".hpp" ||
  1282. ext == "hxx" || ext == ".hxx" ||
  1283. ext == "in" || ext == ".in" ||
  1284. ext == "txx" || ext == ".txx"
  1285. ) { return cmSystemTools::HEADER_FILE_FORMAT; }
  1286. if ( ext == "rc" || ext == ".rc" )
  1287. { return cmSystemTools::RESOURCE_FILE_FORMAT; }
  1288. if ( ext == "def" || ext == ".def" )
  1289. { return cmSystemTools::DEFINITION_FILE_FORMAT; }
  1290. if ( ext == "lib" || ext == ".lib" ||
  1291. ext == "a" || ext == ".a")
  1292. { return cmSystemTools::STATIC_LIBRARY_FILE_FORMAT; }
  1293. if ( ext == "o" || ext == ".o" ||
  1294. ext == "obj" || ext == ".obj")
  1295. { return cmSystemTools::OBJECT_FILE_FORMAT; }
  1296. #ifdef __APPLE__
  1297. if ( ext == "dylib" || ext == ".dylib" )
  1298. { return cmSystemTools::SHARED_LIBRARY_FILE_FORMAT; }
  1299. if ( ext == "so" || ext == ".so" ||
  1300. ext == "bundle" || ext == ".bundle" )
  1301. { return cmSystemTools::MODULE_FILE_FORMAT; }
  1302. #else // __APPLE__
  1303. if ( ext == "so" || ext == ".so" ||
  1304. ext == "sl" || ext == ".sl" ||
  1305. ext == "dll" || ext == ".dll" )
  1306. { return cmSystemTools::SHARED_LIBRARY_FILE_FORMAT; }
  1307. #endif // __APPLE__
  1308. return cmSystemTools::UNKNOWN_FILE_FORMAT;
  1309. }
  1310. bool cmSystemTools::Split(const char* s, std::vector<cmStdString>& l)
  1311. {
  1312. std::vector<std::string> temp;
  1313. bool res = Superclass::Split(s, temp);
  1314. for(std::vector<std::string>::const_iterator i = temp.begin();
  1315. i != temp.end(); ++i)
  1316. {
  1317. l.push_back(*i);
  1318. }
  1319. return res;
  1320. }
  1321. std::string cmSystemTools::ConvertToOutputPath(const char* path)
  1322. {
  1323. #if defined(_WIN32) && !defined(__CYGWIN__)
  1324. if(s_ForceUnixPaths)
  1325. {
  1326. return cmSystemTools::ConvertToUnixOutputPath(path);
  1327. }
  1328. return cmSystemTools::ConvertToWindowsOutputPath(path);
  1329. #else
  1330. return cmSystemTools::ConvertToUnixOutputPath(path);
  1331. #endif
  1332. }
  1333. void cmSystemTools::ConvertToOutputSlashes(std::string& path)
  1334. {
  1335. #if defined(_WIN32) && !defined(__CYGWIN__)
  1336. if(!s_ForceUnixPaths)
  1337. {
  1338. // Convert to windows slashes.
  1339. std::string::size_type pos = 0;
  1340. while((pos = path.find('/', pos)) != std::string::npos)
  1341. {
  1342. path[pos++] = '\\';
  1343. }
  1344. }
  1345. #else
  1346. static_cast<void>(path);
  1347. #endif
  1348. }
  1349. std::string cmSystemTools::ConvertToRunCommandPath(const char* path)
  1350. {
  1351. #if defined(_WIN32) && !defined(__CYGWIN__)
  1352. return cmSystemTools::ConvertToWindowsOutputPath(path);
  1353. #else
  1354. return cmSystemTools::ConvertToUnixOutputPath(path);
  1355. #endif
  1356. }
  1357. bool cmSystemTools::StringEndsWith(const char* str1, const char* str2)
  1358. {
  1359. if ( !str1 || !str2 || strlen(str1) < strlen(str2) )
  1360. {
  1361. return 0;
  1362. }
  1363. return !strncmp(str1 + (strlen(str1)-strlen(str2)), str2, strlen(str2));
  1364. }
  1365. // compute the relative path from here to there
  1366. std::string cmSystemTools::RelativePath(const char* local, const char* remote)
  1367. {
  1368. if(!cmSystemTools::FileIsFullPath(local))
  1369. {
  1370. cmSystemTools::Error("RelativePath must be passed a full path to local: ",
  1371. local);
  1372. }
  1373. if(!cmSystemTools::FileIsFullPath(remote))
  1374. {
  1375. cmSystemTools::Error
  1376. ("RelativePath must be passed a full path to remote: ", remote);
  1377. }
  1378. return cmsys::SystemTools::RelativePath(local, remote);
  1379. }
  1380. class cmDeletingCharVector : public std::vector<char*>
  1381. {
  1382. public:
  1383. ~cmDeletingCharVector()
  1384. {
  1385. for(std::vector<char*>::iterator i = this->begin();
  1386. i != this->end(); ++i)
  1387. {
  1388. delete []*i;
  1389. }
  1390. }
  1391. };
  1392. bool cmSystemTools::PutEnv(const char* value)
  1393. {
  1394. static cmDeletingCharVector localEnvironment;
  1395. char* envVar = new char[strlen(value)+1];
  1396. strcpy(envVar, value);
  1397. int ret = putenv(envVar);
  1398. // save the pointer in the static vector so that it can
  1399. // be deleted on exit
  1400. localEnvironment.push_back(envVar);
  1401. return ret == 0;
  1402. }
  1403. #ifdef CMAKE_BUILD_WITH_CMAKE
  1404. bool cmSystemTools::UnsetEnv(const char* value)
  1405. {
  1406. #if !defined(HAVE_UNSETENV)
  1407. std::string var = value;
  1408. var += "=";
  1409. return cmSystemTools::PutEnv(var.c_str());
  1410. #else
  1411. unsetenv(value);
  1412. return true;
  1413. #endif
  1414. }
  1415. std::vector<std::string> cmSystemTools::GetEnvironmentVariables()
  1416. {
  1417. std::vector<std::string> env;
  1418. int cc;
  1419. for ( cc = 0; environ[cc]; ++ cc )
  1420. {
  1421. env.push_back(environ[cc]);
  1422. }
  1423. return env;
  1424. }
  1425. #endif
  1426. void cmSystemTools::EnableVSConsoleOutput()
  1427. {
  1428. // Visual Studio 8 2005 (devenv.exe or VCExpress.exe) will not
  1429. // display output to the console unless this environment variable is
  1430. // set. We need it to capture the output of these build tools.
  1431. // Note for future work that one could pass "/out \\.\pipe\NAME" to
  1432. // either of these executables where NAME is created with
  1433. // CreateNamedPipe. This would bypass the internal buffering of the
  1434. // output and allow it to be captured on the fly.
  1435. #ifdef _WIN32
  1436. cmSystemTools::PutEnv("vsconsoleoutput=1");
  1437. #endif
  1438. }
  1439. std::string cmSystemTools::MakeXMLSafe(const char* str)
  1440. {
  1441. std::vector<char> result;
  1442. result.reserve(500);
  1443. const char* pos = str;
  1444. for ( ;*pos; ++pos)
  1445. {
  1446. char ch = *pos;
  1447. if ( (ch > 126 || ch < 32) && ch != 9 && ch != 10 && ch != 13
  1448. && ch != '\r' )
  1449. {
  1450. char buffer[33];
  1451. sprintf(buffer, "&lt;%d&gt;", static_cast<int>(ch));
  1452. //sprintf(buffer, "&#x%0x;", (unsigned int)ch);
  1453. result.insert(result.end(), buffer, buffer+strlen(buffer));
  1454. }
  1455. else
  1456. {
  1457. const char* const encodedChars[] = {
  1458. "&amp;",
  1459. "&lt;",
  1460. "&gt;"
  1461. };
  1462. switch ( ch )
  1463. {
  1464. case '&':
  1465. result.insert(result.end(), encodedChars[0], encodedChars[0]+5);
  1466. break;
  1467. case '<':
  1468. result.insert(result.end(), encodedChars[1], encodedChars[1]+4);
  1469. break;
  1470. case '>':
  1471. result.insert(result.end(), encodedChars[2], encodedChars[2]+4);
  1472. break;
  1473. case '\n':
  1474. result.push_back('\n');
  1475. break;
  1476. case '\r': break; // Ignore \r
  1477. default:
  1478. result.push_back(ch);
  1479. }
  1480. }
  1481. }
  1482. if ( result.size() == 0 )
  1483. {
  1484. return "";
  1485. }
  1486. return std::string(&*result.begin(), result.size());
  1487. }
  1488. bool cmSystemTools::IsPathToFramework(const char* path)
  1489. {
  1490. if(cmSystemTools::FileIsFullPath(path))
  1491. {
  1492. std::string libname = path;
  1493. if(libname.find(".framework") == libname.size()+1-sizeof(".framework"))
  1494. {
  1495. return true;
  1496. }
  1497. }
  1498. return false;
  1499. }
  1500. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1501. struct cmSystemToolsGZStruct
  1502. {
  1503. gzFile GZFile;
  1504. };
  1505. extern "C" {
  1506. int cmSystemToolsGZStructOpen(void* call_data, const char *pathname,
  1507. int oflags, mode_t mode);
  1508. int cmSystemToolsGZStructClose(void* call_data);
  1509. ssize_t cmSystemToolsGZStructRead(void* call_data, void* buf, size_t count);
  1510. ssize_t cmSystemToolsGZStructWrite(void* call_data, const void* buf,
  1511. size_t count);
  1512. }
  1513. int cmSystemToolsGZStructOpen(void* call_data, const char *pathname,
  1514. int oflags, mode_t mode)
  1515. {
  1516. const char *gzoflags;
  1517. int fd;
  1518. cmSystemToolsGZStruct* gzf = static_cast<cmSystemToolsGZStruct*>(call_data);
  1519. switch (oflags & O_ACCMODE)
  1520. {
  1521. case O_WRONLY:
  1522. gzoflags = "wb";
  1523. break;
  1524. case O_RDONLY:
  1525. gzoflags = "rb";
  1526. break;
  1527. default:
  1528. case O_RDWR:
  1529. errno = EINVAL;
  1530. return -1;
  1531. }
  1532. fd = open(pathname, oflags, mode);
  1533. if (fd == -1)
  1534. {
  1535. return -1;
  1536. }
  1537. // no fchmod on BeOS 5...do pathname instead.
  1538. #if defined(__BEOS__) && !defined(__ZETA__)
  1539. if ((oflags & O_CREAT) && chmod(pathname, mode))
  1540. {
  1541. return -1;
  1542. }
  1543. #elif !defined(_WIN32) || defined(__CYGWIN__)
  1544. if ((oflags & O_CREAT) && fchmod(fd, mode))
  1545. {
  1546. return -1;
  1547. }
  1548. #endif
  1549. gzf->GZFile = gzdopen(fd, gzoflags);
  1550. if (!gzf->GZFile)
  1551. {
  1552. errno = ENOMEM;
  1553. return -1;
  1554. }
  1555. return fd;
  1556. }
  1557. int cmSystemToolsGZStructClose(void* call_data)
  1558. {
  1559. cmSystemToolsGZStruct* gzf = static_cast<cmSystemToolsGZStruct*>(call_data);
  1560. return gzclose(gzf->GZFile);
  1561. }
  1562. ssize_t cmSystemToolsGZStructRead(void* call_data, void* buf, size_t count)
  1563. {
  1564. cmSystemToolsGZStruct* gzf = static_cast<cmSystemToolsGZStruct*>(call_data);
  1565. return gzread(gzf->GZFile, buf, static_cast<int>(count));
  1566. }
  1567. ssize_t cmSystemToolsGZStructWrite(void* call_data, const void* buf,
  1568. size_t count)
  1569. {
  1570. cmSystemToolsGZStruct* gzf = static_cast<cmSystemToolsGZStruct*>(call_data);
  1571. return gzwrite(gzf->GZFile, (void*)buf, static_cast<int>(count));
  1572. }
  1573. #endif
  1574. bool cmSystemTools::CreateTar(const char* outFileName,
  1575. const std::vector<cmStdString>& files,
  1576. bool gzip, bool verbose)
  1577. {
  1578. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1579. TAR *t;
  1580. char buf[TAR_MAXPATHLEN];
  1581. char pathname[TAR_MAXPATHLEN];
  1582. cmSystemToolsGZStruct gzs;
  1583. tartype_t gztype = {
  1584. (openfunc_t)cmSystemToolsGZStructOpen,
  1585. (closefunc_t)cmSystemToolsGZStructClose,
  1586. (readfunc_t)cmSystemToolsGZStructRead,
  1587. (writefunc_t)cmSystemToolsGZStructWrite,
  1588. &gzs
  1589. };
  1590. // Ok, this libtar is not const safe. for now use auto_ptr hack
  1591. char* realName = new char[ strlen(outFileName) + 1 ];
  1592. std::auto_ptr<char> realNamePtr(realName);
  1593. strcpy(realName, outFileName);
  1594. int options = 0;
  1595. if(verbose)
  1596. {
  1597. options |= TAR_VERBOSE;
  1598. }
  1599. #ifdef __CYGWIN__
  1600. options |= TAR_GNU;
  1601. #endif
  1602. if (tar_open(&t, realName,
  1603. (gzip? &gztype : NULL),
  1604. O_WRONLY | O_CREAT, 0644,
  1605. options) == -1)
  1606. {
  1607. cmSystemTools::Error("Problem with tar_open(): ", strerror(errno));
  1608. return false;
  1609. }
  1610. std::vector<cmStdString>::const_iterator it;
  1611. for (it = files.begin(); it != files.end(); ++ it )
  1612. {
  1613. strncpy(pathname, it->c_str(), sizeof(pathname));
  1614. pathname[sizeof(pathname)-1] = 0;
  1615. strncpy(buf, pathname, sizeof(buf));
  1616. buf[sizeof(buf)-1] = 0;
  1617. if (tar_append_tree(t, buf, pathname) != 0)
  1618. {
  1619. cmOStringStream ostr;
  1620. ostr << "Problem with tar_append_tree(\"" << buf << "\", \""
  1621. << pathname << "\"): "
  1622. << strerror(errno);
  1623. cmSystemTools::Error(ostr.str().c_str());
  1624. tar_close(t);
  1625. return false;
  1626. }
  1627. }
  1628. if (tar_append_eof(t) != 0)
  1629. {
  1630. cmSystemTools::Error("Problem with tar_append_eof(): ", strerror(errno));
  1631. tar_close(t);
  1632. return false;
  1633. }
  1634. if (tar_close(t) != 0)
  1635. {
  1636. cmSystemTools::Error("Problem with tar_close(): ", strerror(errno));
  1637. return false;
  1638. }
  1639. return true;
  1640. #else
  1641. (void)outFileName;
  1642. (void)files;
  1643. (void)gzip;
  1644. (void)verbose;
  1645. return false;
  1646. #endif
  1647. }
  1648. bool cmSystemTools::ExtractTar(const char* outFileName,
  1649. const std::vector<cmStdString>& files,
  1650. bool gzip, bool verbose)
  1651. {
  1652. (void)files;
  1653. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1654. TAR *t;
  1655. cmSystemToolsGZStruct gzs;
  1656. tartype_t gztype = {
  1657. cmSystemToolsGZStructOpen,
  1658. cmSystemToolsGZStructClose,
  1659. cmSystemToolsGZStructRead,
  1660. cmSystemToolsGZStructWrite,
  1661. &gzs
  1662. };
  1663. // Ok, this libtar is not const safe. for now use auto_ptr hack
  1664. char* realName = new char[ strlen(outFileName) + 1 ];
  1665. std::auto_ptr<char> realNamePtr(realName);
  1666. strcpy(realName, outFileName);
  1667. if (tar_open(&t, realName,
  1668. (gzip? &gztype : NULL),
  1669. O_RDONLY
  1670. #ifdef _WIN32
  1671. | O_BINARY
  1672. #endif
  1673. , 0,
  1674. (verbose?TAR_VERBOSE:0)
  1675. | 0) == -1)
  1676. {
  1677. cmSystemTools::Error("Problem with tar_open(): ", strerror(errno));
  1678. return false;
  1679. }
  1680. if (tar_extract_all(t, 0) != 0)
  1681. {
  1682. cmSystemTools::Error("Problem with tar_extract_all(): ", strerror(errno));
  1683. return false;
  1684. }
  1685. if (tar_close(t) != 0)
  1686. {
  1687. cmSystemTools::Error("Problem with tar_close(): ", strerror(errno));
  1688. return false;
  1689. }
  1690. return true;
  1691. #else
  1692. (void)outFileName;
  1693. (void)gzip;
  1694. (void)verbose;
  1695. return false;
  1696. #endif
  1697. }
  1698. bool cmSystemTools::ListTar(const char* outFileName,
  1699. std::vector<cmStdString>& files, bool gzip,
  1700. bool verbose)
  1701. {
  1702. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1703. TAR *t;
  1704. cmSystemToolsGZStruct gzs;
  1705. tartype_t gztype = {
  1706. cmSystemToolsGZStructOpen,
  1707. cmSystemToolsGZStructClose,
  1708. cmSystemToolsGZStructRead,
  1709. cmSystemToolsGZStructWrite,
  1710. &gzs
  1711. };
  1712. // Ok, this libtar is not const safe. for now use auto_ptr hack
  1713. char* realName = new char[ strlen(outFileName) + 1 ];
  1714. std::auto_ptr<char> realNamePtr(realName);
  1715. strcpy(realName, outFileName);
  1716. if (tar_open(&t, realName,
  1717. (gzip? &gztype : NULL),
  1718. O_RDONLY
  1719. #ifdef _WIN32
  1720. | O_BINARY
  1721. #endif
  1722. , 0,
  1723. (verbose?TAR_VERBOSE:0)
  1724. | 0) == -1)
  1725. {
  1726. cmSystemTools::Error("Problem with tar_open(): ", strerror(errno));
  1727. return false;
  1728. }
  1729. while ((th_read(t)) == 0)
  1730. {
  1731. const char* filename = th_get_pathname(t);
  1732. files.push_back(filename);
  1733. if ( verbose )
  1734. {
  1735. th_print_long_ls(t);
  1736. }
  1737. else
  1738. {
  1739. std::cout << filename << std::endl;
  1740. }
  1741. #ifdef DEBUG
  1742. th_print(t);
  1743. #endif
  1744. if (TH_ISREG(t) && tar_skip_regfile(t) != 0)
  1745. {
  1746. cmSystemTools::Error("Problem with tar_skip_regfile(): ",
  1747. strerror(errno));
  1748. return false;
  1749. }
  1750. }
  1751. if (tar_close(t) != 0)
  1752. {
  1753. cmSystemTools::Error("Problem with tar_close(): ", strerror(errno));
  1754. return false;
  1755. }
  1756. return true;
  1757. #else
  1758. (void)outFileName;
  1759. (void)files;
  1760. (void)gzip;
  1761. (void)verbose;
  1762. return false;
  1763. #endif
  1764. }
  1765. int cmSystemTools::WaitForLine(cmsysProcess* process, std::string& line,
  1766. double timeout,
  1767. std::vector<char>& out,
  1768. std::vector<char>& err)
  1769. {
  1770. line = "";
  1771. std::vector<char>::iterator outiter = out.begin();
  1772. std::vector<char>::iterator erriter = err.begin();
  1773. while(1)
  1774. {
  1775. // Check for a newline in stdout.
  1776. for(;outiter != out.end(); ++outiter)
  1777. {
  1778. if((*outiter == '\r') && ((outiter+1) == out.end()))
  1779. {
  1780. break;
  1781. }
  1782. else if(*outiter == '\n' || *outiter == '\0')
  1783. {
  1784. int length = outiter-out.begin();
  1785. if(length > 1 && *(outiter-1) == '\r')
  1786. {
  1787. --length;
  1788. }
  1789. if(length > 0)
  1790. {
  1791. line.append(&out[0], length);
  1792. }
  1793. out.erase(out.begin(), outiter+1);
  1794. return cmsysProcess_Pipe_STDOUT;
  1795. }
  1796. }
  1797. // Check for a newline in stderr.
  1798. for(;erriter != err.end(); ++erriter)
  1799. {
  1800. if((*erriter == '\r') && ((erriter+1) == err.end()))
  1801. {
  1802. break;
  1803. }
  1804. else if(*erriter == '\n' || *erriter == '\0')
  1805. {
  1806. int length = erriter-err.begin();
  1807. if(length > 1 && *(erriter-1) == '\r')
  1808. {
  1809. --length;
  1810. }
  1811. if(length > 0)
  1812. {
  1813. line.append(&err[0], length);
  1814. }
  1815. err.erase(err.begin(), erriter+1);
  1816. return cmsysProcess_Pipe_STDERR;
  1817. }
  1818. }
  1819. // No newlines found. Wait for more data from the process.
  1820. int length;
  1821. char* data;
  1822. int pipe = cmsysProcess_WaitForData(process, &data, &length, &timeout);
  1823. if(pipe == cmsysProcess_Pipe_Timeout)
  1824. {
  1825. // Timeout has been exceeded.
  1826. return pipe;
  1827. }
  1828. else if(pipe == cmsysProcess_Pipe_STDOUT)
  1829. {
  1830. // Append to the stdout buffer.
  1831. std::vector<char>::size_type size = out.size();
  1832. out.insert(out.end(), data, data+length);
  1833. outiter = out.begin()+size;
  1834. }
  1835. else if(pipe == cmsysProcess_Pipe_STDERR)
  1836. {
  1837. // Append to the stderr buffer.
  1838. std::vector<char>::size_type size = err.size();
  1839. err.insert(err.end(), data, data+length);
  1840. erriter = err.begin()+size;
  1841. }
  1842. else if(pipe == cmsysProcess_Pipe_None)
  1843. {
  1844. // Both stdout and stderr pipes have broken. Return leftover data.
  1845. if(!out.empty())
  1846. {
  1847. line.append(&out[0], outiter-out.begin());
  1848. out.erase(out.begin(), out.end());
  1849. return cmsysProcess_Pipe_STDOUT;
  1850. }
  1851. else if(!err.empty())
  1852. {
  1853. line.append(&err[0], erriter-err.begin());
  1854. err.erase(err.begin(), err.end());
  1855. return cmsysProcess_Pipe_STDERR;
  1856. }
  1857. else
  1858. {
  1859. return cmsysProcess_Pipe_None;
  1860. }
  1861. }
  1862. }
  1863. }
  1864. void cmSystemTools::DoNotInheritStdPipes()
  1865. {
  1866. #ifdef _WIN32
  1867. // Check to see if we are attached to a console
  1868. // if so, then do not stop the inherited pipes
  1869. // or stdout and stderr will not show up in dos
  1870. // shell windows
  1871. CONSOLE_SCREEN_BUFFER_INFO hOutInfo;
  1872. HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  1873. if(GetConsoleScreenBufferInfo(hOut, &hOutInfo))
  1874. {
  1875. return;
  1876. }
  1877. {
  1878. HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
  1879. DuplicateHandle(GetCurrentProcess(), out,
  1880. GetCurrentProcess(), &out, 0, FALSE,
  1881. DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE);
  1882. SetStdHandle(STD_OUTPUT_HANDLE, out);
  1883. }
  1884. {
  1885. HANDLE out = GetStdHandle(STD_ERROR_HANDLE);
  1886. DuplicateHandle(GetCurrentProcess(), out,
  1887. GetCurrentProcess(), &out, 0, FALSE,
  1888. DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE);
  1889. SetStdHandle(STD_ERROR_HANDLE, out);
  1890. }
  1891. #endif
  1892. }
  1893. //----------------------------------------------------------------------------
  1894. bool cmSystemTools::CopyFileTime(const char* fromFile, const char* toFile)
  1895. {
  1896. #if defined(_WIN32) && !defined(__CYGWIN__)
  1897. cmSystemToolsWindowsHandle hFrom =
  1898. CreateFile(fromFile, GENERIC_READ, FILE_SHARE_READ, 0,
  1899. OPEN_EXISTING, 0, 0);
  1900. cmSystemToolsWindowsHandle hTo =
  1901. CreateFile(toFile, GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
  1902. if(!hFrom || !hTo)
  1903. {
  1904. return false;
  1905. }
  1906. FILETIME timeCreation;
  1907. FILETIME timeLastAccess;
  1908. FILETIME timeLastWrite;
  1909. if(!GetFileTime(hFrom, &timeCreation, &timeLastAccess, &timeLastWrite))
  1910. {
  1911. return false;
  1912. }
  1913. if(!SetFileTime(hTo, &timeCreation, &timeLastAccess, &timeLastWrite))
  1914. {
  1915. return false;
  1916. }
  1917. #else
  1918. struct stat fromStat;
  1919. if(stat(fromFile, &fromStat) < 0)
  1920. {
  1921. return false;
  1922. }
  1923. struct utimbuf buf;
  1924. buf.actime = fromStat.st_atime;
  1925. buf.modtime = fromStat.st_mtime;
  1926. if(utime(toFile, &buf) < 0)
  1927. {
  1928. return false;
  1929. }
  1930. #endif
  1931. return true;
  1932. }
  1933. //----------------------------------------------------------------------------
  1934. static std::string cmSystemToolsExecutableDirectory;
  1935. void cmSystemTools::FindExecutableDirectory(const char* argv0)
  1936. {
  1937. std::string errorMsg;
  1938. std::string exe;
  1939. if(cmSystemTools::FindProgramPath(argv0, exe, errorMsg))
  1940. {
  1941. cmSystemToolsExecutableDirectory =
  1942. cmSystemTools::GetFilenamePath(exe.c_str());
  1943. }
  1944. else
  1945. {
  1946. // ???
  1947. }
  1948. }
  1949. //----------------------------------------------------------------------------
  1950. const char* cmSystemTools::GetExecutableDirectory()
  1951. {
  1952. return cmSystemToolsExecutableDirectory.c_str();
  1953. }
  1954. //----------------------------------------------------------------------------
  1955. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1956. void cmSystemTools::MakefileColorEcho(int color, const char* message,
  1957. bool newline, bool enabled)
  1958. {
  1959. // On some platforms (an MSYS prompt) cmsysTerminal may not be able
  1960. // to determine whether the stream is displayed on a tty. In this
  1961. // case it assumes no unless we tell it otherwise. Since we want
  1962. // color messages to be displayed for users we will assume yes.
  1963. // However, we can test for some situations when the answer is most
  1964. // likely no.
  1965. int assumeTTY = cmsysTerminal_Color_AssumeTTY;
  1966. if(cmSystemTools::GetEnv("DART_TEST_FROM_DART") ||
  1967. cmSystemTools::GetEnv("DASHBOARD_TEST_FROM_CTEST") ||
  1968. cmSystemTools::GetEnv("CTEST_INTERACTIVE_DEBUG_MODE"))
  1969. {
  1970. // Avoid printing color escapes during dashboard builds.
  1971. assumeTTY = 0;
  1972. }
  1973. if(enabled)
  1974. {
  1975. cmsysTerminal_cfprintf(color | assumeTTY, stdout, "%s%s",
  1976. message, newline? "\n" : "");
  1977. }
  1978. else
  1979. {
  1980. // Color is disabled. Print without color.
  1981. fprintf(stdout, "%s%s", message, newline? "\n" : "");
  1982. }
  1983. }
  1984. #endif