cmSystemTools.cxx 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314
  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 all // from the path just like most unix shells
  645. int start_find = 0;
  646. #ifdef _WIN32
  647. // However, on windows if the first characters are both slashes,
  648. // then keep them that way, so that network paths can be handled.
  649. start_find = 1;
  650. #endif
  651. while((pos = path.find("//", start_find)) != std::string::npos)
  652. {
  653. cmSystemTools::ReplaceString(path, "//", "/");
  654. }
  655. // remove any trailing slash
  656. if(path.size() && path[path.size()-1] == '/')
  657. {
  658. path = path.substr(0, path.size()-1);
  659. }
  660. // if there is a tilda ~ then replace it with HOME
  661. if(path.find("~") == 0)
  662. {
  663. if (getenv("HOME"))
  664. {
  665. path = std::string(getenv("HOME")) + path.substr(1);
  666. }
  667. }
  668. // if there is a /tmp_mnt in a path get rid of it!
  669. // stupid sgi's
  670. if(path.find("/tmp_mnt") == 0)
  671. {
  672. path = path.substr(8);
  673. }
  674. }
  675. // change // to /, and escape any spaces in the path
  676. std::string cmSystemTools::ConvertToUnixOutputPath(const char* path)
  677. {
  678. std::string ret = path;
  679. // remove // except at the beginning might be a cygwin drive
  680. std::string::size_type pos = 1;
  681. while((pos = ret.find("//", pos)) != std::string::npos)
  682. {
  683. ret.erase(pos, 1);
  684. }
  685. // now escape spaces if there is a space in the path
  686. if(ret.find(" ") != std::string::npos)
  687. {
  688. std::string result = "";
  689. char lastch = 1;
  690. for(const char* ch = ret.c_str(); *ch != '\0'; ++ch)
  691. {
  692. // if it is already escaped then don't try to escape it again
  693. if(*ch == ' ' && lastch != '\\')
  694. {
  695. result += '\\';
  696. }
  697. result += *ch;
  698. lastch = *ch;
  699. }
  700. ret = result;
  701. }
  702. return ret;
  703. }
  704. std::string cmSystemTools::EscapeSpaces(const char* str)
  705. {
  706. #if defined(_WIN32) && !defined(__CYGWIN__)
  707. std::string result;
  708. // if there are spaces
  709. std::string temp = str;
  710. if (temp.find(" ") != std::string::npos &&
  711. temp.find("\"")==std::string::npos)
  712. {
  713. result = "\"";
  714. result += str;
  715. result += "\"";
  716. return result;
  717. }
  718. return str;
  719. #else
  720. std::string result = "";
  721. for(const char* ch = str; *ch != '\0'; ++ch)
  722. {
  723. if(*ch == ' ')
  724. {
  725. result += '\\';
  726. }
  727. result += *ch;
  728. }
  729. return result;
  730. #endif
  731. }
  732. std::string cmSystemTools::ConvertToOutputPath(const char* path)
  733. {
  734. #if defined(_WIN32) && !defined(__CYGWIN__)
  735. return cmSystemTools::ConvertToWindowsOutputPath(path);
  736. #else
  737. return cmSystemTools::ConvertToUnixOutputPath(path);
  738. #endif
  739. }
  740. // remove double slashes not at the start
  741. std::string cmSystemTools::ConvertToWindowsOutputPath(const char* path)
  742. {
  743. std::string ret = path;
  744. std::string::size_type pos = 0;
  745. // first convert all of the slashes
  746. while((pos = ret.find('/', pos)) != std::string::npos)
  747. {
  748. ret[pos] = '\\';
  749. pos++;
  750. }
  751. // check for really small paths
  752. if(ret.size() < 2)
  753. {
  754. return ret;
  755. }
  756. // now clean up a bit and remove double slashes
  757. // Only if it is not the first position in the path which is a network
  758. // path on windows
  759. pos = 1; // start at position 1
  760. if(ret[0] == '\"')
  761. {
  762. pos = 2; // if the string is already quoted then start at 2
  763. if(ret.size() < 3)
  764. {
  765. return ret;
  766. }
  767. }
  768. while((pos = ret.find("\\\\", pos)) != std::string::npos)
  769. {
  770. ret.erase(pos, 1);
  771. }
  772. // now double quote the path if it has spaces in it
  773. // and is not already double quoted
  774. if(ret.find(" ") != std::string::npos
  775. && ret[0] != '\"')
  776. {
  777. std::string result;
  778. result = "\"" + ret + "\"";
  779. ret = result;
  780. }
  781. return ret;
  782. }
  783. std::string cmSystemTools::RemoveEscapes(const char* s)
  784. {
  785. std::string result = "";
  786. for(const char* ch = s; *ch; ++ch)
  787. {
  788. if(*ch == '\\' && *(ch+1) != ';')
  789. {
  790. ++ch;
  791. switch (*ch)
  792. {
  793. case '\\': result.insert(result.end(), '\\'); break;
  794. case '"': result.insert(result.end(), '"'); break;
  795. case ' ': result.insert(result.end(), ' '); break;
  796. case 't': result.insert(result.end(), '\t'); break;
  797. case 'n': result.insert(result.end(), '\n'); break;
  798. case 'r': result.insert(result.end(), '\r'); break;
  799. case '0': result.insert(result.end(), '\0'); break;
  800. case '\0':
  801. {
  802. cmSystemTools::Error("Trailing backslash in argument:\n", s);
  803. return result;
  804. }
  805. default:
  806. {
  807. std::string chStr(1, *ch);
  808. cmSystemTools::Error("Invalid escape sequence \\", chStr.c_str(),
  809. "\nin argument ", s);
  810. }
  811. }
  812. }
  813. else
  814. {
  815. result.insert(result.end(), *ch);
  816. }
  817. }
  818. return result;
  819. }
  820. void cmSystemTools::Error(const char* m1, const char* m2,
  821. const char* m3, const char* m4)
  822. {
  823. std::string message = "CMake Error: ";
  824. if(m1)
  825. {
  826. message += m1;
  827. }
  828. if(m2)
  829. {
  830. message += m2;
  831. }
  832. if(m3)
  833. {
  834. message += m3;
  835. }
  836. if(m4)
  837. {
  838. message += m4;
  839. }
  840. cmSystemTools::s_ErrorOccured = true;
  841. cmSystemTools::Message(message.c_str(),"Error");
  842. }
  843. void cmSystemTools::SetErrorCallback(ErrorCallback f, void* clientData)
  844. {
  845. s_ErrorCallback = f;
  846. s_ErrorCallbackClientData = clientData;
  847. }
  848. void cmSystemTools::Message(const char* m1, const char *title)
  849. {
  850. if(s_DisableMessages)
  851. {
  852. return;
  853. }
  854. if(s_ErrorCallback)
  855. {
  856. (*s_ErrorCallback)(m1, title, s_DisableMessages, s_ErrorCallbackClientData);
  857. return;
  858. }
  859. else
  860. {
  861. std::cerr << m1 << std::endl << std::flush;
  862. }
  863. }
  864. bool cmSystemTools::CopyFileIfDifferent(const char* source,
  865. const char* destination)
  866. {
  867. if(cmSystemTools::FilesDiffer(source, destination))
  868. {
  869. cmSystemTools::cmCopyFile(source, destination);
  870. return true;
  871. }
  872. return false;
  873. }
  874. bool cmSystemTools::FilesDiffer(const char* source,
  875. const char* destination)
  876. {
  877. struct stat statSource;
  878. if (stat(source, &statSource) != 0)
  879. {
  880. return true;
  881. }
  882. struct stat statDestination;
  883. if (stat(destination, &statDestination) != 0)
  884. {
  885. return true;
  886. }
  887. if(statSource.st_size != statDestination.st_size)
  888. {
  889. return true;
  890. }
  891. if(statSource.st_size == 0)
  892. {
  893. return false;
  894. }
  895. #if defined(_WIN32) || defined(__CYGWIN__)
  896. std::ifstream finSource(source,
  897. std::ios::binary | std::ios::in);
  898. std::ifstream finDestination(destination,
  899. std::ios::binary | std::ios::in);
  900. #else
  901. std::ifstream finSource(source);
  902. std::ifstream finDestination(destination);
  903. #endif
  904. if(!finSource || !finDestination)
  905. {
  906. return true;
  907. }
  908. char* source_buf = new char[statSource.st_size];
  909. char* dest_buf = new char[statSource.st_size];
  910. finSource.read(source_buf, statSource.st_size);
  911. finDestination.read(dest_buf, statSource.st_size);
  912. if(statSource.st_size != static_cast<long>(finSource.gcount()) ||
  913. statSource.st_size != static_cast<long>(finDestination.gcount()))
  914. {
  915. cmOStringStream msg;
  916. msg << "FilesDiffer failed to read files (allocated: "
  917. << statSource.st_size << ", read source: " << finSource.gcount()
  918. << ", read dest: " << finDestination.gcount();
  919. cmSystemTools::Error(msg.str().c_str());
  920. delete [] source_buf;
  921. delete [] dest_buf;
  922. return false;
  923. }
  924. int ret = memcmp((const void*)source_buf,
  925. (const void*)dest_buf,
  926. statSource.st_size);
  927. delete [] dest_buf;
  928. delete [] source_buf;
  929. return ret != 0;
  930. }
  931. /**
  932. * Copy a file named by "source" to the file named by "destination".
  933. */
  934. void cmSystemTools::cmCopyFile(const char* source,
  935. const char* destination)
  936. {
  937. const int bufferSize = 4096;
  938. char buffer[bufferSize];
  939. // If destination is a directory, try to create a file with the same
  940. // name as the source in that directory.
  941. std::string new_destination;
  942. if(cmSystemTools::FileExists(destination) &&
  943. cmSystemTools::FileIsDirectory(destination))
  944. {
  945. new_destination = destination;
  946. cmSystemTools::ConvertToUnixSlashes(new_destination);
  947. new_destination += '/';
  948. std::string source_name = source;
  949. new_destination += cmSystemTools::GetFilenameName(source_name);
  950. destination = new_destination.c_str();
  951. }
  952. // Create destination directory
  953. std::string destination_dir = destination;
  954. destination_dir = cmSystemTools::GetFilenamePath(destination_dir);
  955. cmSystemTools::MakeDirectory(destination_dir.c_str());
  956. // Open files
  957. #if defined(_WIN32) || defined(__CYGWIN__)
  958. std::ifstream fin(source,
  959. std::ios::binary | std::ios::in);
  960. #else
  961. std::ifstream fin(source);
  962. #endif
  963. if(!fin)
  964. {
  965. std::string m = "CopyFile failed to open input file \"";
  966. m += source;
  967. m += "\"";
  968. m += " System Error: ";
  969. m += strerror(errno);
  970. cmSystemTools::Error(m.c_str());
  971. return;
  972. }
  973. #if defined(_WIN32) || defined(__CYGWIN__)
  974. std::ofstream fout(destination,
  975. std::ios::binary | std::ios::out | std::ios::trunc);
  976. #else
  977. std::ofstream fout(destination,
  978. std::ios::out | std::ios::trunc);
  979. #endif
  980. if(!fout)
  981. {
  982. std::string m = "CopyFile failed to open output file \"";
  983. m += destination;
  984. m += "\"";
  985. m += " System Error: ";
  986. m += strerror(errno);
  987. cmSystemTools::Error(m.c_str());
  988. return;
  989. }
  990. // This copy loop is very sensitive on certain platforms with
  991. // slightly broken stream libraries (like HPUX). Normally, it is
  992. // incorrect to not check the error condition on the fin.read()
  993. // before using the data, but the fin.gcount() will be zero if an
  994. // error occurred. Therefore, the loop should be safe everywhere.
  995. while(fin)
  996. {
  997. fin.read(buffer, bufferSize);
  998. if(fin.gcount())
  999. {
  1000. fout.write(buffer, fin.gcount());
  1001. }
  1002. }
  1003. // Make sure the operating system has finished writing the file
  1004. // before closing it. This will ensure the file is finished before
  1005. // the check below.
  1006. fout.flush();
  1007. fin.close();
  1008. fout.close();
  1009. // More checks
  1010. struct stat statSource, statDestination;
  1011. if (stat(source, &statSource) != 0 ||
  1012. stat(destination, &statDestination) != 0)
  1013. {
  1014. cmSystemTools::Error("CopyFile failed to copy files!");
  1015. }
  1016. else
  1017. {
  1018. if (statSource.st_size != statDestination.st_size)
  1019. {
  1020. cmOStringStream msg;
  1021. msg << "CopyFile failed to copy files (sizes differ, source: "
  1022. << statSource.st_size << " , dest: " << statDestination.st_size;
  1023. cmSystemTools::Error(msg.str().c_str());
  1024. }
  1025. }
  1026. }
  1027. // return true if the file exists
  1028. long int cmSystemTools::ModifiedTime(const char* filename)
  1029. {
  1030. struct stat fs;
  1031. if (stat(filename, &fs) != 0)
  1032. {
  1033. return 0;
  1034. }
  1035. else
  1036. {
  1037. return (long int)fs.st_mtime;
  1038. }
  1039. }
  1040. bool cmSystemTools::RemoveFile(const char* source)
  1041. {
  1042. return unlink(source) != 0 ? false : true;
  1043. }
  1044. bool cmSystemTools::IsOn(const char* val)
  1045. {
  1046. if (!val)
  1047. {
  1048. return false;
  1049. }
  1050. std::basic_string<char> v = val;
  1051. for(std::basic_string<char>::iterator c = v.begin();
  1052. c != v.end(); c++)
  1053. {
  1054. *c = toupper(*c);
  1055. }
  1056. return (v == "ON" || v == "1" || v == "YES" || v == "TRUE" || v == "Y");
  1057. }
  1058. bool cmSystemTools::IsOff(const char* val)
  1059. {
  1060. if (!val || strlen(val) == 0)
  1061. {
  1062. return true;
  1063. }
  1064. std::basic_string<char> v = val;
  1065. for(std::basic_string<char>::iterator c = v.begin();
  1066. c != v.end(); c++)
  1067. {
  1068. *c = toupper(*c);
  1069. }
  1070. return (v == "OFF" || v == "0" || v == "NO" || v == "FALSE" ||
  1071. v == "N" || v == "NOTFOUND" || v == "IGNORE");
  1072. }
  1073. bool cmSystemTools::RunCommand(const char* command,
  1074. std::string& output,
  1075. const char* dir,
  1076. bool verbose,
  1077. int timeout)
  1078. {
  1079. int dummy;
  1080. return cmSystemTools::RunCommand(command, output, dummy,
  1081. dir, verbose, timeout);
  1082. }
  1083. #if defined(WIN32) && !defined(__CYGWIN__)
  1084. #include "cmWin32ProcessExecution.h"
  1085. // use this for shell commands like echo and dir
  1086. bool RunCommandViaWin32(const char* command,
  1087. const char* dir,
  1088. std::string& output,
  1089. int& retVal,
  1090. bool verbose,
  1091. int timeout)
  1092. {
  1093. #if defined(__BORLANDC__)
  1094. return cmWin32ProcessExecution::BorlandRunCommand(command, dir, output,
  1095. retVal,
  1096. verbose, timeout,
  1097. cmSystemTools::GetRunCommandHideConsole());
  1098. #else // Visual studio
  1099. ::SetLastError(ERROR_SUCCESS);
  1100. if ( ! command )
  1101. {
  1102. cmSystemTools::Error("No command specified");
  1103. return false;
  1104. }
  1105. cmWin32ProcessExecution resProc;
  1106. if(cmSystemTools::GetRunCommandHideConsole())
  1107. {
  1108. resProc.SetHideWindows(true);
  1109. }
  1110. if ( cmSystemTools::GetWindows9xComspecSubstitute() )
  1111. {
  1112. resProc.SetConsoleSpawn(cmSystemTools::GetWindows9xComspecSubstitute() );
  1113. }
  1114. if ( !resProc.StartProcess(command, dir, verbose) )
  1115. {
  1116. std::cout << "Problem starting command" << std::endl;
  1117. return false;
  1118. }
  1119. resProc.Wait(timeout);
  1120. output = resProc.GetOutput();
  1121. retVal = resProc.GetExitValue();
  1122. return true;
  1123. #endif
  1124. }
  1125. // use this for shell commands like echo and dir
  1126. bool RunCommandViaSystem(const char* command,
  1127. const char* dir,
  1128. std::string& output,
  1129. int& retVal,
  1130. bool verbose)
  1131. {
  1132. std::cout << "@@ " << command << std::endl;
  1133. const int BUFFER_SIZE = 4096;
  1134. char buffer[BUFFER_SIZE];
  1135. std::string commandInDir;
  1136. if(dir)
  1137. {
  1138. commandInDir = "cd ";
  1139. commandInDir += cmSystemTools::ConvertToOutputPath(dir);
  1140. commandInDir += " && ";
  1141. commandInDir += command;
  1142. }
  1143. else
  1144. {
  1145. commandInDir = command;
  1146. }
  1147. command = commandInDir.c_str();
  1148. std::string commandToFile = command;
  1149. commandToFile += " > ";
  1150. std::string tempFile;
  1151. tempFile += _tempnam(0, "cmake");
  1152. commandToFile += tempFile;
  1153. retVal = system(commandToFile.c_str());
  1154. std::ifstream fin(tempFile.c_str());
  1155. if(!fin)
  1156. {
  1157. if(verbose)
  1158. {
  1159. std::string errormsg = "RunCommand produced no output: command: \"";
  1160. errormsg += command;
  1161. errormsg += "\"";
  1162. errormsg += "\nOutput file: ";
  1163. errormsg += tempFile;
  1164. cmSystemTools::Error(errormsg.c_str());
  1165. }
  1166. fin.close();
  1167. cmSystemTools::RemoveFile(tempFile.c_str());
  1168. return false;
  1169. }
  1170. bool multiLine = false;
  1171. while(fin)
  1172. {
  1173. fin.getline(buffer, BUFFER_SIZE);
  1174. output += buffer;
  1175. if(multiLine)
  1176. {
  1177. output += "\n";
  1178. }
  1179. multiLine = true;
  1180. }
  1181. fin.close();
  1182. cmSystemTools::RemoveFile(tempFile.c_str());
  1183. return true;
  1184. }
  1185. #else // We have popen
  1186. bool RunCommandViaPopen(const char* command,
  1187. const char* dir,
  1188. std::string& output,
  1189. int& retVal,
  1190. bool verbose,
  1191. int /*timeout*/)
  1192. {
  1193. // if only popen worked on windows.....
  1194. std::string commandInDir;
  1195. if(dir)
  1196. {
  1197. commandInDir = "cd \"";
  1198. commandInDir += dir;
  1199. commandInDir += "\" && ";
  1200. commandInDir += command;
  1201. }
  1202. else
  1203. {
  1204. commandInDir = command;
  1205. }
  1206. commandInDir += " 2>&1";
  1207. command = commandInDir.c_str();
  1208. const int BUFFER_SIZE = 4096;
  1209. char buffer[BUFFER_SIZE];
  1210. if(verbose)
  1211. {
  1212. std::cout << "running " << command << std::endl;
  1213. }
  1214. fflush(stdout);
  1215. fflush(stderr);
  1216. FILE* cpipe = popen(command, "r");
  1217. if(!cpipe)
  1218. {
  1219. return false;
  1220. }
  1221. fgets(buffer, BUFFER_SIZE, cpipe);
  1222. while(!feof(cpipe))
  1223. {
  1224. if(verbose)
  1225. {
  1226. std::cout << buffer << std::flush;
  1227. }
  1228. output += buffer;
  1229. fgets(buffer, BUFFER_SIZE, cpipe);
  1230. }
  1231. retVal = pclose(cpipe);
  1232. if (WIFEXITED(retVal))
  1233. {
  1234. retVal = WEXITSTATUS(retVal);
  1235. return true;
  1236. }
  1237. if (WIFSIGNALED(retVal))
  1238. {
  1239. retVal = WTERMSIG(retVal);
  1240. cmOStringStream error;
  1241. error << "\nProcess terminated due to ";
  1242. switch (retVal)
  1243. {
  1244. #ifdef SIGKILL
  1245. case SIGKILL:
  1246. error << "SIGKILL";
  1247. break;
  1248. #endif
  1249. #ifdef SIGFPE
  1250. case SIGFPE:
  1251. error << "SIGFPE";
  1252. break;
  1253. #endif
  1254. #ifdef SIGBUS
  1255. case SIGBUS:
  1256. error << "SIGBUS";
  1257. break;
  1258. #endif
  1259. #ifdef SIGSEGV
  1260. case SIGSEGV:
  1261. error << "SIGSEGV";
  1262. break;
  1263. #endif
  1264. default:
  1265. error << "signal " << retVal;
  1266. break;
  1267. }
  1268. output += error.str();
  1269. }
  1270. return false;
  1271. }
  1272. #endif // endif WIN32 not CYGWIN
  1273. // run a command unix uses popen (easy)
  1274. // windows uses system and ShortPath
  1275. bool cmSystemTools::RunCommand(const char* command,
  1276. std::string& output,
  1277. int &retVal,
  1278. const char* dir,
  1279. bool verbose,
  1280. int timeout)
  1281. {
  1282. if(s_DisableRunCommandOutput)
  1283. {
  1284. verbose = false;
  1285. }
  1286. #if defined(WIN32) && !defined(__CYGWIN__)
  1287. // if the command does not start with a quote, then
  1288. // try to find the program, and if the program can not be
  1289. // found use system to run the command as it must be a built in
  1290. // shell command like echo or dir
  1291. int count = 0;
  1292. if(command[0] == '\"')
  1293. {
  1294. // count the number of quotes
  1295. for(const char* s = command; *s != 0; ++s)
  1296. {
  1297. if(*s == '\"')
  1298. {
  1299. count++;
  1300. if(count > 2)
  1301. {
  1302. break;
  1303. }
  1304. }
  1305. }
  1306. // if there are more than two double quotes use
  1307. // GetShortPathName, the cmd.exe program in windows which
  1308. // is used by system fails to execute if there are more than
  1309. // one set of quotes in the arguments
  1310. if(count > 2)
  1311. {
  1312. cmRegularExpression quoted("^\"([^\"]*)\"[ \t](.*)");
  1313. if(quoted.find(command))
  1314. {
  1315. std::string shortCmd;
  1316. std::string cmd = quoted.match(1);
  1317. std::string args = quoted.match(2);
  1318. if(! cmSystemTools::FileExists(cmd.c_str()) )
  1319. {
  1320. shortCmd = cmd;
  1321. }
  1322. else if(!cmSystemTools::GetShortPath(cmd.c_str(), shortCmd))
  1323. {
  1324. cmSystemTools::Error("GetShortPath failed for " , cmd.c_str());
  1325. return false;
  1326. }
  1327. shortCmd += " ";
  1328. shortCmd += args;
  1329. //return RunCommandViaSystem(shortCmd.c_str(), dir,
  1330. // output, retVal, verbose);
  1331. //return WindowsRunCommand(shortCmd.c_str(), dir,
  1332. //output, retVal, verbose);
  1333. return RunCommandViaWin32(shortCmd.c_str(), dir,
  1334. output, retVal, verbose, timeout);
  1335. }
  1336. else
  1337. {
  1338. cmSystemTools::Error("Could not parse command line with quotes ",
  1339. command);
  1340. }
  1341. }
  1342. }
  1343. // if there is only one set of quotes or no quotes then just run the command
  1344. //return RunCommandViaSystem(command, dir, output, retVal, verbose);
  1345. //return WindowsRunCommand(command, dir, output, retVal, verbose);
  1346. return ::RunCommandViaWin32(command, dir, output, retVal, verbose, timeout);
  1347. #else
  1348. return ::RunCommandViaPopen(command, dir, output, retVal, verbose, timeout);
  1349. #endif
  1350. }
  1351. /**
  1352. * Find the file the given name. Searches the given path and then
  1353. * the system search path. Returns the full path to the file if it is
  1354. * found. Otherwise, the empty string is returned.
  1355. */
  1356. std::string cmSystemTools::FindFile(const char* name,
  1357. const std::vector<std::string>& userPaths)
  1358. {
  1359. // Add the system search path to our path.
  1360. std::vector<std::string> path = userPaths;
  1361. cmSystemTools::GetPath(path);
  1362. std::string tryPath;
  1363. for(std::vector<std::string>::const_iterator p = path.begin();
  1364. p != path.end(); ++p)
  1365. {
  1366. tryPath = *p;
  1367. tryPath += "/";
  1368. tryPath += name;
  1369. if(cmSystemTools::FileExists(tryPath.c_str()) &&
  1370. !cmSystemTools::FileIsDirectory(tryPath.c_str()))
  1371. {
  1372. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1373. }
  1374. }
  1375. // Couldn't find the file.
  1376. return "";
  1377. }
  1378. /**
  1379. * Find the executable with the given name. Searches the given path and then
  1380. * the system search path. Returns the full path to the executable if it is
  1381. * found. Otherwise, the empty string is returned.
  1382. */
  1383. std::string cmSystemTools::FindProgram(const char* name,
  1384. const std::vector<std::string>& userPaths,
  1385. bool no_system_path)
  1386. {
  1387. // See if the executable exists as written.
  1388. if(cmSystemTools::FileExists(name) &&
  1389. !cmSystemTools::FileIsDirectory(name))
  1390. {
  1391. return cmSystemTools::CollapseFullPath(name);
  1392. }
  1393. std::string tryPath = name;
  1394. tryPath += cmSystemTools::GetExecutableExtension();
  1395. if(cmSystemTools::FileExists(tryPath.c_str()) &&
  1396. !cmSystemTools::FileIsDirectory(tryPath.c_str()))
  1397. {
  1398. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1399. }
  1400. // Add the system search path to our path.
  1401. std::vector<std::string> path = userPaths;
  1402. if (!no_system_path)
  1403. {
  1404. cmSystemTools::GetPath(path);
  1405. }
  1406. for(std::vector<std::string>::const_iterator p = path.begin();
  1407. p != path.end(); ++p)
  1408. {
  1409. tryPath = *p;
  1410. tryPath += "/";
  1411. tryPath += name;
  1412. if(cmSystemTools::FileExists(tryPath.c_str()) &&
  1413. !cmSystemTools::FileIsDirectory(tryPath.c_str()))
  1414. {
  1415. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1416. }
  1417. #ifdef _WIN32
  1418. tryPath += ".com";
  1419. if(cmSystemTools::FileExists(tryPath.c_str()) &&
  1420. !cmSystemTools::FileIsDirectory(tryPath.c_str()))
  1421. {
  1422. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1423. }
  1424. tryPath = *p;
  1425. tryPath += "/";
  1426. tryPath += name;
  1427. #endif
  1428. tryPath += cmSystemTools::GetExecutableExtension();
  1429. if(cmSystemTools::FileExists(tryPath.c_str()) &&
  1430. !cmSystemTools::FileIsDirectory(tryPath.c_str()))
  1431. {
  1432. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1433. }
  1434. }
  1435. // Couldn't find the program.
  1436. return "";
  1437. }
  1438. /**
  1439. * Find the library with the given name. Searches the given path and then
  1440. * the system search path. Returns the full path to the library if it is
  1441. * found. Otherwise, the empty string is returned.
  1442. */
  1443. std::string cmSystemTools::FindLibrary(const char* name,
  1444. const std::vector<std::string>& userPaths)
  1445. {
  1446. // See if the executable exists as written.
  1447. if(cmSystemTools::FileExists(name))
  1448. {
  1449. return cmSystemTools::CollapseFullPath(name);
  1450. }
  1451. // Add the system search path to our path.
  1452. std::vector<std::string> path = userPaths;
  1453. cmSystemTools::GetPath(path);
  1454. std::string tryPath;
  1455. for(std::vector<std::string>::const_iterator p = path.begin();
  1456. p != path.end(); ++p)
  1457. {
  1458. #if defined(_WIN32) && !defined(__CYGWIN__)
  1459. tryPath = *p;
  1460. tryPath += "/";
  1461. tryPath += name;
  1462. tryPath += ".lib";
  1463. if(cmSystemTools::FileExists(tryPath.c_str()))
  1464. {
  1465. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1466. }
  1467. #else
  1468. tryPath = *p;
  1469. tryPath += "/lib";
  1470. tryPath += name;
  1471. tryPath += ".so";
  1472. if(cmSystemTools::FileExists(tryPath.c_str()))
  1473. {
  1474. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1475. }
  1476. tryPath = *p;
  1477. tryPath += "/lib";
  1478. tryPath += name;
  1479. tryPath += ".a";
  1480. if(cmSystemTools::FileExists(tryPath.c_str()))
  1481. {
  1482. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1483. }
  1484. tryPath = *p;
  1485. tryPath += "/lib";
  1486. tryPath += name;
  1487. tryPath += ".sl";
  1488. if(cmSystemTools::FileExists(tryPath.c_str()))
  1489. {
  1490. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1491. }
  1492. tryPath = *p;
  1493. tryPath += "/lib";
  1494. tryPath += name;
  1495. tryPath += ".dylib";
  1496. if(cmSystemTools::FileExists(tryPath.c_str()))
  1497. {
  1498. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  1499. }
  1500. #endif
  1501. }
  1502. // Couldn't find the library.
  1503. return "";
  1504. }
  1505. bool cmSystemTools::FileIsDirectory(const char* name)
  1506. {
  1507. struct stat fs;
  1508. if(stat(name, &fs) == 0)
  1509. {
  1510. #if _WIN32
  1511. return ((fs.st_mode & _S_IFDIR) != 0);
  1512. #else
  1513. return S_ISDIR(fs.st_mode);
  1514. #endif
  1515. }
  1516. else
  1517. {
  1518. return false;
  1519. }
  1520. }
  1521. int cmSystemTools::ChangeDirectory(const char *dir)
  1522. {
  1523. return Chdir(dir);
  1524. }
  1525. std::string cmSystemTools::GetCurrentWorkingDirectory()
  1526. {
  1527. char buf[2048];
  1528. std::string path = Getcwd(buf, 2048);
  1529. return path;
  1530. }
  1531. /**
  1532. * Given the path to a program executable, get the directory part of the path with the
  1533. * file stripped off. If there is no directory part, the empty string is returned.
  1534. */
  1535. std::string cmSystemTools::GetProgramPath(const char* in_name)
  1536. {
  1537. std::string dir, file;
  1538. cmSystemTools::SplitProgramPath(in_name, dir, file);
  1539. return dir;
  1540. }
  1541. /**
  1542. * Given the path to a program executable, get the directory part of the path
  1543. * with the file stripped off. If there is no directory part, the empty
  1544. * string is returned.
  1545. */
  1546. void cmSystemTools::SplitProgramPath(const char* in_name,
  1547. std::string& dir,
  1548. std::string& file)
  1549. {
  1550. dir = in_name;
  1551. file = "";
  1552. cmSystemTools::ConvertToUnixSlashes(dir);
  1553. if(!cmSystemTools::FileIsDirectory(dir.c_str()))
  1554. {
  1555. std::string::size_type slashPos = dir.rfind("/");
  1556. if(slashPos != std::string::npos)
  1557. {
  1558. file = dir.substr(slashPos+1);
  1559. dir = dir.substr(0, slashPos);
  1560. }
  1561. else
  1562. {
  1563. file = dir;
  1564. dir = "";
  1565. }
  1566. }
  1567. if((dir != "") && !cmSystemTools::FileIsDirectory(dir.c_str()))
  1568. {
  1569. std::string oldDir = in_name;
  1570. cmSystemTools::ConvertToUnixSlashes(oldDir);
  1571. cmSystemTools::Error("Error splitting file name off end of path:\n",
  1572. oldDir.c_str(), "\nDirectory not found: ",
  1573. dir.c_str());
  1574. dir = in_name;
  1575. return;
  1576. }
  1577. }
  1578. /**
  1579. * Given a path to a file or directory, convert it to a full path.
  1580. * This collapses away relative paths. The full path is returned.
  1581. */
  1582. std::string cmSystemTools::CollapseFullPath(const char* in_name)
  1583. {
  1584. std::string dir, file;
  1585. cmSystemTools::SplitProgramPath(in_name, dir, file);
  1586. #ifdef _WIN32
  1587. // Ultra-hack warning:
  1588. // This changes to the target directory, saves the working directory,
  1589. // and then changes back to the original working directory.
  1590. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  1591. if(dir != "") { Chdir(dir.c_str()); }
  1592. std::string newDir = cmSystemTools::GetCurrentWorkingDirectory();
  1593. Chdir(cwd.c_str());
  1594. cmSystemTools::ConvertToUnixSlashes(newDir);
  1595. std::string newPath = newDir+"/"+file;
  1596. return newPath;
  1597. #else
  1598. # ifdef MAXPATHLEN
  1599. char resolved_name[MAXPATHLEN];
  1600. # else
  1601. # ifdef PATH_MAX
  1602. char resolved_name[PATH_MAX];
  1603. # else
  1604. char resolved_name[5024];
  1605. # endif
  1606. # endif
  1607. if(dir != "")
  1608. {
  1609. realpath(dir.c_str(), resolved_name);
  1610. dir = resolved_name;
  1611. }
  1612. else
  1613. {
  1614. dir = cmSystemTools::GetCurrentWorkingDirectory();
  1615. }
  1616. if(file == "")
  1617. {
  1618. return dir;
  1619. }
  1620. return dir + "/" + file;
  1621. #endif
  1622. }
  1623. bool cmSystemTools::Split(const char* str, std::vector<cmStdString>& lines)
  1624. {
  1625. std::string data(str);
  1626. std::string::size_type lpos = 0;
  1627. while(lpos < data.length())
  1628. {
  1629. std::string::size_type rpos = data.find_first_of("\n", lpos);
  1630. if(rpos == std::string::npos)
  1631. {
  1632. // Line ends at end of string without a newline.
  1633. lines.push_back(data.substr(lpos));
  1634. return false;
  1635. }
  1636. if((rpos > lpos) && (data[rpos-1] == '\r'))
  1637. {
  1638. // Line ends in a "\r\n" pair, remove both characters.
  1639. lines.push_back(data.substr(lpos, (rpos-1)-lpos));
  1640. }
  1641. else
  1642. {
  1643. // Line ends in a "\n", remove the character.
  1644. lines.push_back(data.substr(lpos, rpos-lpos));
  1645. }
  1646. lpos = rpos+1;
  1647. }
  1648. return true;
  1649. }
  1650. /**
  1651. * Return path of a full filename (no trailing slashes).
  1652. * Warning: returned path is converted to Unix slashes format.
  1653. */
  1654. std::string cmSystemTools::GetFilenamePath(const std::string& filename)
  1655. {
  1656. std::string fn = filename;
  1657. cmSystemTools::ConvertToUnixSlashes(fn);
  1658. std::string::size_type slash_pos = fn.rfind("/");
  1659. if(slash_pos != std::string::npos)
  1660. {
  1661. return fn.substr(0, slash_pos);
  1662. }
  1663. else
  1664. {
  1665. return "";
  1666. }
  1667. }
  1668. /**
  1669. * Return file name of a full filename (i.e. file name without path).
  1670. */
  1671. std::string cmSystemTools::GetFilenameName(const std::string& filename)
  1672. {
  1673. std::string fn = filename;
  1674. cmSystemTools::ConvertToUnixSlashes(fn);
  1675. std::string::size_type slash_pos = fn.rfind("/");
  1676. if(slash_pos != std::string::npos)
  1677. {
  1678. return fn.substr(slash_pos + 1);
  1679. }
  1680. else
  1681. {
  1682. return filename;
  1683. }
  1684. }
  1685. /**
  1686. * Return file extension of a full filename (dot included).
  1687. * Warning: this is the longest extension (for example: .tar.gz)
  1688. */
  1689. std::string cmSystemTools::GetFilenameExtension(const std::string& filename)
  1690. {
  1691. std::string name = cmSystemTools::GetFilenameName(filename);
  1692. std::string::size_type dot_pos = name.find(".");
  1693. if(dot_pos != std::string::npos)
  1694. {
  1695. return name.substr(dot_pos);
  1696. }
  1697. else
  1698. {
  1699. return "";
  1700. }
  1701. }
  1702. /**
  1703. * Return file name without extension of a full filename (i.e. without path).
  1704. * Warning: it considers the longest extension (for example: .tar.gz)
  1705. */
  1706. std::string cmSystemTools::GetFilenameWithoutExtension(const std::string& filename)
  1707. {
  1708. std::string name = cmSystemTools::GetFilenameName(filename);
  1709. std::string::size_type dot_pos = name.find(".");
  1710. if(dot_pos != std::string::npos)
  1711. {
  1712. return name.substr(0, dot_pos);
  1713. }
  1714. else
  1715. {
  1716. return name;
  1717. }
  1718. }
  1719. /**
  1720. * Return file name without extension of a full filename (i.e. without path).
  1721. * Warning: it considers the last extension (for example: removes .gz
  1722. * from .tar.gz)
  1723. */
  1724. std::string
  1725. cmSystemTools::GetFilenameWithoutLastExtension(const std::string& filename)
  1726. {
  1727. std::string name = cmSystemTools::GetFilenameName(filename);
  1728. std::string::size_type dot_pos = name.rfind(".");
  1729. if(dot_pos != std::string::npos)
  1730. {
  1731. return name.substr(0, dot_pos);
  1732. }
  1733. else
  1734. {
  1735. return name;
  1736. }
  1737. }
  1738. bool cmSystemTools::FileIsFullPath(const char* in_name)
  1739. {
  1740. std::string name = in_name;
  1741. #if defined(_WIN32)
  1742. // On Windows, the name must be at least two characters long.
  1743. if(name.length() < 2)
  1744. {
  1745. return false;
  1746. }
  1747. if(name[1] == ':')
  1748. {
  1749. return true;
  1750. }
  1751. if(name[0] == '\\')
  1752. {
  1753. return true;
  1754. }
  1755. #else
  1756. // On UNIX, the name must be at least one character long.
  1757. if(name.length() < 1)
  1758. {
  1759. return false;
  1760. }
  1761. #endif
  1762. // On UNIX, the name must begin in a '/'.
  1763. // On Windows, if the name begins in a '/', then it is a full
  1764. // network path.
  1765. if(name[0] == '/')
  1766. {
  1767. return true;
  1768. }
  1769. return false;
  1770. }
  1771. void cmSystemTools::Glob(const char *directory, const char *regexp,
  1772. std::vector<std::string>& files)
  1773. {
  1774. cmDirectory d;
  1775. cmRegularExpression reg(regexp);
  1776. if (d.Load(directory))
  1777. {
  1778. size_t numf;
  1779. unsigned int i;
  1780. numf = d.GetNumberOfFiles();
  1781. for (i = 0; i < numf; i++)
  1782. {
  1783. std::string fname = d.GetFile(i);
  1784. if (reg.find(fname))
  1785. {
  1786. files.push_back(fname);
  1787. }
  1788. }
  1789. }
  1790. }
  1791. void cmSystemTools::GlobDirs(const char *fullPath,
  1792. std::vector<std::string>& files)
  1793. {
  1794. std::string path = fullPath;
  1795. std::string::size_type pos = path.find("/*");
  1796. if(pos == std::string::npos)
  1797. {
  1798. files.push_back(fullPath);
  1799. return;
  1800. }
  1801. std::string startPath = path.substr(0, pos);
  1802. std::string finishPath = path.substr(pos+2);
  1803. cmDirectory d;
  1804. if (d.Load(startPath.c_str()))
  1805. {
  1806. for (unsigned int i = 0; i < d.GetNumberOfFiles(); ++i)
  1807. {
  1808. if((std::string(d.GetFile(i)) != ".")
  1809. && (std::string(d.GetFile(i)) != ".."))
  1810. {
  1811. std::string fname = startPath;
  1812. fname +="/";
  1813. fname += d.GetFile(i);
  1814. if(cmSystemTools::FileIsDirectory(fname.c_str()))
  1815. {
  1816. fname += finishPath;
  1817. cmSystemTools::GlobDirs(fname.c_str(), files);
  1818. }
  1819. }
  1820. }
  1821. }
  1822. }
  1823. void cmSystemTools::ExpandList(std::vector<std::string> const& arguments,
  1824. std::vector<std::string>& newargs)
  1825. {
  1826. std::vector<std::string>::const_iterator i;
  1827. for(i = arguments.begin();i != arguments.end(); ++i)
  1828. {
  1829. cmSystemTools::ExpandListArgument(*i, newargs);
  1830. }
  1831. }
  1832. void cmSystemTools::ExpandListArgument(const std::string& arg,
  1833. std::vector<std::string>& newargs)
  1834. {
  1835. std::string newarg;
  1836. // If argument is empty, it is an empty list.
  1837. if(arg.length() == 0)
  1838. {
  1839. return;
  1840. }
  1841. // if there are no ; in the name then just copy the current string
  1842. if(arg.find(';') == std::string::npos)
  1843. {
  1844. newargs.push_back(arg);
  1845. }
  1846. else
  1847. {
  1848. std::string::size_type start = 0;
  1849. std::string::size_type endpos = 0;
  1850. const std::string::size_type size = arg.size();
  1851. // break up ; separated sections of the string into separate strings
  1852. while(endpos != size)
  1853. {
  1854. endpos = arg.find(';', start);
  1855. if(endpos == std::string::npos)
  1856. {
  1857. endpos = arg.size();
  1858. }
  1859. else
  1860. {
  1861. // skip right over escaped ; ( \; )
  1862. while((endpos != std::string::npos)
  1863. && (endpos > 0)
  1864. && ((arg)[endpos-1] == '\\') )
  1865. {
  1866. endpos = arg.find(';', endpos+1);
  1867. }
  1868. if(endpos == std::string::npos)
  1869. {
  1870. endpos = arg.size();
  1871. }
  1872. }
  1873. std::string::size_type len = endpos - start;
  1874. if (len > 0)
  1875. {
  1876. // check for a closing ] after the start position
  1877. if(arg.find('[', start) == std::string::npos)
  1878. {
  1879. // if there is no [ in the string then keep it
  1880. newarg = arg.substr(start, len);
  1881. }
  1882. else
  1883. {
  1884. int opencount = 0;
  1885. int closecount = 0;
  1886. for(std::string::size_type j = start; j < endpos; ++j)
  1887. {
  1888. if(arg.at(j) == '[')
  1889. {
  1890. ++opencount;
  1891. }
  1892. else if (arg.at(j) == ']')
  1893. {
  1894. ++closecount;
  1895. }
  1896. }
  1897. if(opencount != closecount)
  1898. {
  1899. // skip this one
  1900. endpos = arg.find(';', endpos+1);
  1901. if(endpos == std::string::npos)
  1902. {
  1903. endpos = arg.size();
  1904. }
  1905. len = endpos - start;
  1906. }
  1907. newarg = arg.substr(start, len);
  1908. }
  1909. std::string::size_type pos = newarg.find("\\;");
  1910. if(pos != std::string::npos)
  1911. {
  1912. newarg.erase(pos, 1);
  1913. }
  1914. newargs.push_back(newarg);
  1915. }
  1916. start = endpos+1;
  1917. }
  1918. }
  1919. }
  1920. bool cmSystemTools::GetShortPath(const char* path, std::string& shortPath)
  1921. {
  1922. #if defined(WIN32) && !defined(__CYGWIN__)
  1923. const int size = int(strlen(path)) +1; // size of return
  1924. char *buffer = new char[size]; // create a buffer
  1925. char *tempPath = new char[size]; // create a buffer
  1926. int ret;
  1927. // if the path passed in has quotes around it, first remove the quotes
  1928. if (path[0] == '"' && path[strlen(path)-1] == '"')
  1929. {
  1930. strcpy(tempPath,path+1);
  1931. tempPath[strlen(tempPath)-1] = '\0';
  1932. }
  1933. else
  1934. {
  1935. strcpy(tempPath,path);
  1936. }
  1937. buffer[0] = 0;
  1938. ret = GetShortPathName(tempPath, buffer, size);
  1939. if(buffer[0] == 0 || ret > size)
  1940. {
  1941. if(ret < size)
  1942. {
  1943. LPVOID lpMsgBuf;
  1944. FormatMessage(
  1945. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  1946. FORMAT_MESSAGE_FROM_SYSTEM |
  1947. FORMAT_MESSAGE_IGNORE_INSERTS,
  1948. NULL,
  1949. GetLastError(),
  1950. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  1951. (LPTSTR) &lpMsgBuf,
  1952. 0,
  1953. NULL
  1954. );
  1955. cmSystemTools::Error((LPCTSTR)lpMsgBuf);
  1956. LocalFree( lpMsgBuf );
  1957. }
  1958. cmSystemTools::Error("Unable to get a short path: ", path);
  1959. delete [] tempPath;
  1960. return false;
  1961. }
  1962. else
  1963. {
  1964. shortPath = buffer;
  1965. delete [] buffer;
  1966. delete [] tempPath;
  1967. return true;
  1968. }
  1969. #else
  1970. shortPath = path;
  1971. return true;
  1972. #endif
  1973. }
  1974. bool cmSystemTools::SimpleGlob(const std::string& glob,
  1975. std::vector<std::string>& files,
  1976. int type /* = 0 */)
  1977. {
  1978. files.clear();
  1979. if ( glob[glob.size()-1] != '*' )
  1980. {
  1981. return false;
  1982. }
  1983. std::string path = cmSystemTools::GetFilenamePath(glob);
  1984. std::string ppath = cmSystemTools::GetFilenameName(glob);
  1985. ppath = ppath.substr(0, ppath.size()-1);
  1986. if ( path.size() == 0 )
  1987. {
  1988. path = "/";
  1989. }
  1990. bool res = false;
  1991. cmDirectory d;
  1992. if (d.Load(path.c_str()))
  1993. {
  1994. for (unsigned int i = 0; i < d.GetNumberOfFiles(); ++i)
  1995. {
  1996. if((std::string(d.GetFile(i)) != ".")
  1997. && (std::string(d.GetFile(i)) != ".."))
  1998. {
  1999. std::string fname = path;
  2000. if ( path[path.size()-1] != '/' )
  2001. {
  2002. fname +="/";
  2003. }
  2004. fname += d.GetFile(i);
  2005. std::string sfname = d.GetFile(i);
  2006. if ( type > 0 && cmSystemTools::FileIsDirectory(fname.c_str()) )
  2007. {
  2008. continue;
  2009. }
  2010. if ( type < 0 && !cmSystemTools::FileIsDirectory(fname.c_str()) )
  2011. {
  2012. continue;
  2013. }
  2014. if ( sfname.size() >= ppath.size() &&
  2015. sfname.substr(0, ppath.size()) ==
  2016. ppath )
  2017. {
  2018. files.push_back(fname);
  2019. res = true;
  2020. }
  2021. }
  2022. }
  2023. }
  2024. return res;
  2025. }
  2026. cmSystemTools::FileFormat cmSystemTools::GetFileFormat(const char* cext)
  2027. {
  2028. if ( ! cext || *cext == 0 )
  2029. {
  2030. return cmSystemTools::NO_FILE_FORMAT;
  2031. }
  2032. std::string ext = cmSystemTools::LowerCase(cext);
  2033. if ( ext == "c" || ext == ".c" ) { return cmSystemTools::C_FILE_FORMAT; }
  2034. if ( ext == "cxx" || ext == ".cxx" ||
  2035. ext == "cpp" || ext == ".cpp" ||
  2036. ext == "c++" || ext == ".c++" ||
  2037. ext == "mm" || ext == ".mm" ||
  2038. ext == "cc" || ext == ".cc" ) { return cmSystemTools::CXX_FILE_FORMAT; }
  2039. if ( ext == "java" || ext == ".java" ) { return cmSystemTools::JAVA_FILE_FORMAT; }
  2040. if ( ext == "h" || ext == ".h" ||
  2041. ext == "hpp" || ext == ".hpp" ||
  2042. ext == "h++" || ext == ".h++" ||
  2043. ext == "hxx" || ext == ".hxx" ) { return cmSystemTools::HEADER_FILE_FORMAT; }
  2044. if ( ext == "rc" || ext == ".rc" ) { return cmSystemTools::RESOURCE_FILE_FORMAT; }
  2045. if ( ext == "def" || ext == ".def" ) { return cmSystemTools::DEFINITION_FILE_FORMAT; }
  2046. if ( ext == "lib" || ext == ".lib" ||
  2047. ext == "a" || ext == ".a") { return cmSystemTools::STATIC_LIBRARY_FILE_FORMAT; }
  2048. if ( ext == "o" || ext == ".o" ||
  2049. ext == "obj" || ext == ".obj") { return cmSystemTools::OBJECT_FILE_FORMAT; }
  2050. #ifdef __APPLE__
  2051. if ( ext == "dylib" || ext == ".dylib" )
  2052. { return cmSystemTools::SHARED_LIBRARY_FILE_FORMAT; }
  2053. if ( ext == "so" || ext == ".so" ||
  2054. ext == "bundle" || ext == ".bundle" )
  2055. { return cmSystemTools::MODULE_FILE_FORMAT; }
  2056. #else // __APPLE__
  2057. if ( ext == "so" || ext == ".so" ||
  2058. ext == "sl" || ext == ".sl" ||
  2059. ext == "dll" || ext == ".dll" )
  2060. { return cmSystemTools::SHARED_LIBRARY_FILE_FORMAT; }
  2061. #endif // __APPLE__
  2062. return cmSystemTools::UNKNOWN_FILE_FORMAT;
  2063. }
  2064. void cmSystemTools::SplitProgramFromArgs(const char* path,
  2065. std::string& program, std::string& args)
  2066. {
  2067. if(cmSystemTools::FileExists(path))
  2068. {
  2069. program = path;
  2070. args = "";
  2071. return;
  2072. }
  2073. std::vector<std::string> e;
  2074. std::string findProg = cmSystemTools::FindProgram(path, e);
  2075. if(findProg.size())
  2076. {
  2077. program = findProg;
  2078. args = "";
  2079. return;
  2080. }
  2081. std::string dir = path;
  2082. std::string::size_type spacePos = dir.rfind(' ');
  2083. if(spacePos == std::string::npos)
  2084. {
  2085. program = "";
  2086. args = "";
  2087. return;
  2088. }
  2089. while(spacePos != std::string::npos)
  2090. {
  2091. std::string tryProg = dir.substr(0, spacePos);
  2092. if(cmSystemTools::FileExists(tryProg.c_str()))
  2093. {
  2094. program = tryProg;
  2095. args = dir.substr(spacePos, dir.size()-spacePos);
  2096. return;
  2097. }
  2098. findProg = cmSystemTools::FindProgram(tryProg.c_str(), e);
  2099. if(findProg.size())
  2100. {
  2101. program = findProg;
  2102. args = dir.substr(spacePos, dir.size()-spacePos);
  2103. return;
  2104. }
  2105. spacePos = dir.rfind(' ', spacePos--);
  2106. }
  2107. program = "";
  2108. args = "";
  2109. }
  2110. #if defined(_MSC_VER) && defined(_DEBUG)
  2111. # include <crtdbg.h>
  2112. # include <stdio.h>
  2113. # include <stdlib.h>
  2114. static int cmSystemToolsDebugReport(int, char* message, int*)
  2115. {
  2116. fprintf(stderr, message);
  2117. exit(1);
  2118. return 0;
  2119. }
  2120. void cmSystemTools::EnableMSVCDebugHook()
  2121. {
  2122. if(getenv("DART_TEST_FROM_DART"))
  2123. {
  2124. _CrtSetReportHook(cmSystemToolsDebugReport);
  2125. }
  2126. }
  2127. #else
  2128. void cmSystemTools::EnableMSVCDebugHook()
  2129. {
  2130. }
  2131. #endif