cmInstallTargetGenerator.cxx 28 KB

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