cmFileCommand.cxx 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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::set<cmStdString> components;
  303. std::vector<std::string> files;
  304. int itype = cmTarget::INSTALL_FILES;
  305. std::vector<std::string>::size_type i = 0;
  306. i++; // Get rid of subcommand
  307. std::map<cmStdString, const char*> properties;
  308. // Build a table of permissions flags.
  309. #if defined(_WIN32) && !defined(__CYGWIN__)
  310. mode_t mode_owner_read = S_IREAD;
  311. mode_t mode_owner_write = S_IWRITE;
  312. mode_t mode_owner_execute = S_IEXEC;
  313. mode_t mode_group_read = 0;
  314. mode_t mode_group_write = 0;
  315. mode_t mode_group_execute = 0;
  316. mode_t mode_world_read = 0;
  317. mode_t mode_world_write = 0;
  318. mode_t mode_world_execute = 0;
  319. mode_t mode_setuid = 0;
  320. mode_t mode_setgid = 0;
  321. #else
  322. mode_t mode_owner_read = S_IRUSR;
  323. mode_t mode_owner_write = S_IWUSR;
  324. mode_t mode_owner_execute = S_IXUSR;
  325. mode_t mode_group_read = S_IRGRP;
  326. mode_t mode_group_write = S_IWGRP;
  327. mode_t mode_group_execute = S_IXGRP;
  328. mode_t mode_world_read = S_IROTH;
  329. mode_t mode_world_write = S_IWOTH;
  330. mode_t mode_world_execute = S_IXOTH;
  331. mode_t mode_setuid = S_ISUID;
  332. mode_t mode_setgid = S_ISGID;
  333. #endif
  334. bool in_files = false;
  335. bool in_properties = false;
  336. bool in_permissions = false;
  337. bool in_components = false;
  338. bool use_given_permissions = false;
  339. mode_t permissions = 0;
  340. bool optional = false;
  341. for ( ; i != args.size(); ++i )
  342. {
  343. const std::string* cstr = &args[i];
  344. if ( *cstr == "DESTINATION" && i < args.size()-1 )
  345. {
  346. i++;
  347. destination = args[i];
  348. in_files = false;
  349. in_properties = false;
  350. in_permissions = false;
  351. in_components = false;
  352. }
  353. else if ( *cstr == "TYPE" && i < args.size()-1 )
  354. {
  355. i++;
  356. stype = args[i];
  357. if ( args[i+1] == "OPTIONAL" )
  358. {
  359. i++;
  360. optional = true;
  361. }
  362. in_properties = false;
  363. in_files = false;
  364. in_permissions = false;
  365. in_components = false;
  366. }
  367. else if ( *cstr == "RENAME" && i < args.size()-1 )
  368. {
  369. i++;
  370. rename = args[i];
  371. in_properties = false;
  372. in_files = false;
  373. in_permissions = false;
  374. in_components = false;
  375. }
  376. else if ( *cstr == "PROPERTIES" )
  377. {
  378. in_properties = true;
  379. in_files = false;
  380. in_permissions = false;
  381. in_components = false;
  382. }
  383. else if ( *cstr == "PERMISSIONS" )
  384. {
  385. use_given_permissions = true;
  386. in_properties = false;
  387. in_files = false;
  388. in_permissions = true;
  389. in_components = false;
  390. }
  391. else if ( *cstr == "COMPONENTS" )
  392. {
  393. in_properties = false;
  394. in_files = false;
  395. in_permissions = false;
  396. in_components = true;
  397. }
  398. else if ( *cstr == "FILES" && !in_files)
  399. {
  400. in_files = true;
  401. in_properties = false;
  402. in_permissions = false;
  403. in_components = false;
  404. }
  405. else if ( in_properties && i < args.size()-1 )
  406. {
  407. properties[args[i]] = args[i+1].c_str();
  408. i++;
  409. }
  410. else if ( in_files )
  411. {
  412. files.push_back(*cstr);
  413. }
  414. else if ( in_components )
  415. {
  416. components.insert(*cstr);
  417. }
  418. else if(in_permissions && args[i] == "OWNER_READ")
  419. {
  420. permissions |= mode_owner_read;
  421. }
  422. else if(in_permissions && args[i] == "OWNER_WRITE")
  423. {
  424. permissions |= mode_owner_write;
  425. }
  426. else if(in_permissions && args[i] == "OWNER_EXECUTE")
  427. {
  428. permissions |= mode_owner_execute;
  429. }
  430. else if(in_permissions && args[i] == "GROUP_READ")
  431. {
  432. permissions |= mode_group_read;
  433. }
  434. else if(in_permissions && args[i] == "GROUP_WRITE")
  435. {
  436. permissions |= mode_group_write;
  437. }
  438. else if(in_permissions && args[i] == "GROUP_EXECUTE")
  439. {
  440. permissions |= mode_group_execute;
  441. }
  442. else if(in_permissions && args[i] == "WORLD_READ")
  443. {
  444. permissions |= mode_world_read;
  445. }
  446. else if(in_permissions && args[i] == "WORLD_WRITE")
  447. {
  448. permissions |= mode_world_write;
  449. }
  450. else if(in_permissions && args[i] == "WORLD_EXECUTE")
  451. {
  452. permissions |= mode_world_execute;
  453. }
  454. else if(in_permissions && args[i] == "SETUID")
  455. {
  456. permissions |= mode_setuid;
  457. }
  458. else if(in_permissions && args[i] == "SETGID")
  459. {
  460. permissions |= mode_setgid;
  461. }
  462. else
  463. {
  464. this->SetError("called with inappropriate arguments");
  465. return false;
  466. }
  467. }
  468. if ( destination.size() < 2 )
  469. {
  470. this->SetError("called with inapropriate arguments. "
  471. "No DESTINATION provided or .");
  472. return false;
  473. }
  474. // Check for component-specific installation.
  475. const char* cmake_install_component =
  476. this->Makefile->GetDefinition("CMAKE_INSTALL_COMPONENT");
  477. if(cmake_install_component && *cmake_install_component)
  478. {
  479. // This install rule applies only if it is associated with the
  480. // current component.
  481. if(components.find(cmake_install_component) == components.end())
  482. {
  483. return true;
  484. }
  485. }
  486. int destDirLength = 0;
  487. if ( destdir && *destdir )
  488. {
  489. std::string sdestdir = destdir;
  490. cmSystemTools::ConvertToUnixSlashes(sdestdir);
  491. char ch1 = destination[0];
  492. char ch2 = destination[1];
  493. char ch3 = 0;
  494. if ( destination.size() > 2 )
  495. {
  496. ch3 = destination[2];
  497. }
  498. int skip = 0;
  499. if ( ch1 != '/' )
  500. {
  501. int relative = 0;
  502. if ( ( ch1 >= 'a' && ch1 <= 'z' || ch1 >= 'A' && ch1 <= 'Z' ) &&
  503. ch2 == ':' )
  504. {
  505. // Assume windows
  506. // let's do some destdir magic:
  507. skip = 2;
  508. if ( ch3 != '/' )
  509. {
  510. relative = 1;
  511. }
  512. }
  513. else
  514. {
  515. relative = 1;
  516. }
  517. if ( relative )
  518. {
  519. // This is relative path on unix or windows. Since we are doing
  520. // destdir, this case does not make sense.
  521. this->SetError("called with relative DESTINATION. This "
  522. "does not make sense when using DESTDIR. Specify "
  523. "absolute path or remove DESTDIR environment variable.");
  524. return false;
  525. }
  526. }
  527. else
  528. {
  529. if ( ch2 == '/' )
  530. {
  531. // looks like a network path.
  532. this->SetError("called with network path DESTINATION. This "
  533. "does not make sense when using DESTDIR. Specify local "
  534. "absolute path or remove DESTDIR environment variable.");
  535. return false;
  536. }
  537. }
  538. destination = sdestdir + (destination.c_str() + skip);
  539. destDirLength = int(sdestdir.size());
  540. }
  541. if ( files.size() == 0 )
  542. {
  543. this->SetError(
  544. "called with inapropriate arguments. No FILES provided.");
  545. return false;
  546. }
  547. if ( stype == "EXECUTABLE" )
  548. {
  549. itype = cmTarget::EXECUTABLE;
  550. }
  551. else if ( stype == "PROGRAM" )
  552. {
  553. itype = cmTarget::INSTALL_PROGRAMS;
  554. }
  555. else if ( stype == "STATIC_LIBRARY" )
  556. {
  557. itype = cmTarget::STATIC_LIBRARY;
  558. }
  559. else if ( stype == "SHARED_LIBRARY" )
  560. {
  561. itype = cmTarget::SHARED_LIBRARY;
  562. }
  563. else if ( stype == "MODULE" )
  564. {
  565. itype = cmTarget::MODULE_LIBRARY;
  566. }
  567. else if ( stype == "DIRECTORY" )
  568. {
  569. itype = cmTarget::INSTALL_DIRECTORY;
  570. }
  571. if ( !cmSystemTools::FileExists(destination.c_str()) )
  572. {
  573. if ( !cmSystemTools::MakeDirectory(destination.c_str()) )
  574. {
  575. std::string errstring = "cannot create directory: " + destination +
  576. ". Maybe need administrative privileges.";
  577. this->SetError(errstring.c_str());
  578. return false;
  579. }
  580. }
  581. if ( !cmSystemTools::FileIsDirectory(destination.c_str()) )
  582. {
  583. std::string errstring = "INSTALL destination: " + destination +
  584. " is not a directory.";
  585. this->SetError(errstring.c_str());
  586. return false;
  587. }
  588. // Check rename form.
  589. if(!rename.empty())
  590. {
  591. if(itype != cmTarget::INSTALL_FILES)
  592. {
  593. this->SetError("INSTALL option RENAME may be used only with FILES.");
  594. return false;
  595. }
  596. if(files.size() > 1)
  597. {
  598. this->SetError("INSTALL option RENAME may be used only with one file.");
  599. return false;
  600. }
  601. }
  602. // If permissions were not specified set default permissions for
  603. // this target type.
  604. bool use_source_permissions = false;
  605. if(!use_given_permissions)
  606. {
  607. switch(itype)
  608. {
  609. case cmTarget::SHARED_LIBRARY:
  610. case cmTarget::MODULE_LIBRARY:
  611. #if defined(__linux__)
  612. // Use read/write permissions.
  613. use_given_permissions = true;
  614. permissions = 0;
  615. permissions |= mode_owner_read;
  616. permissions |= mode_owner_write;
  617. permissions |= mode_group_read;
  618. permissions |= mode_world_read;
  619. break;
  620. #endif
  621. case cmTarget::EXECUTABLE:
  622. case cmTarget::INSTALL_PROGRAMS:
  623. // Use read/write/executable permissions.
  624. use_given_permissions = true;
  625. permissions = 0;
  626. permissions |= mode_owner_read;
  627. permissions |= mode_owner_write;
  628. permissions |= mode_owner_execute;
  629. permissions |= mode_group_read;
  630. permissions |= mode_group_execute;
  631. permissions |= mode_world_read;
  632. permissions |= mode_world_execute;
  633. break;
  634. default:
  635. // Use the permissions of the file being copied.
  636. use_source_permissions = true;
  637. break;
  638. }
  639. }
  640. // Get the current manifest.
  641. const char* manifest_files =
  642. this->Makefile->GetDefinition("CMAKE_INSTALL_MANIFEST_FILES");
  643. std::string smanifest_files;
  644. if ( manifest_files )
  645. {
  646. smanifest_files = manifest_files;
  647. }
  648. // Handle each file listed.
  649. for ( i = 0; i < files.size(); i ++ )
  650. {
  651. // Split the input file into its directory and name components.
  652. std::string fromDir = cmSystemTools::GetFilenamePath(files[i]);
  653. std::string fromName = cmSystemTools::GetFilenameName(files[i]);
  654. // Compute the full path to the destination file.
  655. std::string toFile = destination;
  656. toFile += "/";
  657. toFile += rename.empty()? fromName : rename;
  658. // Handle type-specific installation details.
  659. switch(itype)
  660. {
  661. case cmTarget::MODULE_LIBRARY:
  662. case cmTarget::STATIC_LIBRARY:
  663. case cmTarget::SHARED_LIBRARY:
  664. {
  665. // Handle shared library versioning
  666. const char* lib_version = 0;
  667. const char* lib_soversion = 0;
  668. if ( properties.find("VERSION") != properties.end() )
  669. {
  670. lib_version = properties["VERSION"];
  671. }
  672. if ( properties.find("SOVERSION") != properties.end() )
  673. {
  674. lib_soversion = properties["SOVERSION"];
  675. }
  676. if ( !lib_version && lib_soversion )
  677. {
  678. lib_version = lib_soversion;
  679. }
  680. if ( !lib_soversion && lib_version )
  681. {
  682. lib_soversion = lib_version;
  683. }
  684. if ( lib_version && lib_soversion )
  685. {
  686. std::string libname = toFile;
  687. std::string soname = toFile;
  688. std::string soname_nopath = fromName;
  689. soname += ".";
  690. soname += lib_soversion;
  691. soname_nopath += ".";
  692. soname_nopath += lib_soversion;
  693. fromName += ".";
  694. fromName += lib_version;
  695. toFile += ".";
  696. toFile += lib_version;
  697. cmSystemTools::RemoveFile(soname.c_str());
  698. cmSystemTools::RemoveFile(libname.c_str());
  699. if (!cmSystemTools::CreateSymlink(soname_nopath.c_str(), libname.c_str()) )
  700. {
  701. std::string errstring = "error when creating symlink from: " + libname + " to " + soname_nopath;
  702. this->SetError(errstring.c_str());
  703. return false;
  704. }
  705. smanifest_files += ";";
  706. smanifest_files += libname.substr(destDirLength);;
  707. if ( toFile != soname )
  708. {
  709. if ( !cmSystemTools::CreateSymlink(fromName.c_str(), soname.c_str()) )
  710. {
  711. std::string errstring = "error when creating symlink from: " + soname + " to " + fromName;
  712. this->SetError(errstring.c_str());
  713. return false;
  714. }
  715. smanifest_files += ";";
  716. smanifest_files += soname.substr(destDirLength);
  717. }
  718. }
  719. }
  720. break;
  721. case cmTarget::EXECUTABLE:
  722. {
  723. // Handle executable versioning
  724. const char* exe_version = 0;
  725. if ( properties.find("VERSION") != properties.end() )
  726. {
  727. exe_version = properties["VERSION"];
  728. }
  729. if ( exe_version )
  730. {
  731. std::string exename = toFile;
  732. std::string exename_nopath = fromName;
  733. exename_nopath += "-";
  734. exename_nopath += exe_version;
  735. fromName += "-";
  736. fromName += exe_version;
  737. toFile += "-";
  738. toFile += exe_version;
  739. cmSystemTools::RemoveFile(exename.c_str());
  740. if (!cmSystemTools::CreateSymlink(exename_nopath.c_str(), exename.c_str()) )
  741. {
  742. std::string errstring = "error when creating symlink from: " + exename + " to " + exename_nopath;
  743. this->SetError(errstring.c_str());
  744. return false;
  745. }
  746. smanifest_files += ";";
  747. smanifest_files += exename.substr(destDirLength);
  748. }
  749. }
  750. break;
  751. }
  752. // Construct the full path to the source file. The file name may
  753. // have been changed above.
  754. std::string fromFile = fromDir;
  755. fromFile += "/";
  756. fromFile += fromName;
  757. std::string message;
  758. if(!cmSystemTools::SameFile(fromFile.c_str(), toFile.c_str()))
  759. {
  760. if(itype == cmTarget::INSTALL_DIRECTORY &&
  761. cmSystemTools::FileIsDirectory(fromFile.c_str()))
  762. {
  763. // We will install this file. Display the information.
  764. message = "Installing ";
  765. message += toFile.c_str();
  766. this->Makefile->DisplayStatus(message.c_str(), -1);
  767. if(!cmSystemTools::CopyADirectory(fromFile.c_str(), toFile.c_str()))
  768. {
  769. cmOStringStream e;
  770. e << "INSTALL cannot copy directory \"" << fromFile
  771. << "\" to \"" << toFile + "\".";
  772. this->SetError(e.str().c_str());
  773. return false;
  774. }
  775. }
  776. else if(cmSystemTools::FileExists(fromFile.c_str()))
  777. {
  778. // We will install this file. Display the information.
  779. message = "Installing ";
  780. message += toFile.c_str();
  781. this->Makefile->DisplayStatus(message.c_str(), -1);
  782. // If no permissions were already given use the permissions of
  783. // the file being copied.
  784. if(!use_given_permissions &&
  785. (!use_source_permissions ||
  786. !cmSystemTools::GetPermissions(fromFile.c_str(), permissions)))
  787. {
  788. // Set default permissions.
  789. permissions = 0;
  790. permissions |= mode_owner_read;
  791. permissions |= mode_owner_write;
  792. permissions |= mode_group_read;
  793. permissions |= mode_world_read;
  794. }
  795. // Remove the original file and try copying the new file.
  796. // TODO: This should be copy-if-different. Don't forget to
  797. // edit the destination file permissions, or compare files
  798. // first. This would need a new SystemTools::FilesDiffer that
  799. // does not read all of the files at once.
  800. cmSystemTools::RemoveFile(toFile.c_str());
  801. if(!cmSystemTools::CopyFileAlways(fromFile.c_str(), toFile.c_str()))
  802. {
  803. cmOStringStream e;
  804. e << "INSTALL cannot copy file \"" << fromFile
  805. << "\" to \"" << toFile + "\".";
  806. this->SetError(e.str().c_str());
  807. return false;
  808. }
  809. // Perform post-installation processing on the file depending
  810. // on its type.
  811. #if defined(__APPLE_CC__)
  812. // Static libraries need ranlib on this platform.
  813. if(itype == cmTarget::STATIC_LIBRARY)
  814. {
  815. std::string ranlib = "ranlib ";
  816. ranlib += cmSystemTools::ConvertToOutputPath(toFile.c_str());
  817. if(!cmSystemTools::RunSingleCommand(ranlib.c_str()))
  818. {
  819. std::string err = "ranlib failed: ";
  820. err += ranlib;
  821. this->SetError(err.c_str());
  822. return false;
  823. }
  824. }
  825. #endif
  826. // Set permissions of the destination file.
  827. if(!cmSystemTools::SetPermissions(toFile.c_str(), permissions))
  828. {
  829. cmOStringStream e;
  830. e << "Problem setting permissions on file \""
  831. << toFile.c_str() << "\"";
  832. this->SetError(e.str().c_str());
  833. return false;
  834. }
  835. // Add the file to the manifest.
  836. smanifest_files += ";";
  837. smanifest_files += toFile.substr(destDirLength);
  838. }
  839. else if(!optional)
  840. {
  841. // The input file does not exist and installation is not optional.
  842. cmOStringStream e;
  843. e << "INSTALL cannot find file \"" << fromFile << "\" to install.";
  844. this->SetError(e.str().c_str());
  845. return false;
  846. }
  847. }
  848. }
  849. // Save the updated install manifest.
  850. this->Makefile->AddDefinition("CMAKE_INSTALL_MANIFEST_FILES",
  851. smanifest_files.c_str());
  852. return true;
  853. }
  854. //----------------------------------------------------------------------------
  855. bool cmFileCommand::HandleRelativePathCommand(
  856. std::vector<std::string> const& args)
  857. {
  858. if(args.size() != 4 )
  859. {
  860. this->SetError("called with incorrect number of arguments");
  861. return false;
  862. }
  863. const std::string& outVar = args[1];
  864. const std::string& directoryName = args[2];
  865. const std::string& fileName = args[3];
  866. if(!cmSystemTools::FileIsFullPath(directoryName.c_str()))
  867. {
  868. std::string errstring = "RelativePath must be passed a full path to the directory: " + directoryName;
  869. this->SetError(errstring.c_str());
  870. return false;
  871. }
  872. if(!cmSystemTools::FileIsFullPath(fileName.c_str()))
  873. {
  874. std::string errstring = "RelativePath must be passed a full path to the directory: " + directoryName;
  875. this->SetError(errstring.c_str());
  876. return false;
  877. }
  878. std::string res = cmSystemTools::RelativePath(directoryName.c_str(), fileName.c_str());
  879. this->Makefile->AddDefinition(outVar.c_str(),
  880. res.c_str());
  881. return true;
  882. }
  883. //----------------------------------------------------------------------------
  884. bool cmFileCommand::HandleRemove(std::vector<std::string> const& args,
  885. bool recurse)
  886. {
  887. std::string message;
  888. std::vector<std::string>::const_iterator i = args.begin();
  889. i++; // Get rid of subcommand
  890. for(;i != args.end(); ++i)
  891. {
  892. if(cmSystemTools::FileIsDirectory(i->c_str()) && recurse)
  893. {
  894. cmSystemTools::RemoveADirectory(i->c_str());
  895. }
  896. else
  897. {
  898. cmSystemTools::RemoveFile(i->c_str());
  899. }
  900. }
  901. return true;
  902. }
  903. //----------------------------------------------------------------------------
  904. bool cmFileCommand::HandleSystemPathCommand(std::vector<std::string>
  905. const& args)
  906. {
  907. std::vector<std::string>::const_iterator i = args.begin();
  908. if(args.size() != 3)
  909. {
  910. this->SetError("FILE(SYSTEM_PATH ENV result) must be called with "
  911. "only three arguments.");
  912. return false;
  913. }
  914. i++; // Get rid of subcommand
  915. std::vector<std::string> path;
  916. cmSystemTools::GetPath(path, i->c_str());
  917. i++;
  918. const char* var = i->c_str();
  919. std::string value;
  920. for(std::vector<std::string>::iterator j = path.begin();
  921. j != path.end(); ++j)
  922. {
  923. value += *j;
  924. value += ";";
  925. }
  926. this->Makefile->AddDefinition(var, value.c_str());
  927. return true;
  928. }