cmInstallTargetGenerator.cxx 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  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->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->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->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->Target, config,
  326. nameType);
  327. }
  328. //----------------------------------------------------------------------------
  329. std::string
  330. cmInstallTargetGenerator::GetInstallFilename(cmTarget const* target,
  331. const std::string& config,
  332. NameType nameType)
  333. {
  334. std::string fname;
  335. // Compute the name of the library.
  336. cmGeneratorTarget *gtgt = target->GetMakefile()
  337. ->GetGlobalGenerator()
  338. ->GetGeneratorTarget(target);
  339. if(target->GetType() == cmState::EXECUTABLE)
  340. {
  341. std::string targetName;
  342. std::string targetNameReal;
  343. std::string targetNameImport;
  344. std::string targetNamePDB;
  345. gtgt->GetExecutableNames(targetName, targetNameReal,
  346. targetNameImport, targetNamePDB,
  347. config);
  348. if(nameType == NameImplib)
  349. {
  350. // Use the import library name.
  351. if(!gtgt->GetImplibGNUtoMS(targetNameImport, fname,
  352. "${CMAKE_IMPORT_LIBRARY_SUFFIX}"))
  353. {
  354. fname = targetNameImport;
  355. }
  356. }
  357. else if(nameType == NameReal)
  358. {
  359. // Use the canonical name.
  360. fname = targetNameReal;
  361. }
  362. else
  363. {
  364. // Use the canonical name.
  365. fname = targetName;
  366. }
  367. }
  368. else
  369. {
  370. std::string targetName;
  371. std::string targetNameSO;
  372. std::string targetNameReal;
  373. std::string targetNameImport;
  374. std::string targetNamePDB;
  375. gtgt->GetLibraryNames(targetName, targetNameSO, targetNameReal,
  376. targetNameImport, targetNamePDB, config);
  377. if(nameType == NameImplib)
  378. {
  379. // Use the import library name.
  380. if(!gtgt->GetImplibGNUtoMS(targetNameImport, fname,
  381. "${CMAKE_IMPORT_LIBRARY_SUFFIX}"))
  382. {
  383. fname = targetNameImport;
  384. }
  385. }
  386. else if(nameType == NameSO)
  387. {
  388. // Use the soname.
  389. fname = targetNameSO;
  390. }
  391. else if(nameType == NameReal)
  392. {
  393. // Use the real name.
  394. fname = targetNameReal;
  395. }
  396. else
  397. {
  398. // Use the canonical name.
  399. fname = targetName;
  400. }
  401. }
  402. return fname;
  403. }
  404. void cmInstallTargetGenerator::Compute(cmLocalGenerator* lg)
  405. {
  406. this->Target = lg->GetGlobalGenerator()->GetGeneratorTarget(
  407. lg->GetMakefile()->FindTarget(this->TargetName));
  408. }
  409. //----------------------------------------------------------------------------
  410. void
  411. cmInstallTargetGenerator
  412. ::AddTweak(std::ostream& os, Indent const& indent, const std::string& config,
  413. std::string const& file, TweakMethod tweak)
  414. {
  415. std::ostringstream tw;
  416. (this->*tweak)(tw, indent.Next(), config, file);
  417. std::string tws = tw.str();
  418. if(!tws.empty())
  419. {
  420. os << indent << "if(EXISTS \"" << file << "\" AND\n"
  421. << indent << " NOT IS_SYMLINK \"" << file << "\")\n";
  422. os << tws;
  423. os << indent << "endif()\n";
  424. }
  425. }
  426. //----------------------------------------------------------------------------
  427. void
  428. cmInstallTargetGenerator
  429. ::AddTweak(std::ostream& os, Indent const& indent, const std::string& config,
  430. std::vector<std::string> const& files, TweakMethod tweak)
  431. {
  432. if(files.size() == 1)
  433. {
  434. // Tweak a single file.
  435. this->AddTweak(os, indent, config, this->GetDestDirPath(files[0]), tweak);
  436. }
  437. else
  438. {
  439. // Generate a foreach loop to tweak multiple files.
  440. std::ostringstream tw;
  441. this->AddTweak(tw, indent.Next(), config, "${file}", tweak);
  442. std::string tws = tw.str();
  443. if(!tws.empty())
  444. {
  445. Indent indent2 = indent.Next().Next();
  446. os << indent << "foreach(file\n";
  447. for(std::vector<std::string>::const_iterator i = files.begin();
  448. i != files.end(); ++i)
  449. {
  450. os << indent2 << "\"" << this->GetDestDirPath(*i) << "\"\n";
  451. }
  452. os << indent2 << ")\n";
  453. os << tws;
  454. os << indent << "endforeach()\n";
  455. }
  456. }
  457. }
  458. //----------------------------------------------------------------------------
  459. std::string cmInstallTargetGenerator::GetDestDirPath(std::string const& file)
  460. {
  461. // Construct the path of the file on disk after installation on
  462. // which tweaks may be performed.
  463. std::string toDestDirPath = "$ENV{DESTDIR}";
  464. if(file[0] != '/' && file[0] != '$')
  465. {
  466. toDestDirPath += "/";
  467. }
  468. toDestDirPath += file;
  469. return toDestDirPath;
  470. }
  471. //----------------------------------------------------------------------------
  472. void cmInstallTargetGenerator::PreReplacementTweaks(std::ostream& os,
  473. Indent const& indent,
  474. const std::string& config,
  475. std::string const& file)
  476. {
  477. this->AddRPathCheckRule(os, indent, config, file);
  478. }
  479. //----------------------------------------------------------------------------
  480. void cmInstallTargetGenerator::PostReplacementTweaks(std::ostream& os,
  481. Indent const& indent,
  482. const std::string& config,
  483. std::string const& file)
  484. {
  485. this->AddInstallNamePatchRule(os, indent, config, file);
  486. this->AddChrpathPatchRule(os, indent, config, file);
  487. this->AddRanlibRule(os, indent, file);
  488. this->AddStripRule(os, indent, file);
  489. }
  490. //----------------------------------------------------------------------------
  491. void
  492. cmInstallTargetGenerator
  493. ::AddInstallNamePatchRule(std::ostream& os, Indent const& indent,
  494. const std::string& config,
  495. std::string const& toDestDirPath)
  496. {
  497. if(this->ImportLibrary ||
  498. !(this->Target->GetType() == cmState::SHARED_LIBRARY ||
  499. this->Target->GetType() == cmState::MODULE_LIBRARY ||
  500. this->Target->GetType() == cmState::EXECUTABLE))
  501. {
  502. return;
  503. }
  504. // Fix the install_name settings in installed binaries.
  505. std::string installNameTool = this->Target->Target->GetMakefile()
  506. ->GetSafeDefinition("CMAKE_INSTALL_NAME_TOOL");
  507. if(installNameTool.empty())
  508. {
  509. return;
  510. }
  511. // Build a map of build-tree install_name to install-tree install_name for
  512. // shared libraries linked to this target.
  513. std::map<std::string, std::string> install_name_remap;
  514. if(cmComputeLinkInformation* cli = this->Target->GetLinkInformation(config))
  515. {
  516. std::set<cmGeneratorTarget const*> const& sharedLibs
  517. = cli->GetSharedLibrariesLinked();
  518. for(std::set<cmGeneratorTarget const*>::const_iterator j
  519. = sharedLibs.begin(); j != sharedLibs.end(); ++j)
  520. {
  521. cmGeneratorTarget const* tgt = *j;
  522. // The install_name of an imported target does not change.
  523. if(tgt->IsImported())
  524. {
  525. continue;
  526. }
  527. // If the build tree and install tree use different path
  528. // components of the install_name field then we need to create a
  529. // mapping to be applied after installation.
  530. std::string for_build = tgt->GetInstallNameDirForBuildTree(config);
  531. std::string for_install = tgt->GetInstallNameDirForInstallTree();
  532. if(for_build != for_install)
  533. {
  534. // The directory portions differ. Append the filename to
  535. // create the mapping.
  536. std::string fname =
  537. this->GetInstallFilename(tgt->Target, config, NameSO);
  538. // Map from the build-tree install_name.
  539. for_build += fname;
  540. // Map to the install-tree install_name.
  541. for_install += fname;
  542. // Store the mapping entry.
  543. install_name_remap[for_build] = for_install;
  544. }
  545. }
  546. }
  547. // Edit the install_name of the target itself if necessary.
  548. std::string new_id;
  549. if(this->Target->GetType() == cmState::SHARED_LIBRARY)
  550. {
  551. std::string for_build =
  552. this->Target->GetInstallNameDirForBuildTree(config);
  553. std::string for_install =
  554. this->Target->GetInstallNameDirForInstallTree();
  555. if(this->Target->Target->IsFrameworkOnApple() && for_install.empty())
  556. {
  557. // Frameworks seem to have an id corresponding to their own full
  558. // path.
  559. // ...
  560. // for_install = fullDestPath_without_DESTDIR_or_name;
  561. }
  562. // If the install name will change on installation set the new id
  563. // on the installed file.
  564. if(for_build != for_install)
  565. {
  566. // Prepare to refer to the install-tree install_name.
  567. new_id = for_install;
  568. new_id += this->GetInstallFilename(this->Target->Target, config, NameSO);
  569. }
  570. }
  571. // Write a rule to run install_name_tool to set the install-tree
  572. // install_name value and references.
  573. if(!new_id.empty() || !install_name_remap.empty())
  574. {
  575. os << indent << "execute_process(COMMAND \"" << installNameTool;
  576. os << "\"";
  577. if(!new_id.empty())
  578. {
  579. os << "\n" << indent << " -id \"" << new_id << "\"";
  580. }
  581. for(std::map<std::string, std::string>::const_iterator
  582. i = install_name_remap.begin();
  583. i != install_name_remap.end(); ++i)
  584. {
  585. os << "\n" << indent << " -change \""
  586. << i->first << "\" \"" << i->second << "\"";
  587. }
  588. os << "\n" << indent << " \"" << toDestDirPath << "\")\n";
  589. }
  590. }
  591. //----------------------------------------------------------------------------
  592. void
  593. cmInstallTargetGenerator
  594. ::AddRPathCheckRule(std::ostream& os, Indent const& indent,
  595. const std::string& config,
  596. std::string const& toDestDirPath)
  597. {
  598. // Skip the chrpath if the target does not need it.
  599. if(this->ImportLibrary || !this->Target->IsChrpathUsed(config))
  600. {
  601. return;
  602. }
  603. // Skip if on Apple
  604. if(this->Target->Target->GetMakefile()
  605. ->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME"))
  606. {
  607. return;
  608. }
  609. // Get the link information for this target.
  610. // It can provide the RPATH.
  611. cmComputeLinkInformation* cli = this->Target->GetLinkInformation(config);
  612. if(!cli)
  613. {
  614. return;
  615. }
  616. // Get the install RPATH from the link information.
  617. std::string newRpath = cli->GetChrpathString();
  618. // Write a rule to remove the installed file if its rpath is not the
  619. // new rpath. This is needed for existing build/install trees when
  620. // the installed rpath changes but the file is not rebuilt.
  621. os << indent << "file(RPATH_CHECK\n"
  622. << indent << " FILE \"" << toDestDirPath << "\"\n"
  623. << indent << " RPATH \"" << newRpath << "\")\n";
  624. }
  625. //----------------------------------------------------------------------------
  626. void
  627. cmInstallTargetGenerator
  628. ::AddChrpathPatchRule(std::ostream& os, Indent const& indent,
  629. const std::string& config,
  630. std::string const& toDestDirPath)
  631. {
  632. // Skip the chrpath if the target does not need it.
  633. if(this->ImportLibrary || !this->Target->IsChrpathUsed(config))
  634. {
  635. return;
  636. }
  637. // Get the link information for this target.
  638. // It can provide the RPATH.
  639. cmComputeLinkInformation* cli = this->Target->GetLinkInformation(config);
  640. if(!cli)
  641. {
  642. return;
  643. }
  644. cmMakefile* mf = this->Target->Target->GetMakefile();
  645. if(mf->IsOn("CMAKE_PLATFORM_HAS_INSTALLNAME"))
  646. {
  647. // If using install_name_tool, set up the rules to modify the rpaths.
  648. std::string installNameTool =
  649. mf->GetSafeDefinition("CMAKE_INSTALL_NAME_TOOL");
  650. std::vector<std::string> oldRuntimeDirs, newRuntimeDirs;
  651. cli->GetRPath(oldRuntimeDirs, false);
  652. cli->GetRPath(newRuntimeDirs, true);
  653. std::string darwin_major_version_s =
  654. mf->GetSafeDefinition("DARWIN_MAJOR_VERSION");
  655. std::stringstream ss(darwin_major_version_s);
  656. int darwin_major_version;
  657. ss >> darwin_major_version;
  658. if(!ss.fail() && darwin_major_version <= 9 &&
  659. (!oldRuntimeDirs.empty() || !newRuntimeDirs.empty())
  660. )
  661. {
  662. std::ostringstream msg;
  663. msg << "WARNING: Target \"" << this->Target->GetName()
  664. << "\" has runtime paths which cannot be changed during install. "
  665. << "To change runtime paths, OS X version 10.6 or newer is required. "
  666. << "Therefore, runtime paths will not be changed when installing. "
  667. << "CMAKE_BUILD_WITH_INSTALL_RPATH may be used to work around"
  668. " this limitation.";
  669. mf->IssueMessage(cmake::WARNING, msg.str());
  670. }
  671. else
  672. {
  673. // Note: These paths are kept unique to avoid
  674. // install_name_tool corruption.
  675. std::set<std::string> runpaths;
  676. for(std::vector<std::string>::const_iterator i = oldRuntimeDirs.begin();
  677. i != oldRuntimeDirs.end(); ++i)
  678. {
  679. std::string runpath =
  680. mf->GetGlobalGenerator()->ExpandCFGIntDir(*i, config);
  681. if(runpaths.find(runpath) == runpaths.end())
  682. {
  683. runpaths.insert(runpath);
  684. os << indent << "execute_process(COMMAND " << installNameTool <<"\n";
  685. os << indent << " -delete_rpath \"" << runpath << "\"\n";
  686. os << indent << " \"" << toDestDirPath << "\")\n";
  687. }
  688. }
  689. runpaths.clear();
  690. for(std::vector<std::string>::const_iterator i = newRuntimeDirs.begin();
  691. i != newRuntimeDirs.end(); ++i)
  692. {
  693. std::string runpath =
  694. mf->GetGlobalGenerator()->ExpandCFGIntDir(*i, config);
  695. if(runpaths.find(runpath) == runpaths.end())
  696. {
  697. os << indent << "execute_process(COMMAND " << installNameTool <<"\n";
  698. os << indent << " -add_rpath \"" << runpath << "\"\n";
  699. os << indent << " \"" << toDestDirPath << "\")\n";
  700. }
  701. }
  702. }
  703. }
  704. else
  705. {
  706. // Construct the original rpath string to be replaced.
  707. std::string oldRpath = cli->GetRPathString(false);
  708. // Get the install RPATH from the link information.
  709. std::string newRpath = cli->GetChrpathString();
  710. // Skip the rule if the paths are identical
  711. if(oldRpath == newRpath)
  712. {
  713. return;
  714. }
  715. // Write a rule to run chrpath to set the install-tree RPATH
  716. if(newRpath.empty())
  717. {
  718. os << indent << "file(RPATH_REMOVE\n"
  719. << indent << " FILE \"" << toDestDirPath << "\")\n";
  720. }
  721. else
  722. {
  723. os << indent << "file(RPATH_CHANGE\n"
  724. << indent << " FILE \"" << toDestDirPath << "\"\n"
  725. << indent << " OLD_RPATH \"" << oldRpath << "\"\n"
  726. << indent << " NEW_RPATH \"" << newRpath << "\")\n";
  727. }
  728. }
  729. }
  730. //----------------------------------------------------------------------------
  731. void
  732. cmInstallTargetGenerator::AddStripRule(std::ostream& os,
  733. Indent const& indent,
  734. const std::string& toDestDirPath)
  735. {
  736. // don't strip static and import libraries, because it removes the only
  737. // symbol table they have so you can't link to them anymore
  738. if(this->Target->GetType()==cmState::STATIC_LIBRARY || this->ImportLibrary)
  739. {
  740. return;
  741. }
  742. // Don't handle OSX Bundles.
  743. if(this->Target->Target->GetMakefile()->IsOn("APPLE") &&
  744. this->Target->GetPropertyAsBool("MACOSX_BUNDLE"))
  745. {
  746. return;
  747. }
  748. if(! this->Target->Target->GetMakefile()->IsSet("CMAKE_STRIP"))
  749. {
  750. return;
  751. }
  752. os << indent << "if(CMAKE_INSTALL_DO_STRIP)\n";
  753. os << indent << " execute_process(COMMAND \""
  754. << this->Target->Target->GetMakefile()->GetDefinition("CMAKE_STRIP")
  755. << "\" \"" << toDestDirPath << "\")\n";
  756. os << indent << "endif()\n";
  757. }
  758. //----------------------------------------------------------------------------
  759. void
  760. cmInstallTargetGenerator::AddRanlibRule(std::ostream& os,
  761. Indent const& indent,
  762. const std::string& toDestDirPath)
  763. {
  764. // Static libraries need ranlib on this platform.
  765. if(this->Target->GetType() != cmState::STATIC_LIBRARY)
  766. {
  767. return;
  768. }
  769. // Perform post-installation processing on the file depending
  770. // on its type.
  771. if(!this->Target->Target->GetMakefile()->IsOn("APPLE"))
  772. {
  773. return;
  774. }
  775. std::string ranlib =
  776. this->Target->Target->GetMakefile()->GetRequiredDefinition("CMAKE_RANLIB");
  777. if(ranlib.empty())
  778. {
  779. return;
  780. }
  781. os << indent << "execute_process(COMMAND \""
  782. << ranlib << "\" \"" << toDestDirPath << "\")\n";
  783. }