cmSystemTools.cxx 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm 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 "errno.h"
  15. #include <stdio.h>
  16. #include <sys/stat.h>
  17. #include "cmRegularExpression.h"
  18. #include <ctype.h>
  19. #include "cmDirectory.h"
  20. #include "cmMakefile.h"
  21. // support for realpath call
  22. #ifndef _WIN32
  23. #include <limits.h>
  24. #include <stdlib.h>
  25. #include <sys/param.h>
  26. #include <sys/wait.h>
  27. #endif
  28. #if defined(_WIN32) && (defined(_MSC_VER) || defined(__BORLANDC__))
  29. #include <string.h>
  30. #include <windows.h>
  31. #include <direct.h>
  32. #define _unlink unlink
  33. inline int Mkdir(const char* dir)
  34. {
  35. return _mkdir(dir);
  36. }
  37. inline const char* Getcwd(char* buf, unsigned int len)
  38. {
  39. return _getcwd(buf, len);
  40. }
  41. inline int Chdir(const char* dir)
  42. {
  43. #if defined(__BORLANDC__)
  44. return chdir(dir);
  45. #else
  46. return _chdir(dir);
  47. #endif
  48. }
  49. #else
  50. #include <sys/types.h>
  51. #include <fcntl.h>
  52. #include <unistd.h>
  53. inline int Mkdir(const char* dir)
  54. {
  55. return mkdir(dir, 00777);
  56. }
  57. inline const char* Getcwd(char* buf, unsigned int len)
  58. {
  59. return getcwd(buf, len);
  60. }
  61. inline int Chdir(const char* dir)
  62. {
  63. return chdir(dir);
  64. }
  65. #endif
  66. bool cmSystemTools::s_DisableRunCommandOutput = false;
  67. bool cmSystemTools::s_ErrorOccured = false;
  68. bool cmSystemTools::s_DisableMessages = false;
  69. std::string cmSystemTools::s_Windows9xComspecSubstitute = "command.com";
  70. void cmSystemTools::SetWindows9xComspecSubstitute(const char* str)
  71. {
  72. if ( str )
  73. {
  74. cmSystemTools::s_Windows9xComspecSubstitute = str;
  75. }
  76. }
  77. const char* cmSystemTools::GetWindows9xComspecSubstitute()
  78. {
  79. return cmSystemTools::s_Windows9xComspecSubstitute.c_str();
  80. }
  81. void (*cmSystemTools::s_ErrorCallback)(const char*, const char*, bool&, void*);
  82. void* cmSystemTools::s_ErrorCallbackClientData = 0;
  83. // adds the elements of the env variable path to the arg passed in
  84. void cmSystemTools::GetPath(std::vector<std::string>& path)
  85. {
  86. #if defined(_WIN32) && !defined(__CYGWIN__)
  87. const char* pathSep = ";";
  88. #else
  89. const char* pathSep = ":";
  90. #endif
  91. std::string pathEnv = getenv("PATH");
  92. // A hack to make the below algorithm work.
  93. if(pathEnv[pathEnv.length()-1] != ':')
  94. {
  95. pathEnv += pathSep;
  96. }
  97. std::string::size_type start =0;
  98. bool done = false;
  99. while(!done)
  100. {
  101. std::string::size_type endpos = pathEnv.find(pathSep, start);
  102. if(endpos != std::string::npos)
  103. {
  104. path.push_back(pathEnv.substr(start, endpos-start));
  105. start = endpos+1;
  106. }
  107. else
  108. {
  109. done = true;
  110. }
  111. }
  112. for(std::vector<std::string>::iterator i = path.begin();
  113. i != path.end(); ++i)
  114. {
  115. cmSystemTools::ConvertToUnixSlashes(*i);
  116. }
  117. }
  118. const char* cmSystemTools::GetExecutableExtension()
  119. {
  120. #if defined(_WIN32) || defined(__CYGWIN__)
  121. return ".exe";
  122. #else
  123. return "";
  124. #endif
  125. }
  126. bool cmSystemTools::MakeDirectory(const char* path)
  127. {
  128. if(cmSystemTools::FileExists(path))
  129. {
  130. return true;
  131. }
  132. std::string dir = path;
  133. if(dir.size() == 0)
  134. {
  135. return false;
  136. }
  137. cmSystemTools::ConvertToUnixSlashes(dir);
  138. std::string::size_type pos = dir.find(':');
  139. if(pos == std::string::npos)
  140. {
  141. pos = 0;
  142. }
  143. std::string topdir;
  144. while((pos = dir.find('/', pos)) != std::string::npos)
  145. {
  146. topdir = dir.substr(0, pos);
  147. Mkdir(topdir.c_str());
  148. pos++;
  149. }
  150. if(dir[dir.size()-1] == '/')
  151. {
  152. topdir = dir.substr(0, dir.size());
  153. }
  154. else
  155. {
  156. topdir = dir;
  157. }
  158. if(Mkdir(topdir.c_str()) != 0)
  159. {
  160. // There is a bug in the Borland Run time library which makes MKDIR
  161. // return EACCES when it should return EEXISTS
  162. // if it is some other error besides directory exists
  163. // then return false
  164. if( (errno != EEXIST)
  165. #ifdef __BORLANDC__
  166. && (errno != EACCES)
  167. #endif
  168. )
  169. {
  170. cmSystemTools::Error("Faild to create directory:", path);
  171. return false;
  172. }
  173. }
  174. return true;
  175. }
  176. // replace replace with with as many times as it shows up in source.
  177. // write the result into source.
  178. void cmSystemTools::ReplaceString(std::string& source,
  179. const char* replace,
  180. const char* with)
  181. {
  182. std::string::size_type lengthReplace = strlen(replace);
  183. std::string::size_type lengthWith = strlen(with);
  184. std::string rest;
  185. std::string::size_type start = source.find(replace);
  186. while(start != std::string::npos)
  187. {
  188. rest = source.substr(start+lengthReplace);
  189. source = source.substr(0, start);
  190. source += with;
  191. source += rest;
  192. start = source.find(replace, start + lengthWith );
  193. }
  194. }
  195. // Read a registry value.
  196. // Example :
  197. // HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.1\InstallPath
  198. // => will return the data of the "default" value of the key
  199. // HKEY_LOCAL_MACHINE\SOFTWARE\Scriptics\Tcl\8.4;Root
  200. // => will return the data of the "Root" value of the key
  201. #if defined(_WIN32) && !defined(__CYGWIN__)
  202. bool cmSystemTools::ReadRegistryValue(const char *key, std::string &value)
  203. {
  204. std::string primary = key;
  205. std::string second;
  206. std::string valuename;
  207. size_t start = primary.find("\\");
  208. if (start == std::string::npos)
  209. {
  210. return false;
  211. }
  212. size_t valuenamepos = primary.find(";");
  213. if (valuenamepos != std::string::npos)
  214. {
  215. valuename = primary.substr(valuenamepos+1);
  216. }
  217. second = primary.substr(start+1, valuenamepos-start-1);
  218. primary = primary.substr(0, start);
  219. HKEY primaryKey;
  220. if (primary == "HKEY_CURRENT_USER")
  221. {
  222. primaryKey = HKEY_CURRENT_USER;
  223. }
  224. if (primary == "HKEY_CURRENT_CONFIG")
  225. {
  226. primaryKey = HKEY_CURRENT_CONFIG;
  227. }
  228. if (primary == "HKEY_CLASSES_ROOT")
  229. {
  230. primaryKey = HKEY_CLASSES_ROOT;
  231. }
  232. if (primary == "HKEY_LOCAL_MACHINE")
  233. {
  234. primaryKey = HKEY_LOCAL_MACHINE;
  235. }
  236. if (primary == "HKEY_USERS")
  237. {
  238. primaryKey = HKEY_USERS;
  239. }
  240. HKEY hKey;
  241. if(RegOpenKeyEx(primaryKey,
  242. second.c_str(),
  243. 0,
  244. KEY_READ,
  245. &hKey) != ERROR_SUCCESS)
  246. {
  247. return false;
  248. }
  249. else
  250. {
  251. DWORD dwType, dwSize;
  252. dwSize = 1023;
  253. char data[1024];
  254. if(RegQueryValueEx(hKey,
  255. (LPTSTR)valuename.c_str(),
  256. NULL,
  257. &dwType,
  258. (BYTE *)data,
  259. &dwSize) == ERROR_SUCCESS)
  260. {
  261. if (dwType == REG_SZ)
  262. {
  263. value = data;
  264. return true;
  265. }
  266. }
  267. }
  268. return false;
  269. }
  270. #else
  271. bool cmSystemTools::ReadRegistryValue(const char *, std::string &)
  272. {
  273. return false;
  274. }
  275. #endif
  276. // Write a registry value.
  277. // Example :
  278. // HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.1\InstallPath
  279. // => will set the data of the "default" value of the key
  280. // HKEY_LOCAL_MACHINE\SOFTWARE\Scriptics\Tcl\8.4;Root
  281. // => will set the data of the "Root" value of the key
  282. #if defined(_WIN32) && !defined(__CYGWIN__)
  283. bool cmSystemTools::WriteRegistryValue(const char *key, const char *value)
  284. {
  285. std::string primary = key;
  286. std::string second;
  287. std::string valuename;
  288. size_t start = primary.find("\\");
  289. if (start == std::string::npos)
  290. {
  291. return false;
  292. }
  293. size_t valuenamepos = primary.find(";");
  294. if (valuenamepos != std::string::npos)
  295. {
  296. valuename = primary.substr(valuenamepos+1);
  297. }
  298. second = primary.substr(start+1, valuenamepos-start-1);
  299. primary = primary.substr(0, start);
  300. HKEY primaryKey;
  301. if (primary == "HKEY_CURRENT_USER")
  302. {
  303. primaryKey = HKEY_CURRENT_USER;
  304. }
  305. if (primary == "HKEY_CURRENT_CONFIG")
  306. {
  307. primaryKey = HKEY_CURRENT_CONFIG;
  308. }
  309. if (primary == "HKEY_CLASSES_ROOT")
  310. {
  311. primaryKey = HKEY_CLASSES_ROOT;
  312. }
  313. if (primary == "HKEY_LOCAL_MACHINE")
  314. {
  315. primaryKey = HKEY_LOCAL_MACHINE;
  316. }
  317. if (primary == "HKEY_USERS")
  318. {
  319. primaryKey = HKEY_USERS;
  320. }
  321. HKEY hKey;
  322. DWORD dwDummy;
  323. if(RegCreateKeyEx(primaryKey,
  324. second.c_str(),
  325. 0,
  326. "",
  327. REG_OPTION_NON_VOLATILE,
  328. KEY_WRITE,
  329. NULL,
  330. &hKey,
  331. &dwDummy) != ERROR_SUCCESS)
  332. {
  333. return false;
  334. }
  335. if(RegSetValueEx(hKey,
  336. (LPTSTR)valuename.c_str(),
  337. 0,
  338. REG_SZ,
  339. (CONST BYTE *)value,
  340. (DWORD)(strlen(value) + 1)) == ERROR_SUCCESS)
  341. {
  342. return true;
  343. }
  344. return false;
  345. }
  346. #else
  347. bool cmSystemTools::WriteRegistryValue(const char *, const char *)
  348. {
  349. return false;
  350. }
  351. #endif
  352. // Delete a registry value.
  353. // Example :
  354. // HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.1\InstallPath
  355. // => will delete the data of the "default" value of the key
  356. // HKEY_LOCAL_MACHINE\SOFTWARE\Scriptics\Tcl\8.4;Root
  357. // => will delete the data of the "Root" value of the key
  358. #if defined(_WIN32) && !defined(__CYGWIN__)
  359. bool cmSystemTools::DeleteRegistryValue(const char *key)
  360. {
  361. std::string primary = key;
  362. std::string second;
  363. std::string valuename;
  364. size_t start = primary.find("\\");
  365. if (start == std::string::npos)
  366. {
  367. return false;
  368. }
  369. size_t valuenamepos = primary.find(";");
  370. if (valuenamepos != std::string::npos)
  371. {
  372. valuename = primary.substr(valuenamepos+1);
  373. }
  374. second = primary.substr(start+1, valuenamepos-start-1);
  375. primary = primary.substr(0, start);
  376. HKEY primaryKey;
  377. if (primary == "HKEY_CURRENT_USER")
  378. {
  379. primaryKey = HKEY_CURRENT_USER;
  380. }
  381. if (primary == "HKEY_CURRENT_CONFIG")
  382. {
  383. primaryKey = HKEY_CURRENT_CONFIG;
  384. }
  385. if (primary == "HKEY_CLASSES_ROOT")
  386. {
  387. primaryKey = HKEY_CLASSES_ROOT;
  388. }
  389. if (primary == "HKEY_LOCAL_MACHINE")
  390. {
  391. primaryKey = HKEY_LOCAL_MACHINE;
  392. }
  393. if (primary == "HKEY_USERS")
  394. {
  395. primaryKey = HKEY_USERS;
  396. }
  397. HKEY hKey;
  398. if(RegOpenKeyEx(primaryKey,
  399. second.c_str(),
  400. 0,
  401. KEY_WRITE,
  402. &hKey) != ERROR_SUCCESS)
  403. {
  404. return false;
  405. }
  406. else
  407. {
  408. if(RegDeleteValue(hKey,
  409. (LPTSTR)valuename.c_str()) == ERROR_SUCCESS)
  410. {
  411. return true;
  412. }
  413. }
  414. return false;
  415. }
  416. #else
  417. bool cmSystemTools::DeleteRegistryValue(const char *)
  418. {
  419. return false;
  420. }
  421. #endif
  422. // replace replace with with as many times as it shows up in source.
  423. // write the result into source.
  424. #if defined(_WIN32) && !defined(__CYGWIN__)
  425. void cmSystemTools::ExpandRegistryValues(std::string& source)
  426. {
  427. // Regular expression to match anything inside [...] that begins in HKEY.
  428. // Note that there is a special rule for regular expressions to match a
  429. // close square-bracket inside a list delimited by square brackets.
  430. // The "[^]]" part of this expression will match any character except
  431. // a close square-bracket. The ']' character must be the first in the
  432. // list of characters inside the [^...] block of the expression.
  433. cmRegularExpression regEntry("\\[(HKEY[^]]*)\\]");
  434. // check for black line or comment
  435. while (regEntry.find(source))
  436. {
  437. // the arguments are the second match
  438. std::string key = regEntry.match(1);
  439. std::string val;
  440. if (ReadRegistryValue(key.c_str(), val))
  441. {
  442. std::string reg = "[";
  443. reg += key + "]";
  444. cmSystemTools::ReplaceString(source, reg.c_str(), val.c_str());
  445. }
  446. else
  447. {
  448. std::string reg = "[";
  449. reg += key + "]";
  450. cmSystemTools::ReplaceString(source, reg.c_str(), "/registry");
  451. }
  452. }
  453. }
  454. #else
  455. void cmSystemTools::ExpandRegistryValues(std::string&)
  456. {
  457. }
  458. #endif
  459. std::string cmSystemTools::EscapeQuotes(const char* str)
  460. {
  461. std::string result = "";
  462. for(const char* ch = str; *ch != '\0'; ++ch)
  463. {
  464. if(*ch == '"')
  465. {
  466. result += '\\';
  467. }
  468. result += *ch;
  469. }
  470. return result;
  471. }
  472. bool cmSystemTools::SameFile(const char* file1, const char* file2)
  473. {
  474. #ifdef _WIN32
  475. HANDLE hFile1, hFile2;
  476. hFile1 = CreateFile( file1,
  477. GENERIC_READ,
  478. FILE_SHARE_READ ,
  479. NULL,
  480. OPEN_EXISTING,
  481. FILE_FLAG_BACKUP_SEMANTICS,
  482. NULL
  483. );
  484. hFile2 = CreateFile( file2,
  485. GENERIC_READ,
  486. FILE_SHARE_READ,
  487. NULL,
  488. OPEN_EXISTING,
  489. FILE_FLAG_BACKUP_SEMANTICS,
  490. NULL
  491. );
  492. if( hFile1 == INVALID_HANDLE_VALUE || hFile2 == INVALID_HANDLE_VALUE)
  493. {
  494. if(hFile1 != INVALID_HANDLE_VALUE)
  495. {
  496. CloseHandle(hFile1);
  497. }
  498. if(hFile2 != INVALID_HANDLE_VALUE)
  499. {
  500. CloseHandle(hFile2);
  501. }
  502. return false;
  503. }
  504. BY_HANDLE_FILE_INFORMATION fiBuf1;
  505. BY_HANDLE_FILE_INFORMATION fiBuf2;
  506. GetFileInformationByHandle( hFile1, &fiBuf1 );
  507. GetFileInformationByHandle( hFile2, &fiBuf2 );
  508. CloseHandle(hFile1);
  509. CloseHandle(hFile2);
  510. return (fiBuf1.nFileIndexHigh == fiBuf2.nFileIndexHigh &&
  511. fiBuf1.nFileIndexLow == fiBuf2.nFileIndexLow);
  512. #else
  513. struct stat fileStat1, fileStat2;
  514. if (stat(file1, &fileStat1) == 0 && stat(file2, &fileStat2) == 0)
  515. {
  516. // see if the files are the same file
  517. // check the device inode and size
  518. if(memcmp(&fileStat2.st_dev, &fileStat1.st_dev, sizeof(fileStat1.st_dev)) == 0 &&
  519. memcmp(&fileStat2.st_ino, &fileStat1.st_ino, sizeof(fileStat1.st_ino)) == 0 &&
  520. fileStat2.st_size == fileStat1.st_size
  521. )
  522. {
  523. return true;
  524. }
  525. }
  526. return false;
  527. #endif
  528. }
  529. // return true if the file exists
  530. bool cmSystemTools::FileExists(const char* filename)
  531. {
  532. struct stat fs;
  533. if (stat(filename, &fs) != 0)
  534. {
  535. return false;
  536. }
  537. else
  538. {
  539. return true;
  540. }
  541. }
  542. // Return a capitalized string (i.e the first letter is uppercased, all other
  543. // are lowercased)
  544. std::string cmSystemTools::Capitalized(const std::string& s)
  545. {
  546. std::string n;
  547. n.resize(s.size());
  548. n[0] = toupper(s[0]);
  549. for (size_t i = 1; i < s.size(); i++)
  550. {
  551. n[i] = tolower(s[i]);
  552. }
  553. return n;
  554. }
  555. // Return a lower case string
  556. std::string cmSystemTools::LowerCase(const std::string& s)
  557. {
  558. std::string n;
  559. n.resize(s.size());
  560. for (size_t i = 0; i < s.size(); i++)
  561. {
  562. n[i] = tolower(s[i]);
  563. }
  564. return n;
  565. }
  566. // Return a lower case string
  567. std::string cmSystemTools::UpperCase(const std::string& s)
  568. {
  569. std::string n;
  570. n.resize(s.size());
  571. for (size_t i = 0; i < s.size(); i++)
  572. {
  573. n[i] = toupper(s[i]);
  574. }
  575. return n;
  576. }
  577. // convert windows slashes to unix slashes
  578. void cmSystemTools::ConvertToUnixSlashes(std::string& path)
  579. {
  580. std::string::size_type pos = 0;
  581. while((pos = path.find('\\', pos)) != std::string::npos)
  582. {
  583. path[pos] = '/';
  584. pos++;
  585. }
  586. // remove any trailing slash
  587. if(path.size() && path[path.size()-1] == '/')
  588. {
  589. path = path.substr(0, path.size()-1);
  590. }
  591. // if there is a tilda ~ then replace it with HOME
  592. if(path.find("~") == 0)
  593. {
  594. if (getenv("HOME"))
  595. {
  596. path = std::string(getenv("HOME")) + path.substr(1);
  597. }
  598. }
  599. // if there is a /tmp_mnt in a path get rid of it!
  600. // stupid sgi's
  601. if(path.find("/tmp_mnt") == 0)
  602. {
  603. path = path.substr(8);
  604. }
  605. }
  606. // change // to /, and escape any spaces in the path
  607. std::string cmSystemTools::ConvertToUnixOutputPath(const char* path)
  608. {
  609. std::string ret = path;
  610. // remove // except at the beginning might be a cygwin drive
  611. std::string::size_type pos = 1;
  612. while((pos = ret.find("//", pos)) != std::string::npos)
  613. {
  614. ret.erase(pos, 1);
  615. }
  616. // now escape spaces if there is a space in the path
  617. if(ret.find(" ") != std::string::npos)
  618. {
  619. std::string result = "";
  620. char lastch = 1;
  621. for(const char* ch = ret.c_str(); *ch != '\0'; ++ch)
  622. {
  623. // if it is already escaped then don't try to escape it again
  624. if(*ch == ' ' && lastch != '\\')
  625. {
  626. result += '\\';
  627. }
  628. result += *ch;
  629. lastch = *ch;
  630. }
  631. ret = result;
  632. }
  633. return ret;
  634. }
  635. std::string cmSystemTools::EscapeSpaces(const char* str)
  636. {
  637. #if defined(_WIN32) && !defined(__CYGWIN__)
  638. std::string result;
  639. // if there are spaces
  640. std::string temp = str;
  641. if (temp.find(" ") != std::string::npos &&
  642. temp.find("\"")==std::string::npos)
  643. {
  644. result = "\"";
  645. result += str;
  646. result += "\"";
  647. return result;
  648. }
  649. return str;
  650. #else
  651. std::string result = "";
  652. for(const char* ch = str; *ch != '\0'; ++ch)
  653. {
  654. if(*ch == ' ')
  655. {
  656. result += '\\';
  657. }
  658. result += *ch;
  659. }
  660. return result;
  661. #endif
  662. }
  663. std::string cmSystemTools::ConvertToOutputPath(const char* path)
  664. {
  665. #if defined(_WIN32) && !defined(__CYGWIN__)
  666. return cmSystemTools::ConvertToWindowsOutputPath(path);
  667. #else
  668. return cmSystemTools::ConvertToUnixOutputPath(path);
  669. #endif
  670. }
  671. // remove double slashes not at the start
  672. std::string cmSystemTools::ConvertToWindowsOutputPath(const char* path)
  673. {
  674. std::string ret = path;
  675. std::string::size_type pos = 0;
  676. // first convert all of the slashes
  677. while((pos = ret.find('/', pos)) != std::string::npos)
  678. {
  679. ret[pos] = '\\';
  680. pos++;
  681. }
  682. // check for really small paths
  683. if(ret.size() < 2)
  684. {
  685. return ret;
  686. }
  687. // now clean up a bit and remove double slashes
  688. // Only if it is not the first position in the path which is a network
  689. // path on windows
  690. pos = 1; // start at position 1
  691. while((pos = ret.find("\\\\", pos)) != std::string::npos)
  692. {
  693. ret.erase(pos, 1);
  694. }
  695. // now double quote the path if it has spaces in it
  696. // and is not already double quoted
  697. if(ret.find(" ") != std::string::npos
  698. && ret[0] != '\"')
  699. {
  700. std::string result;
  701. result = "\"" + ret + "\"";
  702. ret = result;
  703. }
  704. return ret;
  705. }
  706. inline void RemoveComments(char* ptr)
  707. {
  708. while(*ptr)
  709. {
  710. if(*ptr == '#')
  711. {
  712. *ptr = 0;
  713. break;
  714. }
  715. ++ptr;
  716. }
  717. }
  718. bool cmSystemTools::ParseFunction(std::ifstream& fin,
  719. std::string& name,
  720. std::vector<std::string>& arguments,
  721. const char* filename,
  722. bool& parseError)
  723. {
  724. parseError = false;
  725. name = "";
  726. arguments = std::vector<std::string>();
  727. const int BUFFER_SIZE = 4096;
  728. char inbuffer[BUFFER_SIZE];
  729. if(!fin)
  730. {
  731. return false;
  732. }
  733. if(fin.getline(inbuffer, BUFFER_SIZE ) )
  734. {
  735. RemoveComments(inbuffer);
  736. cmRegularExpression blankLine("^[ \t\r]*$");
  737. // cmRegularExpression comment("^[ \t]*#.*$");
  738. cmRegularExpression oneLiner("^[ \t]*([A-Za-z_0-9]*)[ \t]*\\((.*)\\)[ \t\r]*$");
  739. cmRegularExpression multiLine("^[ \t]*([A-Za-z_0-9]*)[ \t]*\\((.*)$");
  740. cmRegularExpression lastLine("^(.*)\\)[ \t\r]*$");
  741. // check for blank line or comment
  742. if(blankLine.find(inbuffer) )
  743. {
  744. return false;
  745. }
  746. // look for a oneline fun(arg arg2)
  747. else if(oneLiner.find(inbuffer))
  748. {
  749. // the arguments are the second match
  750. std::string args = oneLiner.match(2);
  751. name = oneLiner.match(1);
  752. // break up the arguments
  753. cmSystemTools::GetArguments(args, arguments);
  754. return true;
  755. }
  756. // look for a start of a multiline with no trailing ")" fun(arg arg2
  757. else if(multiLine.find(inbuffer))
  758. {
  759. name = multiLine.match(1);
  760. std::string args = multiLine.match(2);
  761. cmSystemTools::GetArguments(args, arguments);
  762. // Read lines until the closing paren is hit
  763. bool done = false;
  764. while(!done)
  765. {
  766. // read lines until the end paren is found
  767. if(fin.getline(inbuffer, BUFFER_SIZE ) )
  768. {
  769. RemoveComments(inbuffer);
  770. // Check for comment lines and ignore them.
  771. if(blankLine.find(inbuffer))
  772. { continue; }
  773. // Is this the last line?
  774. if(lastLine.find(inbuffer))
  775. {
  776. done = true;
  777. std::string args = lastLine.match(1);
  778. cmSystemTools::GetArguments(args, arguments);
  779. }
  780. else
  781. {
  782. std::string line = inbuffer;
  783. cmSystemTools::GetArguments(line, arguments);
  784. }
  785. }
  786. else
  787. {
  788. parseError = true;
  789. cmSystemTools::Error("Parse error in read function missing end )\nIn File: ",
  790. filename, "\nCurrent line:", inbuffer);
  791. return false;
  792. }
  793. }
  794. return true;
  795. }
  796. else
  797. {
  798. parseError = true;
  799. cmSystemTools::Error("Parse error in read function\nIn file:",
  800. filename, "\nCurrent line:", inbuffer);
  801. return false;
  802. }
  803. }
  804. return false;
  805. }
  806. void cmSystemTools::GetArguments(std::string& line,
  807. std::vector<std::string>& arguments)
  808. {
  809. // Match a normal argument (not quoted, no spaces).
  810. cmRegularExpression normalArgument("[ \t]*(([^ \t\r\\]|[\\].)+)[ \t\r]*");
  811. // Match a quoted argument (surrounded by double quotes, spaces allowed).
  812. cmRegularExpression quotedArgument("[ \t]*(\"([^\"\\]|[\\].)*\")[ \t\r]*");
  813. bool done = false;
  814. while(!done)
  815. {
  816. std::string arg;
  817. std::string::size_type endpos=0;
  818. bool foundQuoted = quotedArgument.find(line.c_str());
  819. bool foundNormal = normalArgument.find(line.c_str());
  820. if(foundQuoted && foundNormal)
  821. {
  822. // Both matches were found. Take the earlier one.
  823. // Favor double-quoted version if there is a tie.
  824. if(normalArgument.start(1) < quotedArgument.start(1))
  825. {
  826. arg = normalArgument.match(1);
  827. endpos = normalArgument.end(1);
  828. }
  829. else
  830. {
  831. arg = quotedArgument.match(1);
  832. endpos = quotedArgument.end(1);
  833. // Strip off the double quotes on the ends.
  834. arg = arg.substr(1, arg.length()-2);
  835. }
  836. }
  837. else if (foundQuoted)
  838. {
  839. arg = quotedArgument.match(1);
  840. endpos = quotedArgument.end(1);
  841. // Strip off the double quotes on the ends.
  842. arg = arg.substr(1, arg.length()-2);
  843. }
  844. else if(foundNormal)
  845. {
  846. arg = normalArgument.match(1);
  847. endpos = normalArgument.end(1);
  848. }
  849. else
  850. {
  851. done = true;
  852. }
  853. if(!done)
  854. {
  855. arguments.push_back(cmSystemTools::RemoveEscapes(arg.c_str()));
  856. line = line.substr(endpos, line.length() - endpos);
  857. }
  858. }
  859. }
  860. std::string cmSystemTools::RemoveEscapes(const char* s)
  861. {
  862. std::string result = "";
  863. for(const char* ch = s; *ch; ++ch)
  864. {
  865. if(*ch == '\\' && *(ch+1) != ';')
  866. {
  867. ++ch;
  868. switch (*ch)
  869. {
  870. case '\\': result.insert(result.end(), '\\'); break;
  871. case '"': result.insert(result.end(), '"'); break;
  872. case ' ': result.insert(result.end(), ' '); break;
  873. case 't': result.insert(result.end(), '\t'); break;
  874. case 'n': result.insert(result.end(), '\n'); break;
  875. case 'r': result.insert(result.end(), '\r'); break;
  876. case '0': result.insert(result.end(), '\0'); break;
  877. case '\0':
  878. {
  879. cmSystemTools::Error("Trailing backslash in argument:\n", s);
  880. return result;
  881. }
  882. default:
  883. {
  884. std::string chStr(1, *ch);
  885. cmSystemTools::Error("Invalid escape sequence \\", chStr.c_str(),
  886. "\nin argument ", s);
  887. }
  888. }
  889. }
  890. else
  891. {
  892. result.insert(result.end(), *ch);
  893. }
  894. }
  895. return result;
  896. }
  897. void cmSystemTools::Error(const char* m1, const char* m2,
  898. const char* m3, const char* m4)
  899. {
  900. std::string message = "CMake Error: ";
  901. if(m1)
  902. {
  903. message += m1;
  904. }
  905. if(m2)
  906. {
  907. message += m2;
  908. }
  909. if(m3)
  910. {
  911. message += m3;
  912. }
  913. if(m4)
  914. {
  915. message += m4;
  916. }
  917. cmSystemTools::s_ErrorOccured = true;
  918. cmSystemTools::Message(message.c_str(),"Error");
  919. }
  920. void cmSystemTools::SetErrorCallback(ErrorCallback f, void* clientData)
  921. {
  922. s_ErrorCallback = f;
  923. s_ErrorCallbackClientData = clientData;
  924. }
  925. void cmSystemTools::Message(const char* m1, const char *title)
  926. {
  927. if(s_DisableMessages)
  928. {
  929. return;
  930. }
  931. if(s_ErrorCallback)
  932. {
  933. (*s_ErrorCallback)(m1, title, s_DisableMessages, s_ErrorCallbackClientData);
  934. return;
  935. }
  936. else
  937. {
  938. std::cerr << m1 << std::endl << std::flush;
  939. }
  940. }
  941. bool cmSystemTools::CopyFileIfDifferent(const char* source,
  942. const char* destination)
  943. {
  944. if(cmSystemTools::FilesDiffer(source, destination))
  945. {
  946. cmSystemTools::cmCopyFile(source, destination);
  947. return true;
  948. }
  949. return false;
  950. }
  951. bool cmSystemTools::FilesDiffer(const char* source,
  952. const char* destination)
  953. {
  954. struct stat statSource;
  955. if (stat(source, &statSource) != 0)
  956. {
  957. return true;
  958. }
  959. struct stat statDestination;
  960. if (stat(destination, &statDestination) != 0)
  961. {
  962. return true;
  963. }
  964. if(statSource.st_size != statDestination.st_size)
  965. {
  966. return true;
  967. }
  968. if(statSource.st_size == 0)
  969. {
  970. return false;
  971. }
  972. #if defined(_WIN32) || defined(__CYGWIN__)
  973. std::ifstream finSource(source,
  974. std::ios::binary | std::ios::in);
  975. std::ifstream finDestination(destination,
  976. std::ios::binary | std::ios::in);
  977. #else
  978. std::ifstream finSource(source);
  979. std::ifstream finDestination(destination);
  980. #endif
  981. if(!finSource || !finDestination)
  982. {
  983. return true;
  984. }
  985. char* source_buf = new char[statSource.st_size];
  986. char* dest_buf = new char[statSource.st_size];
  987. finSource.read(source_buf, statSource.st_size);
  988. finDestination.read(dest_buf, statSource.st_size);
  989. if(statSource.st_size != static_cast<long>(finSource.gcount()) ||
  990. statSource.st_size != static_cast<long>(finDestination.gcount()))
  991. {
  992. cmOStringStream msg;
  993. msg << "FilesDiffer failed to read files (allocated: "
  994. << statSource.st_size << ", read source: " << finSource.gcount()
  995. << ", read dest: " << finDestination.gcount();
  996. cmSystemTools::Error(msg.str().c_str());
  997. delete [] source_buf;
  998. delete [] dest_buf;
  999. return false;
  1000. }
  1001. int ret = memcmp((const void*)source_buf,
  1002. (const void*)dest_buf,
  1003. statSource.st_size);
  1004. delete [] dest_buf;
  1005. delete [] source_buf;
  1006. return ret != 0;
  1007. }
  1008. /**
  1009. * Copy a file named by "source" to the file named by "destination".
  1010. */
  1011. void cmSystemTools::cmCopyFile(const char* source,
  1012. const char* destination)
  1013. {
  1014. const int bufferSize = 4096;
  1015. char buffer[bufferSize];
  1016. // If destination is a directory, try to create a file with the same
  1017. // name as the source in that directory.
  1018. std::string new_destination;
  1019. if(cmSystemTools::FileExists(destination) &&
  1020. cmSystemTools::FileIsDirectory(destination))
  1021. {
  1022. new_destination = destination;
  1023. cmSystemTools::ConvertToUnixSlashes(new_destination);
  1024. new_destination += '/';
  1025. std::string source_name = source;
  1026. new_destination += cmSystemTools::GetFilenameName(source_name);
  1027. destination = new_destination.c_str();
  1028. }
  1029. // Create destination directory
  1030. std::string destination_dir = destination;
  1031. destination_dir = cmSystemTools::GetFilenamePath(destination_dir);
  1032. cmSystemTools::MakeDirectory(destination_dir.c_str());
  1033. // Open files
  1034. #if defined(_WIN32) || defined(__CYGWIN__)
  1035. std::ifstream fin(source,
  1036. std::ios::binary | std::ios::in);
  1037. #else
  1038. std::ifstream fin(source);
  1039. #endif
  1040. if(!fin)
  1041. {
  1042. cmSystemTools::Error("CopyFile failed to open input file \"",
  1043. source, "\"");
  1044. return;
  1045. }
  1046. #if defined(_WIN32) || defined(__CYGWIN__)
  1047. std::ofstream fout(destination,
  1048. std::ios::binary | std::ios::out | std::ios::trunc);
  1049. #else
  1050. std::ofstream fout(destination,
  1051. std::ios::out | std::ios::trunc);
  1052. #endif
  1053. if(!fout)
  1054. {
  1055. cmSystemTools::Error("CopyFile failed to open output file \"",
  1056. destination, "\"");
  1057. return;
  1058. }
  1059. // This copy loop is very sensitive on certain platforms with
  1060. // slightly broken stream libraries (like HPUX). Normally, it is
  1061. // incorrect to not check the error condition on the fin.read()
  1062. // before using the data, but the fin.gcount() will be zero if an
  1063. // error occurred. Therefore, the loop should be safe everywhere.
  1064. while(fin)
  1065. {
  1066. fin.read(buffer, bufferSize);
  1067. if(fin.gcount())
  1068. {
  1069. fout.write(buffer, fin.gcount());
  1070. }
  1071. }
  1072. fin.close();
  1073. fout.close();
  1074. // More checks
  1075. struct stat statSource, statDestination;
  1076. if (stat(source, &statSource) != 0 ||
  1077. stat(destination, &statDestination) != 0)
  1078. {
  1079. cmSystemTools::Error("CopyFile failed to copy files!");
  1080. }
  1081. if (statSource.st_size != statDestination.st_size)
  1082. {
  1083. cmOStringStream msg;
  1084. msg << "CopyFile failed to copy files (sizes differ, source: "
  1085. << statSource.st_size << " , dest: " << statDestination.st_size;
  1086. cmSystemTools::Error(msg.str().c_str());
  1087. }
  1088. }
  1089. // return true if the file exists
  1090. long int cmSystemTools::ModifiedTime(const char* filename)
  1091. {
  1092. struct stat fs;
  1093. if (stat(filename, &fs) != 0)
  1094. {
  1095. return 0;
  1096. }
  1097. else
  1098. {
  1099. return (long int)fs.st_mtime;
  1100. }
  1101. }
  1102. bool cmSystemTools::RemoveFile(const char* source)
  1103. {
  1104. return unlink(source) != 0 ? false : true;
  1105. }
  1106. bool cmSystemTools::IsOn(const char* val)
  1107. {
  1108. if (!val)
  1109. {
  1110. return false;
  1111. }
  1112. std::basic_string<char> v = val;
  1113. for(std::basic_string<char>::iterator c = v.begin();
  1114. c != v.end(); c++)
  1115. {
  1116. *c = toupper(*c);
  1117. }
  1118. return (v == "ON" || v == "1" || v == "YES" || v == "TRUE" || v == "Y");
  1119. }
  1120. bool cmSystemTools::IsOff(const char* val)
  1121. {
  1122. if (!val || strlen(val) == 0)
  1123. {
  1124. return true;
  1125. }
  1126. std::basic_string<char> v = val;
  1127. for(std::basic_string<char>::iterator c = v.begin();
  1128. c != v.end(); c++)
  1129. {
  1130. *c = toupper(*c);
  1131. }
  1132. return (v == "OFF" || v == "0" || v == "NO" || v == "FALSE" ||
  1133. v == "N" || v == "NOTFOUND" || v == "IGNORE");
  1134. }
  1135. bool cmSystemTools::RunCommand(const char* command,
  1136. std::string& output,
  1137. const char* dir,
  1138. bool verbose,
  1139. int timeout)
  1140. {
  1141. int foo;
  1142. return cmSystemTools::RunCommand(command, output, foo, dir, verbose, timeout);
  1143. }
  1144. #if defined(WIN32) && !defined(__CYGWIN__)
  1145. #include "cmWin32ProcessExecution.h"
  1146. // use this for shell commands like echo and dir
  1147. bool RunCommandViaWin32(const char* command,
  1148. const char* dir,
  1149. std::string& output,
  1150. int& retVal,
  1151. bool verbose,
  1152. int timeout)
  1153. {
  1154. #if defined(__BORLANDC__)
  1155. return cmWin32ProcessExecution::BorlandRunCommand(command, dir, output,
  1156. retVal,
  1157. verbose, timeout);
  1158. #else // Visual studio
  1159. ::SetLastError(ERROR_SUCCESS);
  1160. if ( ! command )
  1161. {
  1162. cmSystemTools::Error("No command specified");
  1163. return false;
  1164. }
  1165. //std::cout << "Command: " << command << std::endl;
  1166. if ( dir )
  1167. {
  1168. //std::cout << "Dir: " << dir << std::endl;
  1169. }
  1170. cmWin32ProcessExecution resProc;
  1171. if ( cmSystemTools::GetWindows9xComspecSubstitute() )
  1172. {
  1173. resProc.SetConsoleSpawn(cmSystemTools::GetWindows9xComspecSubstitute() );
  1174. }
  1175. if ( !resProc.StartProcess(command, dir, verbose) )
  1176. {
  1177. std::cout << "Problem starting command" << std::endl;
  1178. return false;
  1179. }
  1180. resProc.Wait(timeout);
  1181. output = resProc.GetOutput();
  1182. retVal = resProc.GetExitValue();
  1183. return true;
  1184. #endif
  1185. }
  1186. // use this for shell commands like echo and dir
  1187. bool RunCommandViaSystem(const char* command,
  1188. const char* dir,
  1189. std::string& output,
  1190. int& retVal,
  1191. bool verbose)
  1192. {
  1193. std::cout << "@@ " << command << std::endl;
  1194. const int BUFFER_SIZE = 4096;
  1195. char buffer[BUFFER_SIZE];
  1196. std::string commandInDir;
  1197. if(dir)
  1198. {
  1199. commandInDir = "cd ";
  1200. commandInDir += cmSystemTools::ConvertToOutputPath(dir);
  1201. commandInDir += " && ";
  1202. commandInDir += command;
  1203. }
  1204. else
  1205. {
  1206. commandInDir = command;
  1207. }
  1208. command = commandInDir.c_str();
  1209. std::string commandToFile = command;
  1210. commandToFile += " > ";
  1211. std::string tempFile;
  1212. tempFile += _tempnam(0, "cmake");
  1213. commandToFile += tempFile;
  1214. retVal = system(commandToFile.c_str());
  1215. std::ifstream fin(tempFile.c_str());
  1216. if(!fin)
  1217. {
  1218. if(verbose)
  1219. {
  1220. std::string errormsg = "RunCommand produced no output: command: \"";
  1221. errormsg += command;
  1222. errormsg += "\"";
  1223. errormsg += "\nOutput file: ";
  1224. errormsg += tempFile;
  1225. cmSystemTools::Error(errormsg.c_str());
  1226. }
  1227. fin.close();
  1228. cmSystemTools::RemoveFile(tempFile.c_str());
  1229. return false;
  1230. }
  1231. bool multiLine = false;
  1232. while(fin)
  1233. {
  1234. fin.getline(buffer, BUFFER_SIZE);
  1235. output += buffer;
  1236. if(multiLine)
  1237. {
  1238. output += "\n";
  1239. }
  1240. multiLine = true;
  1241. }
  1242. fin.close();
  1243. cmSystemTools::RemoveFile(tempFile.c_str());
  1244. return true;
  1245. }
  1246. #else // We have popen
  1247. bool RunCommandViaPopen(const char* command,
  1248. const char* dir,
  1249. std::string& output,
  1250. int& retVal,
  1251. bool verbose,
  1252. int /*timeout*/)
  1253. {
  1254. // if only popen worked on windows.....
  1255. std::string commandInDir;
  1256. if(dir)
  1257. {
  1258. commandInDir = "cd ";
  1259. commandInDir += dir;
  1260. commandInDir += " && ";
  1261. commandInDir += command;
  1262. }
  1263. else
  1264. {
  1265. commandInDir = command;
  1266. }
  1267. commandInDir += " 2>&1";
  1268. command = commandInDir.c_str();
  1269. const int BUFFER_SIZE = 4096;
  1270. char buffer[BUFFER_SIZE];
  1271. if(verbose)
  1272. {
  1273. std::cout << "running " << command << std::endl;
  1274. }
  1275. fflush(stdout);
  1276. fflush(stderr);
  1277. FILE* cpipe = popen(command, "r");
  1278. if(!cpipe)
  1279. {
  1280. return false;
  1281. }
  1282. fgets(buffer, BUFFER_SIZE, cpipe);
  1283. while(!feof(cpipe))
  1284. {
  1285. if(verbose)
  1286. {
  1287. std::cout << buffer << std::flush;
  1288. }
  1289. output += buffer;
  1290. fgets(buffer, BUFFER_SIZE, cpipe);
  1291. }
  1292. retVal = pclose(cpipe);
  1293. if (WIFEXITED(retVal))
  1294. {
  1295. retVal = WEXITSTATUS(retVal);
  1296. return true;
  1297. }
  1298. if (WIFSIGNALED(retVal))
  1299. {
  1300. retVal = WTERMSIG(retVal);
  1301. cmOStringStream error;
  1302. error << "\nProcess terminated due to ";
  1303. switch (retVal)
  1304. {
  1305. #ifdef SIGKILL
  1306. case SIGKILL:
  1307. error << "SIGKILL";
  1308. break;
  1309. #endif
  1310. #ifdef SIGFPE
  1311. case SIGFPE:
  1312. error << "SIGFPE";
  1313. break;
  1314. #endif
  1315. #ifdef SIGBUS
  1316. case SIGBUS:
  1317. error << "SIGBUS";
  1318. break;
  1319. #endif
  1320. #ifdef SIGSEGV
  1321. case SIGSEGV:
  1322. error << "SIGSEGV";
  1323. break;
  1324. #endif
  1325. default:
  1326. error << "signal " << retVal;
  1327. break;
  1328. }
  1329. output += error.str();
  1330. }
  1331. return false;
  1332. }
  1333. #endif // endif WIN32 not CYGWIN
  1334. // run a command unix uses popen (easy)
  1335. // windows uses system and ShortPath
  1336. bool cmSystemTools::RunCommand(const char* command,
  1337. std::string& output,
  1338. int &retVal,
  1339. const char* dir,
  1340. bool verbose,
  1341. int timeout)
  1342. {
  1343. if(s_DisableRunCommandOutput)
  1344. {
  1345. verbose = false;
  1346. }
  1347. #if defined(WIN32) && !defined(__CYGWIN__)
  1348. // if the command does not start with a quote, then
  1349. // try to find the program, and if the program can not be
  1350. // found use system to run the command as it must be a built in
  1351. // shell command like echo or dir
  1352. int count = 0;
  1353. if(command[0] == '\"')
  1354. {
  1355. // count the number of quotes
  1356. for(const char* s = command; *s != 0; ++s)
  1357. {
  1358. if(*s == '\"')
  1359. {
  1360. count++;
  1361. if(count > 2)
  1362. {
  1363. break;
  1364. }
  1365. }
  1366. }
  1367. // if there are more than two double quotes use
  1368. // GetShortPathName, the cmd.exe program in windows which
  1369. // is used by system fails to execute if there are more than
  1370. // one set of quotes in the arguments
  1371. if(count > 2)
  1372. {
  1373. cmRegularExpression quoted("^\"([^\"]*)\"[ \t](.*)");
  1374. if(quoted.find(command))
  1375. {
  1376. std::string shortCmd;
  1377. std::string cmd = quoted.match(1);
  1378. std::string args = quoted.match(2);
  1379. if(! cmSystemTools::FileExists(cmd.c_str()) )
  1380. {
  1381. shortCmd = cmd;
  1382. }
  1383. else if(!cmSystemTools::GetShortPath(cmd.c_str(), shortCmd))
  1384. {
  1385. cmSystemTools::Error("GetShortPath failed for " , cmd.c_str());
  1386. return false;
  1387. }
  1388. shortCmd += " ";
  1389. shortCmd += args;
  1390. //return RunCommandViaSystem(shortCmd.c_str(), dir,
  1391. // output, retVal, verbose);
  1392. //return WindowsRunCommand(shortCmd.c_str(), dir,
  1393. //output, retVal, verbose);
  1394. return RunCommandViaWin32(shortCmd.c_str(), dir,
  1395. output, retVal, verbose, timeout);
  1396. }
  1397. else
  1398. {
  1399. cmSystemTools::Error("Could not parse command line with quotes ",
  1400. command);
  1401. }
  1402. }
  1403. }
  1404. // if there is only one set of quotes or no quotes then just run the command
  1405. //return RunCommandViaSystem(command, dir, output, retVal, verbose);
  1406. //return WindowsRunCommand(command, dir, output, retVal, verbose);
  1407. return ::RunCommandViaWin32(command, dir, output, retVal, verbose, timeout);
  1408. #else
  1409. return ::RunCommandViaPopen(command, dir, output, retVal, verbose, timeout);
  1410. #endif
  1411. }
  1412. /**
  1413. * Find the file the given name. Searches the given path and then
  1414. * the system search path. Returns the full path to the file if it is
  1415. * found. Otherwise, the empty string is returned.
  1416. */
  1417. std::string cmSystemTools::FindFile(const char* name,
  1418. const std::vector<std::string>& userPaths)
  1419. {
  1420. // Add the system search path to our path.
  1421. std::vector<std::string> path = userPaths;
  1422. cmSystemTools::GetPath(path);
  1423. std::string tryPath;
  1424. for(std::vector<std::string>::const_iterator p = path.begin();
  1425. p != path.end(); ++p)
  1426. {
  1427. tryPath = *p;
  1428. tryPath += "/";
  1429. tryPath += name;
  1430. if(cmSystemTools::FileExists(tryPath.c_str()) &&
  1431. !cmSystemTools::FileIsDirectory(tryPath.c_str()))
  1432. {
  1433. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1434. }
  1435. }
  1436. // Couldn't find the file.
  1437. return "";
  1438. }
  1439. /**
  1440. * Find the executable with the given name. Searches the given path and then
  1441. * the system search path. Returns the full path to the executable if it is
  1442. * found. Otherwise, the empty string is returned.
  1443. */
  1444. std::string cmSystemTools::FindProgram(const char* name,
  1445. const std::vector<std::string>& userPaths,
  1446. bool no_system_path)
  1447. {
  1448. // See if the executable exists as written.
  1449. if(cmSystemTools::FileExists(name) &&
  1450. !cmSystemTools::FileIsDirectory(name))
  1451. {
  1452. return cmSystemTools::CollapseFullPath(name);
  1453. }
  1454. std::string tryPath = name;
  1455. tryPath += cmSystemTools::GetExecutableExtension();
  1456. if(cmSystemTools::FileExists(tryPath.c_str()) &&
  1457. !cmSystemTools::FileIsDirectory(tryPath.c_str()))
  1458. {
  1459. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1460. }
  1461. // Add the system search path to our path.
  1462. std::vector<std::string> path = userPaths;
  1463. if (!no_system_path)
  1464. {
  1465. cmSystemTools::GetPath(path);
  1466. }
  1467. for(std::vector<std::string>::const_iterator p = path.begin();
  1468. p != path.end(); ++p)
  1469. {
  1470. tryPath = *p;
  1471. tryPath += "/";
  1472. tryPath += name;
  1473. if(cmSystemTools::FileExists(tryPath.c_str()) &&
  1474. !cmSystemTools::FileIsDirectory(tryPath.c_str()))
  1475. {
  1476. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1477. }
  1478. #ifdef _WIN32
  1479. tryPath += ".com";
  1480. if(cmSystemTools::FileExists(tryPath.c_str()) &&
  1481. !cmSystemTools::FileIsDirectory(tryPath.c_str()))
  1482. {
  1483. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1484. }
  1485. tryPath = *p;
  1486. tryPath += "/";
  1487. tryPath += name;
  1488. #endif
  1489. tryPath += cmSystemTools::GetExecutableExtension();
  1490. if(cmSystemTools::FileExists(tryPath.c_str()) &&
  1491. !cmSystemTools::FileIsDirectory(tryPath.c_str()))
  1492. {
  1493. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1494. }
  1495. }
  1496. // Couldn't find the program.
  1497. return "";
  1498. }
  1499. /**
  1500. * Find the library with the given name. Searches the given path and then
  1501. * the system search path. Returns the full path to the library if it is
  1502. * found. Otherwise, the empty string is returned.
  1503. */
  1504. std::string cmSystemTools::FindLibrary(const char* name,
  1505. const std::vector<std::string>& userPaths,
  1506. const cmMakefile *makefile)
  1507. {
  1508. // See if the executable exists as written.
  1509. if(cmSystemTools::FileExists(name))
  1510. {
  1511. return cmSystemTools::CollapseFullPath(name);
  1512. }
  1513. // Add the system search path to our path.
  1514. std::vector<std::string> path = userPaths;
  1515. cmSystemTools::GetPath(path);
  1516. // Add some lib directories specific to compilers, depending on the
  1517. // current generator, so that library that might have been stored here
  1518. // can be found too.
  1519. // i.e. Microsoft Visual Studio or .Net: path to compiler/../Lib
  1520. // Borland: path to compiler/../Lib
  1521. if (makefile)
  1522. {
  1523. const char* genName = makefile->GetDefinition("CMAKE_GENERATOR");
  1524. if (genName)
  1525. {
  1526. if (!strcmp(genName, "NMake Makefiles") ||
  1527. !strcmp(genName, "Visual Studio 6"))
  1528. {
  1529. const char* compiler = makefile->GetDefinition("CMAKE_CXX_COMPILER");
  1530. if (compiler)
  1531. {
  1532. std::string compiler_path = cmSystemTools::FindProgram(compiler);
  1533. if (compiler_path.size())
  1534. {
  1535. std::string lib_path =
  1536. cmSystemTools::GetFilenamePath(
  1537. cmSystemTools::GetFilenamePath(compiler_path)) + "/Lib";
  1538. path.push_back(lib_path);
  1539. }
  1540. }
  1541. }
  1542. else if (!strcmp(genName, "Visual Studio 7"))
  1543. {
  1544. // It is likely that the compiler won't be in the path for .Net, but
  1545. // we know where devenv is.
  1546. const char* devenv = makefile->GetDefinition("MICROSOFT_DEVENV");
  1547. if (devenv)
  1548. {
  1549. std::string devenv_path = cmSystemTools::FindProgram(devenv);
  1550. if (devenv_path.size())
  1551. {
  1552. std::string vc7_path =
  1553. cmSystemTools::GetFilenamePath(
  1554. cmSystemTools::GetFilenamePath(
  1555. cmSystemTools::GetFilenamePath(devenv_path))) + "/Vc7";
  1556. path.push_back(vc7_path + "/lib");
  1557. path.push_back(vc7_path + "/PlatformSDK/lib");
  1558. }
  1559. }
  1560. }
  1561. else if (!strcmp(genName, "Borland Makefiles"))
  1562. {
  1563. const char* bcb_bin_path = makefile->GetDefinition("BCB_BIN_PATH");
  1564. if (bcb_bin_path)
  1565. {
  1566. std::string lib_path =
  1567. cmSystemTools::GetFilenamePath(bcb_bin_path) + "/Lib";
  1568. path.push_back(lib_path);
  1569. }
  1570. }
  1571. }
  1572. }
  1573. std::string tryPath;
  1574. for(std::vector<std::string>::const_iterator p = path.begin();
  1575. p != path.end(); ++p)
  1576. {
  1577. #if defined(_WIN32) && !defined(__CYGWIN__)
  1578. tryPath = *p;
  1579. tryPath += "/";
  1580. tryPath += name;
  1581. tryPath += ".lib";
  1582. if(cmSystemTools::FileExists(tryPath.c_str()))
  1583. {
  1584. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1585. }
  1586. #else
  1587. tryPath = *p;
  1588. tryPath += "/lib";
  1589. tryPath += name;
  1590. tryPath += ".so";
  1591. if(cmSystemTools::FileExists(tryPath.c_str()))
  1592. {
  1593. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1594. }
  1595. tryPath = *p;
  1596. tryPath += "/lib";
  1597. tryPath += name;
  1598. tryPath += ".a";
  1599. if(cmSystemTools::FileExists(tryPath.c_str()))
  1600. {
  1601. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1602. }
  1603. tryPath = *p;
  1604. tryPath += "/lib";
  1605. tryPath += name;
  1606. tryPath += ".sl";
  1607. if(cmSystemTools::FileExists(tryPath.c_str()))
  1608. {
  1609. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1610. }
  1611. tryPath = *p;
  1612. tryPath += "/lib";
  1613. tryPath += name;
  1614. tryPath += ".dylib";
  1615. if(cmSystemTools::FileExists(tryPath.c_str()))
  1616. {
  1617. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1618. }
  1619. #endif
  1620. }
  1621. // Couldn't find the library.
  1622. return "";
  1623. }
  1624. bool cmSystemTools::FileIsDirectory(const char* name)
  1625. {
  1626. struct stat fs;
  1627. if(stat(name, &fs) == 0)
  1628. {
  1629. #if _WIN32
  1630. return ((fs.st_mode & _S_IFDIR) != 0);
  1631. #else
  1632. return S_ISDIR(fs.st_mode);
  1633. #endif
  1634. }
  1635. else
  1636. {
  1637. return false;
  1638. }
  1639. }
  1640. int cmSystemTools::ChangeDirectory(const char *dir)
  1641. {
  1642. return Chdir(dir);
  1643. }
  1644. std::string cmSystemTools::GetCurrentWorkingDirectory()
  1645. {
  1646. char buf[2048];
  1647. std::string path = Getcwd(buf, 2048);
  1648. return path;
  1649. }
  1650. /**
  1651. * Given the path to a program executable, get the directory part of the path with the
  1652. * file stripped off. If there is no directory part, the empty string is returned.
  1653. */
  1654. std::string cmSystemTools::GetProgramPath(const char* in_name)
  1655. {
  1656. std::string dir, file;
  1657. cmSystemTools::SplitProgramPath(in_name, dir, file);
  1658. return dir;
  1659. }
  1660. /**
  1661. * Given the path to a program executable, get the directory part of the path
  1662. * with the file stripped off. If there is no directory part, the empty
  1663. * string is returned.
  1664. */
  1665. void cmSystemTools::SplitProgramPath(const char* in_name,
  1666. std::string& dir,
  1667. std::string& file)
  1668. {
  1669. dir = in_name;
  1670. file = "";
  1671. cmSystemTools::ConvertToUnixSlashes(dir);
  1672. if(!cmSystemTools::FileIsDirectory(dir.c_str()))
  1673. {
  1674. std::string::size_type slashPos = dir.rfind("/");
  1675. if(slashPos != std::string::npos)
  1676. {
  1677. file = dir.substr(slashPos+1);
  1678. dir = dir.substr(0, slashPos);
  1679. }
  1680. else
  1681. {
  1682. file = dir;
  1683. dir = "";
  1684. }
  1685. }
  1686. if((dir != "") && !cmSystemTools::FileIsDirectory(dir.c_str()))
  1687. {
  1688. std::string oldDir = in_name;
  1689. cmSystemTools::ConvertToUnixSlashes(oldDir);
  1690. cmSystemTools::Error("Error splitting file name off end of path:\n",
  1691. oldDir.c_str(), "\nDirectory not found: ",
  1692. dir.c_str());
  1693. dir = in_name;
  1694. return;
  1695. }
  1696. }
  1697. /**
  1698. * Given a path to a file or directory, convert it to a full path.
  1699. * This collapses away relative paths. The full path is returned.
  1700. */
  1701. std::string cmSystemTools::CollapseFullPath(const char* in_name)
  1702. {
  1703. std::string dir, file;
  1704. cmSystemTools::SplitProgramPath(in_name, dir, file);
  1705. #ifdef _WIN32
  1706. // Ultra-hack warning:
  1707. // This changes to the target directory, saves the working directory,
  1708. // and then changes back to the original working directory.
  1709. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  1710. if(dir != "") { Chdir(dir.c_str()); }
  1711. std::string newDir = cmSystemTools::GetCurrentWorkingDirectory();
  1712. Chdir(cwd.c_str());
  1713. cmSystemTools::ConvertToUnixSlashes(newDir);
  1714. std::string newPath = newDir+"/"+file;
  1715. return newPath;
  1716. #else
  1717. # ifdef MAXPATHLEN
  1718. char resolved_name[MAXPATHLEN];
  1719. # else
  1720. # ifdef PATH_MAX
  1721. char resolved_name[PATH_MAX];
  1722. # else
  1723. char resolved_name[5024];
  1724. # endif
  1725. # endif
  1726. if(dir != "")
  1727. {
  1728. realpath(dir.c_str(), resolved_name);
  1729. dir = resolved_name;
  1730. }
  1731. else
  1732. {
  1733. dir = cmSystemTools::GetCurrentWorkingDirectory();
  1734. }
  1735. if(file == "")
  1736. {
  1737. return dir;
  1738. }
  1739. return dir + "/" + file;
  1740. #endif
  1741. }
  1742. bool cmSystemTools::Split(const char* str, std::vector<cmStdString>& lines)
  1743. {
  1744. std::string data(str);
  1745. std::string::size_type lpos = 0;
  1746. while(lpos < data.length())
  1747. {
  1748. std::string::size_type rpos = data.find_first_of("\n", lpos);
  1749. if(rpos == std::string::npos)
  1750. {
  1751. // Line ends at end of string without a newline.
  1752. lines.push_back(data.substr(lpos));
  1753. return false;
  1754. }
  1755. if((rpos > lpos) && (data[rpos-1] == '\r'))
  1756. {
  1757. // Line ends in a "\r\n" pair, remove both characters.
  1758. lines.push_back(data.substr(lpos, (rpos-1)-lpos));
  1759. }
  1760. else
  1761. {
  1762. // Line ends in a "\n", remove the character.
  1763. lines.push_back(data.substr(lpos, rpos-lpos));
  1764. }
  1765. lpos = rpos+1;
  1766. }
  1767. return true;
  1768. }
  1769. /**
  1770. * Return path of a full filename (no trailing slashes).
  1771. * Warning: returned path is converted to Unix slashes format.
  1772. */
  1773. std::string cmSystemTools::GetFilenamePath(const std::string& filename)
  1774. {
  1775. std::string fn = filename;
  1776. cmSystemTools::ConvertToUnixSlashes(fn);
  1777. std::string::size_type slash_pos = fn.rfind("/");
  1778. if(slash_pos != std::string::npos)
  1779. {
  1780. return fn.substr(0, slash_pos);
  1781. }
  1782. else
  1783. {
  1784. return "";
  1785. }
  1786. }
  1787. /**
  1788. * Return file name of a full filename (i.e. file name without path).
  1789. */
  1790. std::string cmSystemTools::GetFilenameName(const std::string& filename)
  1791. {
  1792. std::string fn = filename;
  1793. cmSystemTools::ConvertToUnixSlashes(fn);
  1794. std::string::size_type slash_pos = fn.rfind("/");
  1795. if(slash_pos != std::string::npos)
  1796. {
  1797. return fn.substr(slash_pos + 1);
  1798. }
  1799. else
  1800. {
  1801. return filename;
  1802. }
  1803. }
  1804. /**
  1805. * Return file extension of a full filename (dot included).
  1806. * Warning: this is the longest extension (for example: .tar.gz)
  1807. */
  1808. std::string cmSystemTools::GetFilenameExtension(const std::string& filename)
  1809. {
  1810. std::string name = cmSystemTools::GetFilenameName(filename);
  1811. std::string::size_type dot_pos = name.find(".");
  1812. if(dot_pos != std::string::npos)
  1813. {
  1814. return name.substr(dot_pos);
  1815. }
  1816. else
  1817. {
  1818. return "";
  1819. }
  1820. }
  1821. /**
  1822. * Return file name without extension of a full filename (i.e. without path).
  1823. * Warning: it considers the longest extension (for example: .tar.gz)
  1824. */
  1825. std::string cmSystemTools::GetFilenameWithoutExtension(const std::string& filename)
  1826. {
  1827. std::string name = cmSystemTools::GetFilenameName(filename);
  1828. std::string::size_type dot_pos = name.find(".");
  1829. if(dot_pos != std::string::npos)
  1830. {
  1831. return name.substr(0, dot_pos);
  1832. }
  1833. else
  1834. {
  1835. return name;
  1836. }
  1837. }
  1838. /**
  1839. * Return file name without extension of a full filename (i.e. without path).
  1840. * Warning: it considers the last extension (for example: removes .gz
  1841. * from .tar.gz)
  1842. */
  1843. std::string
  1844. cmSystemTools::GetFilenameWithoutLastExtension(const std::string& filename)
  1845. {
  1846. std::string name = cmSystemTools::GetFilenameName(filename);
  1847. std::string::size_type dot_pos = name.rfind(".");
  1848. if(dot_pos != std::string::npos)
  1849. {
  1850. return name.substr(0, dot_pos);
  1851. }
  1852. else
  1853. {
  1854. return name;
  1855. }
  1856. }
  1857. bool cmSystemTools::FileIsFullPath(const char* in_name)
  1858. {
  1859. std::string name = in_name;
  1860. #if defined(_WIN32)
  1861. // On Windows, the name must be at least two characters long.
  1862. if(name.length() < 2)
  1863. {
  1864. return false;
  1865. }
  1866. if(name[1] == ':')
  1867. {
  1868. return true;
  1869. }
  1870. if(name[0] == '\\')
  1871. {
  1872. return true;
  1873. }
  1874. #else
  1875. // On UNIX, the name must be at least one character long.
  1876. if(name.length() < 1)
  1877. {
  1878. return false;
  1879. }
  1880. #endif
  1881. // On UNIX, the name must begin in a '/'.
  1882. // On Windows, if the name begins in a '/', then it is a full
  1883. // network path.
  1884. if(name[0] == '/')
  1885. {
  1886. return true;
  1887. }
  1888. return false;
  1889. }
  1890. void cmSystemTools::Glob(const char *directory, const char *regexp,
  1891. std::vector<std::string>& files)
  1892. {
  1893. cmDirectory d;
  1894. cmRegularExpression reg(regexp);
  1895. if (d.Load(directory))
  1896. {
  1897. size_t numf;
  1898. unsigned int i;
  1899. numf = d.GetNumberOfFiles();
  1900. for (i = 0; i < numf; i++)
  1901. {
  1902. std::string fname = d.GetFile(i);
  1903. if (reg.find(fname))
  1904. {
  1905. files.push_back(fname);
  1906. }
  1907. }
  1908. }
  1909. }
  1910. void cmSystemTools::GlobDirs(const char *fullPath,
  1911. std::vector<std::string>& files)
  1912. {
  1913. std::string path = fullPath;
  1914. std::string::size_type pos = path.find("/*");
  1915. if(pos == std::string::npos)
  1916. {
  1917. files.push_back(fullPath);
  1918. return;
  1919. }
  1920. std::string startPath = path.substr(0, pos);
  1921. std::string finishPath = path.substr(pos+2);
  1922. cmDirectory d;
  1923. if (d.Load(startPath.c_str()))
  1924. {
  1925. for (unsigned int i = 0; i < d.GetNumberOfFiles(); ++i)
  1926. {
  1927. if((std::string(d.GetFile(i)) != ".")
  1928. && (std::string(d.GetFile(i)) != ".."))
  1929. {
  1930. std::string fname = startPath;
  1931. fname +="/";
  1932. fname += d.GetFile(i);
  1933. if(cmSystemTools::FileIsDirectory(fname.c_str()))
  1934. {
  1935. fname += finishPath;
  1936. cmSystemTools::GlobDirs(fname.c_str(), files);
  1937. }
  1938. }
  1939. }
  1940. }
  1941. }
  1942. void cmSystemTools::ExpandListArguments(std::vector<std::string> const& arguments,
  1943. std::vector<std::string>& newargs)
  1944. {
  1945. std::vector<std::string>::const_iterator i;
  1946. std::string newarg;
  1947. for(i = arguments.begin();i != arguments.end(); ++i)
  1948. {
  1949. // if there are no ; in the name then just copy the current string
  1950. if(i->find(';') == std::string::npos)
  1951. {
  1952. newargs.push_back(*i);
  1953. }
  1954. else
  1955. {
  1956. std::string::size_type start = 0;
  1957. std::string::size_type endpos = 0;
  1958. const std::string::size_type size = i->size();
  1959. // break up ; separated sections of the string into separate strings
  1960. while(endpos != size)
  1961. {
  1962. endpos = i->find(';', start);
  1963. if(endpos == std::string::npos)
  1964. {
  1965. endpos = i->size();
  1966. }
  1967. else
  1968. {
  1969. // skip right over escaped ; ( \; )
  1970. while((endpos != std::string::npos)
  1971. && (endpos > 0)
  1972. && ((*i)[endpos-1] == '\\') )
  1973. {
  1974. endpos = i->find(';', endpos+1);
  1975. }
  1976. if(endpos == std::string::npos)
  1977. {
  1978. endpos = i->size();
  1979. }
  1980. }
  1981. std::string::size_type len = endpos - start;
  1982. if (len > 0)
  1983. {
  1984. // check for a closing ] after the start position
  1985. if(i->find('[', start) == std::string::npos)
  1986. {
  1987. // if there is no [ in the string then keep it
  1988. newarg = i->substr(start, len);
  1989. }
  1990. else
  1991. {
  1992. int opencount = 0;
  1993. int closecount = 0;
  1994. for(std::string::size_type j = start; j < endpos; ++j)
  1995. {
  1996. if(i->at(j) == '[')
  1997. {
  1998. ++opencount;
  1999. }
  2000. else if (i->at(j) == ']')
  2001. {
  2002. ++closecount;
  2003. }
  2004. }
  2005. if(opencount != closecount)
  2006. {
  2007. // skip this one
  2008. endpos = i->find(';', endpos+1);
  2009. if(endpos == std::string::npos)
  2010. {
  2011. endpos = i->size();
  2012. }
  2013. len = endpos - start;
  2014. }
  2015. newarg = i->substr(start, len);
  2016. }
  2017. std::string::size_type pos = newarg.find("\\;");
  2018. if(pos != std::string::npos)
  2019. {
  2020. newarg.erase(pos, 1);
  2021. }
  2022. newargs.push_back(newarg);
  2023. }
  2024. start = endpos+1;
  2025. }
  2026. }
  2027. }
  2028. }
  2029. bool cmSystemTools::GetShortPath(const char* path, std::string& shortPath)
  2030. {
  2031. #if defined(WIN32) && !defined(__CYGWIN__)
  2032. const int size = int(strlen(path)) +1; // size of return
  2033. char *buffer = new char[size]; // create a buffer
  2034. char *tempPath = new char[size]; // create a buffer
  2035. int ret;
  2036. // if the path passed in has quotes around it, first remove the quotes
  2037. if (path[0] == '"' && path[strlen(path)-1] == '"')
  2038. {
  2039. strcpy(tempPath,path+1);
  2040. tempPath[strlen(tempPath)-1] = '\0';
  2041. }
  2042. else
  2043. {
  2044. strcpy(tempPath,path);
  2045. }
  2046. buffer[0] = 0;
  2047. ret = GetShortPathName(tempPath, buffer, size);
  2048. if(buffer[0] == 0 || ret > size)
  2049. {
  2050. if(ret < size)
  2051. {
  2052. LPVOID lpMsgBuf;
  2053. FormatMessage(
  2054. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  2055. FORMAT_MESSAGE_FROM_SYSTEM |
  2056. FORMAT_MESSAGE_IGNORE_INSERTS,
  2057. NULL,
  2058. GetLastError(),
  2059. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  2060. (LPTSTR) &lpMsgBuf,
  2061. 0,
  2062. NULL
  2063. );
  2064. cmSystemTools::Error((LPCTSTR)lpMsgBuf);
  2065. LocalFree( lpMsgBuf );
  2066. }
  2067. cmSystemTools::Error("Unable to get a short path: ", path);
  2068. delete [] tempPath;
  2069. return false;
  2070. }
  2071. else
  2072. {
  2073. shortPath = buffer;
  2074. delete [] buffer;
  2075. delete [] tempPath;
  2076. return true;
  2077. }
  2078. #else
  2079. shortPath = path;
  2080. return true;
  2081. #endif
  2082. }
  2083. cmSystemTools::e_FileFormat cmSystemTools::GetFileFormat(const char* cext)
  2084. {
  2085. if ( ! cext || *cext == 0 )
  2086. {
  2087. return cmSystemTools::NO_FILE_FORMAT;
  2088. }
  2089. std::string ext = cmSystemTools::LowerCase(cext);
  2090. if ( ext == "c" || ext == ".c" ) { return cmSystemTools::C_FILE_FORMAT; }
  2091. if ( ext == "cxx" || ext == ".cxx" ||
  2092. ext == "cpp" || ext == ".cpp" ||
  2093. ext == "c++" || ext == ".c++" ||
  2094. ext == "cc" || ext == ".cc" ) { return cmSystemTools::CXX_FILE_FORMAT; }
  2095. if ( ext == "java" || ext == ".java" ) { return cmSystemTools::JAVA_FILE_FORMAT; }
  2096. if ( ext == "h" || ext == ".h" ||
  2097. ext == "hpp" || ext == ".hpp" ||
  2098. ext == "h++" || ext == ".h++" ||
  2099. ext == "hxx" || ext == ".hxx" ) { return cmSystemTools::HEADER_FILE_FORMAT; }
  2100. if ( ext == "rc" || ext == ".rc" ) { return cmSystemTools::RESOURCE_FILE_FORMAT; }
  2101. if ( ext == "def" || ext == ".def" ) { return cmSystemTools::DEFINITION_FILE_FORMAT; }
  2102. if ( ext == "lib" || ext == ".lib" ||
  2103. ext == "a" || ext == ".a") { return cmSystemTools::STATIC_LIBRARY_FILE_FORMAT; }
  2104. if ( ext == "o" || ext == ".o" ||
  2105. ext == "obj" || ext == ".obj") { return cmSystemTools::OBJECT_FILE_FORMAT; }
  2106. #ifdef __APPLE__
  2107. if ( ext == "dylib" || ext == ".dylib" )
  2108. { return cmSystemTools::SHARED_LIBRARY_FILE_FORMAT; }
  2109. if ( ext == "so" || ext == ".so" ||
  2110. ext == "bundle" || ext == ".bundle" )
  2111. { return cmSystemTools::MODULE_FILE_FORMAT; }
  2112. #else // __APPLE__
  2113. if ( ext == "so" || ext == ".so" ||
  2114. ext == "sl" || ext == ".sl" ||
  2115. ext == "dll" || ext == ".dll" )
  2116. { return cmSystemTools::SHARED_LIBRARY_FILE_FORMAT; }
  2117. #endif // __APPLE__
  2118. return cmSystemTools::UNKNOWN_FILE_FORMAT;
  2119. }