cmSystemTools.cxx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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 <stdio.h>
  15. #include <ctype.h>
  16. #include <errno.h>
  17. #include <time.h>
  18. #include <cmsys/RegularExpression.hxx>
  19. #include <cmsys/Directory.hxx>
  20. #include <cmsys/Process.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. #else
  34. #include <sys/types.h>
  35. #include <fcntl.h>
  36. #include <unistd.h>
  37. #endif
  38. bool cmSystemTools::s_RunCommandHideConsole = false;
  39. bool cmSystemTools::s_DisableRunCommandOutput = false;
  40. bool cmSystemTools::s_ErrorOccured = false;
  41. bool cmSystemTools::s_FatalErrorOccured = false;
  42. bool cmSystemTools::s_DisableMessages = false;
  43. std::string cmSystemTools::s_Windows9xComspecSubstitute = "command.com";
  44. void cmSystemTools::SetWindows9xComspecSubstitute(const char* str)
  45. {
  46. if ( str )
  47. {
  48. cmSystemTools::s_Windows9xComspecSubstitute = str;
  49. }
  50. }
  51. const char* cmSystemTools::GetWindows9xComspecSubstitute()
  52. {
  53. return cmSystemTools::s_Windows9xComspecSubstitute.c_str();
  54. }
  55. void (*cmSystemTools::s_ErrorCallback)(const char*, const char*, bool&, void*);
  56. void* cmSystemTools::s_ErrorCallbackClientData = 0;
  57. // replace replace with with as many times as it shows up in source.
  58. // write the result into source.
  59. #if defined(_WIN32) && !defined(__CYGWIN__)
  60. void cmSystemTools::ExpandRegistryValues(std::string& source)
  61. {
  62. // Regular expression to match anything inside [...] that begins in HKEY.
  63. // Note that there is a special rule for regular expressions to match a
  64. // close square-bracket inside a list delimited by square brackets.
  65. // The "[^]]" part of this expression will match any character except
  66. // a close square-bracket. The ']' character must be the first in the
  67. // list of characters inside the [^...] block of the expression.
  68. cmsys::RegularExpression regEntry("\\[(HKEY[^]]*)\\]");
  69. // check for black line or comment
  70. while (regEntry.find(source))
  71. {
  72. // the arguments are the second match
  73. std::string key = regEntry.match(1);
  74. std::string val;
  75. if (ReadRegistryValue(key.c_str(), val))
  76. {
  77. std::string reg = "[";
  78. reg += key + "]";
  79. cmSystemTools::ReplaceString(source, reg.c_str(), val.c_str());
  80. }
  81. else
  82. {
  83. std::string reg = "[";
  84. reg += key + "]";
  85. cmSystemTools::ReplaceString(source, reg.c_str(), "/registry");
  86. }
  87. }
  88. }
  89. #else
  90. void cmSystemTools::ExpandRegistryValues(std::string&)
  91. {
  92. }
  93. #endif
  94. std::string cmSystemTools::EscapeQuotes(const char* str)
  95. {
  96. std::string result = "";
  97. for(const char* ch = str; *ch != '\0'; ++ch)
  98. {
  99. if(*ch == '"')
  100. {
  101. result += '\\';
  102. }
  103. result += *ch;
  104. }
  105. return result;
  106. }
  107. std::string cmSystemTools::EscapeSpaces(const char* str)
  108. {
  109. #if defined(_WIN32) && !defined(__CYGWIN__)
  110. std::string result;
  111. // if there are spaces
  112. std::string temp = str;
  113. if (temp.find(" ") != std::string::npos &&
  114. temp.find("\"")==std::string::npos)
  115. {
  116. result = "\"";
  117. result += str;
  118. result += "\"";
  119. return result;
  120. }
  121. return str;
  122. #else
  123. std::string result = "";
  124. for(const char* ch = str; *ch != '\0'; ++ch)
  125. {
  126. if(*ch == ' ')
  127. {
  128. result += '\\';
  129. }
  130. result += *ch;
  131. }
  132. return result;
  133. #endif
  134. }
  135. std::string cmSystemTools::RemoveEscapes(const char* s)
  136. {
  137. std::string result = "";
  138. for(const char* ch = s; *ch; ++ch)
  139. {
  140. if(*ch == '\\' && *(ch+1) != ';')
  141. {
  142. ++ch;
  143. switch (*ch)
  144. {
  145. case '\\': result.insert(result.end(), '\\'); break;
  146. case '"': result.insert(result.end(), '"'); break;
  147. case ' ': result.insert(result.end(), ' '); break;
  148. case 't': result.insert(result.end(), '\t'); break;
  149. case 'n': result.insert(result.end(), '\n'); break;
  150. case 'r': result.insert(result.end(), '\r'); break;
  151. case '0': result.insert(result.end(), '\0'); break;
  152. case '\0':
  153. {
  154. cmSystemTools::Error("Trailing backslash in argument:\n", s);
  155. return result;
  156. }
  157. default:
  158. {
  159. std::string chStr(1, *ch);
  160. cmSystemTools::Error("Invalid escape sequence \\", chStr.c_str(),
  161. "\nin argument ", s);
  162. }
  163. }
  164. }
  165. else
  166. {
  167. result.insert(result.end(), *ch);
  168. }
  169. }
  170. return result;
  171. }
  172. void cmSystemTools::Error(const char* m1, const char* m2,
  173. const char* m3, const char* m4)
  174. {
  175. std::string message = "CMake Error: ";
  176. if(m1)
  177. {
  178. message += m1;
  179. }
  180. if(m2)
  181. {
  182. message += m2;
  183. }
  184. if(m3)
  185. {
  186. message += m3;
  187. }
  188. if(m4)
  189. {
  190. message += m4;
  191. }
  192. cmSystemTools::s_ErrorOccured = true;
  193. cmSystemTools::Message(message.c_str(),"Error");
  194. }
  195. void cmSystemTools::SetErrorCallback(ErrorCallback f, void* clientData)
  196. {
  197. s_ErrorCallback = f;
  198. s_ErrorCallbackClientData = clientData;
  199. }
  200. void cmSystemTools::Message(const char* m1, const char *title)
  201. {
  202. if(s_DisableMessages)
  203. {
  204. return;
  205. }
  206. if(s_ErrorCallback)
  207. {
  208. (*s_ErrorCallback)(m1, title, s_DisableMessages, s_ErrorCallbackClientData);
  209. return;
  210. }
  211. else
  212. {
  213. std::cerr << m1 << std::endl << std::flush;
  214. }
  215. }
  216. void cmSystemTools::ReportLastSystemError(const char* msg)
  217. {
  218. std::string m = msg;
  219. m += ": System Error: ";
  220. m += Superclass::GetLastSystemError();
  221. cmSystemTools::Error(m.c_str());
  222. }
  223. bool cmSystemTools::IsOn(const char* val)
  224. {
  225. if (!val)
  226. {
  227. return false;
  228. }
  229. std::basic_string<char> v = val;
  230. for(std::basic_string<char>::iterator c = v.begin();
  231. c != v.end(); c++)
  232. {
  233. *c = toupper(*c);
  234. }
  235. return (v == "ON" || v == "1" || v == "YES" || v == "TRUE" || v == "Y");
  236. }
  237. bool cmSystemTools::IsNOTFOUND(const char* val)
  238. {
  239. cmsys::RegularExpression reg("-NOTFOUND$");
  240. if(reg.find(val))
  241. {
  242. return true;
  243. }
  244. return std::string("NOTFOUND") == val;
  245. }
  246. bool cmSystemTools::IsOff(const char* val)
  247. {
  248. if (!val || strlen(val) == 0)
  249. {
  250. return true;
  251. }
  252. std::basic_string<char> v = val;
  253. for(std::basic_string<char>::iterator c = v.begin();
  254. c != v.end(); c++)
  255. {
  256. *c = toupper(*c);
  257. }
  258. return (v == "OFF" || v == "0" || v == "NO" || v == "FALSE" ||
  259. v == "N" || cmSystemTools::IsNOTFOUND(v.c_str()) || v == "IGNORE");
  260. }
  261. bool cmSystemTools::RunCommand(const char* command,
  262. std::string& output,
  263. const char* dir,
  264. bool verbose,
  265. int timeout)
  266. {
  267. int dummy;
  268. return cmSystemTools::RunCommand(command, output, dummy,
  269. dir, verbose, timeout);
  270. }
  271. bool cmSystemTools::RunCommand(const char* command,
  272. std::string& output,
  273. int &retVal,
  274. const char* dir,
  275. bool verbose,
  276. int)
  277. {
  278. if(s_DisableRunCommandOutput)
  279. {
  280. verbose = false;
  281. }
  282. std::vector<std::string> args;
  283. std::string arg;
  284. // Split the command into an argv array.
  285. for(const char* c = command; *c;)
  286. {
  287. // Skip over whitespace.
  288. while(*c == ' ' || *c == '\t')
  289. {
  290. ++c;
  291. }
  292. arg = "";
  293. if(*c == '"')
  294. {
  295. // Parse a quoted argument.
  296. ++c;
  297. while(*c && *c != '"')
  298. {
  299. if(*c == '\\')
  300. {
  301. ++c;
  302. if(*c)
  303. {
  304. arg.append(1, *c);
  305. ++c;
  306. }
  307. }
  308. else
  309. {
  310. arg.append(1, *c);
  311. ++c;
  312. }
  313. }
  314. if(*c)
  315. {
  316. ++c;
  317. }
  318. args.push_back(arg);
  319. }
  320. else if(*c)
  321. {
  322. // Parse an unquoted argument.
  323. while(*c && *c != ' ' && *c != '\t')
  324. {
  325. arg.append(1, *c);
  326. ++c;
  327. }
  328. args.push_back(arg);
  329. }
  330. }
  331. std::vector<const char*> argv;
  332. for(std::vector<std::string>::const_iterator a = args.begin();
  333. a != args.end(); ++a)
  334. {
  335. argv.push_back(a->c_str());
  336. }
  337. argv.push_back(0);
  338. if(argv.size() < 2)
  339. {
  340. return false;
  341. }
  342. // Change to specified working directory.
  343. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  344. if(dir)
  345. {
  346. if(cmSystemTools::ChangeDirectory(dir) < 0)
  347. {
  348. return false;
  349. }
  350. }
  351. output = "";
  352. cmsysProcess* cp = cmsysProcess_New();
  353. cmsysProcess_SetCommand(cp, &*argv.begin());
  354. cmsysProcess_Execute(cp);
  355. char* data;
  356. int length;
  357. while(cmsysProcess_WaitForData(cp, (cmsysProcess_Pipe_STDOUT |
  358. cmsysProcess_Pipe_STDERR),
  359. &data, &length, 0))
  360. {
  361. output.append(data, length);
  362. if(verbose)
  363. {
  364. std::cout.write(data, length);
  365. }
  366. }
  367. cmsysProcess_WaitForExit(cp, 0);
  368. bool result = true;
  369. if(cmsysProcess_GetState(cp) == cmsysProcess_State_Exited)
  370. {
  371. retVal = cmsysProcess_GetExitValue(cp);
  372. }
  373. else
  374. {
  375. result = false;
  376. }
  377. cmsysProcess_Delete(cp);
  378. // Restore old working directory.
  379. if(dir)
  380. {
  381. cmSystemTools::ChangeDirectory(cwd.c_str());
  382. }
  383. return result;
  384. }
  385. bool cmSystemTools::DoesFileExistWithExtensions(
  386. const char* name,
  387. const std::vector<std::string>& headerExts)
  388. {
  389. std::string hname;
  390. for( std::vector<std::string>::const_iterator ext = headerExts.begin();
  391. ext != headerExts.end(); ++ext )
  392. {
  393. hname = name;
  394. hname += ".";
  395. hname += *ext;
  396. if(cmSystemTools::FileExists(hname.c_str()))
  397. {
  398. return true;
  399. }
  400. }
  401. return false;
  402. }
  403. bool cmSystemTools::cmCopyFile(const char* source, const char* destination)
  404. {
  405. return Superclass::CopyFileAlways(source, destination);
  406. }
  407. void cmSystemTools::Glob(const char *directory, const char *regexp,
  408. std::vector<std::string>& files)
  409. {
  410. cmsys::Directory d;
  411. cmsys::RegularExpression reg(regexp);
  412. if (d.Load(directory))
  413. {
  414. size_t numf;
  415. unsigned int i;
  416. numf = d.GetNumberOfFiles();
  417. for (i = 0; i < numf; i++)
  418. {
  419. std::string fname = d.GetFile(i);
  420. if (reg.find(fname))
  421. {
  422. files.push_back(fname);
  423. }
  424. }
  425. }
  426. }
  427. void cmSystemTools::GlobDirs(const char *fullPath,
  428. std::vector<std::string>& files)
  429. {
  430. std::string path = fullPath;
  431. std::string::size_type pos = path.find("/*");
  432. if(pos == std::string::npos)
  433. {
  434. files.push_back(fullPath);
  435. return;
  436. }
  437. std::string startPath = path.substr(0, pos);
  438. std::string finishPath = path.substr(pos+2);
  439. cmsys::Directory d;
  440. if (d.Load(startPath.c_str()))
  441. {
  442. for (unsigned int i = 0; i < d.GetNumberOfFiles(); ++i)
  443. {
  444. if((std::string(d.GetFile(i)) != ".")
  445. && (std::string(d.GetFile(i)) != ".."))
  446. {
  447. std::string fname = startPath;
  448. fname +="/";
  449. fname += d.GetFile(i);
  450. if(cmSystemTools::FileIsDirectory(fname.c_str()))
  451. {
  452. fname += finishPath;
  453. cmSystemTools::GlobDirs(fname.c_str(), files);
  454. }
  455. }
  456. }
  457. }
  458. }
  459. void cmSystemTools::ExpandList(std::vector<std::string> const& arguments,
  460. std::vector<std::string>& newargs)
  461. {
  462. std::vector<std::string>::const_iterator i;
  463. for(i = arguments.begin();i != arguments.end(); ++i)
  464. {
  465. cmSystemTools::ExpandListArgument(*i, newargs);
  466. }
  467. }
  468. void cmSystemTools::ExpandListArgument(const std::string& arg,
  469. std::vector<std::string>& newargs)
  470. {
  471. std::string newarg;
  472. // If argument is empty, it is an empty list.
  473. if(arg.length() == 0)
  474. {
  475. return;
  476. }
  477. // if there are no ; in the name then just copy the current string
  478. if(arg.find(';') == std::string::npos)
  479. {
  480. newargs.push_back(arg);
  481. }
  482. else
  483. {
  484. std::string::size_type start = 0;
  485. std::string::size_type endpos = 0;
  486. const std::string::size_type size = arg.size();
  487. // break up ; separated sections of the string into separate strings
  488. while(endpos != size)
  489. {
  490. endpos = arg.find(';', start);
  491. if(endpos == std::string::npos)
  492. {
  493. endpos = arg.size();
  494. }
  495. else
  496. {
  497. // skip right over escaped ; ( \; )
  498. while((endpos != std::string::npos)
  499. && (endpos > 0)
  500. && ((arg)[endpos-1] == '\\') )
  501. {
  502. endpos = arg.find(';', endpos+1);
  503. }
  504. if(endpos == std::string::npos)
  505. {
  506. endpos = arg.size();
  507. }
  508. }
  509. std::string::size_type len = endpos - start;
  510. if (len > 0)
  511. {
  512. // check for a closing ] after the start position
  513. if(arg.find('[', start) == std::string::npos)
  514. {
  515. // if there is no [ in the string then keep it
  516. newarg = arg.substr(start, len);
  517. }
  518. else
  519. {
  520. int opencount = 0;
  521. int closecount = 0;
  522. for(std::string::size_type j = start; j < endpos; ++j)
  523. {
  524. if(arg.at(j) == '[')
  525. {
  526. ++opencount;
  527. }
  528. else if (arg.at(j) == ']')
  529. {
  530. ++closecount;
  531. }
  532. }
  533. if(opencount != closecount)
  534. {
  535. // skip this one
  536. endpos = arg.find(';', endpos+1);
  537. if(endpos == std::string::npos)
  538. {
  539. endpos = arg.size();
  540. }
  541. len = endpos - start;
  542. }
  543. newarg = arg.substr(start, len);
  544. }
  545. std::string::size_type pos = newarg.find("\\;");
  546. if(pos != std::string::npos)
  547. {
  548. newarg.erase(pos, 1);
  549. }
  550. newargs.push_back(newarg);
  551. }
  552. start = endpos+1;
  553. }
  554. }
  555. }
  556. bool cmSystemTools::SimpleGlob(const std::string& glob,
  557. std::vector<std::string>& files,
  558. int type /* = 0 */)
  559. {
  560. files.clear();
  561. if ( glob[glob.size()-1] != '*' )
  562. {
  563. return false;
  564. }
  565. std::string path = cmSystemTools::GetFilenamePath(glob);
  566. std::string ppath = cmSystemTools::GetFilenameName(glob);
  567. ppath = ppath.substr(0, ppath.size()-1);
  568. if ( path.size() == 0 )
  569. {
  570. path = "/";
  571. }
  572. bool res = false;
  573. cmsys::Directory d;
  574. if (d.Load(path.c_str()))
  575. {
  576. for (unsigned int i = 0; i < d.GetNumberOfFiles(); ++i)
  577. {
  578. if((std::string(d.GetFile(i)) != ".")
  579. && (std::string(d.GetFile(i)) != ".."))
  580. {
  581. std::string fname = path;
  582. if ( path[path.size()-1] != '/' )
  583. {
  584. fname +="/";
  585. }
  586. fname += d.GetFile(i);
  587. std::string sfname = d.GetFile(i);
  588. if ( type > 0 && cmSystemTools::FileIsDirectory(fname.c_str()) )
  589. {
  590. continue;
  591. }
  592. if ( type < 0 && !cmSystemTools::FileIsDirectory(fname.c_str()) )
  593. {
  594. continue;
  595. }
  596. if ( sfname.size() >= ppath.size() &&
  597. sfname.substr(0, ppath.size()) ==
  598. ppath )
  599. {
  600. files.push_back(fname);
  601. res = true;
  602. }
  603. }
  604. }
  605. }
  606. return res;
  607. }
  608. cmSystemTools::FileFormat cmSystemTools::GetFileFormat(const char* cext)
  609. {
  610. if ( ! cext || *cext == 0 )
  611. {
  612. return cmSystemTools::NO_FILE_FORMAT;
  613. }
  614. //std::string ext = cmSystemTools::LowerCase(cext);
  615. std::string ext = cext;
  616. if ( ext == "c" || ext == ".c" ) { return cmSystemTools::C_FILE_FORMAT; }
  617. if (
  618. ext == "C" || ext == ".C" ||
  619. ext == "M" || ext == ".M" ||
  620. ext == "c++" || ext == ".c++" ||
  621. ext == "cc" || ext == ".cc" ||
  622. ext == "cpp" || ext == ".cpp" ||
  623. ext == "cxx" || ext == ".cxx" ||
  624. ext == "m" || ext == ".m" ||
  625. ext == "mm" || ext == ".mm"
  626. ) { return cmSystemTools::CXX_FILE_FORMAT; }
  627. if ( ext == "java" || ext == ".java" ) { return cmSystemTools::JAVA_FILE_FORMAT; }
  628. if (
  629. ext == "H" || ext == ".H" ||
  630. ext == "h" || ext == ".h" ||
  631. ext == "h++" || ext == ".h++" ||
  632. ext == "hm" || ext == ".hm" ||
  633. ext == "hpp" || ext == ".hpp" ||
  634. ext == "hxx" || ext == ".hxx" ||
  635. ext == "in" || ext == ".in" ||
  636. ext == "txx" || ext == ".txx"
  637. ) { return cmSystemTools::HEADER_FILE_FORMAT; }
  638. if ( ext == "rc" || ext == ".rc" ) { return cmSystemTools::RESOURCE_FILE_FORMAT; }
  639. if ( ext == "def" || ext == ".def" ) { return cmSystemTools::DEFINITION_FILE_FORMAT; }
  640. if ( ext == "lib" || ext == ".lib" ||
  641. ext == "a" || ext == ".a") { return cmSystemTools::STATIC_LIBRARY_FILE_FORMAT; }
  642. if ( ext == "o" || ext == ".o" ||
  643. ext == "obj" || ext == ".obj") { return cmSystemTools::OBJECT_FILE_FORMAT; }
  644. #ifdef __APPLE__
  645. if ( ext == "dylib" || ext == ".dylib" )
  646. { return cmSystemTools::SHARED_LIBRARY_FILE_FORMAT; }
  647. if ( ext == "so" || ext == ".so" ||
  648. ext == "bundle" || ext == ".bundle" )
  649. { return cmSystemTools::MODULE_FILE_FORMAT; }
  650. #else // __APPLE__
  651. if ( ext == "so" || ext == ".so" ||
  652. ext == "sl" || ext == ".sl" ||
  653. ext == "dll" || ext == ".dll" )
  654. { return cmSystemTools::SHARED_LIBRARY_FILE_FORMAT; }
  655. #endif // __APPLE__
  656. return cmSystemTools::UNKNOWN_FILE_FORMAT;
  657. }
  658. bool cmSystemTools::Split(const char* s, std::vector<cmStdString>& l)
  659. {
  660. std::vector<std::string> temp;
  661. if(!Superclass::Split(s, temp))
  662. {
  663. return false;
  664. }
  665. for(std::vector<std::string>::const_iterator i = temp.begin();
  666. i != temp.end(); ++i)
  667. {
  668. l.push_back(*i);
  669. }
  670. return true;
  671. }