cmFileCommand.cxx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  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 "cmFileCommand.h"
  14. #include "cmGlob.h"
  15. #include <sys/types.h>
  16. #include <sys/stat.h>
  17. #include <cmsys/Directory.hxx>
  18. // cmLibraryCommand
  19. bool cmFileCommand::InitialPass(std::vector<std::string> const& args)
  20. {
  21. if(args.size() < 2 )
  22. {
  23. this->SetError("must be called with at least two arguments.");
  24. return false;
  25. }
  26. std::string subCommand = args[0];
  27. if ( subCommand == "WRITE" )
  28. {
  29. return this->HandleWriteCommand(args, false);
  30. }
  31. else if ( subCommand == "APPEND" )
  32. {
  33. return this->HandleWriteCommand(args, true);
  34. }
  35. else if ( subCommand == "READ" )
  36. {
  37. return this->HandleReadCommand(args);
  38. }
  39. else if ( subCommand == "GLOB" )
  40. {
  41. return this->HandleGlobCommand(args, false);
  42. }
  43. else if ( subCommand == "GLOB_RECURSE" )
  44. {
  45. return this->HandleGlobCommand(args, true);
  46. }
  47. else if ( subCommand == "MAKE_DIRECTORY" )
  48. {
  49. return this->HandleMakeDirectoryCommand(args);
  50. }
  51. else if ( subCommand == "REMOVE" )
  52. {
  53. return this->HandleRemove(args, false);
  54. }
  55. else if ( subCommand == "REMOVE_RECURSE" )
  56. {
  57. return this->HandleRemove(args, true);
  58. }
  59. else if ( subCommand == "INSTALL" )
  60. {
  61. return this->HandleInstallCommand(args);
  62. }
  63. else if ( subCommand == "RELATIVE_PATH" )
  64. {
  65. return this->HandleRelativePathCommand(args);
  66. }
  67. std::string e = "does not recognize sub-command "+subCommand;
  68. this->SetError(e.c_str());
  69. return false;
  70. }
  71. //----------------------------------------------------------------------------
  72. bool cmFileCommand::HandleWriteCommand(std::vector<std::string> const& args,
  73. bool append)
  74. {
  75. std::string message;
  76. std::vector<std::string>::const_iterator i = args.begin();
  77. i++; // Get rid of subcommand
  78. std::string fileName = *i;
  79. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  80. {
  81. fileName = m_Makefile->GetCurrentDirectory();
  82. fileName += "/" + *i;
  83. }
  84. i++;
  85. for(;i != args.end(); ++i)
  86. {
  87. message += *i;
  88. }
  89. std::string dir = cmSystemTools::GetFilenamePath(fileName);
  90. cmSystemTools::MakeDirectory(dir.c_str());
  91. mode_t mode =
  92. #if defined( _MSC_VER ) || defined( __MINGW32__ )
  93. S_IREAD | S_IWRITE
  94. #elif defined( __BORLANDC__ )
  95. S_IRUSR | S_IWUSR
  96. #else
  97. S_IRUSR | S_IWUSR |
  98. S_IRGRP |
  99. S_IROTH
  100. #endif
  101. ;
  102. // Set permissions to writable
  103. if ( cmSystemTools::GetPermissions(fileName.c_str(), mode) )
  104. {
  105. cmSystemTools::SetPermissions(fileName.c_str(),
  106. #if defined( _MSC_VER ) || defined( __MINGW32__ )
  107. S_IREAD | S_IWRITE
  108. #else
  109. S_IRUSR | S_IWUSR
  110. #endif
  111. );
  112. }
  113. // If GetPermissions fails, pretend like it is ok. File open will fail if
  114. // the file is not writable
  115. std::ofstream file(fileName.c_str(), append?std::ios::app: std::ios::out);
  116. if ( !file )
  117. {
  118. std::string error = "Internal CMake error when trying to open file: ";
  119. error += fileName.c_str();
  120. error += " for writing.";
  121. this->SetError(error.c_str());
  122. return false;
  123. }
  124. file << message;
  125. file.close();
  126. cmSystemTools::SetPermissions(fileName.c_str(), mode);
  127. m_Makefile->AddWrittenFile(fileName.c_str());
  128. return true;
  129. }
  130. //----------------------------------------------------------------------------
  131. bool cmFileCommand::HandleReadCommand(std::vector<std::string> const& args)
  132. {
  133. if ( args.size() != 3 )
  134. {
  135. this->SetError("READ must be called with two additional arguments");
  136. return false;
  137. }
  138. std::string fileName = args[1];
  139. if ( !cmsys::SystemTools::FileIsFullPath(args[1].c_str()) )
  140. {
  141. fileName = m_Makefile->GetCurrentDirectory();
  142. fileName += "/" + args[1];
  143. }
  144. std::string variable = args[2];
  145. std::ifstream file(fileName.c_str(), std::ios::in);
  146. if ( !file )
  147. {
  148. std::string error = "Internal CMake error when trying to open file: ";
  149. error += fileName.c_str();
  150. error += " for reading.";
  151. this->SetError(error.c_str());
  152. return false;
  153. }
  154. std::string output;
  155. std::string line;
  156. bool has_newline = false;
  157. while ( cmSystemTools::GetLineFromStream(file, line, &has_newline) )
  158. {
  159. output += line;
  160. if ( has_newline )
  161. {
  162. output += "\n";
  163. }
  164. }
  165. m_Makefile->AddDefinition(variable.c_str(), output.c_str());
  166. return true;
  167. }
  168. //----------------------------------------------------------------------------
  169. bool cmFileCommand::HandleGlobCommand(std::vector<std::string> const& args,
  170. bool recurse)
  171. {
  172. if ( args.size() < 2 )
  173. {
  174. this->SetError("GLOB requires at least a variable name");
  175. return false;
  176. }
  177. std::vector<std::string>::const_iterator i = args.begin();
  178. i++; // Get rid of subcommand
  179. std::string variable = *i;
  180. i++;
  181. cmGlob g;
  182. g.SetRecurse(recurse);
  183. std::string output = "";
  184. bool first = true;
  185. for ( ; i != args.end(); ++i )
  186. {
  187. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  188. {
  189. std::string expr = m_Makefile->GetCurrentDirectory();
  190. // Handle script mode
  191. if ( expr.size() > 0 )
  192. {
  193. expr += "/" + *i;
  194. g.FindFiles(expr);
  195. }
  196. else
  197. {
  198. g.FindFiles(*i);
  199. }
  200. }
  201. else
  202. {
  203. g.FindFiles(*i);
  204. }
  205. std::vector<std::string>::size_type cc;
  206. std::vector<std::string>& files = g.GetFiles();
  207. for ( cc = 0; cc < files.size(); cc ++ )
  208. {
  209. if ( !first )
  210. {
  211. output += ";";
  212. }
  213. output += files[cc];
  214. first = false;
  215. }
  216. }
  217. m_Makefile->AddDefinition(variable.c_str(), output.c_str());
  218. return true;
  219. }
  220. //----------------------------------------------------------------------------
  221. bool cmFileCommand::HandleMakeDirectoryCommand(
  222. std::vector<std::string> const& args)
  223. {
  224. if(args.size() < 2 )
  225. {
  226. this->SetError("called with incorrect number of arguments");
  227. return false;
  228. }
  229. std::vector<std::string>::const_iterator i = args.begin();
  230. i++; // Get rid of subcommand
  231. std::string expr;
  232. for ( ; i != args.end(); ++i )
  233. {
  234. const std::string* cdir = &(*i);
  235. if ( !cmsys::SystemTools::FileIsFullPath(i->c_str()) )
  236. {
  237. expr = m_Makefile->GetCurrentDirectory();
  238. expr += "/" + *i;
  239. cdir = &expr;
  240. }
  241. if ( !cmSystemTools::MakeDirectory(cdir->c_str()) )
  242. {
  243. std::string error = "problem creating directory: " + *cdir;
  244. this->SetError(error.c_str());
  245. return false;
  246. }
  247. }
  248. return true;
  249. }
  250. //----------------------------------------------------------------------------
  251. bool cmFileCommand::HandleInstallCommand(
  252. std::vector<std::string> const& args)
  253. {
  254. if ( args.size() < 6 )
  255. {
  256. this->SetError("called with incorrect number of arguments");
  257. return false;
  258. }
  259. std::string rename = "";
  260. std::string destination = "";
  261. std::string stype = "FILES";
  262. const char* build_type = m_Makefile->GetDefinition("BUILD_TYPE");
  263. if ( build_type && strcmp(build_type, ".") == 0 )
  264. {
  265. build_type = 0;
  266. }
  267. if ( build_type && strncmp(build_type, ".\\", 2) == 0 )
  268. {
  269. build_type += 2;
  270. }
  271. const char* destdir = cmSystemTools::GetEnv("DESTDIR");
  272. std::vector<std::string> files;
  273. int itype = cmTarget::INSTALL_FILES;
  274. std::vector<std::string>::size_type i = 0;
  275. i++; // Get rid of subcommand
  276. std::map<cmStdString, const char*> properties;
  277. // Build a table of permissions flags.
  278. #if defined(_WIN32) && !defined(__CYGWIN__)
  279. mode_t mode_owner_read = S_IREAD;
  280. mode_t mode_owner_write = S_IWRITE;
  281. mode_t mode_owner_execute = S_IEXEC;
  282. mode_t mode_group_read = 0;
  283. mode_t mode_group_write = 0;
  284. mode_t mode_group_execute = 0;
  285. mode_t mode_world_read = 0;
  286. mode_t mode_world_write = 0;
  287. mode_t mode_world_execute = 0;
  288. mode_t mode_setuid = 0;
  289. mode_t mode_setgid = 0;
  290. #else
  291. mode_t mode_owner_read = S_IRUSR;
  292. mode_t mode_owner_write = S_IWUSR;
  293. mode_t mode_owner_execute = S_IXUSR;
  294. mode_t mode_group_read = S_IRGRP;
  295. mode_t mode_group_write = S_IWGRP;
  296. mode_t mode_group_execute = S_IXGRP;
  297. mode_t mode_world_read = S_IROTH;
  298. mode_t mode_world_write = S_IWOTH;
  299. mode_t mode_world_execute = S_IXOTH;
  300. mode_t mode_setuid = S_ISUID;
  301. mode_t mode_setgid = S_ISGID;
  302. #endif
  303. bool in_files = false;
  304. bool in_properties = false;
  305. bool in_permissions = false;
  306. bool use_given_permissions = false;
  307. mode_t permissions = 0;
  308. bool optional = false;
  309. for ( ; i != args.size(); ++i )
  310. {
  311. const std::string* cstr = &args[i];
  312. if ( *cstr == "DESTINATION" && i < args.size()-1 )
  313. {
  314. i++;
  315. destination = args[i];
  316. in_files = false;
  317. in_properties = false;
  318. in_permissions = false;
  319. }
  320. else if ( *cstr == "TYPE" && i < args.size()-1 )
  321. {
  322. i++;
  323. stype = args[i];
  324. if ( args[i+1] == "OPTIONAL" )
  325. {
  326. i++;
  327. optional = true;
  328. }
  329. in_properties = false;
  330. in_files = false;
  331. in_permissions = false;
  332. }
  333. else if ( *cstr == "RENAME" && i < args.size()-1 )
  334. {
  335. i++;
  336. rename = args[i];
  337. in_properties = false;
  338. in_files = false;
  339. in_permissions = false;
  340. }
  341. else if ( *cstr == "PROPERTIES" )
  342. {
  343. in_properties = true;
  344. in_files = false;
  345. in_permissions = false;
  346. }
  347. else if ( *cstr == "PERMISSIONS" )
  348. {
  349. use_given_permissions = true;
  350. in_properties = false;
  351. in_files = false;
  352. in_permissions = true;
  353. }
  354. else if ( *cstr == "FILES" && !in_files)
  355. {
  356. in_files = true;
  357. in_properties = false;
  358. in_permissions = false;
  359. }
  360. else if ( in_properties && i < args.size()-1 )
  361. {
  362. properties[args[i]] = args[i+1].c_str();
  363. i++;
  364. }
  365. else if ( in_files )
  366. {
  367. files.push_back(*cstr);
  368. }
  369. else if(in_permissions && args[i] == "OWNER_READ")
  370. {
  371. permissions |= mode_owner_read;
  372. }
  373. else if(in_permissions && args[i] == "OWNER_WRITE")
  374. {
  375. permissions |= mode_owner_write;
  376. }
  377. else if(in_permissions && args[i] == "OWNER_EXECUTE")
  378. {
  379. permissions |= mode_owner_execute;
  380. }
  381. else if(in_permissions && args[i] == "GROUP_READ")
  382. {
  383. permissions |= mode_group_read;
  384. }
  385. else if(in_permissions && args[i] == "GROUP_WRITE")
  386. {
  387. permissions |= mode_group_write;
  388. }
  389. else if(in_permissions && args[i] == "GROUP_EXECUTE")
  390. {
  391. permissions |= mode_group_execute;
  392. }
  393. else if(in_permissions && args[i] == "WORLD_READ")
  394. {
  395. permissions |= mode_world_read;
  396. }
  397. else if(in_permissions && args[i] == "WORLD_WRITE")
  398. {
  399. permissions |= mode_world_write;
  400. }
  401. else if(in_permissions && args[i] == "WORLD_EXECUTE")
  402. {
  403. permissions |= mode_world_execute;
  404. }
  405. else if(in_permissions && args[i] == "SETUID")
  406. {
  407. permissions |= mode_setuid;
  408. }
  409. else if(in_permissions && args[i] == "SETGID")
  410. {
  411. permissions |= mode_setgid;
  412. }
  413. else
  414. {
  415. this->SetError("called with inappropriate arguments");
  416. return false;
  417. }
  418. }
  419. if ( destination.size() < 2 )
  420. {
  421. this->SetError("called with inapropriate arguments. "
  422. "No DESTINATION provided or .");
  423. return false;
  424. }
  425. int destDirLength = 0;
  426. if ( destdir && *destdir )
  427. {
  428. std::string sdestdir = destdir;
  429. cmSystemTools::ConvertToUnixSlashes(sdestdir);
  430. char ch1 = destination[0];
  431. char ch2 = destination[1];
  432. char ch3 = 0;
  433. if ( destination.size() > 2 )
  434. {
  435. ch3 = destination[2];
  436. }
  437. int skip = 0;
  438. if ( ch1 != '/' )
  439. {
  440. int relative = 0;
  441. if ( ( ch1 >= 'a' && ch1 <= 'z' || ch1 >= 'A' && ch1 <= 'Z' ) &&
  442. ch2 == ':' )
  443. {
  444. // Assume windows
  445. // let's do some destdir magic:
  446. skip = 2;
  447. if ( ch3 != '/' )
  448. {
  449. relative = 1;
  450. }
  451. }
  452. else
  453. {
  454. relative = 1;
  455. }
  456. if ( relative )
  457. {
  458. // This is relative path on unix or windows. Since we are doing
  459. // destdir, this case does not make sense.
  460. this->SetError("called with relative DESTINATION. This "
  461. "does not make sense when using DESTDIR. Specify "
  462. "absolute path or remove DESTDIR environment variable.");
  463. return false;
  464. }
  465. }
  466. else
  467. {
  468. if ( ch2 == '/' )
  469. {
  470. // looks like a network path.
  471. this->SetError("called with network path DESTINATION. This "
  472. "does not make sense when using DESTDIR. Specify local "
  473. "absolute path or remove DESTDIR environment variable.");
  474. return false;
  475. }
  476. }
  477. destination = sdestdir + (destination.c_str() + skip);
  478. destDirLength = int(sdestdir.size());
  479. }
  480. if ( files.size() == 0 )
  481. {
  482. this->SetError(
  483. "called with inapropriate arguments. No FILES provided.");
  484. return false;
  485. }
  486. if ( stype == "EXECUTABLE" )
  487. {
  488. itype = cmTarget::EXECUTABLE;
  489. }
  490. else if ( stype == "PROGRAM" )
  491. {
  492. itype = cmTarget::INSTALL_PROGRAMS;
  493. }
  494. else if ( stype == "STATIC_LIBRARY" )
  495. {
  496. itype = cmTarget::STATIC_LIBRARY;
  497. }
  498. else if ( stype == "SHARED_LIBRARY" )
  499. {
  500. itype = cmTarget::SHARED_LIBRARY;
  501. }
  502. else if ( stype == "MODULE" )
  503. {
  504. itype = cmTarget::MODULE_LIBRARY;
  505. }
  506. if ( !cmSystemTools::FileExists(destination.c_str()) )
  507. {
  508. if ( !cmSystemTools::MakeDirectory(destination.c_str()) )
  509. {
  510. std::string errstring = "cannot create directory: " + destination +
  511. ". Maybe need administrative privileges.";
  512. this->SetError(errstring.c_str());
  513. return false;
  514. }
  515. }
  516. if ( !cmSystemTools::FileIsDirectory(destination.c_str()) )
  517. {
  518. std::string errstring = "INSTALL destination: " + destination +
  519. " is not a directory.";
  520. this->SetError(errstring.c_str());
  521. return false;
  522. }
  523. // Check rename form.
  524. if(!rename.empty())
  525. {
  526. if(itype != cmTarget::INSTALL_FILES)
  527. {
  528. this->SetError("INSTALL option RENAME may be used only with FILES.");
  529. return false;
  530. }
  531. if(files.size() > 1)
  532. {
  533. this->SetError("INSTALL option RENAME may be used only with one file.");
  534. return false;
  535. }
  536. }
  537. // If permissions were not specified set default permissions for
  538. // this target type.
  539. bool use_source_permissions = false;
  540. if(!use_given_permissions)
  541. {
  542. switch(itype)
  543. {
  544. case cmTarget::SHARED_LIBRARY:
  545. case cmTarget::MODULE_LIBRARY:
  546. #if defined(__linux__)
  547. // Use read/write permissions.
  548. use_given_permissions = true;
  549. permissions = 0;
  550. permissions |= mode_owner_read;
  551. permissions |= mode_owner_write;
  552. permissions |= mode_group_read;
  553. permissions |= mode_world_read;
  554. break;
  555. #endif
  556. case cmTarget::EXECUTABLE:
  557. case cmTarget::INSTALL_PROGRAMS:
  558. // Use read/write/executable permissions.
  559. use_given_permissions = true;
  560. permissions = 0;
  561. permissions |= mode_owner_read;
  562. permissions |= mode_owner_write;
  563. permissions |= mode_owner_execute;
  564. permissions |= mode_group_read;
  565. permissions |= mode_group_execute;
  566. permissions |= mode_world_read;
  567. permissions |= mode_world_execute;
  568. break;
  569. default:
  570. // Use the permissions of the file being copied.
  571. use_source_permissions = true;
  572. break;
  573. }
  574. }
  575. // Get the current manifest.
  576. const char* manifest_files =
  577. m_Makefile->GetDefinition("CMAKE_INSTALL_MANIFEST_FILES");
  578. std::string smanifest_files;
  579. if ( manifest_files )
  580. {
  581. smanifest_files = manifest_files;
  582. }
  583. // Handle each file listed.
  584. for ( i = 0; i < files.size(); i ++ )
  585. {
  586. // Split the input file into its directory and name components.
  587. std::string fromDir = cmSystemTools::GetFilenamePath(files[i]);
  588. std::string fromName = cmSystemTools::GetFilenameName(files[i]);
  589. // Compute the full path to the destination file.
  590. std::string toFile = destination;
  591. toFile += "/";
  592. toFile += rename.empty()? fromName : rename;
  593. // Handle type-specific installation details.
  594. switch(itype)
  595. {
  596. case cmTarget::MODULE_LIBRARY:
  597. case cmTarget::STATIC_LIBRARY:
  598. case cmTarget::SHARED_LIBRARY:
  599. {
  600. // Handle shared library versioning
  601. const char* lib_version = 0;
  602. const char* lib_soversion = 0;
  603. if ( properties.find("VERSION") != properties.end() )
  604. {
  605. lib_version = properties["VERSION"];
  606. }
  607. if ( properties.find("SOVERSION") != properties.end() )
  608. {
  609. lib_soversion = properties["SOVERSION"];
  610. }
  611. if ( !lib_version && lib_soversion )
  612. {
  613. lib_version = lib_soversion;
  614. }
  615. if ( !lib_soversion && lib_version )
  616. {
  617. lib_soversion = lib_version;
  618. }
  619. if ( lib_version && lib_soversion )
  620. {
  621. std::string libname = toFile;
  622. std::string soname = toFile;
  623. std::string soname_nopath = fromName;
  624. soname += ".";
  625. soname += lib_soversion;
  626. soname_nopath += ".";
  627. soname_nopath += lib_soversion;
  628. fromName += ".";
  629. fromName += lib_version;
  630. toFile += ".";
  631. toFile += lib_version;
  632. cmSystemTools::RemoveFile(soname.c_str());
  633. cmSystemTools::RemoveFile(libname.c_str());
  634. if (!cmSystemTools::CreateSymlink(soname_nopath.c_str(), libname.c_str()) )
  635. {
  636. std::string errstring = "error when creating symlink from: " + libname + " to " + soname_nopath;
  637. this->SetError(errstring.c_str());
  638. return false;
  639. }
  640. smanifest_files += ";";
  641. smanifest_files += libname.substr(destDirLength);;
  642. if ( toFile != soname )
  643. {
  644. if ( !cmSystemTools::CreateSymlink(fromName.c_str(), soname.c_str()) )
  645. {
  646. std::string errstring = "error when creating symlink from: " + soname + " to " + fromName;
  647. this->SetError(errstring.c_str());
  648. return false;
  649. }
  650. smanifest_files += ";";
  651. smanifest_files += soname.substr(destDirLength);
  652. }
  653. }
  654. }
  655. break;
  656. case cmTarget::EXECUTABLE:
  657. {
  658. // Handle executable versioning
  659. const char* exe_version = 0;
  660. if ( properties.find("VERSION") != properties.end() )
  661. {
  662. exe_version = properties["VERSION"];
  663. }
  664. if ( exe_version )
  665. {
  666. std::string exename = toFile;
  667. std::string exename_nopath = fromName;
  668. exename_nopath += "-";
  669. exename_nopath += exe_version;
  670. fromName += "-";
  671. fromName += exe_version;
  672. toFile += "-";
  673. toFile += exe_version;
  674. cmSystemTools::RemoveFile(exename.c_str());
  675. if (!cmSystemTools::CreateSymlink(exename_nopath.c_str(), exename.c_str()) )
  676. {
  677. std::string errstring = "error when creating symlink from: " + exename + " to " + exename_nopath;
  678. this->SetError(errstring.c_str());
  679. return false;
  680. }
  681. smanifest_files += ";";
  682. smanifest_files += exename.substr(destDirLength);
  683. }
  684. }
  685. break;
  686. }
  687. // Construct the full path to the source file. The file name may
  688. // have been changed above.
  689. std::string fromFile = fromDir;
  690. fromFile += "/";
  691. fromFile += fromName;
  692. std::string message;
  693. if(!cmSystemTools::SameFile(fromFile.c_str(), toFile.c_str()))
  694. {
  695. if(cmSystemTools::FileExists(fromFile.c_str()))
  696. {
  697. // We will install this file. Display the information.
  698. message = "Installing ";
  699. message += toFile.c_str();
  700. m_Makefile->DisplayStatus(message.c_str(), -1);
  701. // If no permissions were already given use the permissions of
  702. // the file being copied.
  703. if(!use_given_permissions &&
  704. (!use_source_permissions ||
  705. !cmSystemTools::GetPermissions(fromFile.c_str(), permissions)))
  706. {
  707. // Set default permissions.
  708. permissions = 0;
  709. permissions |= mode_owner_read;
  710. permissions |= mode_owner_write;
  711. permissions |= mode_group_read;
  712. permissions |= mode_world_read;
  713. }
  714. // Remove the original file and try copying the new file.
  715. // TODO: This should be copy-if-different. Don't forget to
  716. // edit the destination file permissions, or compare files
  717. // first. This would need a new SystemTools::FilesDiffer that
  718. // does not read all of the files at once.
  719. cmSystemTools::RemoveFile(toFile.c_str());
  720. if(!cmSystemTools::CopyFileAlways(fromFile.c_str(), toFile.c_str()))
  721. {
  722. cmOStringStream e;
  723. e << "INSTALL cannot copy file \"" << fromFile
  724. << "\" to \"" << toFile + "\".";
  725. this->SetError(e.str().c_str());
  726. return false;
  727. }
  728. // Perform post-installation processing on the file depending
  729. // on its type.
  730. #if defined(__APPLE_CC__)
  731. // Static libraries need ranlib on this platform.
  732. if(itype == cmTarget::STATIC_LIBRARY)
  733. {
  734. std::string ranlib = "ranlib ";
  735. ranlib += cmSystemTools::ConvertToOutputPath(toFile.c_str());
  736. if(!cmSystemTools::RunSingleCommand(ranlib.c_str()))
  737. {
  738. std::string err = "ranlib failed: ";
  739. err += ranlib;
  740. this->SetError(err.c_str());
  741. return false;
  742. }
  743. }
  744. #endif
  745. // Set permissions of the destination file.
  746. if(!cmSystemTools::SetPermissions(toFile.c_str(), permissions))
  747. {
  748. cmOStringStream e;
  749. e << "Problem setting permissions on file \""
  750. << toFile.c_str() << "\"";
  751. this->SetError(e.str().c_str());
  752. return false;
  753. }
  754. // Add the file to the manifest.
  755. smanifest_files += ";";
  756. smanifest_files += toFile.substr(destDirLength);
  757. }
  758. else if(!optional)
  759. {
  760. // The input file does not exist and installation is not optional.
  761. cmOStringStream e;
  762. e << "INSTALL cannot find file \"" << fromFile << "\" to install.";
  763. this->SetError(e.str().c_str());
  764. return false;
  765. }
  766. }
  767. }
  768. // Save the updated install manifest.
  769. m_Makefile->AddDefinition("CMAKE_INSTALL_MANIFEST_FILES",
  770. smanifest_files.c_str());
  771. return true;
  772. }
  773. //----------------------------------------------------------------------------
  774. bool cmFileCommand::HandleRelativePathCommand(
  775. std::vector<std::string> const& args)
  776. {
  777. if(args.size() != 4 )
  778. {
  779. this->SetError("called with incorrect number of arguments");
  780. return false;
  781. }
  782. const std::string& outVar = args[1];
  783. const std::string& directoryName = args[2];
  784. const std::string& fileName = args[3];
  785. if(!cmSystemTools::FileIsFullPath(directoryName.c_str()))
  786. {
  787. std::string errstring = "RelativePath must be passed a full path to the directory: " + directoryName;
  788. this->SetError(errstring.c_str());
  789. return false;
  790. }
  791. if(!cmSystemTools::FileIsFullPath(fileName.c_str()))
  792. {
  793. std::string errstring = "RelativePath must be passed a full path to the directory: " + directoryName;
  794. this->SetError(errstring.c_str());
  795. return false;
  796. }
  797. std::string res = cmSystemTools::RelativePath(directoryName.c_str(), fileName.c_str());
  798. m_Makefile->AddDefinition(outVar.c_str(),
  799. res.c_str());
  800. return true;
  801. }
  802. //----------------------------------------------------------------------------
  803. bool cmFileCommand::HandleRemove(std::vector<std::string> const& args,
  804. bool recurse)
  805. {
  806. std::string message;
  807. std::vector<std::string>::const_iterator i = args.begin();
  808. i++; // Get rid of subcommand
  809. for(;i != args.end(); ++i)
  810. {
  811. if(cmSystemTools::FileIsDirectory(i->c_str()) && recurse)
  812. {
  813. cmSystemTools::RemoveADirectory(i->c_str());
  814. }
  815. else
  816. {
  817. cmSystemTools::RemoveFile(i->c_str());
  818. }
  819. }
  820. return true;
  821. }