cmSystemTools.cxx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2001 Insight Consortium
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright notice,
  12. this list of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16. * The name of the Insight Consortium, nor the names of any consortium members,
  17. nor of any contributors, may be used to endorse or promote products derived
  18. from this software without specific prior written permission.
  19. * Modified source versions must be plainly marked as such, and must not be
  20. misrepresented as being the original software.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS''
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
  25. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. =========================================================================*/
  32. #include "cmSystemTools.h"
  33. #include "errno.h"
  34. #include "stdio.h"
  35. #include <sys/stat.h>
  36. #include "cmRegularExpression.h"
  37. #include <ctype.h>
  38. #if defined(_MSC_VER) || defined(__BORLANDC__)
  39. #include <windows.h>
  40. #include <direct.h>
  41. #define _unlink unlink
  42. inline int Mkdir(const char* dir)
  43. {
  44. return _mkdir(dir);
  45. }
  46. inline const char* Getcwd(char* buf, unsigned int len)
  47. {
  48. return _getcwd(buf, len);
  49. }
  50. inline int Chdir(const char* dir)
  51. {
  52. return _chdir(dir);
  53. }
  54. #else
  55. #include <sys/types.h>
  56. #include <fcntl.h>
  57. #include <unistd.h>
  58. inline int Mkdir(const char* dir)
  59. {
  60. return mkdir(dir, 00777);
  61. }
  62. inline const char* Getcwd(char* buf, unsigned int len)
  63. {
  64. return getcwd(buf, len);
  65. }
  66. inline int Chdir(const char* dir)
  67. {
  68. return chdir(dir);
  69. }
  70. #endif
  71. bool cmSystemTools::s_ErrorOccured = false;
  72. // adds the elements of the env variable path to the arg passed in
  73. void cmSystemTools::GetPath(std::vector<std::string>& path)
  74. {
  75. #if defined(_WIN32) && !defined(__CYGWIN__)
  76. const char* pathSep = ";";
  77. #else
  78. const char* pathSep = ":";
  79. #endif
  80. std::string pathEnv = getenv("PATH");
  81. std::string::size_type start =0;
  82. bool done = false;
  83. while(!done)
  84. {
  85. std::string::size_type endpos = pathEnv.find(pathSep, start);
  86. if(endpos != std::string::npos)
  87. {
  88. path.push_back(pathEnv.substr(start, endpos-start));
  89. start = endpos+1;
  90. }
  91. else
  92. {
  93. done = true;
  94. }
  95. }
  96. for(std::vector<std::string>::iterator i = path.begin();
  97. i != path.end(); ++i)
  98. {
  99. cmSystemTools::ConvertToUnixSlashes(*i);
  100. }
  101. }
  102. const char* cmSystemTools::GetExecutableExtension()
  103. {
  104. #if defined(_WIN32)
  105. return ".exe";
  106. #else
  107. return "";
  108. #endif
  109. }
  110. bool cmSystemTools::MakeDirectory(const char* path)
  111. {
  112. std::string dir = path;
  113. // replace all of the \ with /
  114. size_t pos = 0;
  115. while((pos = dir.find('\\', pos)) != std::string::npos)
  116. {
  117. dir[pos] = '/';
  118. pos++;
  119. }
  120. pos = dir.find(':');
  121. if(pos == std::string::npos)
  122. {
  123. pos = 0;
  124. }
  125. while((pos = dir.find('/', pos)) != std::string::npos)
  126. {
  127. std::string topdir = dir.substr(0, pos);
  128. Mkdir(topdir.c_str());
  129. pos++;
  130. }
  131. if(Mkdir(path) != 0)
  132. {
  133. // if it is some other error besides directory exists
  134. // then return false
  135. if(errno != EEXIST)
  136. {
  137. return false;
  138. }
  139. }
  140. return true;
  141. }
  142. // replace replace with with as many times as it shows up in source.
  143. // write the result into source.
  144. void cmSystemTools::ReplaceString(std::string& source,
  145. const char* replace,
  146. const char* with)
  147. {
  148. int lengthReplace = strlen(replace);
  149. std::string rest;
  150. size_t start = source.find(replace);
  151. while(start != std::string::npos)
  152. {
  153. rest = source.substr(start+lengthReplace);
  154. source = source.substr(0, start);
  155. source += with;
  156. source += rest;
  157. start = source.find(replace, start + lengthReplace );
  158. }
  159. }
  160. #ifdef _WIN32
  161. // Get the data of key value.
  162. // Example :
  163. // HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.1\InstallPath
  164. // => will return the data of the "default" value of the key
  165. // HKEY_LOCAL_MACHINE\SOFTWARE\Scriptics\Tcl\8.4:Root
  166. // => will return the data of the "Root" value of the key
  167. bool ReadAValue(std::string &res, const char *key)
  168. {
  169. // find the primary key
  170. std::string primary = key;
  171. std::string second;
  172. std::string valuename;
  173. size_t start = primary.find("\\");
  174. if (start == std::string::npos)
  175. {
  176. return false;
  177. }
  178. size_t valuenamepos = primary.find("§");
  179. if (valuenamepos != std::string::npos)
  180. {
  181. valuename = primary.substr(valuenamepos+1);
  182. }
  183. second = primary.substr(start+1, valuenamepos-start-1);
  184. primary = primary.substr(0, start);
  185. HKEY primaryKey;
  186. if (primary == "HKEY_CURRENT_USER")
  187. {
  188. primaryKey = HKEY_CURRENT_USER;
  189. }
  190. if (primary == "HKEY_CURRENT_CONFIG")
  191. {
  192. primaryKey = HKEY_CURRENT_CONFIG;
  193. }
  194. if (primary == "HKEY_CLASSES_ROOT")
  195. {
  196. primaryKey = HKEY_CLASSES_ROOT;
  197. }
  198. if (primary == "HKEY_LOCAL_MACHINE")
  199. {
  200. primaryKey = HKEY_LOCAL_MACHINE;
  201. }
  202. if (primary == "HKEY_USERS")
  203. {
  204. primaryKey = HKEY_USERS;
  205. }
  206. HKEY hKey;
  207. if(RegOpenKeyEx(primaryKey, second.c_str(),
  208. 0, KEY_READ, &hKey) != ERROR_SUCCESS)
  209. {
  210. return false;
  211. }
  212. else
  213. {
  214. DWORD dwType, dwSize;
  215. dwSize = 1023;
  216. char data[1024];
  217. if(RegQueryValueEx(hKey, (LPTSTR)valuename.c_str(), NULL, &dwType,
  218. (BYTE *)data, &dwSize) == ERROR_SUCCESS)
  219. {
  220. if (dwType == REG_SZ)
  221. {
  222. res = data;
  223. return true;
  224. }
  225. }
  226. }
  227. return false;
  228. }
  229. #endif
  230. // replace replace with with as many times as it shows up in source.
  231. // write the result into source.
  232. void cmSystemTools::ExpandRegistryValues(std::string& source)
  233. {
  234. #if _WIN32
  235. cmRegularExpression regEntry("\\[(HKEY[A-Za-z_ §0-9\\.\\\\]*)\\]");
  236. // check for black line or comment
  237. while (regEntry.find(source))
  238. {
  239. // the arguments are the second match
  240. std::string key = regEntry.match(1);
  241. std::string val;
  242. if (ReadAValue(val,key.c_str()))
  243. {
  244. std::string reg = "[";
  245. reg += key + "]";
  246. cmSystemTools::ReplaceString(source, reg.c_str(), val.c_str());
  247. }
  248. else
  249. {
  250. std::string reg = "[";
  251. reg += key + "]";
  252. cmSystemTools::ReplaceString(source, reg.c_str(), "/registry");
  253. }
  254. }
  255. #endif
  256. }
  257. std::string cmSystemTools::EscapeSpaces(const char* str)
  258. {
  259. #if defined(_WIN32) && !defined(__CYGWIN__)
  260. std::string result = str;
  261. return "\""+result+"\"";
  262. #else
  263. std::string result = "";
  264. for(const char* ch = str; *ch != '\0'; ++ch)
  265. {
  266. if(*ch == ' ')
  267. {
  268. result += '\\';
  269. }
  270. result += *ch;
  271. }
  272. return result;
  273. #endif
  274. }
  275. // return true if the file exists
  276. bool cmSystemTools::FileExists(const char* filename)
  277. {
  278. struct stat fs;
  279. if (stat(filename, &fs) != 0)
  280. {
  281. return false;
  282. }
  283. else
  284. {
  285. return true;
  286. }
  287. }
  288. // convert windows slashes to unix slashes \ with /
  289. void cmSystemTools::ConvertToUnixSlashes(std::string& path)
  290. {
  291. std::string::size_type pos = path.find('\\');
  292. while(pos != std::string::npos)
  293. {
  294. path[pos] = '/';
  295. pos = path.find('\\');
  296. }
  297. // remove any trailing slash
  298. if(path[path.size()-1] == '/')
  299. {
  300. path = path.substr(0, path.size()-1);
  301. }
  302. }
  303. int cmSystemTools::Grep(const char* dir, const char* file,
  304. const char* expression)
  305. {
  306. std::string path = dir;
  307. path += "/";
  308. path += file;
  309. std::ifstream fin(path.c_str());
  310. char buffer[2056];
  311. int count = 0;
  312. cmRegularExpression reg(expression);
  313. while(fin)
  314. {
  315. fin.getline(buffer, sizeof(buffer));
  316. count += reg.find(buffer);
  317. }
  318. return count;
  319. }
  320. void cmSystemTools::ConvertCygwinPath(std::string& pathname)
  321. {
  322. if(pathname.find("/cygdrive/") != std::string::npos)
  323. {
  324. std::string cygStuff = pathname.substr(0, 11);
  325. std::string replace;
  326. replace += cygStuff.at(10);
  327. replace += ":";
  328. cmSystemTools::ReplaceString(pathname, cygStuff.c_str(), replace.c_str());
  329. }
  330. }
  331. bool cmSystemTools::ParseFunction(std::ifstream& fin,
  332. std::string& name,
  333. std::vector<std::string>& arguments)
  334. {
  335. name = "";
  336. arguments = std::vector<std::string>();
  337. const int BUFFER_SIZE = 4096;
  338. char inbuffer[BUFFER_SIZE];
  339. if(!fin)
  340. {
  341. return false;
  342. }
  343. if(fin.getline(inbuffer, BUFFER_SIZE ) )
  344. {
  345. cmRegularExpression blankLine("^[ \t]*$");
  346. cmRegularExpression comment("^[ \t]*#.*$");
  347. cmRegularExpression oneLiner("^[ \t]*([A-Za-z_0-9]*)[ \t]*\\((.*)\\)[ \t]*$");
  348. cmRegularExpression multiLine("^[ \t]*([A-Za-z_0-9]*)[ \t]*\\((.*)$");
  349. cmRegularExpression lastLine("^(.*)\\)[ \t]*$");
  350. // check for black line or comment
  351. if(blankLine.find(inbuffer) || comment.find(inbuffer))
  352. {
  353. return false;
  354. }
  355. // look for a oneline fun(arg arg2)
  356. else if(oneLiner.find(inbuffer))
  357. {
  358. // the arguments are the second match
  359. std::string args = oneLiner.match(2);
  360. name = oneLiner.match(1);
  361. // break up the arguments
  362. cmSystemTools::GetArguments(args, arguments);
  363. return true;
  364. }
  365. // look for a start of a multiline with no trailing ")" fun(arg arg2
  366. else if(multiLine.find(inbuffer))
  367. {
  368. name = multiLine.match(1);
  369. std::string args = multiLine.match(2);
  370. cmSystemTools::GetArguments(args, arguments);
  371. // Read lines until the closing paren is hit
  372. bool done = false;
  373. while(!done)
  374. {
  375. // read lines until the end paren is found
  376. if(fin.getline(inbuffer, BUFFER_SIZE ) )
  377. {
  378. // Check for comment lines and ignore them.
  379. if(blankLine.find(inbuffer) || comment.find(inbuffer))
  380. { continue; }
  381. // Is this the last line?
  382. if(lastLine.find(inbuffer))
  383. {
  384. done = true;
  385. std::string args = lastLine.match(1);
  386. cmSystemTools::GetArguments(args, arguments);
  387. }
  388. else
  389. {
  390. std::string line = inbuffer;
  391. cmSystemTools::GetArguments(line, arguments);
  392. }
  393. }
  394. else
  395. {
  396. cmSystemTools::Error("Parse error in read function missing end )",
  397. inbuffer);
  398. return false;
  399. }
  400. }
  401. return true;
  402. }
  403. else
  404. {
  405. cmSystemTools::Error("Parse error in read function ", inbuffer);
  406. return false;
  407. }
  408. }
  409. return false;
  410. }
  411. void cmSystemTools::GetArguments(std::string& line,
  412. std::vector<std::string>& arguments)
  413. {
  414. // Match a normal argument (not quoted, no spaces).
  415. cmRegularExpression normalArgument("[\t ]*([^\" \t]+)[\t ]*");
  416. // Match a quoted argument (surrounded by double quotes, spaces allowed).
  417. cmRegularExpression quotedArgument("[\t ]*(\"[^\"]*\")[\t ]*");
  418. bool done = false;
  419. while(!done)
  420. {
  421. std::string arg;
  422. long endpos;
  423. bool foundQuoted = quotedArgument.find(line.c_str());
  424. bool foundNormal = normalArgument.find(line.c_str());
  425. if(foundQuoted && foundNormal)
  426. {
  427. // Both matches were found. Take the earlier one.
  428. if(normalArgument.start(1) < quotedArgument.start(1))
  429. {
  430. arg = normalArgument.match(1);
  431. endpos = normalArgument.end(1);
  432. }
  433. else
  434. {
  435. arg = quotedArgument.match(1);
  436. endpos = quotedArgument.end(1);
  437. // Strip off the double quotes on the ends.
  438. arg = arg.substr(1, arg.length()-2);
  439. }
  440. }
  441. else if (foundQuoted)
  442. {
  443. arg = quotedArgument.match(1);
  444. endpos = quotedArgument.end(1);
  445. // Strip off the double quotes on the ends.
  446. arg = arg.substr(1, arg.length()-2);
  447. }
  448. else if(foundNormal)
  449. {
  450. arg = normalArgument.match(1);
  451. endpos = normalArgument.end(1);
  452. }
  453. else
  454. {
  455. done = true;
  456. }
  457. if(!done)
  458. {
  459. arguments.push_back(arg);
  460. line = line.substr(endpos, line.length() - endpos);
  461. }
  462. }
  463. }
  464. void cmSystemTools::Error(const char* m1, const char* m2,
  465. const char* m3, const char* m4)
  466. {
  467. std::string message = "CMake Error: ";
  468. if(m1)
  469. {
  470. message += m1;
  471. }
  472. if(m2)
  473. {
  474. message += m2;
  475. }
  476. if(m3)
  477. {
  478. message += m3;
  479. }
  480. if(m4)
  481. {
  482. message += m4;
  483. }
  484. cmSystemTools::s_ErrorOccured = true;
  485. #if defined(_WIN32) && !defined(__CYGWIN__)
  486. ::MessageBox(0, message.c_str(), 0, MB_OK);
  487. std::cerr << message.c_str() << std::endl;
  488. #else
  489. std::cerr << message.c_str() << std::endl;
  490. #endif
  491. }
  492. void cmSystemTools::CopyFileIfDifferent(const char* source,
  493. const char* destination)
  494. {
  495. if(cmSystemTools::FilesDiffer(source, destination))
  496. {
  497. cmSystemTools::cmCopyFile(source, destination);
  498. }
  499. }
  500. bool cmSystemTools::FilesDiffer(const char* source,
  501. const char* destination)
  502. {
  503. struct stat statSource;
  504. if (stat(source, &statSource) != 0)
  505. {
  506. return true;
  507. }
  508. struct stat statDestination;
  509. if (stat(destination, &statDestination) != 0)
  510. {
  511. return true;
  512. }
  513. if(statSource.st_size != statDestination.st_size)
  514. {
  515. return true;
  516. }
  517. std::ifstream finSource(source);
  518. std::ifstream finDestination(destination);
  519. if(!finSource || !finDestination)
  520. {
  521. return true;
  522. }
  523. while(finSource && finDestination)
  524. {
  525. char s, d;
  526. finSource >> s;
  527. finDestination >> d;
  528. if(s != d)
  529. {
  530. return true;
  531. }
  532. }
  533. return false;
  534. }
  535. void cmSystemTools::cmCopyFile(const char* source,
  536. const char* destination)
  537. {
  538. std::ifstream fin(source);
  539. char buff[4096];
  540. std::ofstream fout(destination);
  541. if(!fout )
  542. {
  543. cmSystemTools::Error("CopyFile failed to open input file", source);
  544. }
  545. if(!fin)
  546. {
  547. cmSystemTools::Error("CopyFile failed to open output file", destination);
  548. }
  549. while(fin)
  550. {
  551. fin.getline(buff, 4096);
  552. if(fin)
  553. {
  554. fout << buff << "\n";
  555. }
  556. }
  557. }
  558. // return true if the file exists
  559. long int cmSystemTools::ModifiedTime(const char* filename)
  560. {
  561. struct stat fs;
  562. if (stat(filename, &fs) != 0)
  563. {
  564. return 0;
  565. }
  566. else
  567. {
  568. return (long int)fs.st_mtime;
  569. }
  570. }
  571. void cmSystemTools::RemoveFile(const char* source)
  572. {
  573. unlink(source);
  574. }
  575. bool cmSystemTools::IsOn(const char* val)
  576. {
  577. if (!val)
  578. {
  579. return false;
  580. }
  581. std::basic_string<char> v = val;
  582. for(std::basic_string<char>::iterator c = v.begin();
  583. c != v.end(); c++)
  584. {
  585. *c = toupper(*c);
  586. }
  587. return (v == "ON" || v == "1" || v == "YES" || v == "TRUE" || v == "Y");
  588. }
  589. bool cmSystemTools::IsOff(const char* val)
  590. {
  591. if (!val)
  592. {
  593. return true;
  594. }
  595. std::basic_string<char> v = val;
  596. for(std::basic_string<char>::iterator c = v.begin();
  597. c != v.end(); c++)
  598. {
  599. *c = toupper(*c);
  600. }
  601. return (v == "OFF" || v == "0" || v == "NO" || v == "FALSE" ||
  602. v == "N" || v == "NOTFOUND");
  603. }
  604. bool cmSystemTools::RunCommand(const char* command,
  605. std::string& output)
  606. {
  607. const int BUFFER_SIZE = 4096;
  608. char buffer[BUFFER_SIZE];
  609. #if defined(WIN32) && !defined(__CYGWIN__)
  610. std::string commandToFile = command;
  611. commandToFile += " > ";
  612. std::string tempFile;
  613. tempFile += cmSystemTools::TemporaryFileName();
  614. commandToFile += tempFile;
  615. system(commandToFile.c_str());
  616. std::ifstream fin(tempFile.c_str());
  617. if(!fin)
  618. {
  619. cmSystemTools::Error(command, " from RunCommand Faild to create output file",
  620. tempFile.c_str());
  621. return false;
  622. }
  623. while(fin)
  624. {
  625. fin.getline(buffer, BUFFER_SIZE);
  626. output += buffer;
  627. }
  628. cmSystemTools::RemoveFile(tempFile.c_str());
  629. return true;
  630. #else
  631. std::cout << "runing " << command << std::endl;
  632. FILE* cpipe = popen(command, "r");
  633. if(!cpipe)
  634. {
  635. return false;
  636. }
  637. fgets(buffer, BUFFER_SIZE, cpipe);
  638. while(!feof(cpipe))
  639. {
  640. std::cout << buffer;
  641. output += buffer;
  642. fgets(buffer, BUFFER_SIZE, cpipe);
  643. }
  644. fclose(cpipe);
  645. return true;
  646. #endif
  647. }
  648. #ifdef _MSC_VER
  649. #define tempnam _tempnam
  650. #endif
  651. std::string cmSystemTools::TemporaryFileName()
  652. {
  653. return tempnam(0, "cmake");
  654. }
  655. /**
  656. * Find the executable with the given name. Searches the given path and then
  657. * the system search path. Returns the full path to the executable if it is
  658. * found. Otherwise, the empty string is returned.
  659. */
  660. std::string cmSystemTools::FindProgram(const char* name,
  661. const std::vector<std::string>& userPaths)
  662. {
  663. // See if the executable exists as written.
  664. if(cmSystemTools::FileExists(name))
  665. {
  666. return cmSystemTools::CollapseFullPath(name);
  667. }
  668. // Add the system search path to our path.
  669. std::vector<std::string> path = userPaths;
  670. cmSystemTools::GetPath(path);
  671. for(std::vector<std::string>::const_iterator p = path.begin();
  672. p != path.end(); ++p)
  673. {
  674. std::string tryPath = *p;
  675. tryPath += "/";
  676. tryPath += name;
  677. if(cmSystemTools::FileExists(tryPath.c_str()))
  678. {
  679. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  680. }
  681. tryPath += cmSystemTools::GetExecutableExtension();
  682. if(cmSystemTools::FileExists(tryPath.c_str()))
  683. {
  684. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  685. }
  686. }
  687. // Couldn't find the program.
  688. return "";
  689. }
  690. /**
  691. * Find the library with the given name. Searches the given path and then
  692. * the system search path. Returns the full path to the library if it is
  693. * found. Otherwise, the empty string is returned.
  694. */
  695. std::string cmSystemTools::FindLibrary(const char* name,
  696. const std::vector<std::string>& userPaths)
  697. {
  698. // See if the executable exists as written.
  699. if(cmSystemTools::FileExists(name))
  700. {
  701. return cmSystemTools::CollapseFullPath(name);
  702. }
  703. // Add the system search path to our path.
  704. std::vector<std::string> path = userPaths;
  705. cmSystemTools::GetPath(path);
  706. std::string tryPath;
  707. for(std::vector<std::string>::const_iterator p = path.begin();
  708. p != path.end(); ++p)
  709. {
  710. tryPath = *p;
  711. tryPath += "/";
  712. tryPath += name;
  713. tryPath += ".lib";
  714. if(cmSystemTools::FileExists(tryPath.c_str()))
  715. {
  716. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  717. }
  718. tryPath = *p;
  719. tryPath += "/lib";
  720. tryPath += name;
  721. tryPath + ".so";
  722. if(cmSystemTools::FileExists(tryPath.c_str()))
  723. {
  724. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  725. }
  726. tryPath = *p;
  727. tryPath = "/lib";
  728. tryPath += name;
  729. tryPath + ".a";
  730. if(cmSystemTools::FileExists(tryPath.c_str()))
  731. {
  732. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  733. }
  734. tryPath = *p;
  735. tryPath = "/lib";
  736. tryPath += name;
  737. tryPath + ".sl";
  738. if(cmSystemTools::FileExists(tryPath.c_str()))
  739. {
  740. return cmSystemTools::CollapseFullPath(tryPath.c_str());
  741. }
  742. }
  743. // Couldn't find the library.
  744. return "";
  745. }
  746. bool cmSystemTools::FileIsDirectory(const char* name)
  747. {
  748. struct stat fs;
  749. if(stat(name, &fs) == 0)
  750. {
  751. #if _WIN32
  752. return ((fs.st_mode & _S_IFDIR) != 0);
  753. #else
  754. return S_ISDIR(fs.st_mode);
  755. #endif
  756. }
  757. else
  758. {
  759. return false;
  760. }
  761. }
  762. std::string cmSystemTools::GetCurrentWorkingDirectory()
  763. {
  764. char buf[2048];
  765. std::string path = Getcwd(buf, 2048);
  766. return path;
  767. }
  768. /**
  769. * Given the path to a program executable, get the directory part of the path with the
  770. * file stripped off. If there is no directory part, the empty string is returned.
  771. */
  772. std::string cmSystemTools::GetProgramPath(const char* in_name)
  773. {
  774. std::string dir, file;
  775. cmSystemTools::SplitProgramPath(in_name, dir, file);
  776. return dir;
  777. }
  778. /**
  779. * Given the path to a program executable, get the directory part of the path with the
  780. * file stripped off. If there is no directory part, the empty string is returned.
  781. */
  782. void cmSystemTools::SplitProgramPath(const char* in_name,
  783. std::string& dir,
  784. std::string& file)
  785. {
  786. dir = in_name;
  787. file = "";
  788. cmSystemTools::ConvertToUnixSlashes(dir);
  789. if(!cmSystemTools::FileIsDirectory(dir.c_str()))
  790. {
  791. std::string::size_type slashPos = dir.rfind("/");
  792. if(slashPos != std::string::npos)
  793. {
  794. file = dir.substr(slashPos+1) + file;
  795. dir = dir.substr(0, slashPos);
  796. }
  797. else
  798. {
  799. file = dir;
  800. dir = "";
  801. }
  802. }
  803. if((dir != "") && !cmSystemTools::FileIsDirectory(dir.c_str()))
  804. {
  805. std::string oldDir = in_name;
  806. cmSystemTools::ConvertToUnixSlashes(oldDir);
  807. cmSystemTools::Error("Error splitting file name off end of path:\n",
  808. oldDir.c_str());
  809. dir = in_name;
  810. return;
  811. }
  812. }
  813. /**
  814. * Given a path to a file or directory, convert it to a full path.
  815. * This collapses away relative paths. The full path is returned.
  816. */
  817. std::string cmSystemTools::CollapseFullPath(const char* in_name)
  818. {
  819. std::cerr << "CollapseFullPath " << in_name << "\n";
  820. std::string dir, file;
  821. cmSystemTools::SplitProgramPath(in_name, dir, file);
  822. // Ultra-hack warning:
  823. // This changes to the target directory, saves the working directory,
  824. // and then changes back to the original working directory.
  825. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  826. if(dir != "") { Chdir(dir.c_str()); }
  827. std::string newDir = cmSystemTools::GetCurrentWorkingDirectory();
  828. Chdir(cwd.c_str());
  829. cmSystemTools::ConvertToUnixSlashes(newDir);
  830. std::string newPath = newDir+"/"+file;
  831. return newPath;
  832. }