cmInstallTargetGenerator.cxx 27 KB

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