cmInstallTargetGenerator.cxx 27 KB

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