cmSystemTools.cxx 57 KB

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