cmInstallTargetGenerator.cxx 28 KB

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