cmInstallTargetGenerator.cxx 26 KB

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