cmInstallTargetGenerator.cxx 28 KB

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