cmInstallTargetGenerator.cxx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmInstallTargetGenerator.h"
  11. #include "cmComputeLinkInformation.h"
  12. #include "cmGlobalGenerator.h"
  13. #include "cmLocalGenerator.h"
  14. #include "cmMakefile.h"
  15. #include "cmake.h"
  16. #include <assert.h>
  17. //----------------------------------------------------------------------------
  18. cmInstallTargetGenerator
  19. ::cmInstallTargetGenerator(cmTarget& t, const char* dest, bool implib,
  20. const char* file_permissions,
  21. std::vector<std::string> const& configurations,
  22. const char* component, bool optional):
  23. cmInstallGenerator(dest, configurations, component), Target(&t),
  24. ImportLibrary(implib), FilePermissions(file_permissions), Optional(optional)
  25. {
  26. this->ActionsPerConfig = true;
  27. this->NamelinkMode = NamelinkModeNone;
  28. this->Target->SetHaveInstallRule(true);
  29. }
  30. //----------------------------------------------------------------------------
  31. cmInstallTargetGenerator
  32. ::~cmInstallTargetGenerator()
  33. {
  34. }
  35. //----------------------------------------------------------------------------
  36. void cmInstallTargetGenerator::GenerateScript(std::ostream& os)
  37. {
  38. // Warn if installing an exclude-from-all target.
  39. if(this->Target->GetPropertyAsBool("EXCLUDE_FROM_ALL"))
  40. {
  41. cmOStringStream msg;
  42. msg << "WARNING: Target \"" << this->Target->GetName()
  43. << "\" has EXCLUDE_FROM_ALL set and will not be built by default "
  44. << "but an install rule has been provided for it. CMake does "
  45. << "not define behavior for this case.";
  46. cmSystemTools::Message(msg.str().c_str(), "Warning");
  47. }
  48. // Perform the main install script generation.
  49. this->cmInstallGenerator::GenerateScript(os);
  50. }
  51. //----------------------------------------------------------------------------
  52. void cmInstallTargetGenerator::GenerateScriptForConfig(std::ostream& os,
  53. const char* config,
  54. Indent const& indent)
  55. {
  56. // Compute the build tree directory from which to copy the target.
  57. std::string fromDirConfig;
  58. if(this->Target->NeedRelinkBeforeInstall(config))
  59. {
  60. fromDirConfig = this->Target->GetMakefile()->GetStartOutputDirectory();
  61. fromDirConfig += cmake::GetCMakeFilesDirectory();
  62. fromDirConfig += "/CMakeRelink.dir/";
  63. }
  64. else
  65. {
  66. fromDirConfig = this->Target->GetDirectory(config, this->ImportLibrary);
  67. fromDirConfig += "/";
  68. }
  69. std::string toDir = this->GetInstallDestination();
  70. toDir += "/";
  71. // Compute the list of files to install for this target.
  72. std::vector<std::string> filesFrom;
  73. std::vector<std::string> filesTo;
  74. std::string literal_args;
  75. cmTarget::TargetType targetType = this->Target->GetType();
  76. cmInstallType type = cmInstallType();
  77. switch(targetType)
  78. {
  79. case cmTarget::EXECUTABLE: type = cmInstallType_EXECUTABLE; break;
  80. case cmTarget::STATIC_LIBRARY: type = cmInstallType_STATIC_LIBRARY; break;
  81. case cmTarget::SHARED_LIBRARY: type = cmInstallType_SHARED_LIBRARY; break;
  82. case cmTarget::MODULE_LIBRARY: type = cmInstallType_MODULE_LIBRARY; break;
  83. case cmTarget::INTERFACE_LIBRARY:
  84. // Not reachable. We never create a cmInstallTargetGenerator for
  85. // an INTERFACE_LIBRARY.
  86. assert(!"INTERFACE_LIBRARY targets have no installable outputs.");
  87. break;
  88. case cmTarget::OBJECT_LIBRARY:
  89. case cmTarget::UTILITY:
  90. case cmTarget::GLOBAL_TARGET:
  91. case cmTarget::UNKNOWN_LIBRARY:
  92. this->Target->GetMakefile()->IssueMessage(cmake::INTERNAL_ERROR,
  93. "cmInstallTargetGenerator created with non-installable target.");
  94. return;
  95. }
  96. if(targetType == cmTarget::EXECUTABLE)
  97. {
  98. // There is a bug in cmInstallCommand if this fails.
  99. assert(this->NamelinkMode == NamelinkModeNone);
  100. std::string targetName;
  101. std::string targetNameReal;
  102. std::string targetNameImport;
  103. std::string targetNamePDB;
  104. this->Target->GetExecutableNames(targetName, targetNameReal,
  105. targetNameImport, targetNamePDB,
  106. config);
  107. if(this->ImportLibrary)
  108. {
  109. std::string from1 = fromDirConfig + targetNameImport;
  110. std::string to1 = toDir + targetNameImport;
  111. filesFrom.push_back(from1);
  112. filesTo.push_back(to1);
  113. std::string targetNameImportLib;
  114. if(this->Target->GetImplibGNUtoMS(targetNameImport,
  115. targetNameImportLib))
  116. {
  117. filesFrom.push_back(fromDirConfig + targetNameImportLib);
  118. filesTo.push_back(toDir + targetNameImportLib);
  119. }
  120. // An import library looks like a static library.
  121. type = cmInstallType_STATIC_LIBRARY;
  122. }
  123. else
  124. {
  125. std::string from1 = fromDirConfig + targetName;
  126. std::string to1 = toDir + targetName;
  127. // Handle OSX Bundles.
  128. if(this->Target->IsAppBundleOnApple())
  129. {
  130. // Install the whole app bundle directory.
  131. type = cmInstallType_DIRECTORY;
  132. literal_args += " USE_SOURCE_PERMISSIONS";
  133. from1 += ".app";
  134. // Tweaks apply to the binary inside the bundle.
  135. to1 += ".app/Contents/MacOS/";
  136. to1 += targetName;
  137. }
  138. else
  139. {
  140. // Tweaks apply to the real file, so list it first.
  141. if(targetNameReal != targetName)
  142. {
  143. std::string from2 = fromDirConfig + targetNameReal;
  144. std::string to2 = toDir += targetNameReal;
  145. filesFrom.push_back(from2);
  146. filesTo.push_back(to2);
  147. }
  148. }
  149. filesFrom.push_back(from1);
  150. filesTo.push_back(to1);
  151. }
  152. }
  153. else
  154. {
  155. std::string targetName;
  156. std::string targetNameSO;
  157. std::string targetNameReal;
  158. std::string targetNameImport;
  159. std::string targetNamePDB;
  160. this->Target->GetLibraryNames(targetName, targetNameSO, targetNameReal,
  161. targetNameImport, targetNamePDB,
  162. config);
  163. if(this->ImportLibrary)
  164. {
  165. // There is a bug in cmInstallCommand if this fails.
  166. assert(this->NamelinkMode == NamelinkModeNone);
  167. std::string from1 = fromDirConfig + targetNameImport;
  168. std::string to1 = toDir + targetNameImport;
  169. filesFrom.push_back(from1);
  170. filesTo.push_back(to1);
  171. std::string targetNameImportLib;
  172. if(this->Target->GetImplibGNUtoMS(targetNameImport,
  173. targetNameImportLib))
  174. {
  175. filesFrom.push_back(fromDirConfig + targetNameImportLib);
  176. filesTo.push_back(toDir + targetNameImportLib);
  177. }
  178. // An import library looks like a static library.
  179. type = cmInstallType_STATIC_LIBRARY;
  180. }
  181. else if(this->Target->IsFrameworkOnApple())
  182. {
  183. // There is a bug in cmInstallCommand if this fails.
  184. assert(this->NamelinkMode == NamelinkModeNone);
  185. // Install the whole framework directory.
  186. type = cmInstallType_DIRECTORY;
  187. literal_args += " USE_SOURCE_PERMISSIONS";
  188. std::string from1 = fromDirConfig + targetName;
  189. from1 = cmSystemTools::GetFilenamePath(from1);
  190. // Tweaks apply to the binary inside the bundle.
  191. std::string to1 = toDir + targetNameReal;
  192. filesFrom.push_back(from1);
  193. filesTo.push_back(to1);
  194. }
  195. else if(this->Target->IsCFBundleOnApple())
  196. {
  197. // Install the whole app bundle directory.
  198. type = cmInstallType_DIRECTORY;
  199. literal_args += " USE_SOURCE_PERMISSIONS";
  200. std::string targetNameBase = targetName.substr(0, targetName.find('/'));
  201. std::string from1 = fromDirConfig + targetNameBase;
  202. std::string to1 = toDir + targetName;
  203. filesFrom.push_back(from1);
  204. filesTo.push_back(to1);
  205. }
  206. else
  207. {
  208. bool haveNamelink = false;
  209. // Library link name.
  210. std::string fromName = fromDirConfig + targetName;
  211. std::string toName = toDir + targetName;
  212. // Library interface name.
  213. std::string fromSOName;
  214. std::string toSOName;
  215. if(targetNameSO != targetName)
  216. {
  217. haveNamelink = true;
  218. fromSOName = fromDirConfig + targetNameSO;
  219. toSOName = toDir + targetNameSO;
  220. }
  221. // Library implementation name.
  222. std::string fromRealName;
  223. std::string toRealName;
  224. if(targetNameReal != targetName &&
  225. targetNameReal != targetNameSO)
  226. {
  227. haveNamelink = true;
  228. fromRealName = fromDirConfig + targetNameReal;
  229. toRealName = toDir + targetNameReal;
  230. }
  231. // Add the names based on the current namelink mode.
  232. if(haveNamelink)
  233. {
  234. // With a namelink we need to check the mode.
  235. if(this->NamelinkMode == NamelinkModeOnly)
  236. {
  237. // Install the namelink only.
  238. filesFrom.push_back(fromName);
  239. filesTo.push_back(toName);
  240. }
  241. else
  242. {
  243. // Install the real file if it has its own name.
  244. if(!fromRealName.empty())
  245. {
  246. filesFrom.push_back(fromRealName);
  247. filesTo.push_back(toRealName);
  248. }
  249. // Install the soname link if it has its own name.
  250. if(!fromSOName.empty())
  251. {
  252. filesFrom.push_back(fromSOName);
  253. filesTo.push_back(toSOName);
  254. }
  255. // Install the namelink if it is not to be skipped.
  256. if(this->NamelinkMode != NamelinkModeSkip)
  257. {
  258. filesFrom.push_back(fromName);
  259. filesTo.push_back(toName);
  260. }
  261. }
  262. }
  263. else
  264. {
  265. // Without a namelink there will be only one file. Install it
  266. // if this is not a namelink-only rule.
  267. if(this->NamelinkMode != NamelinkModeOnly)
  268. {
  269. filesFrom.push_back(fromName);
  270. filesTo.push_back(toName);
  271. }
  272. }
  273. }
  274. }
  275. // If this fails the above code is buggy.
  276. assert(filesFrom.size() == filesTo.size());
  277. // Skip this rule if no files are to be installed for the target.
  278. if(filesFrom.empty())
  279. {
  280. return;
  281. }
  282. // Add pre-installation tweaks.
  283. this->AddTweak(os, indent, config, filesTo,
  284. &cmInstallTargetGenerator::PreReplacementTweaks);
  285. // Write code to install the target file.
  286. const char* no_dir_permissions = 0;
  287. const char* no_rename = 0;
  288. bool optional = this->Optional || this->ImportLibrary;
  289. this->AddInstallRule(os, type, filesFrom,
  290. optional,
  291. this->FilePermissions.c_str(), no_dir_permissions,
  292. no_rename, literal_args.c_str(),
  293. indent);
  294. // Add post-installation tweaks.
  295. this->AddTweak(os, indent, config, filesTo,
  296. &cmInstallTargetGenerator::PostReplacementTweaks);
  297. }
  298. //----------------------------------------------------------------------------
  299. std::string
  300. cmInstallTargetGenerator::GetInstallFilename(const char* config) const
  301. {
  302. NameType nameType = this->ImportLibrary? NameImplib : NameNormal;
  303. return
  304. cmInstallTargetGenerator::GetInstallFilename(this->Target, config,
  305. nameType);
  306. }
  307. //----------------------------------------------------------------------------
  308. std::string
  309. cmInstallTargetGenerator::GetInstallFilename(cmTarget const* target,
  310. const char* config,
  311. NameType nameType)
  312. {
  313. std::string fname;
  314. // Compute the name of the library.
  315. if(target->GetType() == cmTarget::EXECUTABLE)
  316. {
  317. std::string targetName;
  318. std::string targetNameReal;
  319. std::string targetNameImport;
  320. std::string targetNamePDB;
  321. target->GetExecutableNames(targetName, targetNameReal,
  322. targetNameImport, targetNamePDB,
  323. config);
  324. if(nameType == NameImplib)
  325. {
  326. // Use the import library name.
  327. if(!target->GetImplibGNUtoMS(targetNameImport, fname,
  328. "${CMAKE_IMPORT_LIBRARY_SUFFIX}"))
  329. {
  330. fname = targetNameImport;
  331. }
  332. }
  333. else if(nameType == NameReal)
  334. {
  335. // Use the canonical name.
  336. fname = targetNameReal;
  337. }
  338. else
  339. {
  340. // Use the canonical name.
  341. fname = targetName;
  342. }
  343. }
  344. else
  345. {
  346. std::string targetName;
  347. std::string targetNameSO;
  348. std::string targetNameReal;
  349. std::string targetNameImport;
  350. std::string targetNamePDB;
  351. target->GetLibraryNames(targetName, targetNameSO, targetNameReal,
  352. targetNameImport, targetNamePDB, config);
  353. if(nameType == NameImplib)
  354. {
  355. // Use the import library name.
  356. if(!target->GetImplibGNUtoMS(targetNameImport, fname,
  357. "${CMAKE_IMPORT_LIBRARY_SUFFIX}"))
  358. {
  359. fname = targetNameImport;
  360. }
  361. }
  362. else if(nameType == NameSO)
  363. {
  364. // Use the soname.
  365. fname = targetNameSO;
  366. }
  367. else if(nameType == NameReal)
  368. {
  369. // Use the real name.
  370. fname = targetNameReal;
  371. }
  372. else
  373. {
  374. // Use the canonical name.
  375. fname = targetName;
  376. }
  377. }
  378. return fname;
  379. }
  380. //----------------------------------------------------------------------------
  381. void
  382. cmInstallTargetGenerator
  383. ::AddTweak(std::ostream& os, Indent const& indent, const char* config,
  384. std::string const& file, TweakMethod tweak)
  385. {
  386. cmOStringStream tw;
  387. (this->*tweak)(tw, indent.Next(), config, file);
  388. std::string tws = tw.str();
  389. if(!tws.empty())
  390. {
  391. os << indent << "if(EXISTS \"" << file << "\" AND\n"
  392. << indent << " NOT IS_SYMLINK \"" << file << "\")\n";
  393. os << tws;
  394. os << indent << "endif()\n";
  395. }
  396. }
  397. //----------------------------------------------------------------------------
  398. void
  399. cmInstallTargetGenerator
  400. ::AddTweak(std::ostream& os, Indent const& indent, const char* config,
  401. std::vector<std::string> const& files, TweakMethod tweak)
  402. {
  403. if(files.size() == 1)
  404. {
  405. // Tweak a single file.
  406. this->AddTweak(os, indent, config, this->GetDestDirPath(files[0]), tweak);
  407. }
  408. else
  409. {
  410. // Generate a foreach loop to tweak multiple files.
  411. cmOStringStream tw;
  412. this->AddTweak(tw, indent.Next(), config, "${file}", tweak);
  413. std::string tws = tw.str();
  414. if(!tws.empty())
  415. {
  416. Indent indent2 = indent.Next().Next();
  417. os << indent << "foreach(file\n";
  418. for(std::vector<std::string>::const_iterator i = files.begin();
  419. i != files.end(); ++i)
  420. {
  421. os << indent2 << "\"" << this->GetDestDirPath(*i) << "\"\n";
  422. }
  423. os << indent2 << ")\n";
  424. os << tws;
  425. os << indent << "endforeach()\n";
  426. }
  427. }
  428. }
  429. //----------------------------------------------------------------------------
  430. std::string cmInstallTargetGenerator::GetDestDirPath(std::string const& file)
  431. {
  432. // Construct the path of the file on disk after installation on
  433. // which tweaks may be performed.
  434. std::string toDestDirPath = "$ENV{DESTDIR}";
  435. if(file[0] != '/' && file[0] != '$')
  436. {
  437. toDestDirPath += "/";
  438. }
  439. toDestDirPath += file;
  440. return toDestDirPath;
  441. }
  442. //----------------------------------------------------------------------------
  443. void cmInstallTargetGenerator::PreReplacementTweaks(std::ostream& os,
  444. Indent const& indent,
  445. const char* config,
  446. std::string const& file)
  447. {
  448. this->AddRPathCheckRule(os, indent, config, file);
  449. }
  450. //----------------------------------------------------------------------------
  451. void cmInstallTargetGenerator::PostReplacementTweaks(std::ostream& os,
  452. Indent const& indent,
  453. const char* config,
  454. std::string const& file)
  455. {
  456. this->AddInstallNamePatchRule(os, indent, config, file);
  457. this->AddChrpathPatchRule(os, indent, config, file);
  458. this->AddRanlibRule(os, indent, file);
  459. this->AddStripRule(os, indent, file);
  460. }
  461. //----------------------------------------------------------------------------
  462. void
  463. cmInstallTargetGenerator
  464. ::AddInstallNamePatchRule(std::ostream& os, Indent const& indent,
  465. const char* config, std::string const& toDestDirPath)
  466. {
  467. if(this->ImportLibrary ||
  468. !(this->Target->GetType() == cmTarget::SHARED_LIBRARY ||
  469. this->Target->GetType() == cmTarget::MODULE_LIBRARY ||
  470. this->Target->GetType() == cmTarget::EXECUTABLE))
  471. {
  472. return;
  473. }
  474. // Fix the install_name settings in installed binaries.
  475. std::string installNameTool =
  476. this->Target->GetMakefile()->GetSafeDefinition("CMAKE_INSTALL_NAME_TOOL");
  477. if(!installNameTool.size())
  478. {
  479. return;
  480. }
  481. // Build a map of build-tree install_name to install-tree install_name for
  482. // shared libraries linked to this target.
  483. std::map<cmStdString, cmStdString> install_name_remap;
  484. if(cmComputeLinkInformation* cli = this->Target->GetLinkInformation(config))
  485. {
  486. std::set<cmTarget const*> const& sharedLibs
  487. = cli->GetSharedLibrariesLinked();
  488. for(std::set<cmTarget const*>::const_iterator j = sharedLibs.begin();
  489. j != sharedLibs.end(); ++j)
  490. {
  491. cmTarget const* tgt = *j;
  492. // The install_name of an imported target does not change.
  493. if(tgt->IsImported())
  494. {
  495. continue;
  496. }
  497. // If the build tree and install tree use different path
  498. // components of the install_name field then we need to create a
  499. // mapping to be applied after installation.
  500. std::string for_build = tgt->GetInstallNameDirForBuildTree(config);
  501. std::string for_install = tgt->GetInstallNameDirForInstallTree();
  502. if(for_build != for_install)
  503. {
  504. // The directory portions differ. Append the filename to
  505. // create the mapping.
  506. std::string fname =
  507. this->GetInstallFilename(tgt, config, NameSO);
  508. // Map from the build-tree install_name.
  509. for_build += fname;
  510. // Map to the install-tree install_name.
  511. for_install += fname;
  512. // Store the mapping entry.
  513. install_name_remap[for_build] = for_install;
  514. }
  515. }
  516. }
  517. // Edit the install_name of the target itself if necessary.
  518. std::string new_id;
  519. if(this->Target->GetType() == cmTarget::SHARED_LIBRARY)
  520. {
  521. std::string for_build =
  522. this->Target->GetInstallNameDirForBuildTree(config);
  523. std::string for_install =
  524. this->Target->GetInstallNameDirForInstallTree();
  525. if(this->Target->IsFrameworkOnApple() && for_install.empty())
  526. {
  527. // Frameworks seem to have an id corresponding to their own full
  528. // path.
  529. // ...
  530. // for_install = fullDestPath_without_DESTDIR_or_name;
  531. }
  532. // If the install name will change on installation set the new id
  533. // on the installed file.
  534. if(for_build != for_install)
  535. {
  536. // Prepare to refer to the install-tree install_name.
  537. new_id = for_install;
  538. new_id += this->GetInstallFilename(this->Target, config, NameSO);
  539. }
  540. }
  541. // Write a rule to run install_name_tool to set the install-tree
  542. // install_name value and references.
  543. if(!new_id.empty() || !install_name_remap.empty())
  544. {
  545. os << indent << "execute_process(COMMAND \"" << installNameTool;
  546. os << "\"";
  547. if(!new_id.empty())
  548. {
  549. os << "\n" << indent << " -id \"" << new_id << "\"";
  550. }
  551. for(std::map<cmStdString, cmStdString>::const_iterator
  552. i = install_name_remap.begin();
  553. i != install_name_remap.end(); ++i)
  554. {
  555. os << "\n" << indent << " -change \""
  556. << i->first << "\" \"" << i->second << "\"";
  557. }
  558. os << "\n" << indent << " \"" << toDestDirPath << "\")\n";
  559. }
  560. }
  561. //----------------------------------------------------------------------------
  562. void
  563. cmInstallTargetGenerator
  564. ::AddRPathCheckRule(std::ostream& os, Indent const& indent,
  565. const char* config, std::string const& toDestDirPath)
  566. {
  567. // Skip the chrpath if the target does not need it.
  568. if(this->ImportLibrary || !this->Target->IsChrpathUsed(config))
  569. {
  570. return;
  571. }
  572. // Skip if on Apple
  573. if(this->Target->GetMakefile()->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME"))
  574. {
  575. return;
  576. }
  577. // Get the link information for this target.
  578. // It can provide the RPATH.
  579. cmComputeLinkInformation* cli = this->Target->GetLinkInformation(config);
  580. if(!cli)
  581. {
  582. return;
  583. }
  584. // Get the install RPATH from the link information.
  585. std::string newRpath = cli->GetChrpathString();
  586. // Write a rule to remove the installed file if its rpath is not the
  587. // new rpath. This is needed for existing build/install trees when
  588. // the installed rpath changes but the file is not rebuilt.
  589. os << indent << "file(RPATH_CHECK\n"
  590. << indent << " FILE \"" << toDestDirPath << "\"\n"
  591. << indent << " RPATH \"" << newRpath << "\")\n";
  592. }
  593. //----------------------------------------------------------------------------
  594. void
  595. cmInstallTargetGenerator
  596. ::AddChrpathPatchRule(std::ostream& os, Indent const& indent,
  597. const char* config, std::string const& toDestDirPath)
  598. {
  599. // Skip the chrpath if the target does not need it.
  600. if(this->ImportLibrary || !this->Target->IsChrpathUsed(config))
  601. {
  602. return;
  603. }
  604. // Get the link information for this target.
  605. // It can provide the RPATH.
  606. cmComputeLinkInformation* cli = this->Target->GetLinkInformation(config);
  607. if(!cli)
  608. {
  609. return;
  610. }
  611. if(this->Target->GetMakefile()->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME"))
  612. {
  613. // If using install_name_tool, set up the rules to modify the rpaths.
  614. std::string installNameTool =
  615. this->Target->GetMakefile()->
  616. GetSafeDefinition("CMAKE_INSTALL_NAME_TOOL");
  617. std::vector<std::string> oldRuntimeDirs, newRuntimeDirs;
  618. cli->GetRPath(oldRuntimeDirs, false);
  619. cli->GetRPath(newRuntimeDirs, true);
  620. // Note: These paths are kept unique to avoid install_name_tool corruption.
  621. std::set<std::string> runpaths;
  622. for(std::vector<std::string>::const_iterator i = oldRuntimeDirs.begin();
  623. i != oldRuntimeDirs.end(); ++i)
  624. {
  625. std::string runpath = this->Target->GetMakefile()->GetLocalGenerator()->
  626. GetGlobalGenerator()->ExpandCFGIntDir(*i, config);
  627. if(runpaths.find(runpath) == runpaths.end())
  628. {
  629. runpaths.insert(runpath);
  630. os << indent << "execute_process(COMMAND " << installNameTool << "\n";
  631. os << indent << " -delete_rpath \"" << runpath << "\"\n";
  632. os << indent << " \"" << toDestDirPath << "\")\n";
  633. }
  634. }
  635. runpaths.clear();
  636. for(std::vector<std::string>::const_iterator i = newRuntimeDirs.begin();
  637. i != newRuntimeDirs.end(); ++i)
  638. {
  639. std::string runpath = this->Target->GetMakefile()->GetLocalGenerator()->
  640. GetGlobalGenerator()->ExpandCFGIntDir(*i, config);
  641. if(runpaths.find(runpath) == runpaths.end())
  642. {
  643. os << indent << "execute_process(COMMAND " << installNameTool << "\n";
  644. os << indent << " -add_rpath \"" << runpath << "\"\n";
  645. os << indent << " \"" << toDestDirPath << "\")\n";
  646. }
  647. }
  648. }
  649. else
  650. {
  651. // Construct the original rpath string to be replaced.
  652. std::string oldRpath = cli->GetRPathString(false);
  653. // Get the install RPATH from the link information.
  654. std::string newRpath = cli->GetChrpathString();
  655. // Skip the rule if the paths are identical
  656. if(oldRpath == newRpath)
  657. {
  658. return;
  659. }
  660. // Write a rule to run chrpath to set the install-tree RPATH
  661. if(newRpath.empty())
  662. {
  663. os << indent << "file(RPATH_REMOVE\n"
  664. << indent << " FILE \"" << toDestDirPath << "\")\n";
  665. }
  666. else
  667. {
  668. os << indent << "file(RPATH_CHANGE\n"
  669. << indent << " FILE \"" << toDestDirPath << "\"\n"
  670. << indent << " OLD_RPATH \"" << oldRpath << "\"\n"
  671. << indent << " NEW_RPATH \"" << newRpath << "\")\n";
  672. }
  673. }
  674. }
  675. //----------------------------------------------------------------------------
  676. void
  677. cmInstallTargetGenerator::AddStripRule(std::ostream& os,
  678. Indent const& indent,
  679. const std::string& toDestDirPath)
  680. {
  681. // don't strip static and import libraries, because it removes the only
  682. // symbol table they have so you can't link to them anymore
  683. if(this->Target->GetType()==cmTarget::STATIC_LIBRARY || this->ImportLibrary)
  684. {
  685. return;
  686. }
  687. // Don't handle OSX Bundles.
  688. if(this->Target->GetMakefile()->IsOn("APPLE") &&
  689. this->Target->GetPropertyAsBool("MACOSX_BUNDLE"))
  690. {
  691. return;
  692. }
  693. if(! this->Target->GetMakefile()->IsSet("CMAKE_STRIP"))
  694. {
  695. return;
  696. }
  697. os << indent << "if(CMAKE_INSTALL_DO_STRIP)\n";
  698. os << indent << " execute_process(COMMAND \""
  699. << this->Target->GetMakefile()->GetDefinition("CMAKE_STRIP")
  700. << "\" \"" << toDestDirPath << "\")\n";
  701. os << indent << "endif()\n";
  702. }
  703. //----------------------------------------------------------------------------
  704. void
  705. cmInstallTargetGenerator::AddRanlibRule(std::ostream& os,
  706. Indent const& indent,
  707. const std::string& toDestDirPath)
  708. {
  709. // Static libraries need ranlib on this platform.
  710. if(this->Target->GetType() != cmTarget::STATIC_LIBRARY)
  711. {
  712. return;
  713. }
  714. // Perform post-installation processing on the file depending
  715. // on its type.
  716. if(!this->Target->GetMakefile()->IsOn("APPLE"))
  717. {
  718. return;
  719. }
  720. std::string ranlib =
  721. this->Target->GetMakefile()->GetRequiredDefinition("CMAKE_RANLIB");
  722. if(ranlib.empty())
  723. {
  724. return;
  725. }
  726. os << indent << "execute_process(COMMAND \""
  727. << ranlib << "\" \"" << toDestDirPath << "\")\n";
  728. }