cmFileCommand.cxx 26 KB

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